Fix(nonce): correctly detect valid script elements#611
Draft
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
…element pairs and add `nonce` attributes to `<script>` tags. * Implement `parseHtmlRecursively` function to parse HTML and add `nonce` attributes recursively. * Implement `addNonceToAttributes` function to handle `nonce` attribute addition. * Update `addNonceToElement` function to use the new recursive parsing approach. * Ensure the algorithm matches HTML element pairs recursively from the outermost to the innermost elements.
There was a problem hiding this comment.
Pull Request Overview
A bug fix PR that refactors nonce injection for HTML tags in SSR mode to correctly detect valid script elements.
- Refactored nonce injection by replacing duplicated regex-based replacements with a reusable helper function (addNonceToElement).
- Added new helper functions (parseHtmlRecursively and addNonceToAttributes) for improved HTML tag processing.
Comments suppressed due to low confidence (1)
src/runtime/nitro/plugins/40-cspSsrNonce.ts:74
- Consider updating the regex to handle self-closing tags (e.g. <link ... />) by allowing an optional '/' before the closing '>'.
const tagRegex = new RegExp(`<${tagName}([^>]*?)>`, 'gi')
| if (nonceRegex.test(rest)) { | ||
| return match.replace(nonceRegex, `nonce="${nonce}"`) | ||
| } | ||
| return `<${tagName} nonce="${nonce}"${rest}>` |
There was a problem hiding this comment.
Ensure that a space is preserved between the injected nonce attribute and the rest of the attributes, especially if 'rest' does not start with a space.
Suggested change
| return `<${tagName} nonce="${nonce}"${rest}>` | |
| return `<${tagName} nonce="${nonce}"${rest.startsWith(' ') ? rest : ' ' + rest}>` |
12 tasks
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.
Closes #594
Closes #610
Types of changes
Description
Checklist: