diff options
author | Rigel Kent <par@rigelk.eu> | 2018-12-13 09:49:45 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-12-13 09:49:45 +0100 |
commit | 5e755fff9d70a7fd3c4f85bb524f1b774dd85b25 (patch) | |
tree | 699a0724de91f4151ec7d67b700f5b7736a78e45 /server/middlewares/csp.ts | |
parent | 9ecac97be024cf2277872986950d7eec85cbc76e (diff) | |
download | PeerTube-5e755fff9d70a7fd3c4f85bb524f1b774dd85b25.tar.gz PeerTube-5e755fff9d70a7fd3c4f85bb524f1b774dd85b25.tar.zst PeerTube-5e755fff9d70a7fd3c4f85bb524f1b774dd85b25.zip |
add Content Security Policy (#1252)
* add Content Security Policy
* remove reflect-metadata on production builds to get rid of unsafe-eval
* fix baseCSP usage
* add SRI to CSP
* add blob: to media-src
* remove SRI
* CSP set to reportOnly
* adding data: to connect-src CSP
* remove block-all-mixed-content
* add report-uri support
Diffstat (limited to 'server/middlewares/csp.ts')
-rw-r--r-- | server/middlewares/csp.ts | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/server/middlewares/csp.ts b/server/middlewares/csp.ts new file mode 100644 index 000000000..a0ed3710b --- /dev/null +++ b/server/middlewares/csp.ts | |||
@@ -0,0 +1,45 @@ | |||
1 | import * as helmet from 'helmet' | ||
2 | import { CONFIG } from '../initializers/constants' | ||
3 | |||
4 | const baseDirectives = Object.assign({}, | ||
5 | { | ||
6 | defaultSrc: ["'none'"], // by default, not specifying default-src = '*' | ||
7 | connectSrc: ['*', 'data:'], | ||
8 | mediaSrc: ["'self'", 'https:', 'blob:'], | ||
9 | fontSrc: ["'self'", 'data:'], | ||
10 | imgSrc: ["'self'", 'data:'], | ||
11 | scriptSrc: ["'self' 'unsafe-inline'"], | ||
12 | styleSrc: ["'self' 'unsafe-inline'"], | ||
13 | // objectSrc: ["'none'"], // only define to allow plugins, else let defaultSrc 'none' block it | ||
14 | formAction: ["'self'"], | ||
15 | frameAncestors: ["'none'"], | ||
16 | baseUri: ["'self'"], | ||
17 | pluginTypes: ["'none'"], | ||
18 | manifestSrc: ["'self'"], | ||
19 | frameSrc: ["'self'"], // instead of deprecated child-src / self because of test-embed | ||
20 | workerSrc: ["'self'"], // instead of deprecated child-src | ||
21 | upgradeInsecureRequests: true | ||
22 | }, | ||
23 | (CONFIG.SERVICES['CSP-LOGGER'] != null) ? { reportUri: CONFIG.SERVICES['CSP-LOGGER'] } : {} | ||
24 | ) | ||
25 | |||
26 | const baseCSP = helmet.contentSecurityPolicy({ | ||
27 | directives: baseDirectives, | ||
28 | browserSniff: false, | ||
29 | reportOnly: true | ||
30 | }) | ||
31 | |||
32 | const embedCSP = helmet.contentSecurityPolicy({ | ||
33 | directives: Object.assign(baseDirectives, { | ||
34 | frameAncestors: ['*'] | ||
35 | }), | ||
36 | browserSniff: false, // assumes a modern browser, but allows CDN in front | ||
37 | reportOnly: true | ||
38 | }) | ||
39 | |||
40 | // --------------------------------------------------------------------------- | ||
41 | |||
42 | export { | ||
43 | baseCSP, | ||
44 | embedCSP | ||
45 | } | ||