Skip to content

Commit

Permalink
add URL validation for Windows paths in extractCallerPath function
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Jan 9, 2025
1 parent edc1a86 commit 246a02e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/stack_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const patterns = isWindows
/\bat\s(.+[^/]\s)?(?<path>\/.+?):\d+:\d+$/,
];

const MaybeUrlRE = /^[a-z]{2,5}:\/\//i;

// only exposed for tests:
export function extractCallerPath(stack: string): string {
const frames = stack.split("\n").filter(Boolean);
Expand All @@ -48,15 +50,20 @@ export function extractCallerPath(stack: string): string {
}
for (let i = callerFrame + 1; i < frames.length; i++) {
const frame = frames[i];
for (const re of patterns) {
const g = toS(frame).trim().match(re)?.groups;
for (const pattern of patterns) {
const g = toS(frame).trim().match(pattern)?.groups;
if (g != null && isNotBlank(g["path"])) {
const path = g["path"];
try {
return new URL(path).pathname;
} catch {
return path;
// Windows requires us to check if it's a reasonable URL, as URL accepts
// "C:\\path\\file.txt" as valid (!!)
if (MaybeUrlRE.test(path)) {
try {
return new URL(path).pathname;
} catch {
// ignore
}
}
return path;
}
}
}
Expand Down

0 comments on commit 246a02e

Please sign in to comment.