Created
October 13, 2015 17:23
-
-
Save mdp/3764582f6c0f3dd24877 to your computer and use it in GitHub Desktop.
Node.js CSP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sources = { | |
'default-src': ['\'self\''], | |
'script-src': ['\'self\'','https://*.myexternalcdn.com'], | |
'frame-src': ['https://someexternalframesource.com'], | |
'img-src': ['\'self\'', 'https:', 'data:'], | |
'style-src': ['\'self\'', 'https:'], | |
'font-src': ['\'self\'', 'https:'], | |
'connect-src': ['\'self\''], | |
} | |
let csp = Object.keys(sources).map(function(key){ | |
return `${key} ${sources[key].join(' ')};` | |
}) | |
export default function(req, res, next){ | |
res.setHeader('Content-Security-Policy', csp.join(' ')) | |
next() | |
} | |
// Usage app.use(cspMiddleware) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment