fix(javascript): reconstruct HTML script loads accurately (#1042)

* fix: parse HTML script references with HTML semantics

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>

* test(javascript): cover exact HTML script resource identity

---------

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
Co-authored-by: N0zoM1z0 <1705004398@qq.com>
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-08 13:24:24 +08:00
committed by GitHub
co-authored by N0zoM1z0
parent 53409ab196
commit 1154f2f03c
11 changed files with 475 additions and 22 deletions
@@ -0,0 +1,87 @@
import { createHash } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { reconstructJavaScriptArtifact } from "../../../src/application/javascript/JavaScriptArtifactReconstruction.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const cases = [
['<script data-src="decoy.js"></script>', []],
['<script x:src="decoy.js"></script>', []],
[`<script title='src="decoy.js"'></script>`, []],
["<script src=actual.js></script>", ["actual.js"]],
['<script title=">" src="actual.js"></script>', ["actual.js"]],
['<script src="actual.js"></script>', ["actual.js"]],
["<script src='actual.js'></script>", ["actual.js"]],
['<script data-src="decoy.js" src="actual.js"></script>', ["actual.js"]],
['<!-- <script src="decoy.js"></script> -->', []],
['<textarea><script src="decoy.js"></script></textarea>', []],
['<template><script src="decoy.js"></script></template>', []],
['<script src="actual&#46;js" src="decoy.js"></script>', ["actual.js"]],
] as const;
it.each(cases)(
"projects local loads from actual HTML attributes in %s",
async (html, expected) => {
const root = await createTestTempDirectory("rea-html-entrypoints-");
await Promise.all([
writeFile(join(root, "index.html"), html),
writeFile(join(root, "actual.js"), "globalThis.ownedActual = true;"),
writeFile(join(root, "decoy.js"), "globalThis.ownedDecoy = true;"),
]);
const result = await reconstructJavaScriptArtifact({ input_path: root });
const loads = result.graph.edges.filter(
({ relation, properties }) =>
relation === "loads" && properties.script_path !== undefined,
);
expect(loads.map(({ properties }) => properties.resolved_path)).toEqual(
expected,
);
},
);
it("preserves source digest, opening-tag range and decoded local base resolution", async () => {
const root = await createTestTempDirectory("rea-html-entrypoint-evidence-");
const opening = '<script title=">" src="actual&#46;js">';
const prefix = '<base data-href="decoy/"><base href=assets/>\n';
const html = `${prefix}${opening}</script>`;
await mkdir(join(root, "assets"));
await Promise.all([
writeFile(join(root, "index.html"), html),
writeFile(
join(root, "assets", "actual.js"),
"globalThis.ownedActual = true;",
),
]);
const result = await reconstructJavaScriptArtifact({ input_path: root });
const loads = result.graph.edges.filter(
({ relation, properties }) =>
relation === "loads" && properties.script_path !== undefined,
);
expect(loads).toHaveLength(1);
expect(loads[0]).toMatchObject({
properties: {
script_path: "actual.js",
base_href: "assets/",
resolved_path: "assets/actual.js",
},
evidence: {
authority: "static-relationship-inference",
artifact: {
available: true,
sha256: createHash("sha256").update(html).digest("hex"),
},
location: {
available: true,
value: {
kind: "source-range",
source: "index.html",
start: { line: 2, column: 0 },
end: { line: 2, column: opening.length },
},
},
},
});
});
@@ -0,0 +1,133 @@
import { mkdir, writeFile } from "node:fs/promises";
import { createRequire } from "node:module";
import { dirname, join, relative, sep } from "node:path";
import { expect, it } from "vitest";
import { reconstructJavaScriptArtifact } from "../../../src/application/javascript/JavaScriptArtifactReconstruction.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const cases: readonly {
name: string;
html: string;
files: Readonly<Record<string, string>>;
expected: readonly string[];
}[] = [
{
name: "extension fallback",
html: '<script src="./app"></script>',
files: { "app.js": "globalThis.owned = true;" },
expected: [],
},
{
name: "directory index",
html: '<script src="./app"></script>',
files: { "app/index.js": "globalThis.owned = true;" },
expected: [],
},
{
name: "directory package",
html: '<script src="./app"></script>',
files: {
"app/package.json": '{"main":"entry.js"}',
"app/entry.js": "globalThis.owned = true;",
},
expected: [],
},
{
name: "base directory",
html: '<base href="assets/"><script src="./app"></script>',
files: { "assets/app.js": "globalThis.owned = true;" },
expected: [],
},
{
name: "exact base resource",
html: '<base href="assets/"><script src="./app.js?cache=1#v2"></script>',
files: {
"assets/app.js": "globalThis.owned = true;",
"app.js": "globalThis.decoy = true;",
},
expected: ["assets/app.js"],
},
{
name: "exact script resource",
html: '<script src="./app.js?cache=1#v2"></script>',
files: { "app.js": "globalThis.owned = true;" },
expected: ["app.js"],
},
];
it.each(cases)(
"uses exact HTML resource identity for $name",
async ({ html, files, expected }) => {
const root = await createTestTempDirectory("rea-html-resources-");
await Promise.all(
Object.entries({ ...files, "index.html": html }).map(
async ([path, text]) => {
await mkdir(dirname(join(root, path)), { recursive: true });
await writeFile(join(root, path), text);
},
),
);
const result = await reconstructJavaScriptArtifact({ input_path: root });
const loads = result.graph.edges.filter(
({ relation, properties }) =>
relation === "loads" && properties.script_path !== undefined,
);
expect(loads.map(({ properties }) => properties.resolved_path)).toEqual(
expected,
);
if (expected.length === 0) {
const reference = result.graph.nodes
.flatMap(({ observations }) => observations)
.find(
({ properties }) => properties.mechanism === "html-script-reference",
);
expect(reference).toMatchObject({
properties: {
script_path: "./app",
resolution_context: "html-reference",
resolution_status: "not-found",
resolved_path: null,
},
evidence: {
authority: "static-relationship-inference",
location: {
available: true,
value: { kind: "source-range", source: "index.html" },
},
limitations: expect.arrayContaining([
expect.stringContaining("exact HTML resource"),
]),
},
});
}
},
);
it("retains CommonJS extension resolution against Node", async () => {
const root = await createTestTempDirectory("rea-html-node-control-");
await Promise.all([
writeFile(
join(root, "main.cjs"),
'const value = require("./app"); module.exports = value;',
),
writeFile(join(root, "app.js"), 'module.exports = "actual";'),
]);
const expected = relative(
root,
createRequire(join(root, "main.cjs")).resolve("./app"),
)
.split(sep)
.join("/");
const result = await reconstructJavaScriptArtifact({ input_path: root });
const edge = result.graph.edges.find(
({ properties }) =>
properties.module_link_kind === "require" &&
properties.specifier === "./app",
);
expect(edge?.properties).toMatchObject({
resolution_status: "resolved",
resolved_path: expected,
});
});