Repository navigation
Conversation
|
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
left a comment
There was a problem hiding this comment.
Double check if ToolHandler.validateAndResolvePathOrUrl and McpContext.loadResource need to apply this fix as we.
| * 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 { |
There was a problem hiding this comment.
Let's take a parameter windows = process.platform === 'win32' to allow easer testing.
| 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]:/; |
There was a problem hiding this comment.
This does not match file:///c%3A/.... Suggestion /^\/[a-z](:|%3a)/i
There was a problem hiding this comment.
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.
| }); | ||
| }); | ||
|
|
||
| describe('resolveFileUriPath', () => { |
There was a problem hiding this comment.
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
| const url = new URL(uri); | ||
| const isPosixPath = | ||
| url.hostname === '' && | ||
| url.pathname.startsWith('/') && |
There was a problem hiding this comment.
Nit: This is always true for file urls.
| url.hostname === '' && | ||
| url.pathname.startsWith('/') && | ||
| !WINDOWS_DRIVE_FILE_URL_PATH.test(url.pathname); | ||
| return path.resolve( |
There was a problem hiding this comment.
We can also use
const pathModule = windows ? path.win32 : path.posix;
There was a problem hiding this comment.
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. ItscatchswallowsERR_INVALID_FILE_URL_PATHand falls through tovalidatePath(filePathOrUrl)with the literal stringfile:///home/user/project/shot.png, which then resolves as a path and reportsCannot resolve base path. So a URI that was never decoded as a URI at all.loadResource— throws straight out, so asourcePathgiven as a POSIX file URL fails with a rawERR_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
left a comment
There was a problem hiding this comment.
marking as requested changes
0e3528b to
f168b22
Compare
|
@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.
f168b22 to
303b50d
Compare
|
Closed due to missing CLA. |
|
@Lightning00Blade @OrKoN, friendly reminder: this PR has new commits since your last review and has been waiting for 12 day(s) (automated reminder) |
…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]>
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 withERR_INVALID_FILE_URL_PATH. There are three client-suppliedfile:URIs, and each one failed differently:Promise.allSettled, so the throw was only logged asCould not resolve configured root …and the root skipped. Every path inside the project was then refused:filePathtool argument was reported as an unreadable path, because the throw was caught and the literal stringfile:///home/user/project/shot.pngwas then resolved as a path.file:loadResourceURL 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 newresolveFileUriPath()insrc/utils/files.ts:{windows: false}.file:///C:/src, and the percent-encodedfile:///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 formresolveCanonicalPathderives for the requested file, so the existingstartsWith(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/projectreturns/c:/Users/user/project, a path that exists nowhere. The historicalfile:///C|/srcform needs no separate case — URL parsing normalizes the pipe to a colon before the helper sees it.resolveFileUriPathtakeswindows = process.platform === 'win32'and selectspath.win32orpath.posixfrom it, so the Windows matrix is testable from any host. It also accepts an already-parsedURL, 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, thefs.realpathcanonicalization of symlinked roots and theallSettledgraceful 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'shttp/https/ws/wssbranch is untouched.telemetry/WatchdogClient.ts'sfileURLToPathis intentionally not routed through the helper: it resolves the server's ownimport.meta.url, which must keep platform defaults or the watchdog spawn breaks on Windows.The resolution moved into
src/utils/files.tsnext toresolveCanonicalPath, 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.tests/utils/files.test.tscovers 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 aURLobject 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 newrootstest drivesloadResource'sfile:branch both ways, reading a file inside a root and still refusing one outside it. The 7 pre-existingresolveCanonicalPathtests still pass.Mutation-checked, because a green suite is worth little on its own here:
/^\/[a-zA-Z]:/c%3Acase failshostname === ''check{windows: true}One limit worth stating plainly. In
windowsmode the POSIX root is asserted aspath.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-latestcovers the UNC-decoding cases but not that cwd, so this single case rests on thepath.win32.resolvecontract rather than an observed end-to-end run. Correspondingly, dropping the trailingpath.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 justwindows-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 currentmainbranch, so they are environmental and not caused by this change. Every other file passes.Checklist
fs.realpath, so symlinked roots stay canonicalized. A root or tool argument that genuinely cannot be resolved still degrades gracefully via the existingallSettledhandler. The one new platform-dependent input is thewindowsparameter, which defaults toprocess.platformand is only overridden by tests.