diff --git a/packages/studio-server/src/helpers/applyFileMutations.test.ts b/packages/studio-server/src/helpers/applyFileMutations.test.ts index 048efa9005..3d340872a9 100644 --- a/packages/studio-server/src/helpers/applyFileMutations.test.ts +++ b/packages/studio-server/src/helpers/applyFileMutations.test.ts @@ -5,12 +5,17 @@ import { mkdtempSync, readFileSync, rmSync, + symlinkSync, writeFileSync, } from "node:fs"; import { join } from "node:path"; import { tmpdir } from "node:os"; import { describe, expect, it } from "vitest"; -import { applyFileMutations, FileChangedError } from "./applyFileMutations.js"; +import { + applyFileMutations, + FileChangedError, + FileOutsideProjectError, +} from "./applyFileMutations.js"; import { fileContentVersion, identifyFileWrite, resetFileWriteReceipts } from "./fileVersion.js"; function expectStaleMutation(after: string, pinVersion = true): void { @@ -38,6 +43,36 @@ function expectStaleMutation(after: string, pinVersion = true): void { } describe("applyFileMutations", () => { + // fails without the fix: a path outside the project was written, with no backup and no error + it.each(["above the project", "through a link out of the project"])( + "refuses a file %s and changes nothing", + (where) => { + if (where.includes("link") && process.platform === "win32") return; + const root = mkdtempSync(join(tmpdir(), "hf-mutation-outside-")); + const projectDir = join(root, "project"); + const inside = join(projectDir, "index.html"); + const outside = join(root, "outside.html"); + try { + mkdirSync(projectDir); + writeFileSync(inside, "inside", "utf8"); + writeFileSync(outside, "outside", "utf8"); + const absPath = where.includes("link") ? join(projectDir, "linked.html") : outside; + if (where.includes("link")) symlinkSync(outside, absPath); + expect(() => + applyFileMutations(projectDir, [ + { sourceFile: "index.html", absPath: inside, after: "inside-after" }, + { sourceFile: "outside.html", absPath, after: "written" }, + ]), + ).toThrow(FileOutsideProjectError); + expect(readFileSync(inside, "utf8")).toBe("inside"); + expect(readFileSync(outside, "utf8")).toBe("outside"); + expect(existsSync(join(projectDir, ".hyperframes"))).toBe(false); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }, + ); + it("refuses a file whose version changed since the caller read it", () => { expectStaleMutation("after"); }); diff --git a/packages/studio-server/src/helpers/applyFileMutations.ts b/packages/studio-server/src/helpers/applyFileMutations.ts index 31bf142b19..b3cffc0b48 100644 --- a/packages/studio-server/src/helpers/applyFileMutations.ts +++ b/packages/studio-server/src/helpers/applyFileMutations.ts @@ -1,6 +1,7 @@ import { readFileSync, statSync } from "node:fs"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { backupPathForResponse, snapshotBeforeWrite } from "./backupJournal.js"; +import { isSafePath } from "./safePath.js"; import { clearFileWriteReceipt, createWriteToken, @@ -34,6 +35,9 @@ export function applyFileMutations( writeFile: WriteFile = (path, content) => replaceFileAtomically(path, content, statSync(path).mode), ): AppliedFileMutation[] { + for (const mutation of mutations) + if (!isSafePath(projectDir, mutation.absPath)) + throw new FileOutsideProjectError(mutation.sourceFile); const prepared = mutations.map((mutation) => ({ ...mutation, before: mutation.before ?? readFileSync(mutation.absPath, "utf-8"), @@ -127,6 +131,12 @@ function applyOneMutation( }; } +export class FileOutsideProjectError extends Error { + constructor(readonly sourceFile: string) { + super("file is outside the project"); + } +} + export class FileChangedError extends Error { constructor(readonly sourceFile: string) { super("file changed since the timeline was read");