mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(studio-server): the preview file check resolves links on volumes that refuse native realpath (#8377)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
@@ -15,7 +15,16 @@ vi.mock("node:fs", async (importOriginal) => {
|
||||
return { ...actual, realpathSync, default: { ...actual, realpathSync } };
|
||||
});
|
||||
|
||||
const { isSafePath } = await import("./safePath.js");
|
||||
vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("node:fs/promises")>();
|
||||
const realpath = () =>
|
||||
Promise.reject(
|
||||
Object.assign(new Error("EISDIR: illegal operation on a directory"), { code: "EISDIR" }),
|
||||
);
|
||||
return { ...actual, realpath, default: { ...actual, realpath } };
|
||||
});
|
||||
|
||||
const { isSafePath, realpathOff } = await import("./safePath.js");
|
||||
const dirs: string[] = [];
|
||||
afterEach(() => {
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
@@ -27,3 +36,16 @@ it("still resolves paths on a volume whose native realpath refuses with EISDIR",
|
||||
expect(isSafePath(base, join(base, "file.txt"))).toBe(true);
|
||||
expect(isSafePath(base, join(tmpdir(), "outside.txt"))).toBe(false);
|
||||
});
|
||||
|
||||
// fails without the fix: off the event loop a link on such a volume never resolved, so the private-file check read it
|
||||
// under its innocuous spelling.
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"resolves a link off the event loop on a volume whose native realpath refuses with EISDIR",
|
||||
async () => {
|
||||
const base = mkdtempSync(join(tmpdir(), "safepath-eisdir-"));
|
||||
dirs.push(base);
|
||||
writeFileSync(join(base, ".env"), "KEY=1");
|
||||
symlinkSync(join(base, ".env"), join(base, "notes.txt"));
|
||||
expect(await realpathOff(join(base, "notes.txt"))).toBe(realpathSync(join(base, ".env")));
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { resolve, sep, join, dirname, basename, relative, isAbsolute, parse } from "node:path";
|
||||
import { lstatSync, mkdirSync, realpathSync, statSync } from "node:fs";
|
||||
import { lstatSync, mkdirSync, realpath as realpathWalk, realpathSync, statSync } from "node:fs";
|
||||
import { realpath as realpathNative } from "node:fs/promises";
|
||||
import { promisify } from "node:util";
|
||||
import { projectPathSegments } from "@hyperframes/parsers/private-path";
|
||||
|
||||
export {
|
||||
@@ -10,12 +12,24 @@ export {
|
||||
projectPathSegments,
|
||||
} from "@hyperframes/parsers/private-path";
|
||||
|
||||
// Some Windows volumes (RAM disks) refuse the native realpath with EISDIR; the JS walk still resolves there.
|
||||
const nativeRefused = (error: unknown) => (error as NodeJS.ErrnoException).code === "EISDIR";
|
||||
|
||||
export function realpath(path: string): string {
|
||||
try {
|
||||
return realpathSync.native(path);
|
||||
} catch (error) {
|
||||
// Some Windows volumes (RAM disks) refuse the native call with EISDIR.
|
||||
if ((error as NodeJS.ErrnoException).code === "EISDIR") return realpathSync(path);
|
||||
if (nativeRefused(error)) return realpathSync(path);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/** `realpath` off the event loop: the same answer, the EISDIR fallback included. */
|
||||
export async function realpathOff(path: string): Promise<string> {
|
||||
try {
|
||||
return await realpathNative(path);
|
||||
} catch (error) {
|
||||
if (nativeRefused(error)) return promisify(realpathWalk)(path);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
// A volume whose native realpath refuses every path with EISDIR, as some Windows RAM disks do.
|
||||
vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("node:fs/promises")>();
|
||||
const realpath = () =>
|
||||
Promise.reject(
|
||||
Object.assign(new Error("EISDIR: illegal operation on a directory"), { code: "EISDIR" }),
|
||||
);
|
||||
return { ...actual, realpath, default: { ...actual, realpath } };
|
||||
});
|
||||
|
||||
const { isPrivateProjectFileOff } = await import("./projectDisk.js");
|
||||
const dirs: string[] = [];
|
||||
afterEach(() => {
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe.skipIf(process.platform === "win32")("the off-loop private-file check", () => {
|
||||
// fails without the fix: native realpath failed at every level, so the check compared the link's own spelling and
|
||||
// served the project's .env under an innocuous name.
|
||||
it("refuses an innocuously named link to a private file on a volume whose native realpath refuses", async () => {
|
||||
const project = mkdtempSync(join(tmpdir(), "private-eisdir-"));
|
||||
dirs.push(project);
|
||||
mkdirSync(join(project, "assets"));
|
||||
writeFileSync(join(project, ".env"), "KEY=1");
|
||||
symlinkSync(join(project, ".env"), join(project, "assets", "notes.txt"));
|
||||
expect(await isPrivateProjectFileOff(project, join(project, "assets", "notes.txt"))).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { isPrivateProjectFileOff } from "./projectDisk.js";
|
||||
|
||||
const dirs: string[] = [];
|
||||
afterEach(() => {
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function project(): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), "private-off-"));
|
||||
dirs.push(dir);
|
||||
mkdirSync(join(dir, "assets"));
|
||||
return dir;
|
||||
}
|
||||
|
||||
describe.skipIf(process.platform === "win32")("the off-loop private-file check", () => {
|
||||
// fails without the fix: a path that is there but will not resolve (a link loop) was judged by its own spelling.
|
||||
it("refuses a path that is there but will not resolve", async () => {
|
||||
const dir = project();
|
||||
symlinkSync(join(dir, "assets", "b.txt"), join(dir, "assets", "a.txt"));
|
||||
symlinkSync(join(dir, "assets", "a.txt"), join(dir, "assets", "b.txt"));
|
||||
expect(await isPrivateProjectFileOff(dir, join(dir, "assets", "a.txt"))).toBe(true);
|
||||
});
|
||||
|
||||
it("allows a plain file and a file not there yet, and refuses a link to a private file", async () => {
|
||||
const dir = project();
|
||||
writeFileSync(join(dir, "assets", "logo.txt"), "x");
|
||||
writeFileSync(join(dir, ".env"), "KEY=1");
|
||||
symlinkSync(join(dir, ".env"), join(dir, "assets", "notes.txt"));
|
||||
expect(await isPrivateProjectFileOff(dir, join(dir, "assets", "logo.txt"))).toBe(false);
|
||||
expect(await isPrivateProjectFileOff(dir, join(dir, "assets", "new.txt"))).toBe(false);
|
||||
expect(await isPrivateProjectFileOff(dir, join(dir, "assets", "notes.txt"))).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Stats } from "node:fs";
|
||||
import { realpath, stat } from "node:fs/promises";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { realpathOff } from "@hyperframes/core/safe-path";
|
||||
import { basename, dirname, join, relative, sep } from "node:path";
|
||||
import { isPrivateProjectPath } from "./safePath.js";
|
||||
|
||||
@@ -12,11 +13,13 @@ export const folderGoneOff = (dir: string): Promise<boolean> =>
|
||||
(error: NodeJS.ErrnoException) => error.code === "ENOENT" || error.code === "ENOTDIR",
|
||||
);
|
||||
|
||||
/** The real path; for a path not there (yet, or any more), the nearest existing folder's real path plus the rest. */
|
||||
/** The real path, or for a missing path its nearest folder's plus the rest; throws for one that will not resolve. */
|
||||
export async function realFilePathOff(filePath: string): Promise<string> {
|
||||
try {
|
||||
return await realpath(filePath);
|
||||
} catch {
|
||||
return await realpathOff(filePath);
|
||||
} catch (error) {
|
||||
const { code } = error as NodeJS.ErrnoException;
|
||||
if (code !== "ENOENT" && code !== "ENOTDIR") throw error;
|
||||
const dir = dirname(filePath);
|
||||
return dir === filePath ? filePath : join(await realFilePathOff(dir), basename(filePath));
|
||||
}
|
||||
@@ -28,9 +31,9 @@ export async function isPrivateProjectFileOff(
|
||||
): Promise<boolean> {
|
||||
const rel = (from: string, to: string) => relative(from, to).split(sep).join("/");
|
||||
if (isPrivateProjectPath(rel(projectDir, filePath))) return true;
|
||||
const [realDir, realFile] = await Promise.all([
|
||||
realFilePathOff(projectDir),
|
||||
realFilePathOff(filePath),
|
||||
]);
|
||||
return isPrivateProjectPath(rel(realDir, realFile));
|
||||
const unresolvable = null;
|
||||
const real = await Promise.all([realFilePathOff(projectDir), realFilePathOff(filePath)]).catch(
|
||||
() => unresolvable,
|
||||
);
|
||||
return real === unresolvable || isPrivateProjectPath(rel(real[0], real[1]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user