Repository navigation
Store node:fs names as written, not percent-encoded - #8663
Conversation
There was a problem hiding this comment.
Superseded by the review of
300739e7.
This review, of aac2e3d0
💬 Approved with comments — The readFile path-as-written fix and the errno, full path and dest changes look correct and are tested. The one INFO note is that other fs calls still report percent-encoded paths.
Findings: 1 info (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
The readFile fix and the errno, full path and dest additions are correct. Other fs calls still report percent-encoded paths.
Recommendation: 💬 Optional Nits
- [INFO] Only readFile reports the path as written. unlink, mkdir, rm, writeAll and rename/copy still report the percent-encoded URL pathname. (
src/workerd/api/filesystem.c++:464)
View full review
The new errorPath handling for readAll is correct. errorPath() runs after normalizePath() in readFileSync, so the null-byte check still runs first. A URL argument still reports its pathname. The new tests cover a space, a literal %20, a quote and a non-ASCII character. The copyFile/ERR_EEXIST change to path=src, dest=dest matches Node, and the Rust errno and dest-in-message changes have unit tests.
One note: only readFile reports the path as written. unlink, mkdir, rm, writeAll and renameOrCopy now report the full path, but they build it from url.getPathname(), so it is still percent-encoded when the path contains special characters.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: aac2e3d0
aac2e3d to
300739e
Compare
There was a problem hiding this comment.
Superseded by the review of
ce0b54f4.
This review, of 300739e7
💬 Approved with comments — The readFile path-as-written fix and the readAll declaration change look correct and are tested. One INFO remains: other fs calls still report percent-encoded paths.
Findings: 1 info
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 info
The filesystem.h change is correct. The earlier note about other calls reporting percent-encoded paths still applies.
Recommendation: 💬 Optional Nits
- [INFO] Only readFile reports the path as written. unlink, mkdir, rm, writeAll, renameOrCopy and the link dest still report the percent-encoded URL pathname. (
src/workerd/api/filesystem.c++:464)
View full review
The filesystem.h change only adds the optional errorPath parameter to the readAll declaration, and it matches the definition and the filesystem.d.ts signature. errorPath() runs after normalizePath(), so the null-byte check still runs first. A URL argument still reports its pathname.
One note, unchanged since the last review: unlink, mkdir, rm, writeAll, renameOrCopy and the link dest still build their error path from url.getPathname(). When a path contains a space, a quote or non-ASCII characters, these calls still report it percent-encoded.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 300739e7
guybedford
left a comment
There was a problem hiding this comment.
I don't think we should patch this per call site. The encoded path in the error is a symptom of node:fs keying the VFS by percent-encoded URL pathname, and that leaks everywhere, not just readFile errors:
- writeFileSync / openSync / statSync / unlinkSync / readdirSync on a missing
/tmp/a b/xstill report/tmp/a%20b/xwith this PR - mkdirSync('/tmp/sp ace å') then readdirSync('/tmp') returns ['sp%20ace%20%C3%A5']
- '/tmp/p%20q' and '/tmp/p q' are the same file, as are '/tmp/%41' and '/tmp/A'
Passing a second display string into readAll fixes one of these and would need repeating for every op (twice for rename/link/copyFile), and it can't fix the names readdir returns or the aliasing.
I'd rather fix the boundary once: make the path -> URL conversion in normalizePath lossless (encode % as %25, as pathToFileURL does) and decode once in NormalizedFilePath, so the VFS stores real names and every error path and readdir result is correct without extra parameters.
Separately, the loadEnvFile test fix is worth keeping on its own: the second block passed when nothing was thrown. That can land now with the code asserts, and the path asserts can follow the boundary fix. Note Node reports .env there, not /bundle/.env.
300739e to
ce0b54f
Compare
There was a problem hiding this comment.
Superseded by the review of
72999d0f.
This review, of ce0b54f4
💬 Approved with comments — Storing decoded names in the VFS fixes the earlier note: every fs call now reports the path as written, and the tests cover it. One WARNING remains: relative paths resolve wrongly when the working directory's name contains %, # or ?.
Findings: 1 warning · 1 info (2 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #8663.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Code Quality] Relative paths resolve against the raw working directory, which is put unencoded into the file: URL base. Under a working directory whose name contains `%`, `#` or `?`, they now reach the wrong file.
- **File:** src/node/internal/internal_fs_utils.ts:179
## INFO (1)
### [Code Quality] A non-file: URL now throws ERR_INVALID_FILE_URL_HOST from getPathFromURLPosix() before any scheme check, where Node.js throws ERR_INVALID_URL_SCHEME.
- **File:** src/node/internal/internal_fs_utils.ts:154
📋 Code Quality: ⚠️ 1 warning · 1 info
The fix is complete across fs calls. Relative paths still break under a working directory whose name contains %, # or ?, and a non-file: URL gets the wrong error.
Recommendation: 📝 Minor Comments
- [WARNING] Relative paths resolve against the raw working directory, which is put unencoded into the file: URL base. Under a working directory whose name contains
%,#or?, they now reach the wrong file. (src/node/internal/internal_fs_utils.ts:179) - [INFO] A non-file: URL now throws ERR_INVALID_FILE_URL_HOST from getPathFromURLPosix() before any scheme check, where Node.js throws ERR_INVALID_URL_SCHEME. (
src/node/internal/internal_fs_utils.ts:154)
View full review
The earlier note is fixed: unlink, mkdir, rm, writeAll, renameOrCopy and the link dest now build their error path with fileUrlToPathString(), and fs-errors-test.js covers them.
Working-directory base is not encoded (src/node/internal/internal_fs_utils.ts:185-190). A relative path resolves against `file://${processImpl.getCwd()}/`. getCwd() returns the decoded kj::Path (src/workerd/api/node/process.c++:221-226), and setCwd() looks the path up in the VFS without decoding it. Now that the VFS stores real names, process.chdir('/tmp/a#b') succeeds. A later readFileSync('x') then resolves against file:///tmp/a#b/, where # starts a fragment, so it reaches /tmp/x. A directory named a%20b resolves as a b, and ? cuts off the rest of the path. The fix is to encode the working directory with the same [%#?\\\t\n\r] replacement, or with a pathToFileURL-style helper, before using it as the base.
Non-file: URL error (src/node/internal/internal_fs_utils.ts:154). getPathFromURLPosix() now runs before any scheme check. new URL('https://example.com/x') throws ERR_INVALID_FILE_URL_HOST, where Node.js throws ERR_INVALID_URL_SCHEME.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
⚡ Performance: ✅ LGTM
The extra decoding and encoding is linear in the path length, and the bundle directory decodes each module name once, when the directory is first built. No measurable regression is expected.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: ce0b54f4
ce0b54f to
72999d0
Compare
There was a problem hiding this comment.
Superseded by the review of
446bc7de.
This review, of 72999d0f
✅ Approved — Both earlier findings (working-directory URL base encoding, non-file: URL error) are fixed, and no new issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
decodeFileName rejects decoded names that contain / or NUL, or that are . or ... Encoded traversal such as %2e%2e or %2F therefore cannot escape the VFS root, and every caller treats a rejected name as invalid. encodeFileName puts ./ before names, so a web-API name cannot be read as a URL scheme.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
- Working-directory base: fixed. The base is now
file://${encodePathChars(processImpl.getCwd())}/(src/node/internal/internal_fs_utils.ts:187), so%,#,?,\, tab, CR and LF in the working directory are encoded.fs-errors-test.jscovers this with a relative write under ac#d%20directory. - Non-file: URL error: fixed.
getPathFromURLPosix()runs only whenpath.protocol === 'file:'(internal_fs_utils.ts:155).
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 72999d0f
72999d0 to
446bc7d
Compare
There was a problem hiding this comment.
Superseded by the review of
9faa80eb.
This review, of 446bc7de
✅ Approved — The only change since the last approved review turns on the loadEnvFile missing-file path assert. That assert matches how readAll reports ENOENT paths, and no new issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found. decodeFileName still rejects decoded names that contain / or NUL, and names that are . or ... Encoded traversal therefore cannot escape the VFS root.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
The update to process-nodejs-test.js turns on the missing-file path assert. That assert holds against the code:
loadEnvFilepasses the path toreadFileSyncas a string.normalizePathencodes%as%25, so the literal%20in the test path is stored as written.readAllreports ENOENT throughfileUrlToPathString, which decodes back to the original string.
The no-argument case is still a TODO, and its comment correctly says why: the default .env resolves against the working directory, so node:fs reports an absolute path where Node.js reports .env.
The pull request accepts a compatibility risk: readdir now returns decoded names, and encoded aliases are now separate files, with no compatibility flag. The description states this and gives the reason.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 446bc7de
guybedford
left a comment
There was a problem hiding this comment.
The boundary approach works well. I ran a scratch worker over symlinks, recursive cp, glob, opendir, rm, rename, realpath, mkdtemp, chdir with relative paths and the web file system, with names containing spaces, %, #, ? and \, and they all behave as written. Two things:
- One lossy case is left in
normalizePath(), see the inline comment. - On shipping without a flag:
/bundlebeing read-only doesn't cover lookups.fs.readFileSync(new URL('./da ta.txt', import.meta.url).pathname)finds the file today and is ENOENT with this change. It also fails in Node.js. The compat rationale in the commit message should cover this case.
446bc7d to
9faa80e
Compare
There was a problem hiding this comment.
Superseded by the review of
1a65cac8.
This review, of 9faa80eb
✅ Approved — The change since the last approved review widens the encoded character range so names with leading or trailing spaces and control characters are kept, and adds tests for them. No issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
decodeFileName still rejects a decoded /, NUL, . or .., and every lookup in the virtual file system goes through it, so an encoded traversal cannot escape the root. A file: URL argument with an encoded / or NUL is rejected, as in Node.js.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
encodePathChars now encodes %, #, ?, \ and \x00-\x20. This covers the leading and trailing spaces and the tabs and newlines that the URL parser would strip. The working directory goes through the same encoder, and the only caller that passes base passes a URL, which is not re-encoded. Every character in the class is ASCII, so encodeURIComponent cannot throw on a lone surrogate. The new fs-errors-test cases cover these names.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 9faa80eb
guybedford
left a comment
There was a problem hiding this comment.
Both points from the last review are fixed. I re-ran my scratch worker on the new head with leading and trailing spaces and C0 control names through write, readdir, symlinks, rename, recursive cp, opendir, glob, mkdtemp and relative paths, and they all round-trip. One non-blocking nit inline.
9faa80e to
1a65cac
Compare
There was a problem hiding this comment.
Superseded by the review of
182da28b.
This review, of 1a65cac8
✅ Approved — The change since the last approved review widens the encoded character range so names with leading or trailing spaces and control characters round-trip, and it adds tests for them. No issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
decodeFileName still rejects a decoded /, NUL, . or .., so an encoded traversal cannot get out of the virtual file system root. A file: URL argument containing an encoded / or NUL is rejected, as in Node.js. Every character in the widened encoding class is ASCII, so the encoder cannot throw on a lone surrogate.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
encodePathChars now encodes %, #, ?, \ and \x00-\x20, which covers the leading and trailing spaces and the control characters that the URL parser would otherwise strip. The working directory goes through the same encoder, and the new fs-errors-test cases cover these names.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 1a65cac8
1a65cac to
182da28
Compare
There was a problem hiding this comment.
Superseded by the review of
89ce281c.
This review, of 182da28b
✅ Approved — No issues found. The changes since the last approved review send the remaining error paths and the web file system API through the shared decoding helpers, and add tests for them.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
decodeFileName returns none for a decoded /, NUL, . or .., so an encoded traversal cannot get out of the VFS root. The KJ_ASSERT_NONNULL(fileUrlToPath(...)) calls in the FsMap getters only see configured root URLs, not caller input. Bundle module names that fail to decode are skipped. Error paths show decoded names, which come from the caller's own input.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
Each pathname segment is decoded once by fileUrlToPath, decodeFileName and fsRelative, and callers handle the new FsError::INVALID_PATH result. The .pathname uses that built error paths, including the chown/lchown fix, now go through getPathFromURLPosix. The remaining getPathname() uses in filesystem.c++ only check for a trailing slash. Tests cover leading and trailing spaces, control characters, encoded aliases, file: URL decoding errors, symlinks, relative paths and the web file system API.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 182da28b
chown(), lchown() and lchownSync() built their ENOENT error from the path argument cast to a URL. For a string or Buffer path that has no pathname, so the error's path and message said undefined: "no such file or directory, chown 'undefined'". In this patch they use the normalized URL, as chownSync() and the chmod functions already do.
node:fs turns a path into a file: URL, and the virtual file system
stored each name as the URL's percent-encoded pathname segment. That
leaked into everything that shows a name. readdirSync() after
mkdirSync('/tmp/sp ace å') returned 'sp%20ace%20%C3%A5', and
errors reported '/tmp/a%20b/x' for '/tmp/a b/x'. Paths that differ only
in encoding were the same file: '/tmp/p%20q' and '/tmp/p q', and
'/tmp/%41' and '/tmp/A'. The web file system API shares the VFS and
had the same aliasing.
In this patch normalizePath() percent-encodes %, #, ?, \, space and
the C0 controls, which the URL parser would otherwise decode, strip or
treat as a delimiter, so a string path converts to a URL without losing
anything. A name with a leading or trailing space keeps it. The VFS percent-decodes each pathname segment
once (fileUrlToPath()), so it stores real names. readdir, error paths
and messages, readlink and mkdtemp results then show names as written,
and encoded spellings are different files. A file: URL argument is
decoded the way fileURLToPath() decodes it: an encoded / throws
ERR_INVALID_FILE_URL_PATH and an encoded NUL throws
ERR_INVALID_ARG_VALUE, as in Node.js. NormalizedFilePath no longer
normalizes the URL's escapes, since decoding makes '/foo' and '/%66oo'
the same file. The web file system API and symbolic link targets
resolve names back to URLs with encodeFileName(), so they reach the
same entries.
fs-access-test checked the old aliasing (a file written as
'/tmp//?#a%FF' was found as '/tmp/%3F%23a%FF') and now checks the
new names.
Compat: this changes the names readdir returns for names with spaces,
non-ASCII or other encoded characters, and splits the encoded aliases
into separate files. Code that passes a URL's percent-encoded pathname
as a string path, for example
fs.readFileSync(new URL('./da ta.txt', import.meta.url).pathname),
found the file before and now gets ENOENT, as it does in Node.js;
passing the URL itself works. Writing and reading a file by the same
string works as before and /tmp is per-request, so we ship it without
a compatibility flag.
The ported loadEnvFile test left its path asserts commented out, since node:fs reported a percent-encoded path. Now that it reports the path as written, the missing-file assert runs. The no-argument case stays a TODO: Node.js reports the relative '.env' as passed, where node:fs reports '/bundle/.env'.
182da28 to
89ce281
Compare
There was a problem hiding this comment.
✅ Approved — No issues found. Since the last approved review, the remaining error paths and the web file system API go through the shared decoding helpers, and the decoding stays consistent across rename, move and copy checks.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found. An encoded /, NUL, . or .. is still rejected when a name is decoded.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
fsRelative() and fsPath() decode each segment once through decodeFileName() and fileUrlToPath(), and NormalizedFilePath rejects paths that fail to decode at construction. Error messages use fileUrlToPathString(). The checks for renaming to the same path, moving into itself and copying into itself compare decoded strings on both sides. The web file system handlers build their entries with encodeFileName(), and the lock lookups use the same spelling.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 89ce281c
node:fs turns a path into a file: URL, and the virtual file system stored each name as the URL's percent-encoded pathname segment. That leaked into everything that shows a name:
The web file system API shares the VFS and had the same aliasing.
In this patch the path to URL conversion is lossless and is decoded once.
normalizePath()percent-encodes%,#,?,\, space and the C0 controls, which the URL parser would otherwise decode, strip or treat as a delimiter, and the VFS percent-decodes each pathname segment, so it stores real names. A name with a leading or trailing space keeps it. readdir, error paths and messages, and the readlink and mkdtemp results show names as written, and encoded spellings are different files. Afile:URL argument is decoded the wayfileURLToPath()decodes it: an encoded/throwsERR_INVALID_FILE_URL_PATHand an encoded NUL throwsERR_INVALID_ARG_VALUE, as in Node.js. The web file system API and symbolic link targets turn names back into URLs with one helper, so all of them reach the same entries.This changes the names readdir returns for names with spaces, non-ASCII or other encoded characters, and splits the encoded aliases into separate files. Code that passes a URL's percent-encoded pathname as a string path, for example
fs.readFileSync(new URL('./da ta.txt', import.meta.url).pathname), found the file before and now gets ENOENT, as it does in Node.js; passing the URL itself works. Writing and reading a file by the same string works as before and/tmpis per-request, so this ships without a compatibility flag. fs-access-test checked the old aliasing and now checks the new names.The first commit fixes
chown(),lchown()andlchownSync(), whose ENOENT saidpath: undefinedfor a string path. The last enables the loadEnvFile missing-filepathassert. The no-argument case stays a TODO, because Node.js reports the relative.envthere.fs-errors-test checks error paths for readFile, writeFile, unlink, stat, open, mkdir and rename on a path with a space,
%20, a quote,#,?, a trailing space and non-ASCII, plus readdir names (including leading and trailing spaces and a tab), alias separation, relative paths under such a working directory,file:URL decoding and the%2F/%00errors. webfs-test checks the same names through the web API, and worker-fs-test and bundle-fs-test cover the C++ helpers and bundle module names. The new tests fail without the fix.