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>
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-05 15:01:06 +08:00
committed by GitHub
parent 4a5673e1dc
commit 056c123759
2 changed files with 84 additions and 7 deletions
+10 -7
View File
@@ -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<Response | undefined> => {
): Promise<{ response: Response; fetchedUrl: string } | undefined> => {
let current = initialUrl;
const visited = new Set<string>();
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<string, string>();
const originalSources = map.sources.map((source, index) => {
const content = map.sourcesContent?.[index];
@@ -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<void>((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<void>((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 }> = [];