|
2 | 2 | // inline scripts and content from untrusted sources. |
3 | 3 |
|
4 | 4 | const { contentSecurityPolicy } = require('helmet') |
| 5 | +const isArchivedVersion = require('../lib/is-archived-version') |
| 6 | +const versionSatisfiesRange = require('../lib/version-satisfies-range') |
5 | 7 |
|
6 | | -module.exports = contentSecurityPolicy({ |
7 | | - directives: { |
8 | | - defaultSrc: ["'none'"], |
9 | | - connectSrc: [ |
10 | | - "'self'", |
11 | | - '*.algolia.net', |
12 | | - '*.algolianet.com' |
13 | | - ], |
14 | | - fontSrc: [ |
15 | | - "'self'", |
16 | | - 'data:', |
17 | | - 'github-images.s3.amazonaws.com' |
18 | | - ], |
19 | | - imgSrc: [ |
20 | | - "'self'", |
21 | | - 'github.githubassets.com', |
22 | | - 'github-images.s3.amazonaws.com', |
23 | | - 'octodex.github.com', |
24 | | - 'placehold.it', |
25 | | - '*.githubusercontent.com', |
26 | | - 'github.com' |
27 | | - ], |
28 | | - objectSrc: [ |
29 | | - "'self'" |
30 | | - ], |
31 | | - scriptSrc: [ |
32 | | - "'self'", |
33 | | - 'data:' |
34 | | - ], |
35 | | - frameSrc: [ // exceptions for GraphQL Explorer |
36 | | - 'https://graphql-explorer.githubapp.com', // production env |
37 | | - 'http://localhost:3000', // development env |
38 | | - 'https://www.youtube-nocookie.com' |
39 | | - ], |
40 | | - styleSrc: [ |
41 | | - "'self'", |
42 | | - "'unsafe-inline'" |
43 | | - ], |
44 | | - childSrc: [ |
45 | | - "'self'" // exception for search in deprecated GHE versions |
46 | | - ] |
| 8 | +// module.exports = contentSecurityPolicy({ |
| 9 | +module.exports = async (req, res, next) => { |
| 10 | + const csp = { |
| 11 | + directives: { |
| 12 | + defaultSrc: ["'none'"], |
| 13 | + connectSrc: [ |
| 14 | + "'self'", |
| 15 | + '*.algolia.net', |
| 16 | + '*.algolianet.com' |
| 17 | + ], |
| 18 | + fontSrc: [ |
| 19 | + "'self'", |
| 20 | + 'data:', |
| 21 | + 'github-images.s3.amazonaws.com' |
| 22 | + ], |
| 23 | + imgSrc: [ |
| 24 | + "'self'", |
| 25 | + 'github.githubassets.com', |
| 26 | + 'github-images.s3.amazonaws.com', |
| 27 | + 'octodex.github.com', |
| 28 | + 'placehold.it', |
| 29 | + '*.githubusercontent.com', |
| 30 | + 'github.com' |
| 31 | + ], |
| 32 | + objectSrc: [ |
| 33 | + "'self'" |
| 34 | + ], |
| 35 | + scriptSrc: [ |
| 36 | + "'self'", |
| 37 | + 'data:' |
| 38 | + ], |
| 39 | + frameSrc: [ // exceptions for GraphQL Explorer |
| 40 | + 'https://graphql-explorer.githubapp.com', // production env |
| 41 | + 'http://localhost:3000', // development env |
| 42 | + 'https://www.youtube-nocookie.com' |
| 43 | + ], |
| 44 | + styleSrc: [ |
| 45 | + "'self'", |
| 46 | + "'unsafe-inline'" |
| 47 | + ], |
| 48 | + childSrc: [ |
| 49 | + "'self'" // exception for search in deprecated GHE versions |
| 50 | + ] |
| 51 | + } |
47 | 52 | } |
48 | | -}) |
| 53 | + |
| 54 | + const { requestedVersion } = isArchivedVersion(req) |
| 55 | + |
| 56 | + // Exception for Algolia instantsearch in deprecated Enterprise docs (Node.js era) |
| 57 | + if (versionSatisfiesRange(requestedVersion, '<=2.19') && versionSatisfiesRange(requestedVersion, '>2.12')) { |
| 58 | + csp.directives.scriptSrc.push("'unsafe-eval'") |
| 59 | + } |
| 60 | + |
| 61 | + // Exception for search in deprecated Enterprise docs <=2.12 (static site era) |
| 62 | + if (versionSatisfiesRange(requestedVersion, '<=2.12')) { |
| 63 | + csp.directives.scriptSrc.push("'unsafe-inline'") |
| 64 | + } |
| 65 | + |
| 66 | + return contentSecurityPolicy(csp)(req, res, next) |
| 67 | +} |
0 commit comments