mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat: external script support for CSP (#2608)
This commit is contained in:
parent
809d38800a
commit
5ebf60f2a1
@ -305,7 +305,9 @@ Options.defaults = {
|
||||
etag: {
|
||||
weak: false
|
||||
},
|
||||
csp: undefined
|
||||
csp: {
|
||||
allowedSouces: []
|
||||
}
|
||||
},
|
||||
watchers: {
|
||||
webpack: {
|
||||
|
@ -67,10 +67,11 @@ module.exports = async function nuxtMiddleware(req, res, next) {
|
||||
res.setHeader('Link', pushAssets.join(','))
|
||||
}
|
||||
|
||||
if (this.options.render.csp) {
|
||||
if (this.options.render.csp.hashAlgorithm) {
|
||||
let allowedSources = cspScriptSrcHashes.concat(this.options.render.csp.allowedSources)
|
||||
res.setHeader(
|
||||
'Content-Security-Policy',
|
||||
`script-src 'self' ${(cspScriptSrcHashes || []).join(' ')}`
|
||||
`script-src 'self' ${(allowedSources || []).join(' ')}`
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -361,7 +361,7 @@ module.exports = class Renderer {
|
||||
isJSON: true
|
||||
})};`
|
||||
let cspScriptSrcHashes = []
|
||||
if (this.options.render.csp) {
|
||||
if (this.options.render.csp.hashAlgorithm) {
|
||||
let hash = crypto.createHash(this.options.render.csp.hashAlgorithm)
|
||||
hash.update(serializedSession)
|
||||
cspScriptSrcHashes.push(
|
||||
|
@ -24,7 +24,10 @@ test.serial('Init Nuxt.js', async t => {
|
||||
stats: false
|
||||
},
|
||||
render: {
|
||||
csp: true
|
||||
csp: {
|
||||
hashAlgorithm: 'sha256',
|
||||
allowedSources: ['https://example.com', 'https://example.io']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,6 +259,8 @@ test('Content-Security-Policy Header', async t => {
|
||||
})
|
||||
// Verify functionality
|
||||
t.regex(headers['content-security-policy'], /script-src 'self' 'sha256-.*'/)
|
||||
t.true(headers['content-security-policy'].includes('https://example.com'))
|
||||
t.true(headers['content-security-policy'].includes('https://example.io'))
|
||||
})
|
||||
|
||||
test('/_nuxt/server-bundle.json should return 404', async t => {
|
||||
|
Loading…
Reference in New Issue
Block a user