vite: vite 6 breaks ability to using special chars in html tags attrs
Describe the bug
when using ejs tags (like html: {cspNonce: '<%= nonce _%>'}, or experimental: { renderBuiltUrl(filename) {return <%= somePath %>/${filename};}})) , or URLS that contains multiple query strings parameters http:host.com/url?query=123&limit=100, in any html tag attribute, the ", ', &, <, and >. are escaped and so the output html is no longer correct.
(the typical use case for the ejs approach is to have express render the built html page and populate those template variables at runtime).
The bug is not present in vite 5 and was introduced by #18067 , to fix issue #18040. The issue seems to me to have no actual foundation (cause if some malicious function is able to inject a tag, escaping attrs is pretty much useless as code can be injected in much easier ways anyway) and the fix mangle every attrs that use the above special character.
Reverting the PR resolves the problem.
Reproduction
n/a
Steps to reproduce
No response
System Info
n/a
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 3
- Comments: 15 (3 by maintainers)
Any movement on this? Just to add another use case which is mentioned above.
Inserting a Content Security Policy via
transformIndexHtmlis outputting some funky stuff - which I would argue is more important than the body examples given above.If this is the goal, the second example below is more valid than the first id argue
Why are we escaping these tags? I agree with @brokenmass here, I think this is Vite is overstepping here
Run
npm run buildandnpm run previewon this StackBlitz to see this for yourself.sure but why should that be vite responsibility, why should it be vite requirement ? There’s an option to inject tags, if the user want, then they can escape the attributes themselves; otherwise the build tool should just “trust” that the user knows what they are doing and leave it unmodified.
Actually this change break the build for everyone already escaping their injected attrs.
Remember this is not an externally defined input that can be hijacked, it’s a build instruction. And, I mean, it has been like this for years in vite and in every other tools before vite, like webpack.
Being defensive ONLY in the tag attributes makes very little sense cause if you want to protect the user from every invalid html then you should escape stuff everywhere (in your example you should escape the tag field too), and possibly even validate/rewrite the input index.html.
Escaping just the double quote to avoid escaping the attr by mistake seems the most reasonable approach to me.if the contents already contains & those should not be escaped (cause maybe the user already escaped the value) and leaving it unescaped does not break anything.
who’s going to take the final decision if this escaping is going to be reverted or not ? Any ETA ?
@hi-ogawa would be nice to know what’s the decision on this, as if the mentioned pr is not going to be reverted we need to find an alternative fix.
Yes i get your point, but HTML-Escaping attributes in a build tools does not seem the standard behaviour, and is definitely an unexpected one. The output of the compilation does not have to be fully valid html as there might be some other task or services that parse/transform the html before serving it.
Sure i think it’s possible to use custom delimiter or move to a different template engine, like pug. but would avoid it if possible.
As a solution, if you do not want to revert the pr, you could allow an option to opt out of this defensive behaviour