Files
hyperframes/packages/cli/src/commands/compositionRoute.encoding.test.ts
T
Miguel Ángel fed45bac7c fix: serve projects and assets whose names contain @ % # & or ? (#4415)
* fix(studio-server): serve projects whose names contain @ % # & or ?

Five routes cut the decoded project id out of Hono's c.req.path, which leaves %40 %25 %23 %26 %3F
encoded, so the cut missed and files, preview assets, sub-compositions, thumbnails and waveforms
404ed or 403ed for those names. One helper now takes the sub-path by segment from the raw URL.

* fix(studio-server): thumbnails and waveforms stay inside the project for any name

The thumbnail route built its preview URL from the raw project id and path, so # ? and % broke it,
and neither route checked that the decoded path stays inside the project. Thumbnails now use the
composition guard and an encoded URL; waveforms use the read-only asset guard.

* fix(cli): play and present serve assets whose names contain @ % # & or ?

Both /composition/* routes cut a prefix out of Hono's c.req.path with no decode. They now share
studio-server's requestSubPath (renamed from projectSubPath and generalised to any route) with the
five studio-server routes, so one helper owns how a request path becomes a file path.

* fix(studio-server): a thumbnail request for the project folder answers 404

An empty thumbnail path resolved to the project directory and crashed reading it (500).

* fix(studio-server): read the thumbnail's composition with one open

Checking the path with stat and then reading it let the file change in between (CodeQL
js/file-system-race). One open now serves the type check, the mtime and the read; a missing file
still thumbnails from the preview as before, anything that is not a file answers 404.
2026-09-24 10:38:56 -04:00

54 lines
1.8 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Hono } from "hono";
import type { ProjectDir } from "../utils/project.js";
import { registerCompositionRoute } from "./play.js";
import { registerPresentCompositionRoute } from "./present.js";
// Hono's c.req.path leaves %40 %25 %23 %26 %3F encoded; Windows file names cannot hold "?".
const NAMES = [
"A @HyperFrames launch",
"50% off",
"#2 take",
"Tom & Jerry",
...(process.platform === "win32" ? [] : ["why?"]),
"two spaces",
"café crème",
"🎬 film",
];
const ROUTES = [
["play", (app: Hono, project: ProjectDir) => registerCompositionRoute(app, project, false)],
["present", registerPresentCompositionRoute],
] as const;
let dir: string | undefined;
afterEach(() => {
if (dir) rmSync(dir, { recursive: true, force: true });
dir = undefined;
});
async function requestAsset(
register: (app: Hono, project: ProjectDir) => Promise<void>,
name: string,
) {
dir = mkdtempSync(join(tmpdir(), "hf-composition-route-"));
mkdirSync(join(dir, "scenes"));
writeFileSync(join(dir, "scenes", `${name}.txt`), `ASSET ${name}`);
const app = new Hono();
await register(app, { dir, name: "test-project", indexPath: join(dir, "index.html") });
const response = await app.request(`/composition/scenes/${encodeURIComponent(`${name}.txt`)}`);
return { status: response.status, text: await response.text() };
}
describe.each(ROUTES)("%s /composition/*", (_route, register) => {
it.each(NAMES)("serves an asset named %j", async (name) => {
const result = await requestAsset(register, name);
expect(result.status).toBe(200);
expect(result.text).toBe(`ASSET ${name}`);
});
});