Skip to content

fix(javascript): keep empty URL path segments significant - #3792

Merged
kaoru0822-kitauji merged 2 commits into
mainfrom
fix/3307-url-empty-segments
Oct 15, 2026
Merged

kaoru0822-kitauji merged 2 commits into
mainfrom
fix/3307-url-empty-segments

Conversation

@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

Fixes #3307

Root cause

JavaScriptArtifactPathResolution joined ESM relative imports and HTML script references (including the document base) with posix.join. POSIX normalization collapses // before it applies .., so ./assets//../app.js became app.js. Under URL semantics, which is how both ESM and HTML resolve, .. removes the preceding empty segment, giving assets/app.js. When both files existed, the graph linked the importer and the renderer to the wrong file and still reported resolved with complete coverage. URL references had the opposite problem: they refused every //, even when dot segments cancel it to an exact path.

Change

There is now one owner for URL-path candidates, urlRelativeCandidate(directory, reference):

  • URL dot-segment rules apply over canonical segments: . is dropped, and .. pops the previous segment even when it is empty.
  • A remaining empty segment, such as ./assets//app.js, stays unavailable, with the existing "Repeated URL path separators…" limitation. A trailing empty segment is directory form and is handled by the existing exact-file check.
  • A reference that climbs above the artifact root still produces a .. candidate, so confineCandidate rejects it as before.

ESM imports (module-specifier + import), HTML references (with and without <base href>, including the base itself) and URL references all use it. CommonJS require and runtime entrypoints keep filesystem path semantics. The docs (javascript-artifact-reconstruction.md) state the rule.

Validation

The issue's public CLI repro (index.html, main.mjs, main.cjs; both app.js and assets/app.js exist):

source main this PR
index.html module script app.js (wrong) assets/app.js
main.mjs ESM import app.js (wrong) assets/app.js
main.cjs require app.js app.js (unchanged, filesystem semantics)

Each target matches new URL(…) and Node's CommonJS resolver respectively. Output grows from 70,675 to 70,696 B, from the longer resolved paths.

Real applications, CLI on an older main build vs this PR:

  • Discord app.asar: normalized output identical (108,108,885 B both; 15.2 s vs 14.3 s).
  • Notion app.asar (--integrity-policy record-and-continue): equivalent once host-budget numbers are erased and hash ordering is ignored (125.8 MB both; 41.3 s vs 38.3 s).

These bundles contain no // references, so nothing changes for them.

Tests:

  • keeps empty URL path segments significant replaces does not collapse empty URL path segments. Each case is checked against new URL first, then across url-reference, html-reference, ESM import and CommonJS require. ./nested//worker.js stays unresolved for the URL contexts. ./nested//../worker.js resolves to dist/nested/worker.js for the URL contexts and to dist/worker.js for require.
  • That test fails on main for both cases; I checked by restoring main's resolver.
  • 158 JavaScript/domain test files pass (2,966 tests), along with npm run check:fast and npm run docs:check.

Behavior change to note: URL references such as new URL("./nested//../worker.js", import.meta.url) previously stayed unresolved. They now resolve to the exact URL target, matching the HTML and ESM contexts and the URL parser.

🤖 Generated with Claude Code

ESM imports and HTML script references were joined with POSIX path
rules, which collapse '//' before applying '..'. URL resolution removes
the empty segment instead, so './assets//../app.js' names assets/app.js,
not app.js, and the graph linked the importer and renderer to the wrong
file. URL references refused every '//' even when dot segments cancel it.

Resolve ESM, HTML (including document base) and URL references through
one URL dot-segment helper. A remaining empty segment stays unavailable,
references above the artifact root are still rejected, and CommonJS keeps
filesystem path semantics.

Fixes #3307

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji kaoru0822-kitauji added the track:perf Performance track PRs (issues & perf loop) label Oct 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-15T01:33:30.191877Z 0b972b8 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; both previous findings are fixed and no new blocking issue was found.

Summary

The PR gives ESM, HTML, and URL references shared URL-path rules while leaving CommonJS filesystem rules unchanged.

  • ESM, HTML, and URL paths let .. remove an empty segment.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[HTML document directory] --> B[Apply base href]
  B --> C[Keep segments and escape count]
  C --> D[Remove final base segment]
  D --> E[Apply script reference]
  E --> F[Reject remaining empty segments]
  F --> G[Check artifact root]
  G --> H[Look up exact file]
Loading

Reviews (2) · Last reviewed commit: "fix(javascript): resolve HTML base and r..." · Reviewed by Greptile

The base href was turned into a canonical path before the script
reference was applied. A base empty segment that the reference's '..'
would remove was rejected early, and an above-root '..' from the base
became a removable segment that the reference's '..' could cancel.

Keep one URL path state through base and reference resolution, with
above-root escapes held apart from removable segments, and reject empty
segments only in the final path. Remove the superseded htmlBaseDirectory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kaoru0822-kitauji
kaoru0822-kitauji merged commit 90c6922 into main Oct 15, 2026
32 checks passed
@kaoru0822-kitauji
kaoru0822-kitauji deleted the fix/3307-url-empty-segments branch October 15, 2026 02:12
@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working track:perf Performance track PRs (issues & perf loop)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ESM imports and HTML scripts resolve to the wrong file after an empty URL path segment

2 participants