Skip to content

fix(server): handle malformed URI in unbundled indexHtml middleware - #23616

Open
maxymlyskov wants to merge 2 commits into
vitejs:mainfrom
maxymlyskov:fix/malformed-uri-in-indexhtml-middleware
Open

maxymlyskov wants to merge 2 commits into
vitejs:mainfrom
maxymlyskov:fix/malformed-uri-in-indexhtml-middleware

Conversation

@maxymlyskov

Copy link
Copy Markdown

An .html request with a broken percent escape crashes the dev server and the preview server. In the unbundled branch, viteIndexHtmlMiddleware calls decodeURIComponent on the url without a guard. The middleware is async and connect only catches synchronous throws, so the URIError becomes an unhandled rejection and Node exits.

With vite packed from main and installed into a small app (Node 22.12):

--- vite dev
/ -> 200
/%E0%A4%A.html -> 000
/ -> 000
vite exited with code 1
URIError: URI malformed
    at viteIndexHtmlMiddleware (…/node_modules/vite/dist/node/chunks/node.js:18132:63)
--- vite preview
/ -> 200   /%E0%A4%A.html -> 000   / -> 000   vite exited with code 1

With this change both return 404 for the bad url and keep serving /.

The bundled-dev branch of the same function has guarded this since #22781. This wraps the two decodes in the unbundled branch the same way and falls through to next(). The decode order stays as it is.

#21313 and #22789 tried this before. On #21313 the process didn't exit; with an installed vite on current main it does, as above. #22789 decoded before fsPathFromId, which changes the order; this keeps it.

The test in indexHtml.spec.ts requests /%E0%A4%A.html and /@fs/%E0%A4%A.html, expects a 404 for each, then a 200 for /. On main it times out with the unhandled URIError.

AI usage: I used AI to check my reasoning and look things up, and had it generate several candidate fixes. I compared them and picked this one myself.

An .html request with a bad percent escape, such as /%E0%A4%A.html,
reaches the non-bundled branch of viteIndexHtmlMiddleware, where
decodeURIComponent throws a URIError. The middleware is async and
connect only catches synchronous errors, so the rejection is unhandled
and the dev and preview servers exit with code 1.

Guard the decode like the bundled-dev branch of the same function does
since vitejs#22781, and fall through to next(). The added test gets no
response on main (it times out with the unhandled URIError) and passes
with this change.
The node-22 job failed in playground/hmr (circular imports poll timeout),
which this change does not touch.
@maxymlyskov
maxymlyskov force-pushed the fix/malformed-uri-in-indexhtml-middleware branch from 97efd82 to 853bd37 Compare October 21, 2026 21:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant