From ca1c3284ba825f4e757cd5d8011b7127cc8e5749 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 06:23:56 -0500 Subject: [PATCH] test(web): verify importer integrity through Chromium (#13076) --- scripts/fixtures/browser-module-site.mjs | 12 +- .../importMapForTrustedResolver.test.ts | 139 ++++++------------ .../modules/importMapForTrustedResolver.ts | 29 +--- 3 files changed, 60 insertions(+), 120 deletions(-) diff --git a/scripts/fixtures/browser-module-site.mjs b/scripts/fixtures/browser-module-site.mjs index 6b98a981c1..fb2b11a2a2 100644 --- a/scripts/fixtures/browser-module-site.mjs +++ b/scripts/fixtures/browser-module-site.mjs @@ -1,12 +1,9 @@ +import { createHash } from "node:crypto"; import { createServer } from "node:http"; /** Source-owned module graph with actual native loading and an uncalled lazy edge. */ export async function startBrowserModuleSite() { const requests = []; - const importMap = { - imports: { "pkg/": "/pkg/", alias: "./global.js", blocked: null }, - scopes: { "/scoped/": { alias: "./scoped.js" } }, - }; const main = ` import {marker as alias} from 'alias'; import {marker as pkg} from 'pkg/nested.js'; @@ -24,6 +21,13 @@ function encodedEscape(){return import('pkg/%2e%2e/escape.js')} function empty(){return import('')} function computed(name){return import(name)} `; + const importMap = { + imports: { "pkg/": "/pkg/", alias: "./global.js", blocked: null }, + integrity: { + "/scoped/main.js?build=2#entry": `sha256-${createHash("sha256").update(main).digest("base64")}`, + }, + scopes: { "/scoped/": { alias: "./scoped.js" } }, + }; const server = createServer((request, response) => { requests.push(request.url); if (request.url === "/") diff --git a/src/browser/modules/importMapForTrustedResolver.test.ts b/src/browser/modules/importMapForTrustedResolver.test.ts index c1ae0f6200..48045fe0ea 100644 --- a/src/browser/modules/importMapForTrustedResolver.test.ts +++ b/src/browser/modules/importMapForTrustedResolver.test.ts @@ -1,98 +1,53 @@ -import { describe, expect, it } from "vitest"; +import { expect, it } from "vitest"; import { importMapForTrustedResolver } from "./importMapForTrustedResolver.js"; -describe("importMapForTrustedResolver", () => { - const baseUrl = "https://app.test/index.html"; - const importerUrl = "https://app.test/main.js"; +const baseUrl = "https://app.test/maps/index.html"; +const importer = "https://app.test/maps/scoped/main.js?build=2#entry"; - it("omits integrity that covers the selected importer URL", () => { - const map = { - imports: { dep: "/dep.js" }, - integrity: { - "https://app.test/main.js": "sha256-importer", - "https://app.test/dep.js": "sha256-dep", - }, - }; - expect(importMapForTrustedResolver(map, importerUrl, baseUrl)).toEqual({ - imports: { dep: "/dep.js" }, - integrity: { "https://app.test/dep.js": "sha256-dep" }, - }); - }); +it("removes importer integrity and serializes every other entry unchanged", () => { + const dependencyIntegrity = { + "./dep.js": "sha256-dep", + "https://other.test/maps/scoped/main.js": "sha256-other-origin", + "./scoped/other.js": "sha256-other-path", + "http://[": "sha256-invalid-url", + ["__proto__"]: "sha256-proto-dep", + }; + const importerIntegrity = { + [importer]: "sha256-exact", + "./scoped/main.js?build=1#other": "sha256-relative", + "/maps/scoped/main.js": "sha256-path", + }; + const map = { + imports: { dep: "./dep.js" }, + scopes: { "/scoped/": { dep: "./scoped.js" } }, + integrity: { ...dependencyIntegrity, ...importerIntegrity }, + }; - it("resolves relative integrity keys against the import-map base URL", () => { - const map = { - imports: { dep: "./dep.js" }, - integrity: { - "./main.js": "sha256-importer", - "./dep.js": "sha256-dep", - }, - }; - expect( - importMapForTrustedResolver(map, "https://app.test/main.js", baseUrl), - ).toEqual({ - imports: { dep: "./dep.js" }, - integrity: { "./dep.js": "sha256-dep" }, - }); - }); - - it("matches importer integrity across query and fragment differences", () => { - const map = { - imports: { dep: "/dep.js" }, - integrity: { - "https://app.test/main.js": "sha256-importer", - "https://app.test/dep.js": "sha256-dep", - }, - }; - expect( - importMapForTrustedResolver( - map, - "https://app.test/main.js?build=1#entry", - baseUrl, - ), - ).toEqual({ - imports: { dep: "/dep.js" }, - integrity: { "https://app.test/dep.js": "sha256-dep" }, - }); - }); - - it("drops an empty integrity member after removing the importer entry", () => { - const map = { - imports: { dep: "/dep.js" }, - integrity: { "https://app.test/main.js": "sha256-importer" }, - }; - expect(importMapForTrustedResolver(map, importerUrl, baseUrl)).toEqual({ - imports: { dep: "/dep.js" }, - }); - }); - - it("preserves maps without integrity and dependency-only integrity", () => { - const without = { imports: { dep: "/dep.js" } }; - expect(importMapForTrustedResolver(without, importerUrl, baseUrl)).toBe( - without, - ); - const dependencyOnly = { - imports: { dep: "/dep.js" }, - integrity: { "https://app.test/dep.js": "sha256-dep" }, - }; - expect( - importMapForTrustedResolver(dependencyOnly, importerUrl, baseUrl), - ).toBe(dependencyOnly); - }); - - it("preserves a dependency integrity key named __proto__ without prototype pollution", () => { - const integrity = Object.assign(Object.create(null), { - "https://app.test/main.js": "sha256-importer", - "https://app.test/dep.js": "sha256-dep", - }); - integrity["__proto__"] = "sha256-proto-dep"; - const map = { imports: { dep: "/dep.js" }, integrity }; - const next = importMapForTrustedResolver(map, importerUrl, baseUrl); - const nextIntegrity = next.integrity as { readonly [key: string]: string }; - expect(nextIntegrity["https://app.test/dep.js"]).toBe("sha256-dep"); - expect( - Object.getOwnPropertyDescriptor(nextIntegrity, "__proto__")?.value, - ).toBe("sha256-proto-dep"); - expect(Object.getPrototypeOf(nextIntegrity)).toBe(null); - expect(Object.prototype.hasOwnProperty.call({}, "__proto__")).toBe(false); + const original = JSON.stringify(map); + const installed = importMapForTrustedResolver(map, importer, baseUrl); + expect(JSON.parse(JSON.stringify(installed))).toStrictEqual({ + imports: map.imports, + scopes: map.scopes, + integrity: dependencyIntegrity, }); + expect(JSON.stringify(map)).toBe(original); +}); + +it("omits integrity only when removing the last entry", () => { + const map = { imports: { dep: "./dep.js" } }; + const dependencyOnly = { ...map, integrity: { "./dep.js": "sha256-dep" } }; + expect(importMapForTrustedResolver(map, importer, baseUrl)).toEqual(map); + expect( + importMapForTrustedResolver(dependencyOnly, importer, baseUrl), + ).toEqual(dependencyOnly); + expect( + importMapForTrustedResolver( + { + ...map, + integrity: { "/maps/scoped/main.js": "sha256-importer" }, + }, + importer, + baseUrl, + ), + ).toEqual(map); }); diff --git a/src/browser/modules/importMapForTrustedResolver.ts b/src/browser/modules/importMapForTrustedResolver.ts index f4e254e2e8..bfcfdf339f 100644 --- a/src/browser/modules/importMapForTrustedResolver.ts +++ b/src/browser/modules/importMapForTrustedResolver.ts @@ -20,7 +20,7 @@ export const importMapForTrustedResolver = ( Array.isArray(integrity) ) return map; - const importer = importerModuleIdentity(importerUrl, baseUrl); + const importer = new URL(importerUrl, baseUrl); const nextIntegrity: { [key: string]: JsonValue } = Object.create(null); let removed = false; for (const [key, value] of Object.entries(integrity)) { @@ -38,36 +38,17 @@ export const importMapForTrustedResolver = ( return { ...map, integrity: nextIntegrity }; }; -const importerModuleIdentity = ( - importerUrl: string, - baseUrl: string, -): { readonly hrefs: ReadonlySet; readonly moduleKey: string } => { - const absolute = new URL(importerUrl, baseUrl); - const withoutHash = new URL(absolute.href); - withoutHash.hash = ""; - return { - hrefs: new Set([absolute.href, withoutHash.href]), - // Chromium's integrity lookup reports the module URL without search/hash. - moduleKey: `${absolute.origin}${absolute.pathname}`, - }; -}; - const integrityKeyMatchesImporter = ( key: string, baseUrl: string, - importer: { - readonly hrefs: ReadonlySet; - readonly moduleKey: string; - }, + importer: URL, ): boolean => { try { const resolved = new URL(key, baseUrl); - const withoutHash = new URL(resolved.href); - withoutHash.hash = ""; + // Compare module URLs without search or fragment. return ( - importer.hrefs.has(resolved.href) || - importer.hrefs.has(withoutHash.href) || - `${resolved.origin}${resolved.pathname}` === importer.moduleKey + resolved.origin === importer.origin && + resolved.pathname === importer.pathname ); } catch { return false;