Skip to content

fix: keep POSIX client roots usable on a Windows-side server - #2840

Closed
Yi-111-a wants to merge 1 commit into
ChromeDevTools:mainfrom
Yi-111-a:fix/wsl2-posix-client-roots
Closed

Yi-111-a wants to merge 1 commit into
ChromeDevTools:mainfrom
Yi-111-a:fix/wsl2-posix-client-roots

Conversation

@Yi-111-a

@Yi-111-a Yi-111-a commented Sep 26, 2026 •

Copy link
Copy Markdown

Description

A WSL2 client can launch this server on the Windows side through WSL interop (Windows node.exe). It then addresses its own workspace with POSIX file URIs, e.g. file:///home/user/project.

fileURLToPath() rejects those on a Windows host with ERR_INVALID_FILE_URL_PATH. There are three client-supplied file: URIs, and each one failed differently:

  • An advertised root was dropped. Root resolution sits inside Promise.allSettled, so the throw was only logged as Could not resolve configured root … and the root skipped. Every path inside the project was then refused:
Access denied: path /home/user/project/shot.png (canonical: \\wsl.localhost\Ubuntu-24.04\home\user\project\shot.png) is not within any of the configured workspace roots.
  • A filePath tool argument was reported as an unreadable path, because the throw was caught and the literal string file:///home/user/project/shot.png was then resolved as a path.
  • A file: loadResource URL threw straight out to the caller.

The requested path itself always resolved correctly, against the server's UNC working directory, to the same canonical form as the root. Only the URIs never got decoded.

Fixes #2839

What Changed

