|
| 1 | +var config = require('./config') |
| 2 | +var uuid = require('uuid') |
| 3 | + |
| 4 | +var CspStrategy = {} |
| 5 | + |
| 6 | +var defaultDirectives = { |
| 7 | + defaultSrc: ['\'self\''], |
| 8 | + scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', 'https://*.disqus.com', '\'unsafe-eval\''], |
| 9 | + // ^ TODO: Remove unsafe-eval - webpack script-loader issues https://github.com/hackmdio/hackmd/issues/594 |
| 10 | + imgSrc: ['*'], |
| 11 | + styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://assets-cdn.github.com'], // unsafe-inline is required for some libs, plus used in views |
| 12 | + fontSrc: ['\'self\'', 'https://public.slidesharecdn.com'], |
| 13 | + objectSrc: ['*'], // Chrome PDF viewer treats PDFs as objects :/ |
| 14 | + childSrc: ['*'], |
| 15 | + connectSrc: ['*'] |
| 16 | +} |
| 17 | + |
| 18 | +var cdnDirectives = { |
| 19 | + scriptSrc: ['https://cdnjs.cloudflare.com', 'https://cdn.mathjax.org'], |
| 20 | + styleSrc: ['https://cdnjs.cloudflare.com', 'https://fonts.googleapis.com'], |
| 21 | + fontSrc: ['https://cdnjs.cloudflare.com', 'https://fonts.gstatic.com'] |
| 22 | +} |
| 23 | + |
| 24 | +CspStrategy.computeDirectives = function () { |
| 25 | + var directives = {} |
| 26 | + mergeDirectives(directives, config.csp.directives) |
| 27 | + mergeDirectivesIf(config.csp.addDefaults, directives, defaultDirectives) |
| 28 | + mergeDirectivesIf(config.usecdn, directives, cdnDirectives) |
| 29 | + if (!areAllInlineScriptsAllowed(directives)) { |
| 30 | + addInlineScriptExceptions(directives) |
| 31 | + } |
| 32 | + addUpgradeUnsafeRequestsOptionTo(directives) |
| 33 | + return directives |
| 34 | +} |
| 35 | + |
| 36 | +function mergeDirectives (existingDirectives, newDirectives) { |
| 37 | + for (var propertyName in newDirectives) { |
| 38 | + var newDirective = newDirectives[propertyName] |
| 39 | + if (newDirective) { |
| 40 | + var existingDirective = existingDirectives[propertyName] || [] |
| 41 | + existingDirectives[propertyName] = existingDirective.concat(newDirective) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function mergeDirectivesIf (condition, existingDirectives, newDirectives) { |
| 47 | + if (condition) { |
| 48 | + mergeDirectives(existingDirectives, newDirectives) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function areAllInlineScriptsAllowed (directives) { |
| 53 | + return directives.scriptSrc.indexOf('\'unsafe-inline\'') !== -1 |
| 54 | +} |
| 55 | + |
| 56 | +function addInlineScriptExceptions (directives) { |
| 57 | + directives.scriptSrc.push(getCspNonce) |
| 58 | + // TODO: This is the SHA-256 hash of the inline script in build/reveal.js/plugins/notes/notes.html |
| 59 | + // Any more clean solution appreciated. |
| 60 | + directives.scriptSrc.push('\'sha256-EtvSSxRwce5cLeFBZbvZvDrTiRoyoXbWWwvEVciM5Ag=\'') |
| 61 | +} |
| 62 | + |
| 63 | +function getCspNonce (req, res) { |
| 64 | + return "'nonce-" + res.locals.nonce + "'" |
| 65 | +} |
| 66 | + |
| 67 | +function addUpgradeUnsafeRequestsOptionTo (directives) { |
| 68 | + if (config.csp.upgradeInsecureRequests === 'auto' && config.usessl) { |
| 69 | + directives.upgradeInsecureRequests = true |
| 70 | + } else if (config.csp.upgradeInsecureRequests === true) { |
| 71 | + directives.upgradeInsecureRequests = true |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +CspStrategy.addNonceToLocals = function (req, res, next) { |
| 76 | + res.locals.nonce = uuid.v4() |
| 77 | + next() |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = CspStrategy |
0 commit comments