fix(asset): preserve IPv6 brackets in server origin for ?url imports - #23336
Closed
vaibhavmashal wants to merge 1 commit into
Closed
fix(asset): preserve IPv6 brackets in server origin for ?url imports#23336vaibhavmashal wants to merge 1 commit into
vaibhavmashal wants to merge 1 commit into
Conversation
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
When
server.origincontains an IPv6 address, Vite percent-encodes the square brackets when generating URLs for?urlimports. 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 duringfetch()because%5B::1%5Dis an invalid hostname, leading toTypeError: 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 theserver.originis explicitly set to an IPv6 literal (e.g.http://[::1]:5173).Solution:
The problem occurs in
encodeURIPathwhere we useencodeURI(filePath). The standard ECMAScriptencodeURIpercent-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%5Band%5Dback 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:
encodeURIPathinpackages/vite/src/node/utils.tsto replace%5Band%5Dback into literal[and]characters exclusively within the host protocol portion of the URI string.packages/vite/src/node/__tests__/utils.spec.tsforencodeURIPathto verify it correctly encodes file paths, while strictly ignoring brackets in the host component of the IPv6 address.Testing:
To reproduce:
import assetUrl from './assets/vite.svg?url'.vite.config.jswithserver: { origin: 'http://[::1]:5173' }.assetUrlfrom the application in Firefox; observeTypeErrordue to invalid URL structure.With this fix, the tests are fully green (
pnpm test-unit utils), ensuring backwards compatibility while the origin correctly remainshttp://[::1]:5173upon usage.Fixes #23108