-
Notifications
You must be signed in to change notification settings - Fork 368
/
index.ts
478 lines (448 loc) · 13.7 KB
/
index.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import type { IncomingMessage, ServerResponse } from "http";
import contentSecurityPolicy, {
type ContentSecurityPolicyOptions,
} from "./middlewares/content-security-policy/index.js";
import crossOriginEmbedderPolicy, {
type CrossOriginEmbedderPolicyOptions,
} from "./middlewares/cross-origin-embedder-policy/index.js";
import crossOriginOpenerPolicy, {
type CrossOriginOpenerPolicyOptions,
} from "./middlewares/cross-origin-opener-policy/index.js";
import crossOriginResourcePolicy, {
type CrossOriginResourcePolicyOptions,
} from "./middlewares/cross-origin-resource-policy/index.js";
import originAgentCluster from "./middlewares/origin-agent-cluster/index.js";
import referrerPolicy, {
type ReferrerPolicyOptions,
} from "./middlewares/referrer-policy/index.js";
import strictTransportSecurity, {
type StrictTransportSecurityOptions,
} from "./middlewares/strict-transport-security/index.js";
import xContentTypeOptions from "./middlewares/x-content-type-options/index.js";
import xDnsPrefetchControl, {
type XDnsPrefetchControlOptions,
} from "./middlewares/x-dns-prefetch-control/index.js";
import xDownloadOptions from "./middlewares/x-download-options/index.js";
import xFrameOptions, {
type XFrameOptionsOptions,
} from "./middlewares/x-frame-options/index.js";
import xPermittedCrossDomainPolicies, {
type XPermittedCrossDomainPoliciesOptions,
} from "./middlewares/x-permitted-cross-domain-policies/index.js";
import xPoweredBy from "./middlewares/x-powered-by/index.js";
import xXssProtection from "./middlewares/x-xss-protection/index.js";
export type HelmetOptions = {
contentSecurityPolicy?: ContentSecurityPolicyOptions | boolean;
crossOriginEmbedderPolicy?: CrossOriginEmbedderPolicyOptions | boolean;
crossOriginOpenerPolicy?: CrossOriginOpenerPolicyOptions | boolean;
crossOriginResourcePolicy?: CrossOriginResourcePolicyOptions | boolean;
originAgentCluster?: boolean;
referrerPolicy?: ReferrerPolicyOptions | boolean;
} & (
| {
strictTransportSecurity?: StrictTransportSecurityOptions | boolean;
hsts?: never;
}
| {
hsts?: StrictTransportSecurityOptions | boolean;
strictTransportSecurity?: never;
}
) &
(
| { xContentTypeOptions?: boolean; noSniff?: never }
| { noSniff?: boolean; xContentTypeOptions?: never }
) &
(
| {
xDnsPrefetchControl?: XDnsPrefetchControlOptions | boolean;
dnsPrefetchControl?: never;
}
| {
dnsPrefetchControl?: XDnsPrefetchControlOptions | boolean;
xDnsPrefetchControl?: never;
}
) &
(
| { xDownloadOptions?: boolean; ieNoOpen?: never }
| { ieNoOpen?: boolean; xDownloadOptions?: never }
) &
(
| { xFrameOptions?: XFrameOptionsOptions | boolean; frameguard?: never }
| { frameguard?: XFrameOptionsOptions | boolean; xFrameOptions?: never }
) &
(
| {
xPermittedCrossDomainPolicies?:
| XPermittedCrossDomainPoliciesOptions
| boolean;
permittedCrossDomainPolicies?: never;
}
| {
permittedCrossDomainPolicies?:
| XPermittedCrossDomainPoliciesOptions
| boolean;
xPermittedCrossDomainPolicies?: never;
}
) &
(
| { xPoweredBy?: boolean; hidePoweredBy?: never }
| { hidePoweredBy?: boolean; xPoweredBy?: never }
) &
(
| { xXssProtection?: boolean; xssFilter?: never }
| { xssFilter?: boolean; xXssProtection?: never }
);
type MiddlewareFunction = (
req: IncomingMessage,
res: ServerResponse,
next: (error?: Error) => void,
) => void;
interface Helmet {
(
options?: Readonly<HelmetOptions>,
): (
req: IncomingMessage,
res: ServerResponse,
next: (err?: unknown) => void,
) => void;
contentSecurityPolicy: typeof contentSecurityPolicy;
crossOriginEmbedderPolicy: typeof crossOriginEmbedderPolicy;
crossOriginOpenerPolicy: typeof crossOriginOpenerPolicy;
crossOriginResourcePolicy: typeof crossOriginResourcePolicy;
originAgentCluster: typeof originAgentCluster;
referrerPolicy: typeof referrerPolicy;
strictTransportSecurity: typeof strictTransportSecurity;
xContentTypeOptions: typeof xContentTypeOptions;
xDnsPrefetchControl: typeof xDnsPrefetchControl;
xDownloadOptions: typeof xDownloadOptions;
xFrameOptions: typeof xFrameOptions;
xPermittedCrossDomainPolicies: typeof xPermittedCrossDomainPolicies;
xPoweredBy: typeof xPoweredBy;
xXssProtection: typeof xXssProtection;
// Legacy aliases
dnsPrefetchControl: typeof xDnsPrefetchControl;
frameguard: typeof xFrameOptions;
hidePoweredBy: typeof xPoweredBy;
hsts: typeof strictTransportSecurity;
ieNoOpen: typeof xDownloadOptions;
noSniff: typeof xContentTypeOptions;
permittedCrossDomainPolicies: typeof xPermittedCrossDomainPolicies;
xssFilter: typeof xXssProtection;
}
function getMiddlewareFunctionsFromOptions(
options: Readonly<HelmetOptions>,
): MiddlewareFunction[] {
const result: MiddlewareFunction[] = [];
switch (options.contentSecurityPolicy) {
case undefined:
case true:
result.push(contentSecurityPolicy());
break;
case false:
break;
default:
result.push(contentSecurityPolicy(options.contentSecurityPolicy));
break;
}
switch (options.crossOriginEmbedderPolicy) {
case undefined:
case false:
break;
case true:
result.push(crossOriginEmbedderPolicy());
break;
default:
result.push(crossOriginEmbedderPolicy(options.crossOriginEmbedderPolicy));
break;
}
switch (options.crossOriginOpenerPolicy) {
case undefined:
case true:
result.push(crossOriginOpenerPolicy());
break;
case false:
break;
default:
result.push(crossOriginOpenerPolicy(options.crossOriginOpenerPolicy));
break;
}
switch (options.crossOriginResourcePolicy) {
case undefined:
case true:
result.push(crossOriginResourcePolicy());
break;
case false:
break;
default:
result.push(crossOriginResourcePolicy(options.crossOriginResourcePolicy));
break;
}
switch (options.originAgentCluster) {
case undefined:
case true:
result.push(originAgentCluster());
break;
case false:
break;
default:
console.warn(
"Origin-Agent-Cluster does not take options. Remove the property to silence this warning.",
);
result.push(originAgentCluster());
break;
}
switch (options.referrerPolicy) {
case undefined:
case true:
result.push(referrerPolicy());
break;
case false:
break;
default:
result.push(referrerPolicy(options.referrerPolicy));
break;
}
if ("strictTransportSecurity" in options && "hsts" in options) {
throw new Error(
"Strict-Transport-Security option was specified twice. Remove `hsts` to silence this warning.",
);
}
const strictTransportSecurityOption =
options.strictTransportSecurity ?? options.hsts;
switch (strictTransportSecurityOption) {
case undefined:
case true:
result.push(strictTransportSecurity());
break;
case false:
break;
default:
result.push(strictTransportSecurity(strictTransportSecurityOption));
break;
}
if ("xContentTypeOptions" in options && "noSniff" in options) {
throw new Error(
"X-Content-Type-Options option was specified twice. Remove `noSniff` to silence this warning.",
);
}
const xContentTypeOptionsOption =
options.xContentTypeOptions ?? options.noSniff;
switch (xContentTypeOptionsOption) {
case undefined:
case true:
result.push(xContentTypeOptions());
break;
case false:
break;
default:
console.warn(
"X-Content-Type-Options does not take options. Remove the property to silence this warning.",
);
result.push(xContentTypeOptions());
break;
}
if ("xDnsPrefetchControl" in options && "dnsPrefetchControl" in options) {
throw new Error(
"X-DNS-Prefetch-Control option was specified twice. Remove `dnsPrefetchControl` to silence this warning.",
);
}
const xDnsPrefetchControlOption =
options.xDnsPrefetchControl ?? options.dnsPrefetchControl;
switch (xDnsPrefetchControlOption) {
case undefined:
case true:
result.push(xDnsPrefetchControl());
break;
case false:
break;
default:
result.push(xDnsPrefetchControl(xDnsPrefetchControlOption));
break;
}
if ("xDownloadOptions" in options && "ieNoOpen" in options) {
throw new Error(
"X-Download-Options option was specified twice. Remove `ieNoOpen` to silence this warning.",
);
}
const xDownloadOptionsOption = options.xDownloadOptions ?? options.ieNoOpen;
switch (xDownloadOptionsOption) {
case undefined:
case true:
result.push(xDownloadOptions());
break;
case false:
break;
default:
console.warn(
"X-Download-Options does not take options. Remove the property to silence this warning.",
);
result.push(xDownloadOptions());
break;
}
if ("xFrameOptions" in options && "frameguard" in options) {
throw new Error(
"X-Frame-Options option was specified twice. Remove `frameguard` to silence this warning.",
);
}
const xFrameOptionsOption = options.xFrameOptions ?? options.frameguard;
switch (xFrameOptionsOption) {
case undefined:
case true:
result.push(xFrameOptions());
break;
case false:
break;
default:
result.push(xFrameOptions(xFrameOptionsOption));
break;
}
if (
"xPermittedCrossDomainPolicies" in options &&
"permittedCrossDomainPolicies" in options
) {
throw new Error(
"X-Permitted-Cross-Domain-Policies option was specified twice. Remove `permittedCrossDomainPolicies` to silence this warning.",
);
}
const xPermittedCrossDomainPoliciesOption =
options.xPermittedCrossDomainPolicies ??
options.permittedCrossDomainPolicies;
switch (xPermittedCrossDomainPoliciesOption) {
case undefined:
case true:
result.push(xPermittedCrossDomainPolicies());
break;
case false:
break;
default:
result.push(
xPermittedCrossDomainPolicies(xPermittedCrossDomainPoliciesOption),
);
break;
}
if ("xPoweredBy" in options && "hidePoweredBy" in options) {
throw new Error(
"X-Powered-By option was specified twice. Remove `hidePoweredBy` to silence this warning.",
);
}
const xPoweredByOption = options.xPoweredBy ?? options.hidePoweredBy;
switch (xPoweredByOption) {
case undefined:
case true:
result.push(xPoweredBy());
break;
case false:
break;
default:
console.warn(
"X-Powered-By does not take options. Remove the property to silence this warning.",
);
result.push(xPoweredBy());
break;
}
if ("xXssProtection" in options && "xssFilter" in options) {
throw new Error(
"X-XSS-Protection option was specified twice. Remove `xssFilter` to silence this warning.",
);
}
const xXssProtectionOption = options.xXssProtection ?? options.xssFilter;
switch (xXssProtectionOption) {
case undefined:
case true:
result.push(xXssProtection());
break;
case false:
break;
default:
console.warn(
"X-XSS-Protection does not take options. Remove the property to silence this warning.",
);
result.push(xXssProtection());
break;
}
return result;
}
const helmet: Helmet = Object.assign(
function helmet(options: Readonly<HelmetOptions> = {}) {
// People should be able to pass an options object with no prototype,
// so we want this optional chaining.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (options.constructor?.name === "IncomingMessage") {
throw new Error(
"It appears you have done something like `app.use(helmet)`, but it should be `app.use(helmet())`.",
);
}
const middlewareFunctions = getMiddlewareFunctionsFromOptions(options);
return function helmetMiddleware(
req: IncomingMessage,
res: ServerResponse,
next: (err?: unknown) => void,
): void {
let middlewareIndex = 0;
(function internalNext(err?: unknown) {
if (err) {
next(err);
return;
}
const middlewareFunction = middlewareFunctions[middlewareIndex];
if (middlewareFunction) {
middlewareIndex++;
middlewareFunction(req, res, internalNext);
} else {
next();
}
})();
};
},
{
contentSecurityPolicy,
crossOriginEmbedderPolicy,
crossOriginOpenerPolicy,
crossOriginResourcePolicy,
originAgentCluster,
referrerPolicy,
strictTransportSecurity,
xContentTypeOptions,
xDnsPrefetchControl,
xDownloadOptions,
xFrameOptions,
xPermittedCrossDomainPolicies,
xPoweredBy,
xXssProtection,
// Legacy aliases
dnsPrefetchControl: xDnsPrefetchControl,
xssFilter: xXssProtection,
permittedCrossDomainPolicies: xPermittedCrossDomainPolicies,
ieNoOpen: xDownloadOptions,
noSniff: xContentTypeOptions,
frameguard: xFrameOptions,
hidePoweredBy: xPoweredBy,
hsts: strictTransportSecurity,
},
);
export default helmet;
export {
contentSecurityPolicy,
crossOriginEmbedderPolicy,
crossOriginOpenerPolicy,
crossOriginResourcePolicy,
originAgentCluster,
referrerPolicy,
strictTransportSecurity,
xContentTypeOptions,
xDnsPrefetchControl,
xDownloadOptions,
xFrameOptions,
xPoweredBy,
xXssProtection,
// Legacy aliases
strictTransportSecurity as hsts,
xContentTypeOptions as noSniff,
xDnsPrefetchControl as dnsPrefetchControl,
xDownloadOptions as ieNoOpen,
xFrameOptions as frameguard,
xPermittedCrossDomainPolicies,
xPermittedCrossDomainPolicies as permittedCrossDomainPolicies,
xPoweredBy as hidePoweredBy,
xXssProtection as xssFilter,
};