fix(core): treat files package managers run by default as tool-trusted (#8129)

npm, pnpm, yarn and bun run `node-gyp rebuild` on install when a binding.gyp
is present and package.json has no install script, and a .gyp file can include
any .gyp or .gypi. npm, pnpm and yarn run `node server.js` for start when
package.json has no start script. A project from `hyperframes init` has
neither script, so both defaults apply.

toolTrustedFileKind now classifies .gyp/.gypi files and server.js, in every
spelling projectPathSegments folds, so Studio's file routes and the registry
installer refuse to write them. Rename and duplicate refusals now report
why: protected_path when the destination is inside the project, as POST and
PUT already do.
This commit is contained in:
andybpan
2026-11-18 01:32:34 +00:00
committed by GitHub
parent 63c8f8ef66
commit 5f06e1b0a3
6 changed files with 80 additions and 9 deletions
@@ -127,6 +127,9 @@ describe("installItem", () => {
"lib/vite.config.ts",
"compositions/Makefile",
"lib/AGENTS.md::$DATA",
"lib/binding.gyp",
"assets/common.gypi",
"compositions/server.js",
])("refuses the install target %s before writing anything", async (planted) => {
const dir = project();
const hostile = { ...item, files: [{ ...item.files[0]!, target: planted }] };
@@ -208,6 +211,24 @@ describe("installItem", () => {
expect(existsSync(join(dir, ".gitignore"))).toBe(true);
});
it.each(["binding.gyp", "server.js"])(
"refuses an example's %s, which a package manager runs on its own",
async (name) => {
const dir = project();
const example = {
...item,
type: "hyperframes:example",
dimensions: { width: 1920, height: 1080 },
duration: 10,
files: [{ path: name, target: name, type: "hyperframes:asset" }],
} as unknown as RegistryItem;
await expect(installItem(example, { destDir: dir, force: true })).rejects.toThrow(
/Unsafe target/,
);
expect(readdirSync(dir)).toEqual([]);
},
);
it("preserves directory symlinks whose destination stays inside the project", async () => {
const dir = project();
mkdirSync(join(dir, "actual"));
+13
View File
@@ -423,6 +423,17 @@ describe("toolTrustedFileKind", () => {
["hooks/pre-commit.fish", "program"],
["NODEMON.JSON.", "tool-config"],
["Docker\ufb01le.dev", "tool-config"],
["binding.gyp", "tool-config"],
["BINDING.GYP. ", "tool-config"],
["binding.gyp::$DATA", "tool-config"],
["BINDIN~1.GYP", "tool-config"],
["addon.gyp", "tool-config"],
["deps/common.gypi", "tool-config"],
["server.js", "program"],
["Server.JS.", "program"],
["server.js::$DATA", "program"],
["sub/server.js", "program"],
["SERVER~1.JS", "short-name"],
] as const)("names %s %s", (path, kind) => {
expect(toolTrustedFileKind(path)).toBe(kind);
});
@@ -453,6 +464,8 @@ describe("toolTrustedFileKind", () => {
"assets/compose.svg",
"config.toml",
"assets/script.js",
"assets/webserver.js",
"assets/server.json",
])("leaves %s to the other rules", (path) => {
expect(toolTrustedFileKind(path)).toBeNull();
});
+3 -1
View File
@@ -167,7 +167,9 @@ const TOOL_CONFIG_FAMILIES = [
/^mise(?:\.[^.]+)*\.toml$/,
/^simple-git-hooks\.(?:json|[cm]?js)$/,
/^[jt]sconfig\..+\.json$/,
/\.gypi?$/,
];
const RUN_BY_PACKAGE_MANAGER_DEFAULT = new Set(["server.js"]);
// Tools load these on their own: vite.config.ts, jest.config.json, docker-compose.yml, gulpfile.js and the like.
const TOOL_CONFIG =
/\.config\.(?:[cm]?[jt]sx?|json[c5]?|ya?ml|toml)$|^(?:gulp|grunt)file\.[cm]?[jt]s$/;
@@ -225,7 +227,7 @@ export function toolTrustedFileKind(relPath: string): ToolTrustedKind | null {
if (LOCK_FILES.has(name)) return "lock-file";
if (isToolConfig(name, segments)) return "tool-config";
if (AGENT_INSTRUCTIONS.test(name)) return "agent-instructions";
if (PROGRAM.test(name)) return "program";
if (PROGRAM.test(name) || RUN_BY_PACKAGE_MANAGER_DEFAULT.has(name)) return "program";
return null;
}
@@ -173,8 +173,8 @@ describe("isWritableProjectFile and the project's package.json", () => {
it("writes as before when package.json is missing or not JSON", () => {
const projectDir = createProjectDir();
expect(isWritableProjectFile(projectDir, join(projectDir, "server.js"))).toBe(true);
expect(isWritableProjectFile(projectDir, join(projectDir, "run.js"))).toBe(true);
writeFileSync(join(projectDir, "package.json"), "{ not json");
expect(isWritableProjectFile(projectDir, join(projectDir, "server.js"))).toBe(true);
expect(isWritableProjectFile(projectDir, join(projectDir, "run.js"))).toBe(true);
});
});
@@ -279,6 +279,11 @@ describe("files other programs run or trust", () => {
"run.\u017fh",
"Make\ufb01le",
"jest.config.json",
"binding.gyp",
"BINDING.GYP.",
"deps/common.gypi",
"server.js",
"Server.JS",
];
const put = (app: Hono, path: string, headers: Record<string, string> = {}) =>
app.request(fileUrl(path), { method: "PUT", headers, body: "planted" });
@@ -406,6 +411,34 @@ describe("files other programs run or trust", () => {
});
});
it("refuses the files a package manager runs without a script in package.json", async () => {
const { app, project } = fixture();
writeFileSync(join(project, "package.json"), '{"scripts":{"dev":"npx hyperframes preview"}}');
writeFileSync(join(project, "addon.gyp"), "{}");
for (const path of ["binding.gyp", "server.js", "server.js::$DATA", "deps/common.gypi "]) {
for (const method of ["POST", "PUT"]) {
const response = await app.request(fileUrl(path), { method, body: "content" });
expect(response.status).toBe(403);
expect(await response.json()).toMatchObject({ why: "protected_path" });
}
}
const rename = await app.request(fileUrl("inside.txt"), {
method: "PATCH",
headers: { "content-type": "application/json" },
body: JSON.stringify({ newPath: "Server.JS." }),
});
const duplicate = await app.request("http://localhost/projects/demo/duplicate-file", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ path: "addon.gyp" }),
});
for (const response of [rename, duplicate]) {
expect(response.status).toBe(403);
expect(await response.json()).toMatchObject({ why: "protected_path" });
}
expect(readdirSync(project).sort()).toEqual(["addon.gyp", "inside.txt", "package.json"]);
});
it("still writes compositions, stylesheets, script assets and uploads", async () => {
const { app, project } = fixture();
for (const path of [
+8 -6
View File
@@ -641,6 +641,12 @@ function ensureDir(projectDir: string, filePath: string) {
mkdirWithinProject(projectDir, dirname(filePath));
}
function forbiddenWriteTarget(c: RouteContext, projectDir: string, path: string) {
return resolveWithinProject(projectDir, path)
? c.json({ error: "forbidden", why: "protected_path" }, 403)
: c.json({ error: "forbidden" }, 403);
}
/**
* Generate a copy name: foo.html → foo (copy).html → foo (copy 2).html
*/
@@ -3765,9 +3771,7 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
}
const newAbs = resolveWritableWithinProject(res.project.dir, newPath);
if (!newAbs) {
return c.json({ error: "forbidden" }, 403);
}
if (!newAbs) return forbiddenWriteTarget(c, res.project.dir, newPath);
if (existsSync(newAbs)) {
return c.json({ error: "already exists" }, 409);
}
@@ -3803,9 +3807,7 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
const copyPath = generateCopyPath(project.dir, body.path);
const destAbs = resolveWritableWithinProject(project.dir, copyPath);
if (!destAbs) {
return c.json({ error: "forbidden" }, 403);
}
if (!destAbs) return forbiddenWriteTarget(c, project.dir, copyPath);
ensureDir(project.dir, destAbs);
try {