Root resolution, tool-argument URLs and resource URLs now all read a file: URI with the semantics it was written in, via a new resolveFileUriPath() in src/utils/files.ts:

  • A file URL with no host and no drive letter is a POSIX path, so it is parsed with {windows: false}.
  • Drive-letter (file:///C:/src, and the percent-encoded file:///c%3A/Users) and UNC (file://server/share, file://wsl.localhost/…) URIs keep Node's default platform behaviour, so nothing changes for them.

path.resolve() then maps a POSIX path against the server's working directory, which for a WSL2 working directory is a UNC path, producing \\wsl.localhost\<distro>\home\user\project — the same canonical form resolveCanonicalPath derives for the requested file, so the existing startsWith(canonicalRoot + path.sep) containment check matches.

Matching a drive letter has to allow the percent-encoded colon: read as POSIX, file:///c%3A/Users/user/project returns /c:/Users/user/project, a path that exists nowhere. The historical file:///C|/src form needs no separate case — URL parsing normalizes the pipe to a colon before the helper sees it.

resolveFileUriPath takes windows = process.platform === 'win32' and selects path.win32 or path.posix from it, so the Windows matrix is testable from any host. It also accepts an already-parsed URL, since two of the three call sites have one in hand.

Scope is deliberately limited to decoding. Every result still goes through validatePath(), so the workspace-root containment check, the fs.realpath canonicalization of symlinked roots and the allSettled graceful skip for an unresolvable root are unchanged — changing the URI spelling cannot move a request across the containment boundary, because the URI decodes to the same string the equivalent plain path already decoded to. ToolHandler's http/https/ws/wss branch is untouched.

telemetry/WatchdogClient.ts's fileURLToPath is intentionally not routed through the helper: it resolves the server's own import.meta.url, which must keep platform defaults or the watchdog spawn breaks on Windows.

The resolution moved into src/utils/files.ts next to resolveCanonicalPath, following the existing convention for path helpers, which also makes it unit-testable without a browser.

Testing

Unit tests only, per AGENTS.md; no test launches a browser.

$ npm run build     # tsc clean
$ npx eslint src/utils/files.ts src/McpContext.ts src/ToolHandler.ts tests/
$ npx prettier --check <same paths>
$ node scripts/test.js tests/utils/files.test.ts tests/roots.test.ts tests/ToolHandler.test.ts tests/McpContext.test.ts

tests/utils/files.test.ts covers each URI shape in both modes — POSIX, drive-letter, percent-encoded drive letter, C| form, UNC share, file://wsl.localhost/… — plus the local round-trip, percent-decoding, that a URL object and its string form agree, and the invariant the original bug broke: a POSIX file URI and the equivalent plain path resolve to the same absolute path. A new roots test drives loadResource's file: branch both ways, reading a file inside a root and still refusing one outside it. The 7 pre-existing resolveCanonicalPath tests still pass.

Mutation-checked, because a green suite is worth little on its own here:

mutation result
restore /^\/[a-zA-Z]:/ the c%3A case fails
drop the hostname === '' check both UNC cases fail
hardcode the path module 8 failures
force {windows: true} 7 failures

One limit worth stating plainly. In windows mode the POSIX root is asserted as path.win32.resolve('/home/user/project'). On a Linux box that is \home\user\project, since the cwd is not a UNC path, so the assertion is correct but currently tautological. It starts discriminating only on a Windows host whose working directory is \\wsl.localhost\…. windows-latest covers the UNC-decoding cases but not that cwd, so this single case rests on the path.win32.resolve contract rather than an observed end-to-end run. Correspondingly, dropping the trailing path.resolve() fails nothing on Linux, because that step is what performs the UNC mapping.

Tested on Node 22.20.0 Linux x64. The POSIX-root and drive-letter/UNC cases are mode-parameterized, so the Windows half of the matrix now runs on every host in run-tests.yml, not just windows-latest.

Pre-existing environmental failures. The full suite's browser-driven files (tests/e2e/chrome-devtools-commands, tests/e2e/chrome-devtools-start-stop, tests/index, tools/pages, tools/performance, tools/screenshot, tools/script) time out on this headless VM with puppeteer navigation timeouts. Those same 7 files fail identically on an unmodified checkout of the current main branch, so they are environmental and not caused by this change. Every other file passes.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Self-reviewed for correctness, security, and performance
  • Automated tests added/updated
  • Cross-platform impact considered — POSIX hosts are unchanged by construction; the Windows path is the one being fixed. No new privileges and no change to the containment check: the helper only chooses how the URI text is decoded, and root resolution still goes through fs.realpath, so symlinked roots stay canonicalized. A root or tool argument that genuinely cannot be resolved still degrades gracefully via the existing allSettled handler. The one new platform-dependent input is the windows parameter, which defaults to process.platform and is only overridden by tests.

@google-cla

google-cla Bot commented Sep 26, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Lightning00Blade Lightning00Blade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Double check if ToolHandler.validateAndResolvePathOrUrl and McpContext.loadResource need to apply this fix as we.

Comment thread src/utils/files.ts Outdated
* Drive-letter (`file:///C:/src`) and UNC (`file://server/share`) URIs keep
* Windows semantics; only unambiguous POSIX paths are reinterpreted.
*/
export function resolveFileUriPath(uri: string): string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's take a parameter windows = process.platform === 'win32' to allow easer testing.

Comment thread src/utils/files.ts Outdated
import {fileURLToPath} from 'node:url';

// A file URL whose path is a Windows drive path, e.g. `file:///C:/src`.
const WINDOWS_DRIVE_FILE_URL_PATH = /^\/[a-zA-Z]:/;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This does not match file:///c%3A/.... Suggestion /^\/[a-z](:|%3a)/i

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

All four taken, and two of them were real bugs rather than style.

The c%3A one is a genuine defect I had wrong. Confirmed directly:

file:///c%3A/Users/user/project   pathname /c%3A/Users/user/project
  windows:true   -> c:\Users\user\project
  windows:false  -> /c:/Users/user/project

My /^\/[a-zA-Z]:/ does not match /c%3A/, so the encoded form took the POSIX branch and came back as /c:/Users/user/project — a path that exists nowhere. Using your /^\/[a-z](:|%3a)/i verbatim; it also covers uppercase %3A, which a case-sensitive check would miss.

file:///C|/src needs no extra case, and I did not add one. URL parsing normalizes the pipe before we ever see it, so pathname is already /C:/src and the existing pattern catches it:

file:///C|/src   pathname /C:/src   windows:true -> C:\src

Adding a \| alternation would have been dead code that looks like coverage. It is asserted anyway, in the matrix.

On the windows parameter — this was the right call and it paid off more than I expected. I had four tests that could only run on windows-latest, which is precisely the reviewer who reported the bug. resolveFileUriPath(uri, windows = process.platform === 'win32') plus path.win32/path.posix moved all of them onto every host, so the whole Windows matrix is now checked on Ubuntu CI.

The pathname.startsWith('/') nit — agreed and dropped. It is always true for a file: URL; all three call sites are already inside a file: branch. I noted the file:-only precondition in the doc comment instead of keeping a dead guard.

Your six cases are the test matrix verbatim, each asserted in both modes. The three mutation checks confirm they have teeth rather than just passing: restoring the old regex fails exactly the c%3A case, dropping the hostname check fails both UNC cases, and hardcoding the path module fails 8.

One limit I could not engineer away, so I do not want it read as covered: file:///home/user/project in windows mode is asserted as path.win32.resolve('/home/user/project'). On this Linux box that is \home\user\project, because the cwd is not a UNC path. The equality is real but currently tautological; it only starts discriminating on a Windows host whose cwd is \\wsl.localhost\..., which is the actual WSL2 setup. windows-latest gives us the UNC-decoding cases but not that cwd, so this one case rests on the path.win32.resolve contract rather than on an observed end-to-end result.

Comment thread tests/utils/files.test.ts
});
});

