-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathindex.js
More file actions
296 lines (273 loc) · 8.24 KB
/
index.js
File metadata and controls
296 lines (273 loc) · 8.24 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
// SPDX-License-Identifier: MIT
import { normalizeHttpResponse } from "@middy/util";
// Code and Defaults heavily based off https://helmetjs.github.io/
const defaults = {
contentSecurityPolicy: {
// Fetch directives
// 'child-src': '', // fallback default-src
// 'connect-src': '', // fallback default-src
"default-src": "'report-sample' 'report-sha256'",
// 'font-src':'', // fallback default-src
// 'frame-src':'', // fallback child-src > default-src
// 'img-src':'', // fallback default-src
// 'manifest-src':'', // fallback default-src
// 'media-src':'', // fallback default-src
// 'object-src':'', // fallback default-src
// 'prefetch-src':'', // fallback default-src
// 'script-src':'', // fallback default-src
// 'script-src-elem':'', // fallback script-src > default-src
// 'script-src-attr':'', // fallback script-src > default-src
// 'style-src':'', // fallback default-src
// 'style-src-elem':'', // fallback style-src > default-src
// 'style-src-attr':'', // fallback style-src > default-src
// 'worker-src':'', // fallback child-src > script-src > default-src
// Document directives
"base-uri": "'none'",
sandbox: "",
// Navigation directives
"form-action": "'none'",
"frame-ancestors": "'none'",
// Reporting directives
"report-to": "default",
// Other directives
"require-trusted-types-for": "'script'",
"upgrade-insecure-requests": "",
},
contentSecurityPolicyReportOnly: false,
contentTypeOptions: {
action: "nosniff",
},
crossOriginEmbedderPolicy: {
policy: "require-corp",
},
crossOriginOpenerPolicy: {
policy: "same-origin",
},
crossOriginResourcePolicy: {
policy: "same-origin",
},
dnsPrefetchControl: {
allow: false,
},
downloadOptions: {
action: "noopen",
},
frameOptions: {
action: "deny",
},
originAgentCluster: {},
permissionsPolicy: {
// Standard
accelerometer: "",
"all-screens-capture": "",
"ambient-light-sensor": "",
autoplay: "",
battery: "",
camera: "",
"cross-origin-isolated": "",
"display-capture": "",
"document-domain": "",
"encrypted-media": "",
"execution-while-not-rendered": "",
"execution-while-out-of-viewport": "",
fullscreen: "",
geolocation: "",
gyroscope: "",
"keyboard-map": "",
magnetometer: "",
microphone: "",
midi: "",
monetization: "",
"navigation-override": "",
payment: "",
"picture-in-picture": "",
"publickey-credentials-get": "",
"screen-wake-lock": "",
"sync-xhr": "",
usb: "",
"web-share": "",
"xr-spatial-tracking": "",
// Proposed
"clipboard-read": "",
"clipboard-write": "",
gamepad: "",
"speaker-selection": "",
// Experimental
"conversion-measurement": "",
"focus-without-user-activation": "",
hid: "",
"idle-detection": "",
"interest-cohort": "",
serial: "",
"sync-script": "",
"trust-token-redemption": "",
"window-placement": "",
"vertical-scroll": "",
},
permittedCrossDomainPolicies: {
policy: "none", // none, master-only, by-content-type, by-ftp-filename, all
},
poweredBy: true,
referrerPolicy: {
policy: "no-referrer",
},
reportingEndpoints: {},
reportTo: {
maxAge: 365 * 24 * 60 * 60,
includeSubdomains: true,
},
strictTransportSecurity: {
maxAge: 180 * 24 * 60 * 60,
includeSubDomains: true,
preload: true,
},
xssProtection: false,
};
const helmet = {};
// *** https://github.com/helmetjs/helmet/tree/main/middlewares *** //
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
helmet.contentSecurityPolicy = (reportOnly) => (headers, config) => {
let header = Object.keys(config)
.map((policy) => (config[policy] ? `${policy} ${config[policy]}` : ""))
.filter((str) => str)
.join("; ");
if (config.sandbox === "") {
header += "; sandbox";
}
if (config["upgrade-insecure-requests"] === "") {
header += "; upgrade-insecure-requests";
}
const cspHeaderName = reportOnly
? "Content-Security-Policy-Report-Only"
: "Content-Security-Policy";
headers[cspHeaderName] = header;
};
// crossdomain - N/A - for Adobe products
helmet.crossOriginEmbedderPolicy = (headers, config) => {
headers["Cross-Origin-Embedder-Policy"] = config.policy;
};
helmet.crossOriginOpenerPolicy = (headers, config) => {
headers["Cross-Origin-Opener-Policy"] = config.policy;
};
helmet.crossOriginResourcePolicy = (headers, config) => {
headers["Cross-Origin-Resource-Policy"] = config.policy;
};
// DEPRECATED: expectCt
// DEPRECATED: hpkp
// https://www.permissionspolicy.com/
helmet.permissionsPolicy = (headers, config) => {
headers["Permissions-Policy"] = Object.keys(config)
.map(
(policy) =>
`${policy}=${config[policy] === "*" ? "*" : `(${config[policy]})`}`,
)
.join(", ");
};
helmet.originAgentCluster = (headers) => {
headers["Origin-Agent-Cluster"] = "?1";
};
// https://github.com/helmetjs/referrer-policy
helmet.referrerPolicy = (headers, config) => {
headers["Referrer-Policy"] = config.policy;
};
// DEPRECATED by reportingEndpoints
helmet.reportTo = (headers, config) => {
const keys = Object.keys(config);
headers["Report-To"] = keys
.map((group) => {
if (group === "includeSubdomains" || group === "maxAge") return "";
const includeSubdomains =
group === "default"
? `, "include_subdomains": ${config.includeSubdomains}`
: "";
return config[group] && group !== "includeSubdomains"
? `{ "group": "default", "max_age": ${config.maxAge}, "endpoints": [ { "url": "${config[group]}" } ]${includeSubdomains} }`
: "";
})
.filter((str) => str)
.join(", ");
};
helmet.reportingEndpoints = (headers, config) => {
const keys = Object.keys(config);
headers["Reporting-Endpoints"] = keys
.map((key) => `${key}="${config[key]}"`)
.join(", ");
};
// https://github.com/helmetjs/hsts
helmet.strictTransportSecurity = (headers, config) => {
let header = `max-age=${Math.round(config.maxAge)}`;
if (config.includeSubDomains) {
header += "; includeSubDomains";
}
if (config.preload) {
header += "; preload";
}
headers["Strict-Transport-Security"] = header;
};
// noCache - N/A - separate middleware
// X-* //
// https://github.com/helmetjs/dont-sniff-mimetype
helmet.contentTypeOptions = (headers, config) => {
headers["X-Content-Type-Options"] = config.action;
};
// https://github.com/helmetjs/dns-Prefetch-control
helmet.dnsPrefetchControl = (headers, config) => {
headers["X-DNS-Prefetch-Control"] = config.allow ? "on" : "off";
};
// https://github.com/helmetjs/ienoopen
helmet.downloadOptions = (headers, config) => {
headers["X-Download-Options"] = config.action;
};
// https://github.com/helmetjs/frameOptions
helmet.frameOptions = (headers, config) => {
headers["X-Frame-Options"] = config.action.toUpperCase();
};
// https://github.com/helmetjs/crossdomain
helmet.permittedCrossDomainPolicies = (headers, config) => {
headers["X-Permitted-Cross-Domain-Policies"] = config.policy;
};
// https://github.com/helmetjs/hide-powered-by
helmet.poweredBy = (headers, config) => {
headers.Server = undefined;
headers["X-Powered-By"] = undefined;
};
// https://github.com/helmetjs/x-xss-protection
helmet.xssProtection = (headers, config) => {
headers["X-XSS-Protection"] = "0";
};
const httpSecurityHeadersMiddleware = (opts = {}) => {
const options = { ...defaults, ...opts };
const httpSecurityHeadersMiddlewareAfter = (request) => {
normalizeHttpResponse(request);
for (const key of Object.keys(helmet)) {
if (!options[key]) continue;
const config = { ...defaults[key], ...options[key] };
if (key === "contentSecurityPolicy") {
helmet[key](options.contentSecurityPolicyReportOnly)(
request.response.headers,
config,
);
} else {
helmet[key](request.response.headers, config);
}
}
// Clean up headers removals
const headers = {};
for (const key of Object.keys(request.response.headers)) {
if (typeof request.response.headers[key] !== "undefined") {
headers[key] = request.response.headers[key];
}
}
request.response.headers = headers;
};
const httpSecurityHeadersMiddlewareOnError = async (request) => {
if (typeof request.response === "undefined") return;
await httpSecurityHeadersMiddlewareAfter(request);
};
return {
after: httpSecurityHeadersMiddlewareAfter,
onError: httpSecurityHeadersMiddlewareOnError,
};
};
export default httpSecurityHeadersMiddleware;