mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
test(web): verify importer integrity through Chromium (#13076)
This commit is contained in:
1 parent
82224d9f6c
commit
ca1c3284ba
3 files changed
+60
-120
No files matched your search
@@ -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 === "/")
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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<string>; 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<string>;
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user