feat(core): allow configuring extra allowed origins for the DevTools WS transport#471
Merged
Merged
Conversation
…WS transport The WS transport's origin check only recognizes literal loopback hostname patterns (localhost, 127.0.0.1, etc) — it's a string match, not a DNS/hosts-file resolution, so a custom dev hostname that resolves to loopback via /etc/hosts (a common pattern for running multiple HTTPS dev servers locally) is rejected with a 403 before the WS handshake completes, even though the connection is genuinely local. Adds `DevToolsConfig.allowedOrigins` and threads it through to `attachWsRpcTransport`, which already supports this via its own `allowedOrigins` option — it just wasn't exposed.
antfu
approved these changes
Jul 24, 2026
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.
Summary
The WS transport's origin check (
isAllowedOriginindevframe'sattachWsRpcTransport) only recognizes literal loopback hostname patterns (localhost,127.0.0.1,::1,*.localhost) — it's a plain string match against theOriginheader, not a DNS/hosts-file resolution. A custom dev hostname that resolves to loopback via/etc/hosts(a common pattern for running several HTTPS dev servers locally, each with its own hostname/cert) is invisible to that check, so every real browser connection gets rejected with a403before the WS handshake completes — even though the connection is genuinely local.attachWsRpcTransportalready accepts anallowedOriginsoption for exactly this case, but@vitejs/devtools's owncreateWsServernever passes it through, andDevToolsConfighas no field for it.This PR:
DevToolsConfig.allowedOrigins?: string[]attachWsRpcTransport({ ..., allowedOrigins })inpackages/core/src/node/ws.tsRead through a narrow local cast rather than
context.viteConfig.devtools?.config?.allowedOriginsdirectly, since Vite's published types bundle a frozen snapshot ofDevToolsConfigand won't see a brand-new field until re-vendored — the same gapvite-augment.tsalready works around forPlugin.devtools.Test plan
Reproduced against a local embedded (route-bound) DevTools instance behind a custom
/etc/hosts-aliased HTTPS hostname:/__devtools/__wsgot403 Forbiddenimmediately (confirmed via a rawhttps.requestupgrade probe with the browser's actualOriginheader) — the client sat instatus: "error"/isTrusted: falseand eventually surfaced "Unauthorized" / a 60s RPC-trust timeout in the UI.devtools: { allowedOrigins: [...] }: the WS handshake completes (101), the client reachesstatus: "connected"/isTrusted: true, and a custom dock renders live data.tsc --noEmitonpackages/coreshows no new errors from these two files.oxlinton the two touched files is clean.