Skip to content

Commit

Permalink
feat(vue-renderer): add csp option for csp v1 compatibility (#5975)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong authored and Atinux committed Jul 30, 2019
1 parent 4bfd12a commit 6eb9fea
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/config/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ export function getNuxtConfig(_options) {
hashAlgorithm: 'sha256',
allowedSources: undefined,
policies: undefined,
addMeta: Boolean(options.target === 'static'),
addMeta: Boolean(options._generate),
unsafeInlineCompatiblity: false,
reportOnly: options.debug
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/config/test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('config: options', () => {
expect(csp).toEqual({
hashAlgorithm: 'sha256',
addMeta: false,
unsafeInlineCompatiblity: false,
allowedSources: true,
policies: undefined,
reportOnly: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-renderer/src/renderers/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class SSRRenderer extends BaseRenderer {
if (csp) {
// Only add the hash if 'unsafe-inline' rule isn't present to avoid conflicts (#5387)
const containsUnsafeInlineScriptSrc = csp.policies && csp.policies['script-src'] && csp.policies['script-src'].includes(`'unsafe-inline'`)
if (!containsUnsafeInlineScriptSrc) {
if (csp.unsafeInlineCompatiblity || !containsUnsafeInlineScriptSrc) {
const hash = crypto.createHash(csp.hashAlgorithm)
hash.update(serializedSession)
cspScriptSrcHashes.push(`'${csp.hashAlgorithm}-${hash.digest('base64')}'`)
Expand Down
53 changes: 53 additions & 0 deletions test/unit/basic.ssr.csp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,34 @@ describe('basic ssr csp', () => {
expect(headers[cspHeader]).toMatch(/script-src 'self' 'unsafe-inline'$/)
}
)

test(
'Contain hash and \'unsafe-inline\' when unsafeInlineCompatiblity is enabled',
async () => {
const policies = {
'script-src': [`'unsafe-inline'`]
}

nuxt = await startCspServer({
unsafeInlineCompatiblity: true,
policies
})

for (let i = 0; i < 5; i++) {
await rp(url('/stateless'), {
resolveWithFullResponse: true
})
}

const { headers } = await rp(url('/stateful'), {
resolveWithFullResponse: true
})

expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/)
}
)
})

describe('debug mode', () => {
test(
'Not contain Content-Security-Policy-Report-Only header, when csp is false',
Expand Down Expand Up @@ -390,5 +417,31 @@ describe('basic ssr csp', () => {
expect(headers[reportOnlyHeader]).toMatch(/script-src 'self' 'unsafe-inline'$/)
}
)

test(
'Contain hash and \'unsafe-inline\' when unsafeInlineCompatiblity is enabled',
async () => {
const policies = {
'script-src': [`'unsafe-inline'`]
}

nuxt = await startCspServer({
unsafeInlineCompatiblity: true,
policies
})

for (let i = 0; i < 5; i++) {
await rp(url('/stateless'), {
resolveWithFullResponse: true
})
}

const { headers } = await rp(url('/stateful'), {
resolveWithFullResponse: true
})

expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/)
}
)
})
})

0 comments on commit 6eb9fea

Please sign in to comment.