Skip to content

Commit edb6e87

Browse files
fix(studio-server): make sub-composition ETags ByteString-safe (heygen-com#3982)
Hash the decoded sub-composition path before including it in the preview ETag. This keeps ETags deterministic and path-specific while ensuring Hono receives an ASCII-safe header value for Unicode composition filenames. Add regression coverage for percent-encoded CJK sub-composition paths, including stability and distinctness assertions. Co-authored-by: heygengenesis[bot] <262951085+heygengenesis[bot]@users.noreply.github.com> Co-authored-by: miguel.sierra <[email protected]>
1 parent ae18e3a commit edb6e87

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎packages/studio-server/src/routes/preview.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,35 @@ describe("hf-id surfacing in preview route", () => {
464464
expect(readFileSync(compPath, "utf-8")).toContain('data-hf-id="hf-');
465465
});
466466

467+
it("returns ByteString-safe stable and distinct ETags for percent-encoded CJK sub-comp paths", async () => {
468+
const projectDir = createProjectDir();
469+
mkdirSync(join(projectDir, "compositions"));
470+
writeFileSync(join(projectDir, "compositions/測試.html"), "<div>First</div>");
471+
writeFileSync(join(projectDir, "compositions/別頁.html"), "<div>Second</div>");
472+
const app = new Hono();
473+
registerPreviewRoutes(
474+
app,
475+
createAdapter(projectDir, { getProjectSignature: () => "stable-signature" }),
476+
);
477+
478+
const first = await app.request(
479+
"http://localhost/projects/demo/preview/comp/compositions/%E6%B8%AC%E8%A9%A6.html",
480+
);
481+
const repeat = await app.request(
482+
"http://localhost/projects/demo/preview/comp/compositions/%E6%B8%AC%E8%A9%A6.html",
483+
);
484+
const other = await app.request(
485+
"http://localhost/projects/demo/preview/comp/compositions/%E5%88%A5%E9%A0%81.html",
486+
);
487+
488+
expect([first.status, repeat.status, other.status]).toEqual([200, 200, 200]);
489+
const firstEtag = first.headers.get("ETag");
490+
expect(firstEtag).toBeTruthy();
491+
expect(firstEtag).toMatch(/^[\x20-\x7e]+$/);
492+
expect(repeat.headers.get("ETag")).toBe(firstEtag);
493+
expect(other.headers.get("ETag")).not.toBe(firstEtag);
494+
});
495+
467496
it("sub-comp served ids equal disk ids even when relative asset paths are rewritten", async () => {
468497
// Regression guard for the setTiming element_not_found divergence class:
469498
// the sub-comp route rewrites relative src/href BEFORE minting, so an

‎packages/studio-server/src/routes/preview.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ export function registerPreviewRoutes(api: Hono, adapter: PreviewApiAdapter): vo
470470
// "v2" salts the etag for the hf-id-pinning change below: a client holding
471471
// a pre-pin cached response (preview-only ids, unstamped disk file) must
472472
// not revalidate to a 304 that skips the pin.
473-
const etag = `"comp:v2:${compPath}:${signature}${variablesEtagSalt(vars.raw)}"`;
473+
const compPathHash = createHash("sha1").update(compPath).digest("hex");
474+
const etag = `"comp:v2:${compPathHash}:${signature}${variablesEtagSalt(vars.raw)}"`;
474475
const ifNoneMatch = c.req.header("If-None-Match");
475476
if (ifNoneMatch === etag) {
476477
return new Response(null, {

0 commit comments

Comments
 (0)