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];