Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/core/src/utils/node-stack-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ export function node(getModule?: GetModuleFn): StackLineParserFn {
filename = lineMatch[5];
}

const maybeDecodedFilename = filename ? _safeDecodeURI(filename) : undefined;
return {
filename: filename ? decodeURI(filename) : undefined,
module: getModule ? getModule(filename) : undefined,
filename: maybeDecodedFilename ?? filename,
module: maybeDecodedFilename && getModule?.(maybeDecodedFilename),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module not computed when URI decoding fails

Medium Severity

When _safeDecodeURI returns undefined for a malformed URI, maybeDecodedFilename && getModule?.(...) short-circuits to undefined, so the module field is always lost. The raw filename is available and could still be passed to getModule to compute a meaningful module value. The fallback for filename correctly uses ?? filename, but module doesn't apply the same fallback strategy.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional. getModule implementors also need to call decodeURI, so they'll end up with the same error being thrown in this code path. I think it's reasonable here to just not include the module, given that we can't make a file lookup anyway if we're dealing with a malformed filename (for reasons unknown)

function: functionName,
lineno: _parseIntOrUndefined(lineMatch[3]),
colno: _parseIntOrUndefined(lineMatch[4]),
Expand Down Expand Up @@ -148,3 +149,11 @@ export function nodeStackLineParser(getModule?: GetModuleFn): StackLineParser {
function _parseIntOrUndefined(input: string | undefined): number | undefined {
return parseInt(input || '', 10) || undefined;
}

function _safeDecodeURI(filename: string): string | undefined {
try {
return decodeURI(filename);
} catch {
return undefined;
}
}
17 changes: 17 additions & 0 deletions packages/core/test/lib/utils/stacktrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,21 @@ describe('node', () => {

expect(node(input)).toEqual(expectedOutput);
});

it('returns the raw filename when decodeURI throws a URIError', () => {
const malformedFilename = '/path/to/%file%.js';
const input = `at myFunction (${malformedFilename}:10:5)`;

const result = node(input);

expect(result?.filename).toBe('/path/to/%file%.js');
});

it('decodes a valid percent-encoded filename', () => {
const input = 'at myFunction (/path/to/my%20file.js:10:5)';

const result = node(input);

expect(result?.filename).toBe('/path/to/my file.js');
});
});
Loading