Skip to content

Write HTML. Render video. Built for agents.

fix(studio-server): a folder rename past an unreadable folder or file answers ok (#4860)

Miguel Ángelb6c879c4e919
diff --git a/packages/studio-server/src/routes/files.pathSafety.test.ts b/packages/studio-server/src/routes/files.pathSafety.test.ts
index 71cf279e3d..f7cb5cdf76 100644
--- a/packages/studio-server/src/routes/files.pathSafety.test.ts
+++ b/packages/studio-server/src/routes/files.pathSafety.test.ts
@@ -6,6 +6,7 @@ import {
mkdirSync,
mkdtempSync,
readFileSync,
+ readdirSync,
renameSync,
rmSync,
symlinkSync,
@@ -649,3 +650,48 @@ describe("upload collision races", () => {
await expectProjectGone(await upload(app), project);
});
});
+
+describe("rename reference updates", () => {
+ const renameInside = (app: Hono) =>
+ app.request(fileUrl("inside.txt"), {
+ method: "PATCH",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ newPath: "moved.txt" }),
+ });
+
+ // Windows and root read every folder, so the rename never meets one it may not read there.
+ it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
+ "a rename past an unreadable folder or file still answers ok and updates what it can read",
+ async () => {
+ const { app, project } = fixture();
+ writeFileSync(join(project, "index.html"), '<img src="inside.txt">');
+ mkdirSync(join(project, "private"));
+ writeFileSync(join(project, "locked.html"), '<img src="inside.txt">');
+ chmodSync(join(project, "private"), 0o000);
+ chmodSync(join(project, "locked.html"), 0o000);
+ try {
+ expect(() => readdirSync(join(project, "private"))).toThrow(/EACCES|EPERM/);
+ const rename = await renameInside(app);
+ expect(rename.status).toBe(200);
+ expect(existsSync(join(project, "inside.txt"))).toBe(false);
+ expect(readFileSync(join(project, "moved.txt"), "utf8")).toBe("inside");
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe('<img src="moved.txt">');
+ } finally {
+ chmodSync(join(project, "private"), 0o755);
+ chmodSync(join(project, "locked.html"), 0o644);
+ }
+ },
+ );
+
+ it.skipIf(process.platform === "win32")(
+ "a rename whose reference scan fails for another reason still says so",
+ async () => {
+ const { app, project } = fixture();
+ // A link named like a text file that leads to a folder: reading it fails, and not for want of permission.
+ mkdirSync(join(project, "folder"));
+ symlinkSync(join(project, "folder"), join(project, "link.html"), "dir");
+ const rename = await renameInside(app);
+ expect(rename.status).toBe(500);
+ },
+ );
+});
diff --git a/packages/studio-server/src/routes/files.ts b/packages/studio-server/src/routes/files.ts
index d710117fac..b01bd21c72 100644
--- a/packages/studio-server/src/routes/files.ts
+++ b/packages/studio-server/src/routes/files.ts
@@ -17,6 +17,7 @@ import {
fstatSync,
renameSync,
readdirSync,
+ type Dirent,
} from "node:fs";
import { resolve, dirname, join } from "node:path";
import type { StudioApiAdapter } from "../types.js";
@@ -620,7 +621,7 @@ function generateCopyPath(projectDir: string, originalPath: string): string {
*/
function walkFiles(dir: string, filter: (name: string) => boolean): string[] {
const results: string[] = [];
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
+ for (const entry of readableEntries(dir)) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
if (
@@ -638,6 +639,30 @@ function walkFiles(dir: string, filter: (name: string) => boolean): string[] {
return results;
}
+const SKIPPED_ENTRY = new Set(["EACCES", "EPERM", "ENOENT"]);
+
+function skippable(error: unknown): boolean {
+ return error instanceof Error && SKIPPED_ENTRY.has((error as NodeJS.ErrnoException).code ?? "");
+}
+
+function readableEntries(dir: string): Dirent[] {
+ try {
+ return readdirSync(dir, { withFileTypes: true });
+ } catch (error) {
+ if (skippable(error)) return [];
+ throw error;
+ }
+}
+
+function readableText(file: string): string | null {
+ try {
+ return readFileSync(file, "utf-8");
+ } catch (error) {
+ if (skippable(error)) return null;
+ throw error;
+ }
+}
+
/**
* After a rename, update all references to the old path in project files.
* Scans HTML, CSS, JS, and JSON files for the old filename/path and replaces.
@@ -650,7 +675,8 @@ function updateReferences(projectDir: string, oldPath: string, newPath: string):
let updatedCount = 0;
for (const file of textFiles) {
if (!isSafePath(projectDir, file)) continue;
- const content = readFileSync(file, "utf-8");
+ const content = readableText(file);
+ if (content === null) continue;
// Only replace full relative paths — never bare filenames, which can
// corrupt unrelated content (e.g. "logo.png" inside "my-logo.png").