test(audit): consolidate release exposure document routing (#13347)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 09:11:52 +00:00
1 parent 0ecb6dde4b
commit cdba576469
1 file changed
+113 -179
+113 -179
View File
@@ -1665,34 +1665,40 @@ describe("ReleaseExposureReader member allowances", () => {
},
);
it(
"keeps a later source map readable after many large binary members",
{ tags: ["long-running"] },
async () => {
const root = await createTestTempDirectory(
"rea-release-exposure-budget-",
);
const binary = Buffer.alloc(MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES);
const oversizedScript = Buffer.alloc(
MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES + 1,
0x20,
);
await writeFile(
join(root, "app.asar"),
asarArchive([
...Array.from({ length: 160 }, (_, index) => ({
name: `a${String(index)}.bin`,
bytes: binary,
})),
{ name: "b.js", bytes: oversizedScript },
{ name: "z.js.map", bytes: embeddedSourceMap() },
]),
);
const scanned = await inspect(join(root, "app.asar"));
expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 1 });
expect(scanned.extractable.readable_members).toBe(162);
},
);
it("keeps later text and maps readable after large ASAR binary members", async () => {
const root = await createTestTempDirectory("rea-release-exposure-budget-");
const binary = Buffer.alloc(MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES);
const binaryCount = MAX_RELEASE_EXPOSURE_TEXT_BYTES / binary.length + 1;
const path = join(root, "app.asar");
await writeFile(
path,
asarArchive([
...Array.from({ length: binaryCount }, (_, index) => ({
name: `a${String(index)}.bin`,
bytes: binary,
})),
{
name: "b.js",
bytes: Buffer.alloc(MAX_RELEASE_EXPOSURE_TEXT_MEMBER_BYTES + 1, 0x20),
},
{ name: "z.txt", bytes: Buffer.from("release notes\n") },
{ name: "z.js.map", bytes: embeddedSourceMap() },
]),
);
const scanned = await inspect(path);
expect(scanned.source_maps).toMatchObject({
embedded: 1,
embedded_paths: ["z.js.map"],
skipped: 1,
});
expect(scanned.textLocations).toEqual(["z.js.map", "z.txt"]);
expect(scanned.text_members).toEqual({ scanned: 2, skipped: 1 });
expect(scanned.extractable).toMatchObject({
readable_members: binaryCount + 3,
unreadable_members: 0,
});
});
it("reports an oversized linker map as a debug companion wherever it ships", async () => {
const root = await createTestTempDirectory("rea-release-exposure-link-");
@@ -1725,29 +1731,27 @@ describe("ReleaseExposureReader member allowances", () => {
expect(packaged.source_maps.skipped).toBe(0);
});
it("leaves an oversized JSON .map undecided as both map kinds", async () => {
it("keeps oversized generic maps with JSON or whitespace prefixes unresolved", async () => {
const root = await createTestTempDirectory("rea-release-exposure-json-");
const map = Buffer.alloc(MAX_RELEASE_EXPOSURE_SOURCE_MAP_BYTES + 1, 0x20);
map.write('{"version":3,"sources":[]}');
await writeFile(join(root, "bundle.map"), map);
const scanned = await inspect(root);
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 1 });
expect(scanned.source_maps.skipped).toBe(1);
expect(scanned.text_members.skipped).toBe(1);
});
it("keeps whitespace-only prefixes of oversized maps unresolved", async () => {
const root = await createTestTempDirectory("rea-release-exposure-spaces-");
await writeFile(
join(root, "bundle.map"),
Buffer.concat([
Buffer.alloc(MAX_RELEASE_EXPOSURE_SOURCE_MAP_BYTES, 0x20),
embeddedSourceMap(),
]),
const json = embeddedSourceMap();
const whitespace = Buffer.alloc(
MAX_RELEASE_EXPOSURE_SOURCE_MAP_BYTES,
0x20,
);
await writeFile(join(root, "json.map"), Buffer.concat([json, whitespace]));
await writeFile(
join(root, "spaces.map"),
Buffer.concat([whitespace, json]),
);
const scanned = await inspect(root);
expect(scanned.source_maps.skipped).toBe(1);
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 1 });
expect(scanned.source_maps).toMatchObject({
embedded: 0,
unparsed: 0,
skipped: 2,
});
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 2 });
expect(scanned.text_members.skipped).toBe(2);
});
it("does not spend the source-map allowance reading a JavaScript map past the count", async () => {
@@ -4456,17 +4460,30 @@ describe("ReleaseExposureReader inline map reads", () => {
const dataUrl = `data:application/json;base64,${map}`;
it.each([
["a classic script", "<script>"],
["a module script", '<script type="module">'],
])("reads an inline map in %s of an HTML file", async (_, open) => {
it("finds inline maps in classic and module bodies and excludes inactive bodies", async () => {
const root = await createTestTempDirectory("rea-release-exposure-html-");
const withMap = (opening: string) =>
`${opening}\nrun();\n//# sourceMappingURL=${dataUrl}\n</script>`;
await writeFile(join(root, "app.js"), "run();\n");
await writeFile(join(root, "classic.html"), withMap("<script>"));
await writeFile(
join(root, "index.html"),
`<!doctype html>${open}\nrun();\n//# sourceMappingURL=${dataUrl}\n</script>`,
join(root, "module.html"),
withMap('<script type="module">'),
);
await writeFile(
join(root, "inactive.html"),
withMap('<script type="application/json">') +
withMap('<script src="app.js?v=1">') +
'<script src="https://cdn.example/lib"></script>' +
'<script type="text/plain" src="notes"></script>',
);
const scanned = await inspect(root);
expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 0 });
expect(scanned.source_maps).toMatchObject({
embedded: 2,
embedded_paths: [join(root, "classic.html"), join(root, "module.html")],
skipped: 0,
});
expect(sourceMapSourcesStatus(scanned)).toBe("exposed");
});
@@ -4487,19 +4504,6 @@ describe("ReleaseExposureReader inline map reads", () => {
expect(sourceMapSourcesStatus(await inspect(path))).toBe("exposed");
});
it("does not read a map in a data block or a script that loads by URL", async () => {
const root = await createTestTempDirectory("rea-release-exposure-html-");
await writeFile(join(root, "app.js"), "run();\n");
await writeFile(
join(root, "index.html"),
`<script type="application/json">//# sourceMappingURL=${dataUrl}\n</script>` +
`<script src="app.js">//# sourceMappingURL=${dataUrl}\n</script>`,
);
const scanned = await inspect(root);
expect(scanned.source_maps).toMatchObject({ embedded: 0, skipped: 0 });
expect(sourceMapSourcesStatus(scanned)).toBe("not_observed");
});
it("keeps HTML map coverage unknown for scripts it did not read", async () => {
const root = await createTestTempDirectory("rea-release-exposure-html-");
const path = join(root, "index.html");
@@ -5105,24 +5109,6 @@ describe("ReleaseExposureReader map target allowance", () => {
expect(debugCompanionsStatus(scanned)).toBe("unknown");
expect(scanned.source_maps.skipped).toBeGreaterThan(0);
});
it("does not read a .map script past the target allowance as a linker map", async () => {
const baseBytes = 64 * 1024;
const references = Array.from(
{ length: MAX_RELEASE_EXPOSURE_SCRIPT_TARGET_BYTES / baseBytes + 1 },
(_, index) => `<script src="s${String(index)}"></script>`,
);
const root = await createTestTempDirectory("rea-release-exposure-maps-");
await writeFile(
join(root, "a.html"),
`<base href="/${"a".repeat(baseBytes)}/">${references.join("")}`,
);
await writeFile(join(root, "b.html"), '<script src="boot.map"></script>');
await writeFile(join(root, "boot.map"), "run();\n");
const scanned = await inspect(root);
expect(scanned.debug_companions.paths).toEqual([]);
expect(debugCompanionsStatus(scanned)).toBe("unknown");
});
});
describe("ReleaseExposureReader script roles over a .map name", () => {
@@ -5150,46 +5136,25 @@ describe("ReleaseExposureReader script roles over a .map name", () => {
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 0 });
});
it("reads a .map script target whose bytes are a source map as a map", async () => {
const root = await createTestTempDirectory("rea-release-exposure-html-");
await writeFile(
join(root, "index.html"),
'<script src="main.map"></script>',
);
await writeFile(
join(root, "main.map"),
JSON.stringify({
version: 3,
sources: ["src/app.ts"],
sourcesContent: ["export {};"],
mappings: "",
}),
);
const scanned = await inspect(root);
expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 0 });
expect(sourceMapSourcesStatus(scanned)).toBe("exposed");
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 0 });
});
it("reads a UTF-16 .map script target from the text that recognized it", async () => {
const root = await createTestTempDirectory("rea-release-exposure-html-");
await writeFile(
join(root, "index.html"),
'<script src="main.map"></script>',
);
const map = JSON.stringify({
version: 3,
sources: ["src/app.ts"],
sourcesContent: ["export {};"],
mappings: "",
});
const map = Buffer.from(embeddedSourceMap().toString("utf8"), "utf16le");
await writeFile(
join(root, "main.map"),
Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(map, "utf16le")]),
Buffer.concat([Buffer.from([0xff, 0xfe]), map]),
);
const scanned = await inspect(root);
expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 0 });
expect(sourceMapSourcesStatus(scanned)).toBe("exposed");
expect(scanned.source_maps).toMatchObject({
embedded: 1,
embedded_paths: [join(root, "main.map")],
unparsed: 0,
skipped: 0,
});
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 0 });
});
@@ -5216,9 +5181,8 @@ describe("ReleaseExposureReader script roles over a .map name", () => {
);
for (const target of [root, archive]) {
const scanned = await inspect(target);
expect(scanned.debug_companions.paths).toEqual([]);
expect(debugCompanionsStatus(scanned)).not.toBe("exposed");
expect(scanned.source_maps.skipped).toBeGreaterThan(0);
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 0 });
expect(scanned.source_maps.skipped).toBe(1);
expect(sourceMapSourcesStatus(scanned)).toBe("unknown");
}
});
@@ -5243,17 +5207,6 @@ describe("ReleaseExposureReader HTML script references", () => {
return root;
};
it("reads the inline map of an extensionless script an HTML file loads", async () => {
const scanned = await inspect(
await directory({
"index.html": '<script src="main"></script>',
main: inlineMap,
}),
);
expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 0 });
expect(sourceMapSourcesStatus(scanned)).toBe("exposed");
});
it("reads an extensionless script an ASAR's HTML loads", async () => {
const root = await createTestTempDirectory("rea-release-exposure-html-");
const path = join(root, "app.asar");
@@ -5270,44 +5223,37 @@ describe("ReleaseExposureReader HTML script references", () => {
expect(sourceMapSourcesStatus(await inspect(path))).toBe("exposed");
});
it("resolves a script against the document base", async () => {
it("resolves two pages' script URLs to the same extensionless member", async () => {
const root = await directory({
"index.html": '<base href="static/"><script src="main?v=1"></script>',
"a.html": '<base href="static/"><script src="main?v=1"></script>',
"z.html": '<script src="static/main"></script>',
});
await mkdir(join(root, "static"));
await writeFile(join(root, "static", "main"), inlineMap);
expect(sourceMapSourcesStatus(await inspect(root))).toBe("exposed");
});
const script = join(root, "static", "main");
await writeFile(script, inlineMap);
it("does not count a script two pages share as unread", async () => {
const scanned = await inspect(
await directory({
"a.html": '<script src="main"></script>',
main: inlineMap,
"z.html": '<script src="main"></script>',
}),
);
expect(scanned.source_maps).toMatchObject({ embedded: 1, skipped: 0 });
});
it("reads the scripts of a standalone SVG", async () => {
const scanned = await inspect(
await directory({
"screen.svg": `<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg"><script><![CDATA[\nrun();\n${inlineMap}]]></script></svg>`,
}),
);
expect(scanned.source_maps.embedded).toBe(1);
const scanned = await inspect(root);
expect(scanned.source_maps).toMatchObject({
embedded: 1,
embedded_paths: [script],
skipped: 0,
});
expect(sourceMapSourcesStatus(scanned)).toBe("exposed");
});
it("finds nothing in an SVG without scripts", async () => {
const scanned = await inspect(
await directory({
"icon.svg":
'<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0"/></svg>',
}),
);
expect(scanned.source_maps).toMatchObject({ embedded: 0, skipped: 0 });
it("finds inline maps only in the standalone SVG that contains a script", async () => {
const root = await directory({
"icon.svg":
'<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0"/></svg>',
"screen.svg": `<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg"><script><![CDATA[\nrun();\n${inlineMap}]]></script></svg>`,
});
const scanned = await inspect(root);
expect(scanned.source_maps).toMatchObject({
embedded: 1,
embedded_paths: [join(root, "screen.svg")],
skipped: 0,
});
});
it("still reads a page another page loads as a script as HTML", async () => {
@@ -5321,7 +5267,7 @@ describe("ReleaseExposureReader HTML script references", () => {
expect(sourceMapSourcesStatus(scanned)).toBe("exposed");
});
it("keeps a script past the target allowance unassessed", async () => {
it("keeps a .map script past the target allowance unassessed", async () => {
const baseBytes = 64 * 1024;
const references = Array.from(
{ length: MAX_RELEASE_EXPOSURE_SCRIPT_TARGET_BYTES / baseBytes + 1 },
@@ -5330,14 +5276,16 @@ describe("ReleaseExposureReader HTML script references", () => {
const scanned = await inspect(
await directory({
"a.html": `<base href="/${"a".repeat(baseBytes)}/">${references.join("")}`,
"b.html": '<script src="main"></script>',
main: inlineMap,
"b.html": '<script src="main.map"></script>',
"main.map": inlineMap,
}),
);
expect(scanned.source_maps).toMatchObject({
embedded: 0,
skipped: references.length + 1,
skipped: references.length + 2,
});
expect(debugCompanionsStatus(scanned)).toBe("unknown");
expect(scanned.debug_companions).toEqual({ paths: [], skipped: 1 });
expect(sourceMapSourcesStatus(scanned)).toBe("unknown");
});
@@ -5371,20 +5319,6 @@ describe("ReleaseExposureReader HTML script references", () => {
expect(scanned.source_maps.skipped).toBe(1);
expect(sourceMapSourcesStatus(scanned)).toBe("unknown");
});
it("leaves coverage alone for remote, named, and data-block references", async () => {
const scanned = await inspect(
await directory({
"app.js": "run();\n",
"index.html":
'<script src="https://cdn.example/lib"></script>' +
'<script src="app.js?v=1"></script>' +
'<script type="text/plain" src="notes"></script>',
}),
);
expect(scanned.source_maps).toMatchObject({ embedded: 0, skipped: 0 });
expect(sourceMapSourcesStatus(scanned)).toBe("not_observed");
});
});
describe("ReleaseExposureReader cumulative text allowance", () => {