Skip to content

Better handle cross-platform URIs#161

Open
anasdeyra wants to merge 1 commit intoloanlink-nl:masterfrom
anasdeyra:master
Open

Better handle cross-platform URIs#161
anasdeyra wants to merge 1 commit intoloanlink-nl:masterfrom
anasdeyra:master

Conversation

@anasdeyra
Copy link

@anasdeyra anasdeyra commented Oct 2, 2025

To handle both Linux and Windows URIs more reliably, consider this version:
/^file:(?:\/\/\/?)([A-Za-z]:\/.*|\.?\.?\/?.*)/

This:

  • Accepts both "file://" and "file:///"
  • Captures Windows drive-letter paths (C:/...)
  • Captures relative paths (./... , ../.. )
  • Still works for Linux-style paths (/home/...)

Small notice: this Regex is generated with AI as I can't get them in my head 😭

Summary by CodeRabbit

  • Bug Fixes
    • Improved parsing of file: URIs to correctly support Windows drive-letter paths (e.g., C:/...) and relative paths.
    • More reliable recognition of local files across platforms, reducing mis-parsing and related errors when opening resources.
    • Prevents failures when pasting or clicking file links from external sources.
    • Maintains existing behavior for other URL types; no changes to user workflows required.

Better handle cross-platform URIs
@coderabbitai
Copy link

coderabbitai bot commented Oct 2, 2025

Walkthrough

Updated the regex for parsing file: URIs in src/Address.ts to handle Windows drive-letter and relative paths. No other logic, control flow, or exports were changed.

Changes

Cohort / File(s) Change Summary
File URI parsing
src/Address.ts
Replaced file: URI regex with `/^file:(?:///?)([A-Za-z]:/.*

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A nibble of regex, hop hop hooray,
I sniff out paths where files might lay.
C:\ drives and ../ trails align,
My whiskers tingle—parsing fine!
With gentle paws I tread the line,
file: dreams mapped, byte by byte—divine. 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “Better handle cross-platform URIs” directly reflects the primary update to the file URI parsing regex, succinctly conveying that the change improves support for both Windows and Linux file paths without extraneous detail.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 2, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
E Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Address.ts (1)

59-61: Update regex and add tests for edge-case file URIs

  • Plain file:C:/… currently isn’t matched
  • file:// and file:/// capture an empty string and resolve to CWD instead of root (/)
  • UNC URLs (file://host/share/…) are treated as relative paths—decide whether to support or reject and adjust the pattern/tests accordingly
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 273b3f3 and b8ef42b.

📒 Files selected for processing (1)
  • src/Address.ts (1 hunks)

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://".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant