Skip to content
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 and ssg usage #181

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,32 @@ const registerSecurityNitroPlugins = (
config.plugins.push(
normalize(
fileURLToPath(
new URL("./runtime/nitro/plugins/hidePoweredBy", import.meta.url)
new URL("./runtime/nitro/plugins/01-hidePoweredBy", import.meta.url)
)
)
);
}

// Nitro plugin to enable nonce for CSP
if (nuxt.options.security.nonce) {
// Register nitro plugin to enable CSP for SSG
if (
typeof securityOptions.headers === "object" &&
securityOptions.headers.contentSecurityPolicy
) {
config.plugins.push(
normalize(
fileURLToPath(
new URL("./runtime/nitro/plugins/cspNonce", import.meta.url)
new URL("./runtime/nitro/plugins/02-cspSsg", import.meta.url)
)
)
);
}

// Register nitro plugin to enable CSP for SSG
if (
typeof securityOptions.headers === "object" &&
securityOptions.headers.contentSecurityPolicy
) {
// Nitro plugin to enable nonce for CSP
if (nuxt.options.security.nonce) {
config.plugins.push(
normalize(
fileURLToPath(
new URL("./runtime/nitro/plugins/cspSsg", import.meta.url)
new URL("./runtime/nitro/plugins/99-cspNonce", import.meta.url)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export default <NitroAppPlugin> function (nitro) {

if (!nonce) { return }

// Replace nonce attribute in http-equiv meta tag
html.head = html.head.map((meta) => {
if (!meta.startsWith('<meta http-equiv="Content-Security-Policy"')) { return meta }
return meta.replaceAll('{{nonce}}', nonce)
})

// Add nonce attribute to all link tags
html.head = html.head.map(link => link.replaceAll(tagNotPrecededByQuotes('link'), `<link nonce="${nonce}"`))
html.bodyAppend = html.bodyAppend.map(link => link.replaceAll(tagNotPrecededByQuotes('link'), `<link nonce="${nonce}"`))
Expand Down
Loading