Skip to content

Commit 91101c8

Browse files
committed
Change CSP config format to be more intuitive
1 parent 5b83deb commit 91101c8

5 files changed

Lines changed: 48 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Environment variables (will overwrite other server configs)
158158
| HMD_HSTS_INCLUDE_SUBDOMAINS | `true` | set to include subdomains in HSTS (default is `true`) |
159159
| HMD_HSTS_MAX_AGE | `31536000` | max duration in seconds to tell clients to keep HSTS status (default is a year) |
160160
| HMD_HSTS_PRELOAD | `true` | whether to allow preloading of the site's HSTS status (e.g. into browsers) |
161+
| HMD_CSP_ENABLE | `true` | whether to enable Content Security Policy (directives cannot be configured with environment variables) |
161162

162163
Application settings `config.json`
163164
---
@@ -171,6 +172,7 @@ Application settings `config.json`
171172
| alloworigin | `['localhost']` | domain name whitelist |
172173
| usessl | `true` or `false` | set to use ssl server (if true will auto turn on `protocolusessl`) |
173174
| hsts | `{"enable": "true", "maxAgeSeconds": "31536000", "includeSubdomains": "true", "preload": "true"}` | [HSTS](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security) options to use with HTTPS (default is the example value, max age is a year) |
175+
| csp | `{"enable": "true", "directives": {"scriptSrc": "trustwodthy-scripts.example.com"}, "upgradeInsecureRequests": "auto", "addDefaults": "true"}` | Configures [Content Security Policy](https://helmetjs.github.io/docs/csp/). Directives are directly passed to Helmet, so [their format](https://helmetjs.github.io/docs/csp/) applies. Further, some defaults are added so that the application doesn't break. To disable adding these defaults, set `addDefaults` to `false`. If `usecdn` is on, default CDN locations are allowed too. By default (`auto`), insecure (HTTP) requests are upgraded to HTTPS via CSP if `usessl` is on. To change this behaviour, set `upgradeInsecureRequests` to either `true` or `false`. |
174176
| protocolusessl | `true` or `false` | set to use ssl protocol for resources path (only applied when domain is set) |
175177
| urladdport | `true` or `false` | set to add port on callback url (port 80 or 443 won't applied) (only applied when domain is set) |
176178
| usecdn | `true` or `false` | set to use CDN resources or not (default is `true`) |

app.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,28 @@ function getCspWebSocketUrl (req, res) {
125125
return (req.protocol === 'http' ? 'ws:' : 'wss:') + config.serverurl.replace(/https?:/, "")
126126
}
127127

128+
function mergeWithDefaults(configured, defaultDirective, cdnDirective) {
129+
var directive = [].concat(configured)
130+
if (config.csp.addDefaults && defaultDirective) {
131+
directive = directive.concat(defaultDirective)
132+
}
133+
if (config.usecdn && cdnDirective) {
134+
directive = directive.concat(cdnDirective)
135+
}
136+
return directive
137+
}
138+
128139
if (config.csp.enable) {
140+
var defaultDirectives = {
141+
defaultSrc: ['\'self\''],
142+
scriptSrc: ['\'self\'', 'vimeo.com', 'https://gist.github.com', 'www.slideshare.net', 'https://query.yahooapis.com', 'https://*.disqus.com', '\'unsafe-eval\''], // TODO: Remove unsafe-eval - webpack script-loader issues
143+
imgSrc: ['*'],
144+
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'https://assets-cdn.github.com'], // unsafe-inline is required for some libs, plus used in views
145+
fontSrc: ['\'self\'', 'https://public.slidesharecdn.com'],
146+
objectSrc: ['*'], // Chrome PDF viewer treats PDFs as objects :/
147+
childSrc: ['*'],
148+
connectSrc: ['\'self\'', 'https://links.services.disqus.com', 'wss://realtime.services.disqus.com']
149+
};
129150
var cdnDirectives = {
130151
scriptSrc: ['https://cdnjs.cloudflare.com', 'https://cdn.mathjax.org'],
131152
styleSrc: ['https://cdnjs.cloudflare.com', 'https://fonts.googleapis.com'],
@@ -134,11 +155,20 @@ if (config.csp.enable) {
134155
var directives = {}
135156
for (var propertyName in config.csp.directives) {
136157
if (config.csp.directives.hasOwnProperty(propertyName)) {
137-
var directive = [].concat(config.csp.directives[propertyName])
138-
if (config.usecdn && !!cdnDirectives[propertyName]) {
139-
directive = directive.concat(cdnDirectives[propertyName])
140-
}
141-
directives[propertyName] = directive
158+
directives[propertyName] = mergeWithDefaults(
159+
config.csp.directives[propertyName],
160+
defaultDirectives[propertyName],
161+
cdnDirectives[propertyName]
162+
)
163+
}
164+
}
165+
for (var propertyName in defaultDirectives) {
166+
if (!directives[propertyName]) {
167+
directives[propertyName] = mergeWithDefaults(
168+
[],
169+
defaultDirectives[propertyName],
170+
cdnDirectives[propertyName]
171+
)
142172
}
143173
}
144174
directives.scriptSrc.push(getCspNonce)

config.json.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
"includeSubdomains": "true",
2323
"preload": "true"
2424
},
25+
csp: {
26+
"enable": "true",
27+
"directives": {
28+
},
29+
"upgradeInsecureRequests": "auto"
30+
"addDefaults": "true"
31+
},
2532
"db": {
2633
"username": "",
2734
"password": "",

lib/config/default.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@ module.exports = {
1515
},
1616
csp: {
1717
enable: true,
18-
reportUri: '',
1918
directives: {
20-
defaultSrc: ["'self'"],
21-
scriptSrc: ["'self'", "'unsafe-eval'", "vimeo.com", "https://gist.github.com", "www.slideshare.net", "https://query.yahooapis.com", "https://*.disqus.com"],
22-
imgSrc: ["*"],
23-
styleSrc: ["'self'", "'unsafe-inline'", "https://assets-cdn.github.com"],
24-
fontSrc: ["'self'", "https://public.slidesharecdn.com"],
25-
objectSrc: ["*"],
26-
childSrc: ["*"],
27-
connectSrc: ["'self'", "https://links.services.disqus.com", "wss://realtime.services.disqus.com"]
2819
},
20+
addDefaults: true,
2921
upgradeInsecureRequests: 'auto'
3022
},
3123
protocolusessl: false,

lib/config/environment.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
includeSubdomains: toBooleanConfig(process.env.HMD_HSTS_INCLUDE_SUBDOMAINS),
1515
preload: toBooleanConfig(process.env.HMD_HSTS_PRELOAD)
1616
},
17+
csp: {
18+
enable: toBooleanConfig(process.env.HMD_CSP_ENABLE)
19+
},
1720
protocolusessl: toBooleanConfig(process.env.HMD_PROTOCOL_USESSL),
1821
alloworigin: process.env.HMD_ALLOW_ORIGIN ? process.env.HMD_ALLOW_ORIGIN.split(',') : undefined,
1922
usecdn: toBooleanConfig(process.env.HMD_USECDN),

0 commit comments

Comments
 (0)