Skip to content

Commit b81bb5b

Browse files
committed
fix: directory boundary check with trailing separator + cross-dir sourcemap header
1. startsWith(absDir) now uses absDir + '/' to prevent prefix collisions (e.g. /dist matching /dist-backup). 2. sourcemapFilename now uses relative path from JS dir to map file instead of bare basename, so cross-directory maps link correctly in the Sourcemap header.
1 parent ff84b44 commit b81bb5b

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/commands/sourcemap/upload.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* env vars, config defaults) — no slash-separated arg parsing needed.
77
*/
88

9-
import { basename, relative, resolve } from "node:path";
9+
import { dirname, relative, resolve } from "node:path";
1010
import type { SentryContext } from "../../context.js";
1111
import {
1212
type ArtifactFile,
@@ -309,7 +309,13 @@ export const uploadCommand = buildCommand({
309309
mapRelative = stripPrefix(mapRelative, pathPrefixToStrip);
310310
}
311311

312-
const mapBasename = basename(mapPath);
312+
// Sourcemap header is resolved relative to the JS file's URL.
313+
// Use relative path from JS dir to map file so cross-directory
314+
// maps (e.g. maps/bundle.js.map) link correctly.
315+
const sourcemapRef = relative(dirname(jsPath), mapPath).replaceAll(
316+
"\\",
317+
"/"
318+
);
313319
return [
314320
{
315321
path: jsPath,
@@ -318,7 +324,7 @@ export const uploadCommand = buildCommand({
318324
...(debugId ? { debugId } : {}),
319325
type: "minified_source" as const,
320326
url: `${urlPrefix}${jsRelative}`,
321-
sourcemapFilename: mapBasename,
327+
sourcemapFilename: sourcemapRef,
322328
},
323329
{
324330
path: mapPath,

src/lib/sourcemap/inject.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ export async function discoverFilePairs(
297297
// Guard against sourceMappingURL directives that resolve outside the
298298
// upload directory (e.g. "../../other/app.js.map"). Convention-based
299299
// maps (foo.js.map) are always adjacent so they're inherently safe.
300-
if (!mapPath.startsWith(absDir)) {
300+
// Trailing separator prevents prefix collisions (e.g. /dist vs /dist-backup).
301+
const dirPrefix = absDir.endsWith("/") ? absDir : `${absDir}/`;
302+
if (!mapPath.startsWith(dirPrefix)) {
301303
log.debug(
302304
`skipping sourcemap outside directory: ${mapPath} (resolved from ${entry.absolutePath})`
303305
);

0 commit comments

Comments
 (0)