2018-07-26 13:48:28 +00:00
|
|
|
import { getPort, loadFixture, Nuxt, rp } from '../utils'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
let port
|
2023-10-16 21:23:38 +00:00
|
|
|
const url = route => 'http://127.0.0.1:' + port + route
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
const startCspServer = async (csp, isProduction = true) => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const options = await loadFixture('basic', {
|
2018-07-30 16:04:02 +00:00
|
|
|
debug: !isProduction,
|
|
|
|
render: { csp }
|
|
|
|
})
|
2018-03-18 23:41:14 +00:00
|
|
|
const nuxt = new Nuxt(options)
|
2019-03-08 20:43:23 +00:00
|
|
|
await nuxt.ready()
|
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
port = await getPort()
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen(port, '0.0.0.0')
|
2018-02-01 13:31:02 +00:00
|
|
|
return nuxt
|
|
|
|
}
|
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
const getHeader = debug => debug ? 'content-security-policy-report-only' : 'content-security-policy'
|
|
|
|
const cspHeader = getHeader(false)
|
|
|
|
const reportOnlyHeader = getHeader(true)
|
|
|
|
|
2018-08-10 07:41:23 +00:00
|
|
|
const startCspDevServer = csp => startCspServer(csp, false)
|
2018-07-30 16:04:02 +00:00
|
|
|
|
2019-11-28 12:19:22 +00:00
|
|
|
describe('basic ssr csp', () => {
|
2018-07-30 16:04:02 +00:00
|
|
|
let nuxt
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await nuxt.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('production mode', () => {
|
|
|
|
test(
|
|
|
|
'Not contain Content-Security-Policy header, when csp is false',
|
|
|
|
async () => {
|
|
|
|
nuxt = await startCspServer(false)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toBe(undefined)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp is set',
|
|
|
|
async () => {
|
|
|
|
nuxt = await startCspServer(true)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy-Report-Only header, when explicitly asked for',
|
|
|
|
async () => {
|
2018-08-31 20:34:12 +00:00
|
|
|
nuxt = await startCspDevServer({ reportOnly: true })
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain only unique hashes in header when csp is set',
|
|
|
|
async () => {
|
|
|
|
nuxt = await startCspServer(true)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
const hashes = headers[cspHeader].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
const uniqueHashes = [...new Set(hashes)]
|
|
|
|
|
|
|
|
expect(uniqueHashes.length).toBe(hashes.length)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp.allowedSources set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
allowedSources: ['https://example.com', 'https://example.io']
|
2018-03-18 19:31:32 +00:00
|
|
|
}
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
nuxt = await startCspServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/^script-src 'self' 'sha256-.*'/)
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(headers[cspHeader]).toContain('https://example.com')
|
|
|
|
expect(headers[cspHeader]).toContain('https://example.io')
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp.policies set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'none\''],
|
2018-07-30 16:04:02 +00:00
|
|
|
'script-src': ['https://example.com', 'https://example.io']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/default-src 'none'/)
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'sha256-(.*)?' 'self'/)
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(headers[cspHeader]).toContain('https://example.com')
|
|
|
|
expect(headers[cspHeader]).toContain('https://example.io')
|
2018-03-18 19:31:32 +00:00
|
|
|
}
|
2018-07-30 16:04:02 +00:00
|
|
|
)
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp.policies.script-src is not set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'none\'']
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
nuxt = await startCspServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
expect(headers[cspHeader]).toMatch(/default-src 'none'/)
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-05-04 18:24:17 +00:00
|
|
|
test(
|
|
|
|
'Contain report-uri in Content-Security-Policy-Report-Only header, when explicitly asked for CSRP, allowedSources, csp.report-url',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
allowedSources: ['https://example.com', 'https://example.io'],
|
|
|
|
reportOnly: true,
|
|
|
|
policies: {
|
|
|
|
'report-uri': '/csp_report_uri'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nuxt = await startCspDevServer(cspOption)
|
|
|
|
const { headers } = await rp(url('/stateless'))
|
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'/)
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.com')
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.io')
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('report-uri /csp_report_uri')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
test(
|
|
|
|
'Contain only unique hashes in header when csp.policies is set',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'self\''],
|
|
|
|
'script-src': ['\'self\''],
|
|
|
|
'style-src': ['\'self\'']
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
policies
|
|
|
|
})
|
2018-03-18 19:31:32 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2018-03-18 19:31:32 +00:00
|
|
|
}
|
2018-07-30 16:04:02 +00:00
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
const hashes = headers[cspHeader].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
const uniqueHashes = [...new Set(hashes)]
|
|
|
|
|
|
|
|
expect(uniqueHashes.length).toBe(hashes.length)
|
|
|
|
}
|
|
|
|
)
|
2019-03-29 16:09:53 +00:00
|
|
|
|
|
|
|
test(
|
|
|
|
'Not contain hash when \'unsafe-inline\' option is present in script-src policy',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'script-src': ['\'unsafe-inline\'']
|
2019-03-29 16:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2019-03-29 16:09:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2019-03-29 16:09:53 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'self' 'unsafe-inline'$/)
|
|
|
|
}
|
|
|
|
)
|
2019-06-26 15:22:45 +00:00
|
|
|
|
|
|
|
test(
|
2019-10-19 08:15:07 +00:00
|
|
|
'Contain hash and \'unsafe-inline\' when unsafeInlineCompatibility is enabled',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'script-src': ['\'unsafe-inline\'']
|
2019-10-19 08:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
unsafeInlineCompatibility: true,
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2019-10-19 08:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2019-10-19 08:15:07 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-06-07 19:10:41 +00:00
|
|
|
test('Contain nonce on ssr links and scripts', async () => {
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
generateNonce: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const { body, headers } = await rp(url('/stateless'))
|
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src .* 'nonce-.*'/)
|
|
|
|
|
|
|
|
const nonceValue = headers[cspHeader].match(/'nonce-(.*?)'/)[1]
|
|
|
|
|
|
|
|
for (const link of body.match(/<link[^>]+?>/g)) {
|
|
|
|
expect(link).toContain(`nonce="${nonceValue}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const script of body.match(/<script[^>]+?>/g)) {
|
|
|
|
expect(script).toContain(`nonce="${nonceValue}"`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-19 08:15:07 +00:00
|
|
|
// TODO: Remove this test in Nuxt 3, we will stop supporting this typo (more on: https://github.com/nuxt/nuxt.js/pull/6583)
|
|
|
|
test(
|
|
|
|
'Contain hash and \'unsafe-inline\' when the typo property unsafeInlineCompatiblity is enabled',
|
2019-06-26 15:22:45 +00:00
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'script-src': ['\'unsafe-inline\'']
|
2019-06-26 15:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
unsafeInlineCompatiblity: true,
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2019-06-26 15:22:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2019-06-26 15:22:45 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/)
|
|
|
|
}
|
|
|
|
)
|
2018-07-30 16:04:02 +00:00
|
|
|
})
|
2019-06-26 15:22:45 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
describe('debug mode', () => {
|
|
|
|
test(
|
|
|
|
'Not contain Content-Security-Policy-Report-Only header, when csp is false',
|
|
|
|
async () => {
|
|
|
|
nuxt = await startCspDevServer(false)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toBe(undefined)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when explicitly asked for',
|
|
|
|
async () => {
|
2018-08-31 20:34:12 +00:00
|
|
|
nuxt = await startCspDevServer({ reportOnly: false })
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp is set',
|
|
|
|
async () => {
|
|
|
|
nuxt = await startCspDevServer(true)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain only unique hashes in header when csp is set',
|
|
|
|
async () => {
|
|
|
|
nuxt = await startCspDevServer(true)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
const hashes = headers[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
const uniqueHashes = [...new Set(hashes)]
|
|
|
|
|
|
|
|
expect(uniqueHashes.length).toBe(hashes.length)
|
2018-03-18 19:31:32 +00:00
|
|
|
}
|
2018-07-30 16:04:02 +00:00
|
|
|
)
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy-Report-Only header, when csp.allowedSources set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
allowedSources: ['https://example.com', 'https://example.io']
|
|
|
|
}
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
nuxt = await startCspDevServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'/)
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.com')
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.io')
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy-Report-Only header, when csp.policies set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'none\''],
|
2018-07-30 16:04:02 +00:00
|
|
|
'script-src': ['https://example.com', 'https://example.io']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspDevServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/default-src 'none'/)
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/script-src 'sha256-(.*)?' 'self'/)
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.com')
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.io')
|
2018-07-26 13:48:28 +00:00
|
|
|
}
|
2018-07-30 16:04:02 +00:00
|
|
|
)
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2020-05-04 18:24:17 +00:00
|
|
|
test(
|
|
|
|
'Contain report-uri in Content-Security-Policy-Report-Only header, when explicitly asked for CSRP, allowedSources, csp.report-url',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
allowedSources: ['https://example.com', 'https://example.io'],
|
|
|
|
reportOnly: true,
|
|
|
|
policies: {
|
|
|
|
'report-uri': '/csp_report_uri'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nuxt = await startCspDevServer(cspOption)
|
|
|
|
const { headers } = await rp(url('/stateless'))
|
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/^script-src 'self' 'sha256-.*'/)
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.com')
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('https://example.io')
|
|
|
|
expect(headers[reportOnlyHeader]).toContain('report-uri /csp_report_uri')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy-Report-Only header, when csp.policies.script-src is not set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'none\'']
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
nuxt = await startCspDevServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/default-src 'none'/)
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/script-src 'sha256-.*' 'self'$/)
|
2018-07-26 13:48:28 +00:00
|
|
|
}
|
2018-07-30 16:04:02 +00:00
|
|
|
)
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
test(
|
|
|
|
'Contain only unique hashes in header when csp.policies is set',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'self\''],
|
|
|
|
'script-src': ['\'self\''],
|
|
|
|
'style-src': ['\'self\'']
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
nuxt = await startCspDevServer({
|
|
|
|
policies
|
|
|
|
})
|
2018-07-26 13:48:28 +00:00
|
|
|
|
2018-07-30 16:04:02 +00:00
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2018-07-30 16:04:02 +00:00
|
|
|
}
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2018-07-30 16:04:02 +00:00
|
|
|
|
|
|
|
const hashes = headers[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
const uniqueHashes = [...new Set(hashes)]
|
|
|
|
|
|
|
|
expect(uniqueHashes.length).toBe(hashes.length)
|
|
|
|
}
|
|
|
|
)
|
2019-03-29 16:09:53 +00:00
|
|
|
|
2018-12-12 06:29:28 +00:00
|
|
|
test(
|
|
|
|
'Not contain old hashes when loading new page',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
2019-11-26 22:42:39 +00:00
|
|
|
'default-src': ['\'self\''],
|
2018-12-12 06:29:28 +00:00
|
|
|
'script-src': ['https://example.com', 'https://example.io']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nuxt = await startCspDevServer(cspOption)
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers: user1Header } = await rp(url('/users/1'))
|
2018-12-12 06:29:28 +00:00
|
|
|
const user1Hashes = user1Header[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers: user2Header } = await rp(url('/users/2'))
|
2018-12-12 06:29:28 +00:00
|
|
|
const user2Hashes = new Set(user2Header[reportOnlyHeader].split(' ').filter(s => s.startsWith('\'sha256-')))
|
|
|
|
|
|
|
|
const intersection = new Set(user1Hashes.filter(x => user2Hashes.has(x)))
|
|
|
|
expect(intersection.size).toBe(0)
|
|
|
|
}
|
|
|
|
)
|
2019-03-29 16:09:53 +00:00
|
|
|
|
|
|
|
test(
|
|
|
|
'Not contain hash when \'unsafe-inline\' option is present in script-src policy',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'script-src': ['\'unsafe-inline\'']
|
2019-03-29 16:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspDevServer({
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2019-03-29 16:09:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2019-03-29 16:09:53 +00:00
|
|
|
|
|
|
|
expect(headers[reportOnlyHeader]).toMatch(/script-src 'self' 'unsafe-inline'$/)
|
|
|
|
}
|
|
|
|
)
|
2019-06-26 15:22:45 +00:00
|
|
|
|
|
|
|
test(
|
2019-10-19 08:15:07 +00:00
|
|
|
'Contain hash and \'unsafe-inline\' when unsafeInlineCompatibility is enabled',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'script-src': ['\'unsafe-inline\'']
|
2019-10-19 08:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
unsafeInlineCompatibility: true,
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2019-10-19 08:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2019-10-19 08:15:07 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Remove this test in Nuxt 3, we will stop supporting this typo (more on: https://github.com/nuxt/nuxt.js/pull/6583)
|
|
|
|
test(
|
|
|
|
'Contain hash and \'unsafe-inline\' when the typo property unsafeInlineCompatiblity is enabled',
|
2019-06-26 15:22:45 +00:00
|
|
|
async () => {
|
|
|
|
const policies = {
|
2019-11-26 22:42:39 +00:00
|
|
|
'script-src': ['\'unsafe-inline\'']
|
2019-06-26 15:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nuxt = await startCspServer({
|
|
|
|
unsafeInlineCompatiblity: true,
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2019-12-02 15:23:56 +00:00
|
|
|
await rp(url('/stateless'))
|
2019-06-26 15:22:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:23:56 +00:00
|
|
|
const { headers } = await rp(url('/stateful'))
|
2019-06-26 15:22:45 +00:00
|
|
|
|
|
|
|
expect(headers[cspHeader]).toMatch(/script-src 'sha256-.*' 'self' 'unsafe-inline'$/)
|
|
|
|
}
|
|
|
|
)
|
2018-07-30 16:04:02 +00:00
|
|
|
})
|
2018-02-02 02:51:16 +00:00
|
|
|
})
|