Repository navigation
fix(runtime): decode URL-encoded ESM specifiers - #167
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: beb540d1f8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let specifier_utf8 = if let Some(decoded) = &decoded_specifier { | ||
| decoded_specifier_utf8 = decoded.to_utf8(); | ||
| &decoded_specifier_utf8 |
There was a problem hiding this comment.
Return the decoded path from the preserve-symlinks fast path
On POSIX with --preserve-symlinks, an absolute ESM specifier containing a literal backslash and another escape (for example, /tmp/foo\\space%20name.mjs) is checked for existence using this decoded value, but the backslash fast path later returns specifier.clone(), which is still percent-encoded. The subsequent ResolvedEsm load deliberately skips decoding, so Bun attempts to load the nonexistent %20 filename even though the decoded file was just found; preserve and return the decoded specifier on that path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fd75d01e95
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if bun_core::strings::contains_char(path, b'\\') | ||
| && !bun_core::strings::contains_char(path, 0) | ||
| && bun_paths::is_absolute(path) |
There was a problem hiding this comment.
Preserve the suffix in the POSIX backslash fast path
On POSIX with the default preserve_symlinks=false, an absolute ESM path containing a legal backslash, a percent escape, and a query or fragment reaches this fast path after encoded_suffix has been separated. A successful realpath returns before the common epilogue copies that suffix into the query output, so an import such as /tmp/a\\b%20c.mjs?one is registered as plain /tmp/a\\b c.mjs; distinct query imports collapse to one module and import.meta.url loses the suffix. Populate the query before this fast return or route the result through the common epilogue.
Useful? React with 👍 / 👎.
| format_args!("Invalid module \"{}\" must not include encoded \"/\" or \"\\\" characters imported from {}", url_path, referrer), | ||
| ).to_js())); | ||
| } | ||
| let decoded = bun_url::PercentEncoding::decode_alloc(path); |
There was a problem hiding this comment.
Decode before applying the filesystem path-length limit
For percent-heavy but otherwise valid paths, resolve_maybe_needs_trailing_slash applies its existing 1.5 * MAX_PATH_BYTES limit to the encoded specifier before decoding occurs here. For example, on macOS a decoded path of roughly 600 spaces spread across legal components remains below the 1024-byte filesystem limit, while its %20 spelling exceeds the 1536-character guard; Bun therefore reports ENAMETOOLONG for a file Node can load. Apply the filesystem limit after splitting and decoding the pathname, or otherwise account for percent-encoding expansion.
Useful? React with 👍 / 👎.
Decode authored relative and absolute ESM path specifiers after splitting URL query and fragment syntax. Preserve encoded filename delimiters through module-key construction and reject encoded separators and malformed UTF-8 with Node-compatible errors. Keep CommonJS paths, resolved plugin paths, and synthetic CLI entry paths literal. Adapt the ESM decoding approach from oven-sh#35614.
Replace the positional authored-specifier flag with named provenance and remove redundant borrows from the plugin lookup calls. This resolves the Rust CI lint findings without changing module-resolution behavior.
Limit ESM URL decoding to relative and URL-rooted paths so Bun's raw Windows drive-letter and UNC path extensions retain literal behavior. Add a compatibility control and document the boundary. Keep the private provenance enum copyable for its by-value resolver argument.
9683b7d to
c1ffd2e
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c1ffd2e7e6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| && (authored.starts_with(b"./") | ||
| || authored.starts_with(b"../") | ||
| || authored.starts_with(b"/")) |
There was a problem hiding this comment.
Preserve forward-slash UNC specifiers on Windows
On Windows, //server/share/literal%20.mjs is a UNC filesystem path—Bun's path helpers explicitly recognize matching // separators as UNC—but this condition treats every leading / as URL-rooted and decodes it to //server/share/literal .mjs. This changes which module is loaded for Bun's existing forward-slash UNC form, despite the intended literal handling for raw UNC paths. Use the platform-aware path classification to exclude both UNC spellings from decoding.
AGENTS.md reference: src/AGENTS.md:L199-L203
Useful? React with 👍 / 👎.
What does this PR do?
ESM imports with URL-encoded paths fail to load files such as
space # percent% helper.mjs. Decode relative and URL-rooted absolute ESM path specifiers once, after separating their query/fragment, and preserve decoded filename delimiters through the existing module-key path. This also fixes static dependencies in modules loaded throughrequire(esm).Node 24 is the reference.
%3Fnames a literal?in a filename; an unescaped?starts a query.%2Fand%5Creject withTypeError/ERR_INVALID_MODULE_SPECIFIER. Malformed escapes and malformed UTF-8 reject withURIError. Raw Windows drive-letter/UNC path extensions, ordinary CommonJS require paths, already-resolved ESM keys, plugin-resolved filesystem paths, already-decoded file URLs, and synthetic CLI entry paths stay literal. Embedded modules retain their canonical single key.Adapts the ESM-only decoding approach from @robobun in oven-sh/bun#35614. Differences from that proposal are intentional Node parity:
./and../. Raw Windows drive-letter/UNC spellings retain Bun’s literal-path behavior; Node rejects those spellings.?filenames, with distinct query/fragment syntax.URIError.#as a fragment; the existing literal-#test now uses%23to name that file.Each case passes with Node 24.21.0 through static imports, dynamic imports, and ESM graphs loaded by
require(). Related upstream work: #35601. No competing upstream PR is proposed.How did you verify your code works?
bun run build:release test test/js/bun/resolve/resolve.test.ts -t 'ESM specifier percent-encoding': unpatched: 3 pass / 10 fail; patched: 13 pass / 0 fail. One unrelated filtered platform test is skipped in both runs.bun run build:release test test/js/bun/resolve test/js/bun/module-graph test/js/node/module, with the release test-internals environment and test dependencies installed: 3,491 pass, 10 existing skips, 5 todo, 4 inherited failures across 60 files. All resolver and node:module tests pass. The four ModuleGraph failures (two constructor-column expectations, CommonJS namespace keys, and package-exports error wording) reproduce on the unpatched binary from the same base.BUN_CODEGEN_DIR="$PWD/build/release/codegen" bun run rust:check-all: 2 targets pass, 10 targets skipped because their standard libraries are unavailable.src/plugins/native-module-require.test.tsatb658d87152fbb7d667a494435b2e73c5fddc6341: unpatched Bun 15 pass / 1 fail / 1 existing skip; patched Bun 16 pass / 1 existing skip; Node 17 pass. Runs use private HOME/state/temp directories and the selected Bun first on PATH. The existing skip is gated on Node module-hook availability.