Skip to content

fix(asset): preserve IPv6 brackets in server origin for ?url imports - #23336

Closed
vaibhavmashal wants to merge 1 commit into
vitejs:mainfrom
vaibhavmashal:fix/ipv6-brackets-url-encoding
Closed

fix(asset): preserve IPv6 brackets in server origin for ?url imports#23336
vaibhavmashal wants to merge 1 commit into
vitejs:mainfrom
vaibhavmashal:fix/ipv6-brackets-url-encoding

Conversation

@vaibhavmashal

Copy link
Copy Markdown

Problem:
When server.origin contains an IPv6 address, Vite percent-encodes the square brackets when generating URLs for ?url imports. Since the brackets are part of the IPv6 host, they should remain literal instead of being percent-escaped. As a result, browsers like Firefox correctly reject the generated value during fetch() because %5B::1%5D is an invalid hostname, leading to TypeError: Window.fetch: http://%5B::1%5D:5173/... is not a valid URL. This primarily breaks loading of external assets (e.g. WASM or web workers) when the server.origin is explicitly set to an IPv6 literal (e.g. http://[::1]:5173).

Solution:
The problem occurs in encodeURIPath where we use encodeURI(filePath). The standard ECMAScript encodeURI percent-encodes square brackets ([ and ]). Since we use this function to encode the final generated asset URLs, any valid IPv6 bracket characters in the host origin inadvertently become escaped.

The solution ensures we preserve literal brackets [ and ] but ONLY within the host part of the URL (i.e., http://[::1]). We do this by applying a regex to the encoded URI that only targets the <protocol>://<host> substring and safely un-encodes %5B and %5D back to [ and ], while leaving any encoded brackets in the pathname intact. This surgical approach maintains the percent-encoding for file paths that genuinely contain square brackets.

Changes Made:

  • Modified encodeURIPath in packages/vite/src/node/utils.ts to replace %5B and %5D back into literal [ and ] characters exclusively within the host protocol portion of the URI string.
  • Added tests in packages/vite/src/node/__tests__/utils.spec.ts for encodeURIPath to verify it correctly encodes file paths, while strictly ignoring brackets in the host component of the IPv6 address.

Testing:
To reproduce:

  1. Initialize a minimal Vite project with an asset import assetUrl from './assets/vite.svg?url'.
  2. Configure vite.config.js with server: { origin: 'http://[::1]:5173' }.
  3. Try fetching assetUrl from the application in Firefox; observe TypeError due to invalid URL structure.

With this fix, the tests are fully green (pnpm test-unit utils), ensuring backwards compatibility while the origin correctly remains http://[::1]:5173 upon usage.

Fixes #23108

Copilot AI lite review requested due to automatic review settings August 23, 2026 20:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR label Aug 23, 2026
@github-actions

Copy link
Copy Markdown

This PR has been automatically flagged as likely to be created by a bot, LLM, or agent, and will be automatically closed. These contributions harm the maintenance of the project. Please read our AI policy for more information.

If you believe this is a mistake, please reply to this comment and we will review it.

@github-actions github-actions Bot closed this Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

?url imports percent-encode IPv6 brackets from server.origin

2 participants