describe('resolveFileUriPath', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please verify that all the following test cases are handled


file:///home/user/project → path.win32.resolve('/home/user/project')
file:///C:/src → C:\src
file:///c%3A/Users/user/project → c:\Users\user\project (the regression case)
file:///C|/src → C:\src
file://server/share/project → \\server\share\project
file://wsl.localhost/Ubuntu-24.04/home/user/project → \\wsl.localhost\Ubuntu-24.04\home\user\project

Comment thread src/utils/files.ts Outdated
const url = new URL(uri);
const isPosixPath =
url.hostname === '' &&
url.pathname.startsWith('/') &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: This is always true for file urls.

Comment thread src/utils/files.ts Outdated
url.hostname === '' &&
url.pathname.startsWith('/') &&
!WINDOWS_DRIVE_FILE_URL_PATH.test(url.pathname);
return path.resolve(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can also use

const pathModule = windows ? path.win32 : path.posix;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Both do, and you were right to ask — this was the wrong scope cut on my part. There are three client-supplied file: URIs, and I fixed one while leaving two to throw:

  • validateAndResolvePathOrUrl — worse than a plain throw. Its catch swallows ERR_INVALID_FILE_URL_PATH and falls through to validatePath(filePathOrUrl) with the literal string file:///home/user/project/shot.png, which then resolves as a path and reports Cannot resolve base path. So a URI that was never decoded as a URI at all.
  • loadResource — throws straight out, so a sourcePath given as a POSIX file URL fails with a raw ERR_INVALID_FILE_URL_PATH.

All three now go through resolveFileUriPath. 303b50d rebased onto current main and covers both.

On the containment check, since that was the part I most wanted to be sure about: this only decides how the URI text is decoded. Both results still go through validatePath(), so the root comparison, the fs.realpath canonicalization and the allSettled graceful skip are untouched. Nothing can reach outside a root by changing the URI spelling — a POSIX URI decodes to the same string the equivalent plain path already decoded to, so it lands on the same side of startsWith(canonicalRoot + path.sep). There is a test that pins that rather than trusting it: loadResource reads a file inside a root and still refuses one outside it.

I also dropped the now-wrong line in my description claiming the tool-argument fileURLToPath was deliberately out of scope. It wasn't a considered decision, it was the scope I had already written.

One thing worth flagging for whoever picks this up: the third fileURLToPath in the tree, telemetry/WatchdogClient.ts:29, resolves the server's own import.meta.url. That one must keep platform defaults — a posix reinterpretation there would break the watchdog spawn on Windows. It is intentionally not routed through the helper.

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

marking as requested changes

@Yi-111-a
Yi-111-a force-pushed the fix/wsl2-posix-client-roots branch from 0e3528b to f168b22 Compare October 1, 2026 12:07
@OrKoN

OrKoN commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

@Yi-111-a please sign the CLA as well (see the failing check)

A WSL2 client can launch a Windows-side server through WSL interop
(Windows node.exe), and it then addresses its own workspace with POSIX
file URIs such as file:///home/user/project. Node's fileURLToPath()
rejects those on Windows with ERR_INVALID_FILE_URL_PATH.

That threw inside three places, and the three disagreed with each
other, which is what made the result confusing rather than merely
limited. McpContext.validatePath() dropped every such advertised root,
because the root resolution sat inside Promise.allSettled and only
logged "Could not resolve configured root", leaving every path in the
project denied. ToolHandler.validateAndResolvePathOrUrl() swallowed the
same throw in its catch and then re-resolved the literal string
"file:///home/user/project/shot.png" as a path, so the client got
"Cannot resolve base path" instead. McpContext.loadResource() threw it
straight out to the caller. Meanwhile the plain-path case worked all
along: path.resolve("/home/user/project") against the server's UNC
working directory already produced the same canonical form the
requested file resolves to.

Fixes #2839

Adds resolveFileUriPath() next to resolveCanonicalPath() and routes all
three call sites through it, so a root, a tool argument and a resource
URL are all decoded the same way. This only decides how the URI text is
decoded: all three results still go through validatePath(), so the
workspace-root containment check, the fs.realpath canonicalization of
symlinked roots and the allSettled graceful degradation for an
unresolvable root are unchanged, and ToolHandler's http/https/ws/wss
branch is untouched. The helper takes a string or an already-parsed URL,
since two of the three call sites have one in hand.

A file URL with no host and no drive letter is read with
{windows: false}; drive-letter and UNC URIs keep Node's default
platform behaviour. Matching the drive letter has to allow the
percent-encoded colon, because file:///c%3A/Users/user/project is a
drive path that /^\/[a-zA-Z]:/ misses, and reading it as POSIX returns
/c:/Users/user/project. The historical file:///C|/src form needs no
extra case: URL parsing already normalizes the pipe to a colon.

resolveFileUriPath takes windows = process.platform === 'win32' and
picks path.win32 or path.posix from it, so the Windows matrix runs on
any host. Testing is unit-level, per AGENTS.md; nothing here launches a
browser. The cases table covers each URI shape from the review, in both
modes. A roots test drives loadResource's file: branch both ways,
reading a file inside a root and still refusing one outside it.

Mutation-checked, because a green suite is worth little on its own here:
restoring the old regex fails the c%3A case, dropping the hostname check
fails both UNC cases, hardcoding the path module fails 8, and forcing
Windows semantics fails 7. One honest limit: the POSIX root resolving
against a UNC working directory is asserted as
path.win32.resolve(<the plain path>), which only bites on a real
Windows host with that UNC cwd. The windows-latest job in
run-tests.yml covers it; nothing on this machine can.

Verified on Node 22.20.0 Linux x64: tsc clean, eslint clean, prettier
clean. The full suite's browser-driven files (tests/e2e/*, index,
tools/pages, tools/performance, tools/screenshot, tools/script) time out
on this headless VM; those same 7 files fail identically on an unmodified
checkout of the current main branch, so they are environmental. Every
other file passes with this change.
@Yi-111-a
Yi-111-a force-pushed the fix/wsl2-posix-client-roots branch from f168b22 to 303b50d Compare October 1, 2026 12:15
@OrKoN
OrKoN self-requested a review October 14, 2026 08:09
@OrKoN

OrKoN commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Closed due to missing CLA.

@OrKoN OrKoN closed this Oct 14, 2026
@OrKoN

OrKoN commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

@Lightning00Blade @OrKoN, friendly reminder: this PR has new commits since your last review and has been waiting for 12 day(s) (automated reminder)

daedal-ping Bot pushed a commit to DaedalGames/chrome-devtools-mcp that referenced this pull request Nov 17, 2026
…romeDevTools#3380)

A WSL2 client that launches the server on the Windows side advertises
roots like `file:///home/user/project`. On Windows `fileURLToPath`
throws `File URL path must be absolute`, the root is dropped, and every
path inside it is denied (ChromeDevTools#2839).

New `fileUrlToPathWithPosixFallback()` in `src/utils/files.ts` reads a
file URL with no host and no drive letter as POSIX; `path.resolve` then
maps it onto the server's `\\wsl.localhost\<distro>` cwd, the same
canonical form `validatePath` gives the file. Drive-letter and UNC URLs
are unchanged.

User-visible changes on Windows:
- Roots without a drive letter are accepted instead of dropped. If the
server's cwd is a normal drive rather than UNC, `file:///home/user`
resolves to `<current drive>:\home\user`.
- `file:` tool arguments without a drive letter
(`ToolHandler.validateAndResolvePathOrUrl`) are read as POSIX paths
instead of rejected.

Validation (Windows 11, Node 24.18):
- New roots and ToolHandler tests fail on main, pass here. `npm run
test` passes except a symlink test (no privilege here); Linux (docker):
new tests pass.
- Manual WSL2 repro from the issue: client in WSL runs `node.exe`,
`take_screenshot` to `/home/clab/cdm-wsl-test/shot.png`. main denies it,
this branch writes the file.

Fixes ChromeDevTools#2839. Picks up ChromeDevTools#2840, closed for a missing CLA.

---------

Co-authored-by: Nikolay Vitkov <[email protected]>
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.

Client roots from a WSL2 client are dropped by a Windows-side server (fileURLToPath ERR_INVALID_FILE_URL_PATH)

3 participants