refactor(csp): remove unsafe-eval in dev mode (#7659)

This commit is contained in:
Xin Du (Clark) 2020-07-02 18:37:55 +01:00 committed by GitHub
parent d4363d4477
commit 03424513ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 4 deletions

View File

@ -74,7 +74,7 @@ export default ({ options, nuxt, renderRoute, resources }) => async function nux
const isReportOnly = !!options.render.csp.reportOnly const isReportOnly = !!options.render.csp.reportOnly
const cspHeader = isReportOnly ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy' const cspHeader = isReportOnly ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy'
res.setHeader(cspHeader, getCspString({ cspScriptSrcHashes, allowedSources, policies, isDev: options.dev, isReportOnly })) res.setHeader(cspHeader, getCspString({ cspScriptSrcHashes, allowedSources, policies, isReportOnly }))
} }
// Send response // Send response
@ -126,9 +126,9 @@ const defaultPushAssets = (preloadFiles, shouldPush, publicPath, options) => {
return links return links
} }
const getCspString = ({ cspScriptSrcHashes, allowedSources, policies, isDev, isReportOnly }) => { const getCspString = ({ cspScriptSrcHashes, allowedSources, policies, isReportOnly }) => {
const joinedHashes = cspScriptSrcHashes.join(' ') const joinedHashes = cspScriptSrcHashes.join(' ')
const baseCspStr = `script-src 'self'${isDev ? ' \'unsafe-eval\'' : ''} ${joinedHashes}` const baseCspStr = `script-src 'self' ${joinedHashes}`
const policyObjectAvailable = typeof policies === 'object' && policies !== null && !Array.isArray(policies) const policyObjectAvailable = typeof policies === 'object' && policies !== null && !Array.isArray(policies)
if (Array.isArray(allowedSources) && allowedSources.length) { if (Array.isArray(allowedSources) && allowedSources.length) {

View File

@ -265,7 +265,7 @@ describe('server: nuxtMiddleware', () => {
expect(res.setHeader).nthCalledWith( expect(res.setHeader).nthCalledWith(
1, 1,
'Content-Security-Policy-Report-Only', 'Content-Security-Policy-Report-Only',
"script-src 'self' 'unsafe-eval' sha256-hashes /nuxt/*.js /nuxt/images/*" "script-src 'self' sha256-hashes /nuxt/*.js /nuxt/images/*"
) )
}) })