2020-04-12 07:11:06 -04:00
|
|
|
import { config } from './config'
|
2020-04-12 13:07:26 -04:00
|
|
|
import { IHelmetContentSecurityPolicyDirectives } from 'helmet'
|
|
|
|
import uuid from 'uuid'
|
|
|
|
import { NextFunction, Request, Response } from 'express'
|
2020-04-12 07:11:06 -04:00
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
type CSPDirectives = IHelmetContentSecurityPolicyDirectives
|
2017-10-21 19:22:48 -04:00
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
const defaultDirectives = {
|
2017-10-21 19:22:48 -04:00
|
|
|
defaultSrc: ['\'self\''],
|
2018-03-30 10:33:32 -04:00
|
|
|
scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', '\'unsafe-eval\''],
|
2018-06-24 08:13:38 -04:00
|
|
|
// ^ TODO: Remove unsafe-eval - webpack script-loader issues https://github.com/hackmdio/codimd/issues/594
|
2017-10-21 19:22:48 -04:00
|
|
|
imgSrc: ['*'],
|
2018-12-20 16:38:31 -05:00
|
|
|
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://github.githubassets.com'], // unsafe-inline is required for some libs, plus used in views
|
2018-10-03 21:02:55 -04:00
|
|
|
fontSrc: ['\'self\'', 'data:', 'https://public.slidesharecdn.com'],
|
2017-10-21 19:22:48 -04:00
|
|
|
objectSrc: ['*'], // Chrome PDF viewer treats PDFs as objects :/
|
2018-03-25 14:50:30 -04:00
|
|
|
mediaSrc: ['*'],
|
2017-10-21 19:22:48 -04:00
|
|
|
childSrc: ['*'],
|
|
|
|
connectSrc: ['*']
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
const cdnDirectives = {
|
2017-10-21 19:22:48 -04:00
|
|
|
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']
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
const disqusDirectives = {
|
2018-12-05 07:14:34 -05:00
|
|
|
scriptSrc: ['https://disqus.com', 'https://*.disqus.com', 'https://*.disquscdn.com'],
|
2018-03-30 10:33:32 -04:00
|
|
|
styleSrc: ['https://*.disquscdn.com'],
|
|
|
|
fontSrc: ['https://*.disquscdn.com']
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
const googleAnalyticsDirectives = {
|
2018-03-30 10:33:32 -04:00
|
|
|
scriptSrc: ['https://www.google-analytics.com']
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
function mergeDirectives (existingDirectives: CSPDirectives, newDirectives: CSPDirectives): void {
|
|
|
|
for (const propertyName in newDirectives) {
|
|
|
|
const newDirective = newDirectives[propertyName]
|
2017-10-21 19:22:48 -04:00
|
|
|
if (newDirective) {
|
2020-04-12 13:07:26 -04:00
|
|
|
const existingDirective = existingDirectives[propertyName] || []
|
2017-10-21 19:22:48 -04:00
|
|
|
existingDirectives[propertyName] = existingDirective.concat(newDirective)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
function mergeDirectivesIf (condition: boolean, existingDirectives: CSPDirectives, newDirectives: CSPDirectives): void {
|
2017-10-21 19:22:48 -04:00
|
|
|
if (condition) {
|
|
|
|
mergeDirectives(existingDirectives, newDirectives)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
function areAllInlineScriptsAllowed (directives: CSPDirectives): boolean {
|
|
|
|
if (directives.scriptSrc) {
|
|
|
|
return directives.scriptSrc.includes('\'unsafe-inline\'')
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCspNonce (req: Request, res: Response): string {
|
|
|
|
return "'nonce-" + res.locals.nonce + "'"
|
2017-10-21 19:22:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
function addInlineScriptExceptions (directives: CSPDirectives): void {
|
|
|
|
if (!directives.scriptSrc) {
|
|
|
|
directives.scriptSrc = []
|
|
|
|
}
|
2017-10-21 19:22:48 -04:00
|
|
|
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.
|
2020-02-01 06:50:07 -05:00
|
|
|
directives.scriptSrc.push('\'sha256-81acLZNZISnyGYZrSuoYhpzwDTTxi7vC1YM4uNxqWaM=\'')
|
2017-10-21 19:22:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
function addUpgradeUnsafeRequestsOptionTo (directives: CSPDirectives): void {
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.csp.upgradeInsecureRequests === 'auto' && config.useSSL) {
|
2017-10-21 19:22:48 -04:00
|
|
|
directives.upgradeInsecureRequests = true
|
|
|
|
} else if (config.csp.upgradeInsecureRequests === true) {
|
|
|
|
directives.upgradeInsecureRequests = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
function addReportURI (directives): void {
|
2018-03-10 08:34:14 -05:00
|
|
|
if (config.csp.reportURI) {
|
|
|
|
directives.reportUri = config.csp.reportURI
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
export function addNonceToLocals (req: Request, res: Response, next: NextFunction): void {
|
2017-10-21 19:22:48 -04:00
|
|
|
res.locals.nonce = uuid.v4()
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:07:26 -04:00
|
|
|
export function computeDirectives (): CSPDirectives {
|
|
|
|
const directives: CSPDirectives = {}
|
|
|
|
mergeDirectives(directives, config.csp.directives)
|
|
|
|
mergeDirectivesIf(config.csp.addDefaults, directives, defaultDirectives)
|
|
|
|
mergeDirectivesIf(config.useCDN, directives, cdnDirectives)
|
|
|
|
mergeDirectivesIf(config.csp.addDisqus, directives, disqusDirectives)
|
|
|
|
mergeDirectivesIf(config.csp.addGoogleAnalytics, directives, googleAnalyticsDirectives)
|
|
|
|
if (!areAllInlineScriptsAllowed(directives)) {
|
|
|
|
addInlineScriptExceptions(directives)
|
|
|
|
}
|
|
|
|
addUpgradeUnsafeRequestsOptionTo(directives)
|
|
|
|
addReportURI(directives)
|
|
|
|
return directives
|
|
|
|
}
|