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

feat(vue-renderer): add csp option for csp v1 compatibility #5975

Merged
merged 1 commit into from
Jun 26, 2019
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
1 change: 1 addition & 0 deletions packages/config/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export function getNuxtConfig(_options) {
allowedSources: undefined,
policies: undefined,
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'$/)
}
)
})
})