diff --git a/packages/cli/src/registry/installer.test.ts b/packages/cli/src/registry/installer.test.ts index d486d677eb..c51470357e 100644 --- a/packages/cli/src/registry/installer.test.ts +++ b/packages/cli/src/registry/installer.test.ts @@ -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")); diff --git a/packages/core/src/safePath.test.ts b/packages/core/src/safePath.test.ts index a5ca9bd866..00ec9f7062 100644 --- a/packages/core/src/safePath.test.ts +++ b/packages/core/src/safePath.test.ts @@ -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(); }); diff --git a/packages/core/src/safePath.ts b/packages/core/src/safePath.ts index 74e9fe14d3..863b32d3ce 100644 --- a/packages/core/src/safePath.ts +++ b/packages/core/src/safePath.ts @@ -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; } diff --git a/packages/studio-server/src/helpers/safePath.test.ts b/packages/studio-server/src/helpers/safePath.test.ts index 2ed69bf770..c39cf01ff7 100644 --- a/packages/studio-server/src/helpers/safePath.test.ts +++ b/packages/studio-server/src/helpers/safePath.test.ts @@ -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); }); }); diff --git a/packages/studio-server/src/routes/files.pathSafety.test.ts b/packages/studio-server/src/routes/files.pathSafety.test.ts index 9290d5e79f..4b016cb0a1 100644 --- a/packages/studio-server/src/routes/files.pathSafety.test.ts +++ b/packages/studio-server/src/routes/files.pathSafety.test.ts @@ -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 = {}) => 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 [ diff --git a/packages/studio-server/src/routes/files.ts b/packages/studio-server/src/routes/files.ts index 011842acd0..38f5579489 100644 --- a/packages/studio-server/src/routes/files.ts +++ b/packages/studio-server/src/routes/files.ts @@ -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 {