-
-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathinstallHelmet.ts
More file actions
60 lines (54 loc) · 1.91 KB
/
installHelmet.ts
File metadata and controls
60 lines (54 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Express } from "express";
import type { HelmetOptions } from "helmet" assert { "resolution-mode": "import" };
const tmpRootUrl = process.env.ROOT_URL;
if (!tmpRootUrl || typeof tmpRootUrl !== "string") {
throw new Error("Envvar ROOT_URL is required.");
}
const ROOT_URL = tmpRootUrl;
const isDev = process.env.NODE_ENV === "development";
const isTest = process.env.NODE_ENV === "test";
export default async function installHelmet(app: Express) {
const { default: helmet, contentSecurityPolicy } = await import("helmet");
const options: HelmetOptions = {
contentSecurityPolicy: {
directives: {
...contentSecurityPolicy.getDefaultDirectives(),
"connect-src": [
"'self'",
// Safari doesn't allow using wss:// origins as 'self' from
// an https:// page, so we have to translate explicitly for
// it.
ROOT_URL.replace(/^http/, "ws"),
],
},
},
};
// Appease TypeScript
if (
typeof options.contentSecurityPolicy === "boolean" ||
!options.contentSecurityPolicy
) {
throw new Error(`contentSecurityPolicy must be an object`);
}
if (isDev) {
// Disable HSTS in dev so browsers don't cache "always use HTTPS" for localhost
options.hsts = false;
// Remove upgrade-insecure-requests in dev — it causes browsers to upgrade
// subresource requests to HTTPS even when the server only speaks HTTP.
options.contentSecurityPolicy.directives!["upgrade-insecure-requests"] =
null;
}
if (isDev || isTest) {
// Dev needs 'unsafe-eval' due to
// https://github.com/vercel/next.js/issues/14221
options.contentSecurityPolicy.directives!["script-src"] = [
"'self'",
"'unsafe-eval'",
];
}
if (isDev || isTest || !!process.env.ENABLE_GRAPHIQL) {
// Enables prettier script and SVG icon in GraphiQL
options.crossOriginEmbedderPolicy = false;
}
app.use(helmet(options));
}