Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Address {
addressURL = url.pathToFileURL(cachePath).href;
this.isLocal = true;
} else if (addressURI.substring(0, 5) == "file:") {
const match = addressURI.match(/^file:(?:\/\/)?(\.?\.?\/?.*)/);
const match = addressURI.match(/^file:(?:\/\/\/?)([A-Za-z]:\/.*|\.?\.?\/?.*)/);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Overly permissive regex pattern risks matching malformed URIs.

The second alternative \.?\.?\/?.* in the capture group matches almost anything (including empty strings), which undermines validation. For example:

  • file://xyz captures xyz as a relative path
  • file:// captures an empty string
  • file://C:/path captures C:/path correctly, but so would file://nonsense

This makes it difficult to distinguish valid file URIs from malformed ones.

Consider a more explicit pattern that separately handles each case:

-      const match = addressURI.match(/^file:(?:\/\/\/?)([A-Za-z]:\/.*|\.?\.?\/?.*)/)
+      const match = addressURI.match(/^file:(?:\/\/\/?([A-Za-z]:[\/\\].*|\/.*)|\/\/?(\.\.?(?:[\/\\].*)?)|\/?(\.\.[\/\\].*))/)

Or, for clarity, handle Windows and Unix paths separately:

// Match Windows: file:///C:/... or file://C:/... or file:C:/...
const winMatch = addressURI.match(/^file:(?:\/\/\/?)?([A-Za-z]:[\/\\].*)/);
// Match Unix absolute: file:///... or file://localhost/...
const unixMatch = addressURI.match(/^file:\/\/(?:localhost)?(\/.*)$/);
// Match relative: file:./... or file:../... or file://./... or file://../...
const relMatch = addressURI.match(/^file:(?:\/\/)?(\.\.[\/\\].*|\.[\/\\].*)/);

const match = winMatch || unixMatch || relMatch;

This approach makes the intent clearer and reduces false positives.

🤖 Prompt for AI Agents
In src/Address.ts around line 49, the current single regex is overly permissive
and can capture empty or malformed paths; replace it with explicit, separate
checks for Windows absolute paths, Unix absolute/localhost paths, and relative
paths (e.g. three regexes for win, unix, rel), ensure each pattern requires a
non-empty path component (no empty-string captures), try them in a clear order
(windows → unix → relative) and use the first successful match as the canonical
capture; this makes intent explicit and prevents false positives like
"file://xyz" or "file://".

if (!match) {
throw new Error(`Invalid input: "${addressURI}"`);
}
Expand Down