Skip to content

Commit 9469790

Browse files
committed
fix(producer): a src with an encoded folder re-points its sibling assets; format
1 parent 6e1404c commit 9469790

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

‎packages/producer/src/services/htmlCompiler.test.ts‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,25 +1046,53 @@ describe("detectRenderModeHints", () => {
10461046
}
10471047
});
10481048

1049-
it.each([
1050-
["dot-dot segments", "../outside-scene.html"],
1051-
["an encoded ../", "%2e%2e%2foutside-scene.html"],
1052-
["a leading slash and dot-dot", "/../outside-scene.html"],
1053-
["an encoded leading slash and dot-dot", "%2f%2e%2e%2foutside-scene.html"],
1054-
])("compileForRender refuses a data-composition-src that leaves the project through %s", async (_label, src) => {
1055-
const projectDir = makeSubCompProject("hf-outside-subcomp-", [{ id: "intro", src }], {});
1056-
const outside = join(dirname(projectDir), "outside-scene.html");
1057-
writeFileSync(outside, validSubCompHtml("intro", "Outside"));
1049+
it("compileForRender re-points a sibling asset of a sub-composition whose folder is %20-encoded", async () => {
1050+
const projectDir = makeSubCompProject(
1051+
"hf-encoded-dir-subcomp-",
1052+
[{ id: "intro", src: "my%20dir/scene.html" }],
1053+
{},
1054+
);
10581055
try {
1059-
await expect(
1060-
compileForRender(projectDir, join(projectDir, "index.html"), projectDir),
1061-
).rejects.toThrow(/outside the project folder/);
1056+
mkdirSync(join(projectDir, "my dir"), { recursive: true });
1057+
writeFileSync(join(projectDir, "my dir", "img.png"), "png");
1058+
writeFileSync(
1059+
join(projectDir, "my dir", "scene.html"),
1060+
`<!doctype html><html><body><div data-composition-id="intro" data-width="100" data-height="100"><img id="pic" src="img.png"></div></body></html>`,
1061+
);
1062+
const compiled = await compileForRender(
1063+
projectDir,
1064+
join(projectDir, "index.html"),
1065+
projectDir,
1066+
);
1067+
const src = parseHTML(compiled.html).document.getElementById("pic")?.getAttribute("src");
1068+
expect(decodeURIComponent(src ?? "")).toContain("my dir/img.png");
10621069
} finally {
1063-
rmSync(outside, { force: true });
10641070
rmSync(projectDir, { recursive: true, force: true });
10651071
}
10661072
});
10671073

1074+
it.each([
1075+
["dot-dot segments", "../outside-scene.html"],
1076+
["an encoded ../", "%2e%2e%2foutside-scene.html"],
1077+
["a leading slash and dot-dot", "/../outside-scene.html"],
1078+
["an encoded leading slash and dot-dot", "%2f%2e%2e%2foutside-scene.html"],
1079+
])(
1080+
"compileForRender refuses a data-composition-src that leaves the project through %s",
1081+
async (_label, src) => {
1082+
const projectDir = makeSubCompProject("hf-outside-subcomp-", [{ id: "intro", src }], {});
1083+
const outside = join(dirname(projectDir), "outside-scene.html");
1084+
writeFileSync(outside, validSubCompHtml("intro", "Outside"));
1085+
try {
1086+
await expect(
1087+
compileForRender(projectDir, join(projectDir, "index.html"), projectDir),
1088+
).rejects.toThrow(/outside the project folder/);
1089+
} finally {
1090+
rmSync(outside, { force: true });
1091+
rmSync(projectDir, { recursive: true, force: true });
1092+
}
1093+
},
1094+
);
1095+
10681096
it("compileForRender preserves a bare fragment's markup, sibling styles, and script", async () => {
10691097
const projectDir = makeSubCompProject(
10701098
"hf-fragment-subcomp-",

‎packages/producer/src/services/htmlCompiler.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { decodedUrlPath } from "@hyperframes/parsers/asset-paths";
1313

1414
import { createHash } from "node:crypto";
1515
import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs";
16-
import { join, dirname, resolve, basename, relative } from "path";
16+
import { join, dirname, resolve, basename, relative, sep } from "path";
1717
import { parseHTML } from "linkedom";
1818
import {
1919
COMPOSITION_SOURCE_URL,
@@ -213,6 +213,16 @@ class EmptyCompositionError extends Error {
213213
}
214214
}
215215

216+
/** The html with each data-composition-src as the project-relative path it names (%xx decoded, no leading /). */
217+
function normalizeCompositionSrcs(html: string, projectDir: string): string {
218+
return html.replace(/(\bdata-composition-src=)(["'])([^"']*)\2/g, (whole, name, quote, value) => {
219+
if (!value.includes("%") && !value.startsWith("/")) return whole;
220+
const file = resolveCompositionSrc(projectDir, value);
221+
if (file === null) return whole;
222+
return `${name}${quote}${relative(projectDir, file).split(sep).join("/")}${quote}`;
223+
});
224+
}
225+
216226
/**
217227
* Recursively walk every `data-composition-src` reference reachable from
218228
* `html` (including nested sub-compositions) and verify each resolves to a
@@ -796,7 +806,7 @@ async function parseSubCompositions(
796806
continue;
797807
}
798808

799-
const rawSubHtml = read.text;
809+
const rawSubHtml = normalizeCompositionSrcs(read.text, projectDir);
800810
const nestedVisited = new Set(visited);
801811
nestedVisited.add(filePath);
802812

@@ -2073,7 +2083,10 @@ export async function compileForRender(
20732083
options: CompileForRenderOptions = {},
20742084
): Promise<CompiledComposition> {
20752085
const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath);
2076-
const rawHtml = rewriteUnresolvableGsapToCdn(entryHtml, projectDir);
2086+
const rawHtml = normalizeCompositionSrcs(
2087+
rewriteUnresolvableGsapToCdn(entryHtml, projectDir),
2088+
projectDir,
2089+
);
20772090

20782091
// Pre-flight: every data-composition-src reference must resolve to a
20792092
// usable file before we spend any time compiling, launching a browser, or

0 commit comments

Comments
 (0)