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
|
2018-02-01 13:31:02 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const startCSPTestServer = async (csp) => {
|
2018-03-18 23:41:14 +00:00
|
|
|
const options = loadFixture('basic', { render: { csp } })
|
|
|
|
const nuxt = new Nuxt(options)
|
|
|
|
port = await getPort()
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.listen(port, '0.0.0.0')
|
2018-02-01 13:31:02 +00:00
|
|
|
return nuxt
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
describe('basic ssr csp', () => {
|
|
|
|
test(
|
2018-07-01 12:39:30 +00:00
|
|
|
'Not contain Content-Security-Policy header, when csp is false',
|
2018-03-18 19:31:32 +00:00
|
|
|
async () => {
|
2018-07-01 12:39:30 +00:00
|
|
|
const nuxt = await startCSPTestServer(false)
|
2018-03-18 19:31:32 +00:00
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['content-security-policy']).toBe(undefined)
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
|
|
|
}
|
|
|
|
)
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test(
|
2018-07-01 12:39:30 +00:00
|
|
|
'Contain Content-Security-Policy header, when csp is set',
|
2018-03-18 19:31:32 +00:00
|
|
|
async () => {
|
2018-07-01 12:39:30 +00:00
|
|
|
const nuxt = await startCSPTestServer(true)
|
2018-03-18 19:31:32 +00:00
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['content-security-policy']).toMatch(/^script-src 'self' 'sha256-.*'$/)
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-07-26 13:48:28 +00:00
|
|
|
test(
|
|
|
|
'Contain only unique hashes in header when csp is set',
|
|
|
|
async () => {
|
|
|
|
const nuxt = await startCSPTestServer(true)
|
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const hashes = headers['content-security-policy'].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
const uniqueHashes = [...new Set(hashes)]
|
|
|
|
|
|
|
|
expect(uniqueHashes.length).toBe(hashes.length)
|
|
|
|
|
|
|
|
await nuxt.close()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp.allowedSources set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
allowedSources: ['https://example.com', 'https://example.io']
|
|
|
|
}
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const nuxt = await startCSPTestServer(cspOption)
|
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['content-security-policy']).toMatch(/^script-src 'self' 'sha256-.*'/)
|
|
|
|
expect(headers['content-security-policy'].includes('https://example.com')).toBe(true)
|
|
|
|
expect(headers['content-security-policy'].includes('https://example.io')).toBe(true)
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
2018-02-01 13:31:02 +00:00
|
|
|
}
|
2018-03-18 19:31:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp.policies set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
|
|
|
'default-src': [`'none'`],
|
|
|
|
'script-src': ['https://example.com', 'https://example.io']
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const nuxt = await startCSPTestServer(cspOption)
|
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2018-02-01 13:31:02 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['content-security-policy']).toMatch(/default-src 'none'/)
|
2018-07-26 13:48:28 +00:00
|
|
|
expect(headers['content-security-policy']).toMatch(/script-src 'sha256-(.*)?' 'self'/)
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['content-security-policy'].includes('https://example.com')).toBe(true)
|
|
|
|
expect(headers['content-security-policy'].includes('https://example.io')).toBe(true)
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
2018-02-02 02:51:16 +00:00
|
|
|
}
|
2018-03-18 19:31:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain Content-Security-Policy header, when csp.policies.script-src is not set',
|
|
|
|
async () => {
|
|
|
|
const cspOption = {
|
|
|
|
enabled: true,
|
|
|
|
policies: {
|
|
|
|
'default-src': [`'none'`]
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
const nuxt = await startCSPTestServer(cspOption)
|
|
|
|
const { headers } = await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
expect(headers['content-security-policy']).toMatch(/default-src 'none'/)
|
2018-07-26 13:48:28 +00:00
|
|
|
expect(headers['content-security-policy']).toMatch(/script-src 'sha256-.*' 'self'$/)
|
|
|
|
|
|
|
|
await nuxt.close()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
test(
|
|
|
|
'Contain only unique hashes in header when csp.policies is set',
|
|
|
|
async () => {
|
|
|
|
const policies = {
|
|
|
|
'default-src': [`'self'`],
|
|
|
|
'script-src': [`'self'`],
|
|
|
|
'style-src': [`'self'`]
|
|
|
|
}
|
|
|
|
|
|
|
|
const nuxt = await startCSPTestServer({
|
|
|
|
policies
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
await rp(url('/stateless'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const { headers } = await rp(url('/stateful'), {
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const hashes = headers['content-security-policy'].split(' ').filter(s => s.startsWith('\'sha256-'))
|
|
|
|
const uniqueHashes = [...new Set(hashes)]
|
|
|
|
|
|
|
|
expect(uniqueHashes.length).toBe(hashes.length)
|
2018-02-02 02:51:16 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
|
|
|
}
|
|
|
|
)
|
2018-02-02 02:51:16 +00:00
|
|
|
})
|