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: csp false in rc5 removes custom csp header #322

Merged
merged 1 commit into from Dec 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/module.ts
Expand Up @@ -200,6 +200,8 @@ function mergeSecurityPerRoute(nuxt: Nuxt) {
// Because in the pre-rc1 syntax, standard headers could also be supplied in object format
standardHeadersAsObject[optionKey] = headerValue
}
// Delete security header from route rules to avoid route resolution overlaps
delete standardHeaders[headerName]
}
})
}
Expand Down
7 changes: 2 additions & 5 deletions src/runtime/nitro/plugins/02-securityHeaders.ts
@@ -1,4 +1,4 @@
import { getRouteRules, defineNitroPlugin, setResponseHeader, removeResponseHeader } from '#imports'
import { getRouteRules, defineNitroPlugin, setResponseHeader } from '#imports'
import { type OptionKey } from '../../../types/headers'
import { getNameFromKey, headerStringFromObject } from '../../utils/headers'

Expand All @@ -10,10 +10,7 @@ export default defineNitroPlugin((nitroApp) => {
Object.entries(headers).forEach(([key, optionValue]) => {
const optionKey = key as OptionKey
const headerName = getNameFromKey(optionKey)
if (optionValue === false) {
removeResponseHeader(event, headerName)
}
else {
if (optionValue !== false) {
const headerValue = headerStringFromObject(optionKey, optionValue)
setResponseHeader(event, headerName, headerValue)
}
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/perRoute/nuxt.config.ts
Expand Up @@ -237,6 +237,13 @@ export default defineNuxtConfig({
sri: false
}
},
'/preserve-middleware': {
security: {
headers: {
contentSecurityPolicy: false
}
}
},
},
security: {
headers: {
Expand Down
@@ -0,0 +1,3 @@
<template>
<div>basic</div>
</template>
3 changes: 3 additions & 0 deletions test/fixtures/perRoute/pages/preserve-middleware/index.vue
@@ -0,0 +1,3 @@
<template>
<div>basic</div>
</template>
5 changes: 5 additions & 0 deletions test/fixtures/perRoute/server/middleware/preserve-test.ts
@@ -0,0 +1,5 @@
export default defineEventHandler((event) => {
if (event.path.startsWith('/preserve-middleware')) {
appendHeader(event, 'Content-Security-Policy', 'example')
}
})
20 changes: 20 additions & 0 deletions test/perRoute.test.ts
Expand Up @@ -883,6 +883,26 @@ describe('[nuxt-security] Per-route Configuration', async () => {
expect(elementsWithIntegrity).toHaveLength(6)
})

it ('should not remove middleware headers when false', async () => {
const res = await fetch('/preserve-middleware')
expect(res.status).toBe(200)

const { headers } = res
const csp = headers.get('content-security-policy')
expect(csp).toBeDefined()
expect(csp).toBe('example')
})

it ('should overwrite middleware headers when not false', async () => {
const res = await fetch('/preserve-middleware/deep/page')
expect(res.status).toBe(200)

const { headers } = res
const csp = headers.get('content-security-policy')
expect(csp).toBeDefined()
expect(csp).toBe("base-uri 'none'; font-src 'self' https: data:; form-action 'self'; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; script-src 'self' https: 'unsafe-inline' 'strict-dynamic'; upgrade-insecure-requests;")
})

})