mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(studio-server): file edits refuse a target outside the project and change nothing (#8737)
This commit is contained in:
@@ -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");
|
||||
});
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user