From 056c1237591de3710cb96a51ddd71fa94317b906 Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Mon, 5 Oct 2026 03:01:06 -0400 Subject: [PATCH] fix(browser): resolve redirected source map paths from final URL (#518) * fix(browser): resolve redirected source map paths from final URL Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com> * test(browser): separate redirected source map boundary cases Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com> --------- Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com> --- src/browser/WebSourceMapFetcher.ts | 17 +++-- .../browser/webSourceMapFetcher.test.ts | 74 +++++++++++++++++++ 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts index 4cde48282..66b28fd6e 100644 --- a/src/browser/WebSourceMapFetcher.ts +++ b/src/browser/WebSourceMapFetcher.ts @@ -64,25 +64,26 @@ const fetchOne = async ( "Declared source-map URL is outside the approved exact origins.", ); try { - const response = await fetchFollowingApprovedRedirects( + const fetched = await fetchFollowingApprovedRedirects( request.fetchUrl, input.allowed_origins, signal, host, ); - if (response === undefined) + if (fetched === undefined) return emptySourceMapItem( request, "policy_filtered", "A source-map redirect left the approved exact origins.", ); + const { response, fetchedUrl } = fetched; if (!response.ok) return emptySourceMapItem( request, "fetch_failed", `Source-map server returned HTTP ${String(response.status)}.`, ); - return normalizeSourceMap(request, await response.text()); + return normalizeSourceMap(request, await response.text(), fetchedUrl); } catch (cause: unknown) { if (signal?.aborted === true) throw cause; return emptySourceMapItem( @@ -98,7 +99,7 @@ const fetchFollowingApprovedRedirects = async ( allowedOrigins: readonly string[], signal: AbortSignal | undefined, host: SourceMapFetchHost, -): Promise => { +): Promise<{ response: Response; fetchedUrl: string } | undefined> => { let current = initialUrl; const visited = new Set(); for (;;) { @@ -115,9 +116,10 @@ const fetchFollowingApprovedRedirects = async ( referrerPolicy: "no-referrer", ...(signal === undefined ? {} : { signal }), }); - if (response.status < 300 || response.status >= 400) return response; + if (response.status < 300 || response.status >= 400) + return { response, fetchedUrl: current }; const location = response.headers.get("location"); - if (location === null) return response; + if (location === null) return { response, fetchedUrl: current }; current = new URL(location, current).href; } }; @@ -125,6 +127,7 @@ const fetchFollowingApprovedRedirects = async ( const normalizeSourceMap = ( request: WebSourceMapRequest, text: string, + fetchedUrl: string, ): SourceMapItem => { if (!validSourceMapEnvelope(text)) return emptySourceMapItem( @@ -133,7 +136,7 @@ const normalizeSourceMap = ( "Source-map JSON is not a version 3 map.", ); try { - const map = new AnyMap(text, request.fetchUrl); + const map = new AnyMap(text, fetchedUrl); const resolvedBySource = new Map(); const originalSources = map.sources.map((source, index) => { const content = map.sourcesContent?.[index]; diff --git a/tests/boundary/browser/webSourceMapFetcher.test.ts b/tests/boundary/browser/webSourceMapFetcher.test.ts index c0fc490ae..d6859947e 100644 --- a/tests/boundary/browser/webSourceMapFetcher.test.ts +++ b/tests/boundary/browser/webSourceMapFetcher.test.ts @@ -1,3 +1,5 @@ +import { createServer } from "node:http"; + import { describe, expect, it } from "vitest"; import { fetchWebSourceMaps } from "../../../src/browser/WebSourceMapFetcher.js"; @@ -26,6 +28,78 @@ const expectInvalidIncludedSourceMaps = (result: WebSourceMaps): void => { expectInvalidSourceMaps({ ...result, status: "unavailable" }); }; +describe("source-map redirect URL resolution", () => { + it.each(["/maps/current", "/assets/v2/app.js.map"])( + "resolves relative sources against the delivered map at %s", + async (initialPath) => { + const calls: string[] = []; + const server = createServer((incoming, response) => { + calls.push(incoming.url ?? ""); + if (incoming.url === "/maps/current") { + response.writeHead(302, { location: "/assets/v2/app.js.map" }).end(); + return; + } + response.setHeader("content-type", "application/json"); + response.end( + JSON.stringify({ + version: 3, + names: [], + sources: ["../src/main.ts"], + sourcesContent: ["import './dependency.ts';"], + mappings: "AAAA", + }), + ); + }); + try { + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", resolve); + }); + const address = server.address(); + if (address === null || typeof address === "string") + throw new TypeError("Expected a TCP listener address"); + const localOrigin = `http://127.0.0.1:${String(address.port)}`; + const declaredUrl = `${localOrigin}${initialPath}`; + const result = await fetchWebSourceMaps( + [{ ...request, fetchUrl: declaredUrl, declaredUrl }], + input({ allowed_origins: [localOrigin] }), + ); + expect(calls).toEqual( + initialPath === "/maps/current" + ? [initialPath, "/assets/v2/app.js.map"] + : [initialPath], + ); + expect(result).toMatchObject({ + status: "included", + items: [ + { + declared_url: declaredUrl, + original_sources: [ + { source: `${localOrigin}/assets/src/main.ts` }, + ], + mappings: [{ source: `${localOrigin}/assets/src/main.ts` }], + original_module_edges: [ + { + from_source: `${localOrigin}/assets/src/main.ts`, + resolved_source: `${localOrigin}/assets/src/dependency.ts`, + }, + ], + }, + ], + }); + } finally { + server.closeAllConnections(); + if (server.listening) + await new Promise((resolve, reject) => { + server.close((error) => + error === undefined ? resolve() : reject(error), + ); + }); + } + }, + ); +}); + describe("web source-map fetching and validation", () => { it("fetches without credentials and derives mappings and original modules", async () => { const calls: Array<{ url: string; init: RequestInit | undefined }> = [];