Skip to content

Commit 71fedb1

Browse files
authored
Update to helmet 5 (github#28488)
* Update to helmet 5 * Disable cross-origin-embedder-policy * Update helmet.js * Update helmet.js * Add CORS on get / options * Update helmet.js * Update helmet.js * Update helmet.js * Update helmet.js * Revert "Update helmet.js" This reverts commit 61ad2fb641ca16a31bf148164c395f2ba91e1734. * Open up github domains * Include self * Update helmet.js * Update helmet.js
1 parent 6c7319a commit 71fedb1

File tree

6 files changed

+118
-119
lines changed

6 files changed

+118
-119
lines changed

middleware/cors.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

middleware/csp.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

middleware/helmet.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import helmet from 'helmet'
2+
import { cloneDeep } from 'lodash-es'
3+
import isArchivedVersion from '../lib/is-archived-version.js'
4+
import versionSatisfiesRange from '../lib/version-satisfies-range.js'
5+
6+
const isDev = process.env.NODE_ENV === 'development'
7+
const AZURE_STORAGE_URL = 'githubdocs.azureedge.net'
8+
const GITHUB_DOMAINS = [
9+
"'self'",
10+
'github.com',
11+
'*.github.com',
12+
'*.githubusercontent.com',
13+
'*.githubassets.com',
14+
]
15+
16+
const DEFAULT_OPTIONS = {
17+
crossOriginResourcePolicy: true,
18+
crossOriginEmbedderPolicy: false, // doesn't work with youtube
19+
referrerPolicy: {
20+
policy: 'strict-origin-when-cross-origin',
21+
},
22+
// This module defines a Content Security Policy (CSP) to disallow
23+
// inline scripts and content from untrusted sources.
24+
contentSecurityPolicy: {
25+
directives: {
26+
defaultSrc: ["'none'"],
27+
prefetchSrc: ["'self'"],
28+
// When doing local dev, especially in Safari, you need to add `ws:`
29+
// which NextJS uses for the hot module reloading.
30+
connectSrc: ["'self'", isDev && 'ws:'].filter(Boolean),
31+
fontSrc: ["'self'", 'data:', AZURE_STORAGE_URL],
32+
imgSrc: [...GITHUB_DOMAINS, 'data:', AZURE_STORAGE_URL, 'placehold.it'],
33+
objectSrc: ["'self'"],
34+
// For use during development only!
35+
// `unsafe-eval` allows us to use a performant webpack devtool setting (eval)
36+
// https://webpack.js.org/configuration/devtool/#devtool
37+
scriptSrc: ["'self'", isDev && "'unsafe-eval'"].filter(Boolean),
38+
frameSrc: [
39+
...GITHUB_DOMAINS,
40+
isDev && 'http://localhost:3000',
41+
'https://www.youtube-nocookie.com',
42+
].filter(Boolean),
43+
frameAncestors: [...GITHUB_DOMAINS],
44+
styleSrc: ["'self'", "'unsafe-inline'"],
45+
childSrc: ["'self'"], // exception for search in deprecated GHE versions
46+
},
47+
},
48+
}
49+
50+
const NODE_DEPRECATED_OPTIONS = cloneDeep(DEFAULT_OPTIONS)
51+
const { directives: ndDirs } = NODE_DEPRECATED_OPTIONS.contentSecurityPolicy
52+
ndDirs.scriptSrc.push(
53+
"'unsafe-eval'",
54+
"'unsafe-inline'",
55+
'http://www.google-analytics.com',
56+
'https://ssl.google-analytics.com'
57+
)
58+
ndDirs.connectSrc.push('https://www.google-analytics.com')
59+
ndDirs.imgSrc.push('http://www.google-analytics.com', 'https://ssl.google-analytics.com')
60+
61+
const STATIC_DEPRECATED_OPTIONS = cloneDeep(DEFAULT_OPTIONS)
62+
STATIC_DEPRECATED_OPTIONS.contentSecurityPolicy.directives.scriptSrc.push("'unsafe-inline'")
63+
64+
const defaultHelmet = helmet(DEFAULT_OPTIONS)
65+
const nodeDeprecatedHelmet = helmet(NODE_DEPRECATED_OPTIONS)
66+
const staticDeprecatedHelmet = helmet(STATIC_DEPRECATED_OPTIONS)
67+
68+
export default function helmetMiddleware(req, res, next) {
69+
// Enable CORS
70+
if (['GET', 'OPTIONS'].includes(req.method)) {
71+
res.set('access-control-allow-origin', '*')
72+
}
73+
74+
// Determine version for exceptions
75+
const { requestedVersion } = isArchivedVersion(req)
76+
77+
// Exception for deprecated Enterprise docs (Node.js era)
78+
if (
79+
versionSatisfiesRange(requestedVersion, '<=2.19') &&
80+
versionSatisfiesRange(requestedVersion, '>2.12')
81+
) {
82+
return nodeDeprecatedHelmet(req, res, next)
83+
}
84+
85+
// Exception for search in deprecated Enterprise docs <=2.12 (static site era)
86+
if (versionSatisfiesRange(requestedVersion, '<=2.12')) {
87+
return staticDeprecatedHelmet(req, res, next)
88+
}
89+
90+
return defaultHelmet(req, res, next)
91+
}

middleware/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import abort from './abort.js'
99
import timeout from './timeout.js'
1010
import morgan from 'morgan'
1111
import datadog from './connect-datadog.js'
12-
import cors from './cors.js'
13-
import helmet from 'helmet'
14-
import csp from './csp.js'
12+
import helmet from './helmet.js'
1513
import cookieParser from './cookie-parser.js'
1614
import csrf from './csrf.js'
1715
import handleCsrfErrors from './handle-csrf-errors.js'
@@ -208,17 +206,7 @@ export default function (app) {
208206
app.use(instrument(handleNextDataPath, './handle-next-data-path'))
209207

210208
// *** Security ***
211-
app.use(cors)
212-
app.use(
213-
helmet({
214-
// Override referrerPolicy to match the browser's default: "strict-origin-when-cross-origin".
215-
// Helmet now defaults to "no-referrer", which is a problem for our archived assets proxying.
216-
referrerPolicy: {
217-
policy: 'strict-origin-when-cross-origin',
218-
},
219-
})
220-
)
221-
app.use(csp) // Must come after helmet
209+
app.use(helmet)
222210
app.use(cookieParser) // Must come before csrf
223211
app.use(express.json()) // Must come before csrf
224212

package-lock.json

Lines changed: 24 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"hast-util-select": "^5.0.1",
3838
"hast-util-to-string": "^2.0.0",
3939
"hastscript": "^7.0.2",
40-
"helmet": "^4.6.0",
40+
"helmet": "^5.1.0",
4141
"highlight.js": "11.4.0",
4242
"highlightjs-curl": "^1.3.0",
4343
"highlightjs-graphql": "^1.0.2",

0 commit comments

Comments
 (0)