2017-10-21 23:22:48 +00:00
|
|
|
var config = require('./config')
|
|
|
|
var uuid = require('uuid')
|
|
|
|
|
|
|
|
var CspStrategy = {}
|
|
|
|
|
|
|
|
var defaultDirectives = {
|
|
|
|
defaultSrc: ['\'self\''],
|
2018-03-30 14:33:32 +00:00
|
|
|
scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', '\'unsafe-eval\''],
|
2018-06-24 12:13:38 +00:00
|
|
|
// ^ TODO: Remove unsafe-eval - webpack script-loader issues https://github.com/hackmdio/codimd/issues/594
|
2017-10-21 23:22:48 +00:00
|
|
|
imgSrc: ['*'],
|
2018-12-20 21:38:31 +00:00
|
|
|
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://github.githubassets.com'], // unsafe-inline is required for some libs, plus used in views
|
2018-10-04 01:02:55 +00:00
|
|
|
fontSrc: ['\'self\'', 'data:', 'https://public.slidesharecdn.com'],
|
2017-10-21 23:22:48 +00:00
|
|
|
objectSrc: ['*'], // Chrome PDF viewer treats PDFs as objects :/
|
2018-03-25 18:50:30 +00:00
|
|
|
mediaSrc: ['*'],
|
2017-10-21 23:22:48 +00:00
|
|
|
childSrc: ['*'],
|
|
|
|
connectSrc: ['*']
|
|
|
|
}
|
|
|
|
|
|
|
|
var cdnDirectives = {
|
|
|
|
scriptSrc: ['https://cdnjs.cloudflare.com', 'https://cdn.mathjax.org'],
|
|
|
|
styleSrc: ['https://cdnjs.cloudflare.com', 'https://fonts.googleapis.com'],
|
|
|
|
fontSrc: ['https://cdnjs.cloudflare.com', 'https://fonts.gstatic.com']
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:33:32 +00:00
|
|
|
var disqusDirectives = {
|
2018-12-05 12:14:34 +00:00
|
|
|
scriptSrc: ['https://disqus.com', 'https://*.disqus.com', 'https://*.disquscdn.com'],
|
2018-03-30 14:33:32 +00:00
|
|
|
styleSrc: ['https://*.disquscdn.com'],
|
|
|
|
fontSrc: ['https://*.disquscdn.com']
|
|
|
|
}
|
|
|
|
|
|
|
|
var googleAnalyticsDirectives = {
|
|
|
|
scriptSrc: ['https://www.google-analytics.com']
|
|
|
|
}
|
|
|
|
|
2017-10-21 23:22:48 +00:00
|
|
|
CspStrategy.computeDirectives = function () {
|
|
|
|
var directives = {}
|
|
|
|
mergeDirectives(directives, config.csp.directives)
|
|
|
|
mergeDirectivesIf(config.csp.addDefaults, directives, defaultDirectives)
|
2018-03-07 14:17:35 +00:00
|
|
|
mergeDirectivesIf(config.useCDN, directives, cdnDirectives)
|
2018-03-30 14:33:32 +00:00
|
|
|
mergeDirectivesIf(config.csp.addDisqus, directives, disqusDirectives)
|
|
|
|
mergeDirectivesIf(config.csp.addGoogleAnalytics, directives, googleAnalyticsDirectives)
|
2017-10-21 23:22:48 +00:00
|
|
|
if (!areAllInlineScriptsAllowed(directives)) {
|
|
|
|
addInlineScriptExceptions(directives)
|
|
|
|
}
|
|
|
|
addUpgradeUnsafeRequestsOptionTo(directives)
|
2018-03-10 13:34:14 +00:00
|
|
|
addReportURI(directives)
|
2017-10-21 23:22:48 +00:00
|
|
|
return directives
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeDirectives (existingDirectives, newDirectives) {
|
|
|
|
for (var propertyName in newDirectives) {
|
|
|
|
var newDirective = newDirectives[propertyName]
|
|
|
|
if (newDirective) {
|
|
|
|
var existingDirective = existingDirectives[propertyName] || []
|
|
|
|
existingDirectives[propertyName] = existingDirective.concat(newDirective)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mergeDirectivesIf (condition, existingDirectives, newDirectives) {
|
|
|
|
if (condition) {
|
|
|
|
mergeDirectives(existingDirectives, newDirectives)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function areAllInlineScriptsAllowed (directives) {
|
|
|
|
return directives.scriptSrc.indexOf('\'unsafe-inline\'') !== -1
|
|
|
|
}
|
|
|
|
|
|
|
|
function addInlineScriptExceptions (directives) {
|
|
|
|
directives.scriptSrc.push(getCspNonce)
|
|
|
|
// TODO: This is the SHA-256 hash of the inline script in build/reveal.js/plugins/notes/notes.html
|
|
|
|
// Any more clean solution appreciated.
|
2018-12-05 10:32:05 +00:00
|
|
|
directives.scriptSrc.push('\'sha256-Lc+VnBdinzYTTAkFrIoUqdoA9EQFeS1AF9ybmF+LLfM=\'')
|
2017-10-21 23:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCspNonce (req, res) {
|
|
|
|
return "'nonce-" + res.locals.nonce + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
function addUpgradeUnsafeRequestsOptionTo (directives) {
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.csp.upgradeInsecureRequests === 'auto' && config.useSSL) {
|
2017-10-21 23:22:48 +00:00
|
|
|
directives.upgradeInsecureRequests = true
|
|
|
|
} else if (config.csp.upgradeInsecureRequests === true) {
|
|
|
|
directives.upgradeInsecureRequests = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 13:34:14 +00:00
|
|
|
function addReportURI (directives) {
|
|
|
|
if (config.csp.reportURI) {
|
|
|
|
directives.reportUri = config.csp.reportURI
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 23:22:48 +00:00
|
|
|
CspStrategy.addNonceToLocals = function (req, res, next) {
|
|
|
|
res.locals.nonce = uuid.v4()
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CspStrategy
|