Skip to content

Commit 9cbe03d

Browse files
authored
Merge pull request #761 from SISheogorath/feature/reportURI
Add config option for report URI in CSP
2 parents 976657d + efa490a commit 9cbe03d

4 files changed

Lines changed: 12 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ There are some config settings you need to change in the files below.
207207
| `HMD_HSTS_MAX_AGE` | `31536000` | max duration in seconds to tell clients to keep HSTS status (default is a year) |
208208
| `HMD_HSTS_PRELOAD` | `true` | whether to allow preloading of the site's HSTS status (e.g. into browsers) |
209209
| `HMD_CSP_ENABLE` | `true` | whether to enable Content Security Policy (directives cannot be configured with environment variables) |
210+
| `HMD_CSP_REPORTURI` | `https://<someid>.report-uri.com/r/d/csp/enforce` | Allows to add a URL for CSP reports in case of violations |
210211

211212
## Application settings `config.json`
212213

lib/config/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module.exports = {
1818
directives: {
1919
},
2020
addDefaults: true,
21-
upgradeInsecureRequests: 'auto'
21+
upgradeInsecureRequests: 'auto',
22+
reportURI: undefined
2223
},
2324
protocolusessl: false,
2425
usecdn: true,

lib/config/environment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = {
1515
preload: toBooleanConfig(process.env.HMD_HSTS_PRELOAD)
1616
},
1717
csp: {
18-
enable: toBooleanConfig(process.env.HMD_CSP_ENABLE)
18+
enable: toBooleanConfig(process.env.HMD_CSP_ENABLE),
19+
reportURI: process.env.HMD_CSP_REPORTURI
1920
},
2021
protocolusessl: toBooleanConfig(process.env.HMD_PROTOCOL_USESSL),
2122
alloworigin: toArrayConfig(process.env.HMD_ALLOW_ORIGIN),

lib/csp.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ CspStrategy.computeDirectives = function () {
3030
addInlineScriptExceptions(directives)
3131
}
3232
addUpgradeUnsafeRequestsOptionTo(directives)
33+
addReportURI(directives)
3334
return directives
3435
}
3536

@@ -72,6 +73,12 @@ function addUpgradeUnsafeRequestsOptionTo (directives) {
7273
}
7374
}
7475

76+
function addReportURI (directives) {
77+
if (config.csp.reportURI) {
78+
directives.reportUri = config.csp.reportURI
79+
}
80+
}
81+
7582
CspStrategy.addNonceToLocals = function (req, res, next) {
7683
res.locals.nonce = uuid.v4()
7784
next()

0 commit comments

Comments
 (0)