Improper escaping of HTML attribute when transformIndexHtml
is used
#18040
Labels
contribution welcome
feat: html
p3-minor-bug
An edge case that only affects very specific usage (priority)
Describe the bug
Vite plugins which implement the
transformIndexHtml()
function can choose to return an array, representing HTML tags to be added to the document. Theattrs
of these tags are improperly escaped and can lead to arbitrary HTML/scripts being injected into the index.html file.The
serializeAttrs
function used by the built-inhtml
plugin incorrectly escapes HTML attributes usingJSON.stringify
:vite/packages/vite/src/node/plugins/html.ts
Line 1513 in 1a76300
This code dates back to 4 years ago: 9ce2ab4#diff-89bae1df62862bb7f4a03d82a1e9cbf4ac6d0c042f21fbbacb0a2238bd050042R140
Reproduction
https://stackblitz.com/edit/vitejs-vite-swzvsz?file=vite.config.ts
Steps to reproduce
Using the following
vite.config.ts
:The resulting index.html file now includes this:
(The leading
"
was incorrectly escaped with\
. One correct way to escape this in a HTML attribute would be"
.)System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: