From 246a02e9627d516b472fda8461d94ace217d5547 Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Wed, 8 Jan 2025 16:31:50 -0800 Subject: [PATCH] add URL validation for Windows paths in extractCallerPath function --- src/stack_path.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/stack_path.ts b/src/stack_path.ts index f9c42d3..4851753 100644 --- a/src/stack_path.ts +++ b/src/stack_path.ts @@ -35,6 +35,8 @@ const patterns = isWindows /\bat\s(.+[^/]\s)?(?\/.+?):\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); @@ -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; } } }