-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix(nonce): correctly detect valid script elements #611
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
return `<${tagName} nonce="${nonce}"${rest}>` | |
return `<${tagName} nonce="${nonce}"${rest.startsWith(' ') ? rest : ' ' + rest}>` |
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
Closes #594
Closes #610
Types of changes
Description
Checklist: