Skip to content

Commit

Permalink
feat(vue-renderer): add csp meta tags (#5354)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 authored and pi0 committed Apr 1, 2019
1 parent 57a673d commit b978a37
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
19 changes: 8 additions & 11 deletions packages/config/src/options.js
Expand Up @@ -3,7 +3,6 @@ import fs from 'fs'
import defaultsDeep from 'lodash/defaultsDeep'
import defaults from 'lodash/defaults'
import pick from 'lodash/pick'
import isObject from 'lodash/isObject'
import uniq from 'lodash/uniq'
import consola from 'consola'
import { guardDir, isNonEmptyString, isPureObject, isUrl, getMainModule } from '@nuxt/utils'
Expand Down Expand Up @@ -179,16 +178,14 @@ export function getNuxtConfig(_options) {
}

// Apply default hash to CSP option
const { csp } = options.render

const cspDefaults = {
hashAlgorithm: 'sha256',
allowedSources: undefined,
policies: undefined,
reportOnly: options.debug
}
if (csp) {
options.render.csp = defaults(isObject(csp) ? csp : {}, cspDefaults)
if (options.render.csp) {
options.render.csp = defaults({}, options.render.csp, {
hashAlgorithm: 'sha256',
allowedSources: undefined,
policies: undefined,
addMeta: Boolean(options._generate),
reportOnly: options.debug
})
}

// cssSourceMap
Expand Down
1 change: 1 addition & 0 deletions packages/config/test/options.test.js
Expand Up @@ -81,6 +81,7 @@ describe('config: options', () => {
const { render: { csp } } = getNuxtConfig({ render: { csp: { allowedSources: true, test: true } } })
expect(csp).toEqual({
hashAlgorithm: 'sha256',
addMeta: false,
allowedSources: true,
policies: undefined,
reportOnly: false,
Expand Down
27 changes: 16 additions & 11 deletions packages/vue-renderer/src/renderer.js
Expand Up @@ -407,20 +407,25 @@ export default class VueRenderer {
APP += `<script>${serializedSession}</script>`

// Calculate CSP hashes
const { csp } = this.context.options.render
const cspScriptSrcHashes = []
const csp = this.context.options.render.csp
const containsUnsafeInlineScriptSrc = csp && csp.policies && csp.policies['script-src'] && csp.policies['script-src'].includes(`'unsafe-inline'`)
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) {
const hash = crypto.createHash(csp.hashAlgorithm)
hash.update(serializedSession)
cspScriptSrcHashes.push(`'${csp.hashAlgorithm}-${hash.digest('base64')}'`)
}

// 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)
cspScriptSrcHashes.push(`'${hashAlgorithm}-${hash.digest('base64')}'`)
}
// Call ssr:csp hook
await this.context.nuxt.callHook('vue-renderer:ssr:csp', cspScriptSrcHashes)

// Call ssr:csp hook
await this.context.nuxt.callHook('vue-renderer:ssr:csp', cspScriptSrcHashes)
// Add csp meta tags
if (csp.addMeta) {
HEAD += `<meta http-equiv="Content-Security-Policy" content="script-src ${cspScriptSrcHashes.join()}">`
}
}

// Prepend scripts
APP += this.renderScripts(context)
Expand Down

0 comments on commit b978a37

Please sign in to comment.