Skip to content

Commit

Permalink
fix(vue-renderer): add the csp hash if unsafe-inline hasn't been sp…
Browse files Browse the repository at this point in the history
…ecified (#5387)
  • Loading branch information
sambowler authored and pi0 committed Mar 29, 2019
1 parent 91f4eb0 commit 97db6a4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/vue-renderer/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ export default class VueRenderer {

// Calculate CSP hashes
const cspScriptSrcHashes = []
if (this.context.options.render.csp) {
const csp = this.context.options.render.csp
const containsUnsafeInlineScriptSrc = csp && csp.policies && csp.policies['script-src'] && csp.policies['script-src'].includes(`'unsafe-inline'`)

// Only add the hash if 'unsafe-inline' rule isn't present to avoid conflicts (#5387)
if (csp && !containsUnsafeInlineScriptSrc) {
const { hashAlgorithm } = this.context.options.render.csp
const hash = crypto.createHash(hashAlgorithm)
hash.update(serializedSession)
Expand Down
51 changes: 51 additions & 0 deletions test/unit/basic.ssr.csp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,31 @@ describe('basic ssr csp', () => {
expect(uniqueHashes.length).toBe(hashes.length)
}
)

test(
'Not contain hash when \'unsafe-inline\' option is present in script-src policy',
async () => {
const policies = {
'script-src': [`'unsafe-inline'`]
}

nuxt = await startCspServer({
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 'self' 'unsafe-inline'$/)
}
)
})
describe('debug mode', () => {
test(
Expand Down Expand Up @@ -314,6 +339,7 @@ describe('basic ssr csp', () => {
expect(uniqueHashes.length).toBe(hashes.length)
}
)

test(
'Not contain old hashes when loading new page',
async () => {
Expand All @@ -339,5 +365,30 @@ describe('basic ssr csp', () => {
expect(intersection.size).toBe(0)
}
)

test(
'Not contain hash when \'unsafe-inline\' option is present in script-src policy',
async () => {
const policies = {
'script-src': [`'unsafe-inline'`]
}

nuxt = await startCspDevServer({
policies
})

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

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

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

0 comments on commit 97db6a4

Please sign in to comment.