Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/runtime/nitro/plugins/40-cspSsrNonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
import { resolveSecurityRules } from '../context'
import { generateRandomNonce } from '../../../utils/crypto'

const LINK_RE = /<link([^>]*?>)/gi
const NONCE_RE = /nonce="[^"]+"/i
const SCRIPT_RE = /<script([^>]*?>)/gi
const STYLE_RE = /<style([^>]*?>)/gi


/**
* This plugin generates a nonce for the current request and adds it to the HTML.
* It only runs in SSR mode.
Expand Down Expand Up @@ -58,20 +52,11 @@
return element;
}
// Add nonce to all link tags
element = element.replace(LINK_RE, (match, rest) => {
if (NONCE_RE.test(rest)) {
return match.replace(NONCE_RE, `nonce="${nonce}"`);
}
return `<link nonce="${nonce}"` + rest
})
element = addNonceToElement(element, 'link', nonce)
// Add nonce to all script tags
element = element.replace(SCRIPT_RE, (match, rest) => {
return `<script nonce="${nonce}"` + rest
})
element = addNonceToElement(element, 'script', nonce)
// Add nonce to all style tags
element = element.replace(STYLE_RE, (match, rest) => {
return `<style nonce="${nonce}"` + rest
})
element = addNonceToElement(element, 'style', nonce)
return element
})
}
Expand All @@ -84,3 +69,32 @@
}
})
})

function addNonceToElement(element: string, tagName: string, nonce: string): string {
const tagRegex = new RegExp(`<${tagName}([^>]*?)>`, 'gi')
const nonceRegex = /nonce="[^"]+"/i

return element.replace(tagRegex, (match, rest) => {
if (nonceRegex.test(rest)) {
return match.replace(nonceRegex, `nonce="${nonce}"`)
}
return `<${tagName} nonce="${nonce}"${rest}>`
Copy link
Preview

Copilot AI Mar 20, 2025

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.

Suggested change
return `<${tagName} nonce="${nonce}"${rest}>`
return `<${tagName} nonce="${nonce}"${rest.startsWith(' ') ? rest : ' ' + rest}>`

Copilot uses AI. Check for mistakes.

})
}

function parseHtmlRecursively(html: string, nonce: string): string {

Check warning on line 85 in src/runtime/nitro/plugins/40-cspSsrNonce.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

'parseHtmlRecursively' is defined but never used
const tagRegex = /<([a-zA-Z]+)([^>]*)>(.*?)<\/\1>/gs
return html.replace(tagRegex, (match, tagName, attributes, innerHtml) => {
const updatedAttributes = addNonceToAttributes(attributes, nonce)
const updatedInnerHtml = parseHtmlRecursively(innerHtml, nonce)
return `<${tagName}${updatedAttributes}>${updatedInnerHtml}</${tagName}>`
})
}

function addNonceToAttributes(attributes: string, nonce: string): string {
const nonceRegex = /nonce="[^"]+"/i
if (nonceRegex.test(attributes)) {
return attributes.replace(nonceRegex, `nonce="${nonce}"`)
}
return ` nonce="${nonce}"${attributes}`
}