Files
openclaw/scripts/lib/output-root-guard.mjs
wangyan2026 d865c9b8f5 fix(build): fail closed on symlinked build output roots (#116705)
Reject symlinked generated-output roots before build and postbuild mutation paths can recurse into unrelated targets. Cover aggregate, UI, export-template, bundled-plugin, Docker-prune, and plugin-runtime entry points with regression tests and migration guidance.

Closes #116498

Co-authored-by: WangYan <wang.yan29@xydigit.com>
2026-07-31 19:37:05 +08:00

29 lines
913 B
JavaScript

// Fail-closed output-root guard shared by build and postbuild mutators.
import fs from "node:fs";
/**
* Throws when a generated output root is a symbolic link. readdir/rm through a
* symlinked root rewrites the link target — observed deleting a live gateway
* build tree — so recursive cleanup and replacement must fail closed here.
*/
export function assertRealOutputRoot(rootPath, params = {}) {
const fsImpl = params.fs ?? fs;
let stat;
try {
stat = fsImpl.lstatSync(rootPath);
} catch (error) {
// Missing roots are normal on a first build; the build recreates them.
if (error?.code === "ENOENT") {
return;
}
throw error;
}
if (!stat.isSymbolicLink()) {
return;
}
throw new Error(
`Build output root "${rootPath}" is a symbolic link; refusing to mutate it. ` +
`Remove the symlink or replace it with a real directory before building.`,
);
}