mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): subfolder renders read an ancestor's record however it is stored or spelled (#7847)
The received-files lookup above a rendered folder now treats a record path that exists but is not a readable file (a folder, a dangling link, any other kind of file) as an unreadable record, the same answer the folder's own record gives. Only a missing record means nothing was received. A listed file now counts as inside the rendered subfolder when one of its parent folders is the same folder on disk, so naming the subfolder in another letter case on a case-insensitive disk keeps its received files.
This commit is contained in:
@@ -34,21 +34,32 @@ interface ReceivedRecord {
|
||||
listed: unknown;
|
||||
}
|
||||
|
||||
function recordFileAt(dir: string): boolean {
|
||||
type RecordKind = "absent" | "file" | "other" | "unknown";
|
||||
|
||||
function recordKind(dir: string): RecordKind {
|
||||
const path = join(dir, RECEIVED_RECORD);
|
||||
let found;
|
||||
try {
|
||||
return statSync(join(dir, RECEIVED_RECORD)).isFile();
|
||||
found = lstatSync(path, { throwIfNoEntry: false });
|
||||
} catch (error) {
|
||||
return (error as NodeJS.ErrnoException).code === "ENOTDIR" ? "absent" : "unknown";
|
||||
}
|
||||
if (!found) return "absent";
|
||||
try {
|
||||
return statSync(path).isFile() ? "file" : "other";
|
||||
} catch {
|
||||
return false;
|
||||
return "other";
|
||||
}
|
||||
}
|
||||
|
||||
function readRecord(dir: string, ownFolder: boolean): { listed: unknown } | null {
|
||||
if (!ownFolder && !recordFileAt(dir)) return null;
|
||||
const kind = recordKind(dir);
|
||||
if (kind === "absent" || (kind === "unknown" && !ownFolder)) return null;
|
||||
if (kind !== "file") return { listed: undefined };
|
||||
try {
|
||||
return { listed: JSON.parse(readFileSync(join(dir, RECEIVED_RECORD), "utf-8")).files };
|
||||
} catch (error) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
return ownFolder && (code === "ENOENT" || code === "ENOTDIR") ? null : { listed: undefined };
|
||||
} catch {
|
||||
return { listed: undefined };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,10 +172,22 @@ export function receivedFiles(projectDir: string): (path: string) => boolean {
|
||||
}
|
||||
|
||||
function listedUnder(record: ReceivedRecord): string[] {
|
||||
const entries = (record.listed as unknown[]).map(String);
|
||||
const prefix = record.projectDirFromRoot ? `${record.projectDirFromRoot}/`.normalize("NFC") : "";
|
||||
return (record.listed as unknown[])
|
||||
.map(String)
|
||||
.filter((entry) => entry.normalize("NFC").startsWith(prefix))
|
||||
const folder = prefix ? fileIdentity(join(record.root, record.projectDirFromRoot)) : null;
|
||||
const identities = new Map<string, string | null>();
|
||||
const inFolderOnDisk = (path: string) => {
|
||||
for (let dir = dirname(path); folder && dir.length > record.root.length; dir = dirname(dir)) {
|
||||
if (!identities.has(dir)) identities.set(dir, fileIdentity(dir));
|
||||
if (identities.get(dir) === folder) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
return entries
|
||||
.filter(
|
||||
(entry) =>
|
||||
entry.normalize("NFC").startsWith(prefix) || inFolderOnDisk(join(record.root, entry)),
|
||||
)
|
||||
.map((entry) => join(record.root, entry));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
chmodSync,
|
||||
existsSync,
|
||||
linkSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
@@ -59,6 +60,15 @@ function project(record?: string): string {
|
||||
|
||||
const received = (...files: string[]) => JSON.stringify({ files });
|
||||
|
||||
function caseInsensitiveDisk(): boolean {
|
||||
const probe = mkdtempSync(join(tmpdir(), "hf-case-probe-"));
|
||||
try {
|
||||
return existsSync(probe.toUpperCase()) && existsSync(probe.toLowerCase());
|
||||
} finally {
|
||||
rmSync(probe, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
const SOMEONE_ELSE = "came from someone else and is not a media, font or image file";
|
||||
|
||||
function decide(dir: string, entry?: string) {
|
||||
@@ -196,6 +206,34 @@ describe("outsideAssetsAllowed", () => {
|
||||
expect(line).toContain(`index.html ${SOMEONE_ELSE}`);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["a folder", (path: string) => mkdirSync(path, { recursive: true })],
|
||||
["a dangling link", (path: string) => symlinkSync(join(path, "..", "gone.json"), path)],
|
||||
])("refuses outside files when an ancestor's record path holds %s", (_kind, place) => {
|
||||
root = mkdtempSync(join(tmpdir(), "hf-received-render-"));
|
||||
write(join(root, "outside", "pic.png"), SIGNATURES.png);
|
||||
const dir = join(root, "sub");
|
||||
write(join(dir, "index.html"), `<img src="../outside/pic.png">`);
|
||||
mkdirSync(join(root, ".hyperframes"));
|
||||
place(join(root, ".hyperframes", "received-files.json"));
|
||||
const { allowed, line } = decide(dir);
|
||||
expect(allowed).toBe(false);
|
||||
expect(line).toContain("Could not read");
|
||||
});
|
||||
|
||||
it.skipIf(!caseInsensitiveDisk())(
|
||||
"refuses outside files for a received subfolder named in another letter case",
|
||||
() => {
|
||||
root = mkdtempSync(join(tmpdir(), "hf-received-render-"));
|
||||
write(join(root, "outside", "pic.png"), SIGNATURES.png);
|
||||
write(join(root, "Scenes", "Intro", "index.html"), `<img src="../../outside/pic.png">`);
|
||||
write(join(root, ".hyperframes", "received-files.json"), received("Scenes/Intro/index.html"));
|
||||
const { allowed, line } = decide(join(root, "scenes", "intro"));
|
||||
expect(allowed).toBe(false);
|
||||
expect(line).toContain(`index.html ${SOMEONE_ELSE}`);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps outside files when the record lists no files", () => {
|
||||
expect(decide(project(received())).allowed).toBe(true);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user