Files
hyperframes/packages/studio-server/src/routes/projectIdEncoding.test.ts
T
Miguel Ángel 621ddd4693 fix(studio): stop file names with %, apostrophes or $ from breaking Studio (#5156)
* fix(studio): serve files whose name has a bare percent sign

A browser sends src="100%.png" with the % unescaped, and decoding the whole path threw, so Studio answered 500. Only well-formed escapes are decoded now.

* fix(cli): serve files with a bare percent sign from the check and snapshot server

One shared decode rule for request paths; the image thumbnail route's malformed-escape 400 could no longer fire, so a name that is not a file is a 404.

* fix(cli): name a failed file with a bare percent sign in check

* fix(studio): keep apostrophes and dollar signs in file names when editing attributes

An attribute value is matched by its own quote kind, so src="it's.png" is replaced instead of duplicated, and every insert uses a function replacer, so $1 or $& in a file name is written as is.

* fix(studio): rename references spelled with %20 or & and read quoted media sources

WIP: not yet tested; see the resume note.

* fix(studio): match whole attribute names when editing media sources

* fix(studio): compare decoded names and escape replacement references

* fix(studio): preserve file references across URL and string consumers

* docs: clarify raw file references and URL fields

* fix(studio): keep raw path identity distinct from URL spelling

* fix: share attribute grammar and decode media URLs at their readers

* fix: preserve local filename identity in HTML URL output

* fix: decode entity references before rename candidate matching

* style: format rename entity handling and regressions

* fix: preserve platform file identity when renaming references

* fix(core): decode CSS escapes before rebasing asset URLs

* fix(core): consume CSS continuations before URL encoding

* fix(core): preserve CSS URL identity and escape rebased suffixes

* fix(core): express CSS suffix controls with Unicode categories

* fix(core): preserve URL backslash identity while rebasing

* test(core): respect platform backslash filename semantics

* fix(studio): a dropped file with %, # or ? in its name loads, and an encoded reference renames

* fix(studio): read an authored src as a URL so dropped files with spaces, %, # or ? load

* fix(studio): media routes find files named with ? or #, and the probe reads a src as a URL

* fix(studio-server): one rule for request file paths, so loudness finds files named with %, # or ?

* chore: drop the design note from the change

* fix(core): rename replaced clips whose src is URL-encoded
2026-10-08 16:37:49 +00:00

231 lines
8.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { Hono } from "hono";
import { mkdtempSync, writeFileSync, rmSync, mkdirSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { registerFileRoutes } from "./files.js";
import { PREVIEW_CAPTURE_PARAM, registerPreviewRoutes } from "./preview.js";
import { registerThumbnailRoutes } from "./thumbnail.js";
import { registerWaveformRoutes } from "./waveform.js";
import { buildWaveformCacheKey } from "../helpers/waveform.js";
import type { StudioApiAdapter } from "../types.js";
// Project ids come from folder names, so any character a folder allows must survive the URL.
function createAdapter(projectDir: string): StudioApiAdapter {
return {
listProjects: () => [],
resolveProject: async (id: string) => ({ id, dir: projectDir }),
bundle: async () => null,
lint: async () => ({ findings: [] }),
runtimeUrl: "/api/runtime.js",
rendersDir: () => "/tmp/renders",
startRender: () => ({
id: "job-1",
status: "rendering",
progress: 0,
outputPath: "/tmp/out.mp4",
}),
} as unknown as StudioApiAdapter;
}
function projectWithComposition(): { dir: string; cleanup: () => void } {
const dir = mkdtempSync(join(tmpdir(), "hf-projectid-"));
writeFileSync(join(dir, "index.html"), "<html><body>COMPOSITION</body></html>");
mkdirSync(join(dir, "scenes"), { recursive: true });
writeFileSync(join(dir, "scenes", "scene-1.html"), "<html><body>SCENE ONE</body></html>");
return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
}
async function readComposition(projectId: string, compPath = "index.html") {
const { dir, cleanup } = projectWithComposition();
try {
const app = new Hono();
registerFileRoutes(app, createAdapter(dir));
const url = `http://localhost/projects/${encodeURIComponent(projectId)}/files/${encodeURIComponent(compPath)}?optional=1`;
const response = await app.request(url);
const body = (await response.json()) as { content?: string };
return { status: response.status, content: body.content };
} finally {
cleanup();
}
}
describe("project ids that percent-encode in a URL", () => {
it("serves a composition from a plain ASCII project id", async () => {
const result = await readComposition("demo-project");
expect(result.status).toBe(200);
expect(result.content).toContain("COMPOSITION");
});
it("serves a composition when the project folder name has a space", async () => {
const result = await readComposition("my video");
expect(result.status).toBe(200);
expect(result.content).toContain("COMPOSITION");
});
it("serves a composition when the project folder name is non-ASCII", async () => {
const result = await readComposition("用故事板做视频");
expect(result.status).toBe(200);
expect(result.content).toContain("COMPOSITION");
});
// Sub-compositions are the house pattern — one per scene — so the active
// composition is very often in a subdirectory. encodeURIComponent turns the
// separator into %2F, which is what the client sends.
it("serves a sub-composition in a subdirectory", async () => {
const result = await readComposition("demo-project", "scenes/scene-1.html");
expect(result.status).toBe(200);
expect(result.content).toContain("SCENE ONE");
});
});
// A Home sentence with an @ mention names the project; Hono leaves %40 %25 %23 %26 %3F encoded in c.req.path.
const RESERVED_NAMES = [
"A @HyperFrames launch",
"50% off",
"#2 take",
"Tom & Jerry",
"why?",
"two spaces",
"café crème",
"🎬 film",
];
type ThumbnailCall = { compPath: string; previewUrl: string; served?: string };
async function requestProject(projectId: string, route: string, encodedSubPath: string) {
const { dir, cleanup } = projectWithComposition();
writeFileSync(join(dir, "scenes", "voice.wav"), "RIFF");
// The waveform route's own cache hit, so no audio decoder runs.
const voice = statSync(join(dir, "scenes", "voice.wav"));
mkdirSync(join(dir, ".waveform-cache"));
writeFileSync(
join(dir, ".waveform-cache", buildWaveformCacheKey("scenes/voice.wav", voice)),
"[0.5]",
);
const thumbnails: ThumbnailCall[] = [];
const app = new Hono();
const adapter = {
...createAdapter(dir),
generateThumbnail: async (opts: ThumbnailCall) => {
const page = await app.request(opts.previewUrl.replace("/api/", "/"));
const served = page.ok ? await page.text() : `${page.status}`;
thumbnails.push({ compPath: opts.compPath, previewUrl: opts.previewUrl, served });
return Buffer.from("jpeg");
},
} as StudioApiAdapter;
try {
registerFileRoutes(app, adapter);
registerPreviewRoutes(app, adapter);
registerThumbnailRoutes(app, adapter);
registerWaveformRoutes(app, adapter);
const response = await app.request(
`http://localhost/projects/${encodeURIComponent(projectId)}/${route}/${encodedSubPath}`,
);
return { status: response.status, text: await response.text(), thumbnails };
} finally {
cleanup();
}
}
describe.each(RESERVED_NAMES)("project id %j", (projectId) => {
it("reads a project file", async () => {
const result = await readComposition(projectId, "scenes/scene-1.html");
expect(result.status).toBe(200);
expect(result.content).toContain("SCENE ONE");
});
it("serves a preview asset", async () => {
const result = await requestProject(projectId, "preview", "scenes/scene-1.html");
expect(result.status).toBe(200);
expect(result.text).toContain("SCENE ONE");
});
it("serves a preview sub-composition", async () => {
const result = await requestProject(projectId, "preview/comp", "scenes/scene-1.html");
expect(result.status).toBe(200);
expect(result.text).toContain("SCENE ONE");
});
it("thumbnails a sub-composition through a preview URL that parses back to it", async () => {
const result = await requestProject(projectId, "thumbnail", "scenes/scene-1.html");
expect(result.status).toBe(200);
expect(result.thumbnails).toHaveLength(1);
expect(result.thumbnails[0]?.compPath).toBe("scenes/scene-1.html");
const url = new URL(result.thumbnails[0]?.previewUrl ?? "");
expect(url.search + url.hash).toBe(`?${PREVIEW_CAPTURE_PARAM}=1`);
expect(result.thumbnails[0]?.served).toContain("SCENE ONE");
const segments = url.pathname.split("/").map(decodeURIComponent);
expect(segments).toEqual([
"",
"api",
"projects",
projectId,
"preview",
"comp",
"scenes",
"scene-1.html",
]);
});
it("serves an audio waveform", async () => {
const result = await requestProject(projectId, "waveform", "scenes/voice.wav");
expect(result.status).toBe(200);
expect(JSON.parse(result.text)).toEqual({ peaks: [0.5] });
});
});
// Encoded as one segment: a literal ../ is normalised away before routing.
const OUTSIDE_PROJECT = encodeURIComponent(`${"../".repeat(24)}etc/hosts`);
describe("a sub-path that decodes to a parent directory", () => {
it("is not thumbnailed", async () => {
const result = await requestProject("demo-project", "thumbnail", OUTSIDE_PROJECT);
expect(result.status).toBe(404);
expect(result.thumbnails).toHaveLength(0);
});
it("thumbnails nothing for the project folder itself", async () => {
const result = await requestProject("demo-project", "thumbnail", "");
expect(result.status).toBe(404);
expect(result.thumbnails).toHaveLength(0);
});
it("is not read for a waveform", async () => {
const result = await requestProject("demo-project", "waveform", OUTSIDE_PROJECT);
expect(result.status).toBe(404);
});
});
// A browser sends src="100%.png" with the % unescaped, since no two hex digits follow it.
describe("a file name with a bare percent sign", () => {
async function servePreviewAsset(rawSubPath: string) {
const { dir, cleanup } = projectWithComposition();
writeFileSync(join(dir, "scenes", "100%.png"), "PNG BYTES");
writeFileSync(join(dir, "scenes", "50% off.png"), "SALE BYTES");
try {
const app = new Hono();
registerPreviewRoutes(app, createAdapter(dir));
const response = await app.request(`http://localhost/projects/film/preview/${rawSubPath}`);
return { status: response.status, text: await response.text() };
} finally {
cleanup();
}
}
it("serves the file when the % arrives unescaped", async () => {
expect(await servePreviewAsset("scenes/100%.png")).toEqual({ status: 200, text: "PNG BYTES" });
});
it("still decodes the escapes beside a bare %", async () => {
expect(await servePreviewAsset("scenes/50%%20off.png")).toEqual({
status: 200,
text: "SALE BYTES",
});
expect(await servePreviewAsset("scenes/100%25.png")).toEqual({
status: 200,
text: "PNG BYTES",
});
});
});