fix(javascript): match source-map originals that name parent directories (#1215)

* fix(javascript): match source-map originals that name parent directories

* fix(javascript): resolve parent segments against the source map's directory

* fix(javascript): resolve source originals against their map location

* fix(javascript): preserve URI and empty source map path semantics

* fix(javascript): separate source URL paths from opaque URI payloads

* fix(javascript): preserve filesystem names during URI classification

* style(javascript): format source URI presence guard

* fix(javascript): resolve source map references with URL semantics

* fix(javascript): preserve literal UNC source map paths

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Jay Hemnani
2026-10-09 15:34:25 +05:30
committed by GitHub
parent 24a3aa08d1
commit 889fb374df
4 changed files with 713 additions and 75 deletions
@@ -0,0 +1,69 @@
import { posix } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
interface SourceMapPath {
readonly value: string;
readonly scope: "artifact-relative" | "suffix";
}
/** Resolve a source-map URL reference without changing its recorded identity. */
export const resolveJavaScriptSourceMapPath = (
source: string,
mapPath?: string,
): SourceMapPath | null => {
const portable = source.replaceAll("\\", "/");
const windowsPath = /^[a-z]:\//iu.test(portable) || source.startsWith("\\\\");
// Preserve parent traversal rather than letting URL resolution clamp at '/'.
const anchor = `/${"source/".repeat(portable.split("/").length + 1)}`;
const directory = posix.join(
anchor,
posix.dirname(mapPath?.replaceAll("\\", "/") ?? "."),
);
const base = pathToFileURL(`${directory}/`, { windows: false });
const absolute = windowsPath ? windowsFileUrl(source) : URL.parse(source);
const resolved = windowsPath ? absolute : URL.parse(source, base);
if (
resolved === null ||
!resolved.pathname.startsWith("/") ||
resolved.pathname.endsWith("/")
)
return null;
const relative =
absolute === null
? !portable.startsWith("/")
: absolute.href !== resolved.href;
const path = decodedUrlPath(resolved);
if (path === null) return null;
const value = relative
? posix.relative(anchor, path)
: path.replace(/^\/+/, "");
if (value === "" || value === ".." || value.startsWith("../")) return null;
return {
value,
scope: relative && mapPath !== undefined ? "artifact-relative" : "suffix",
};
};
const windowsFileUrl = (path: string): URL | null => {
try {
return pathToFileURL(path, { windows: true });
} catch (cause: unknown) {
if (cause instanceof TypeError) return null;
throw cause;
}
};
const decodedUrlPath = (url: URL): string | null => {
// Encoded separators must not manufacture a different filesystem hierarchy.
if (/%(?:2f|5c)/iu.test(url.pathname)) return null;
try {
if (url.protocol === "file:")
return fileURLToPath(url, {
windows: url.hostname !== "" && url.hostname !== "localhost",
}).replaceAll("\\", "/");
return `${url.host}${decodeURIComponent(url.pathname)}`;
} catch (cause: unknown) {
if (cause instanceof URIError || cause instanceof TypeError) return null;
throw cause;
}
};