@hoajs/secure-headers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,811 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ contentSecurityPolicy: () => contentSecurityPolicy_default,
23
+ crossOriginEmbedderPolicy: () => crossOriginEmbedderPolicy_default,
24
+ crossOriginOpenerPolicy: () => crossOriginOpenerPolicy_default,
25
+ crossOriginResourcePolicy: () => crossOriginResourcePolicy_default,
26
+ default: () => index_default,
27
+ dnsPrefetchControl: () => xDnsPrefetchControl_default,
28
+ frameguard: () => xFrameOptions_default,
29
+ hsts: () => strictTransportSecurity_default,
30
+ ieNoOpen: () => xDownloadOptions_default,
31
+ noSniff: () => xContentTypeOptions_default,
32
+ originAgentCluster: () => originAgentCluster_default,
33
+ permissionPolicy: () => permissionPolicy_default,
34
+ permittedCrossDomainPolicies: () => xPermittedCrossDomainPolicies_default,
35
+ referrerPolicy: () => referrerPolicy_default,
36
+ secureHeaders: () => secureHeaders,
37
+ strictTransportSecurity: () => strictTransportSecurity_default,
38
+ xContentTypeOptions: () => xContentTypeOptions_default,
39
+ xDnsPrefetchControl: () => xDnsPrefetchControl_default,
40
+ xDownloadOptions: () => xDownloadOptions_default,
41
+ xFrameOptions: () => xFrameOptions_default,
42
+ xPermittedCrossDomainPolicies: () => xPermittedCrossDomainPolicies_default,
43
+ xXssProtection: () => xXssProtection_default,
44
+ xssFilter: () => xXssProtection_default
45
+ });
46
+ module.exports = __toCommonJS(index_exports);
47
+ var import_hoa = require("hoa");
48
+
49
+ // src/contentSecurityPolicy.ts
50
+ var dangerouslyDisableDefaultSrc = Symbol("dangerouslyDisableDefaultSrc");
51
+ var SHOULD_BE_QUOTED = /* @__PURE__ */ new Set([
52
+ "none",
53
+ "self",
54
+ "strict-dynamic",
55
+ "report-sample",
56
+ "inline-speculation-rules",
57
+ "unsafe-inline",
58
+ "unsafe-eval",
59
+ "unsafe-hashes",
60
+ "wasm-unsafe-eval"
61
+ ]);
62
+ var getDefaultDirectives = () => ({
63
+ "default-src": ["'self'"],
64
+ "base-uri": ["'self'"],
65
+ "font-src": ["'self'", "https:", "data:"],
66
+ "form-action": ["'self'"],
67
+ "frame-ancestors": ["'self'"],
68
+ "img-src": ["'self'", "data:"],
69
+ "object-src": ["'none'"],
70
+ "script-src": ["'self'"],
71
+ "script-src-attr": ["'none'"],
72
+ "style-src": ["'self'", "https:", "'unsafe-inline'"],
73
+ "upgrade-insecure-requests": []
74
+ });
75
+ var dashify = (str) => str.replace(/[A-Z]/g, (capitalLetter) => "-" + capitalLetter.toLowerCase());
76
+ var assertDirectiveValueIsValid = (directiveName, directiveValue) => {
77
+ if (/;|,/.test(directiveValue)) {
78
+ throw new Error(
79
+ `Content-Security-Policy received an invalid directive value for ${JSON.stringify(
80
+ directiveName
81
+ )}`
82
+ );
83
+ }
84
+ };
85
+ var assertDirectiveValueEntryIsValid = (directiveName, directiveValueEntry) => {
86
+ if (SHOULD_BE_QUOTED.has(directiveValueEntry) || directiveValueEntry.startsWith("nonce-") || directiveValueEntry.startsWith("sha256-") || directiveValueEntry.startsWith("sha384-") || directiveValueEntry.startsWith("sha512-")) {
87
+ throw new Error(
88
+ `Content-Security-Policy received an invalid directive value for ${JSON.stringify(
89
+ directiveName
90
+ )}. ${JSON.stringify(directiveValueEntry)} should be quoted`
91
+ );
92
+ }
93
+ };
94
+ function normalizeDirectives(options) {
95
+ const defaultDirectives = getDefaultDirectives();
96
+ const { useDefaults = true, directives: rawDirectives = defaultDirectives } = options;
97
+ const result = /* @__PURE__ */ new Map();
98
+ const directiveNamesSeen = /* @__PURE__ */ new Set();
99
+ const directivesExplicitlyDisabled = /* @__PURE__ */ new Set();
100
+ for (const rawDirectiveName in rawDirectives) {
101
+ if (!Object.hasOwn(rawDirectives, rawDirectiveName)) {
102
+ continue;
103
+ }
104
+ if (rawDirectiveName.length === 0 || /[^a-zA-Z0-9-]/.test(rawDirectiveName)) {
105
+ throw new Error(
106
+ `Content-Security-Policy received an invalid directive name ${JSON.stringify(
107
+ rawDirectiveName
108
+ )}`
109
+ );
110
+ }
111
+ const directiveName = dashify(rawDirectiveName);
112
+ if (directiveNamesSeen.has(directiveName)) {
113
+ throw new Error(
114
+ `Content-Security-Policy received a duplicate directive ${JSON.stringify(
115
+ directiveName
116
+ )}`
117
+ );
118
+ }
119
+ directiveNamesSeen.add(directiveName);
120
+ const rawDirectiveValue = rawDirectives[rawDirectiveName];
121
+ let directiveValue;
122
+ if (rawDirectiveValue === null) {
123
+ if (directiveName === "default-src") {
124
+ throw new Error(
125
+ "Content-Security-Policy needs a default-src but it was set to `null`. If you really want to disable it, set it to `contentSecurityPolicy.dangerouslyDisableDefaultSrc`."
126
+ );
127
+ }
128
+ directivesExplicitlyDisabled.add(directiveName);
129
+ continue;
130
+ } else if (typeof rawDirectiveValue === "string") {
131
+ directiveValue = [rawDirectiveValue];
132
+ } else if (!rawDirectiveValue) {
133
+ throw new Error(
134
+ `Content-Security-Policy received an invalid directive value for ${JSON.stringify(
135
+ directiveName
136
+ )}`
137
+ );
138
+ } else if (rawDirectiveValue === dangerouslyDisableDefaultSrc) {
139
+ if (directiveName === "default-src") {
140
+ directivesExplicitlyDisabled.add("default-src");
141
+ continue;
142
+ } else {
143
+ throw new Error(
144
+ `Content-Security-Policy: tried to disable ${JSON.stringify(
145
+ directiveName
146
+ )} as if it were default-src; simply omit the key`
147
+ );
148
+ }
149
+ } else {
150
+ directiveValue = rawDirectiveValue;
151
+ }
152
+ for (const element of directiveValue) {
153
+ if (typeof element !== "string") continue;
154
+ assertDirectiveValueIsValid(directiveName, element);
155
+ assertDirectiveValueEntryIsValid(directiveName, element);
156
+ }
157
+ result.set(directiveName, directiveValue);
158
+ }
159
+ if (useDefaults) {
160
+ Object.entries(defaultDirectives).forEach(
161
+ ([defaultDirectiveName, defaultDirectiveValue]) => {
162
+ if (!result.has(defaultDirectiveName) && !directivesExplicitlyDisabled.has(defaultDirectiveName)) {
163
+ result.set(defaultDirectiveName, defaultDirectiveValue);
164
+ }
165
+ }
166
+ );
167
+ }
168
+ if (!result.size) {
169
+ throw new Error(
170
+ "Content-Security-Policy has no directives. Either set some or disable the header"
171
+ );
172
+ }
173
+ if (!result.has("default-src") && !directivesExplicitlyDisabled.has("default-src")) {
174
+ throw new Error(
175
+ "Content-Security-Policy needs a default-src but none was provided. If you really want to disable it, set it to `contentSecurityPolicy.dangerouslyDisableDefaultSrc`."
176
+ );
177
+ }
178
+ return result;
179
+ }
180
+ function getHeaderValue(ctx, normalizedDirectives) {
181
+ const result = [];
182
+ for (const [directiveName, rawDirectiveValue] of normalizedDirectives) {
183
+ let directiveValue = "";
184
+ for (const element of rawDirectiveValue) {
185
+ if (typeof element === "function") {
186
+ const newElement = element(ctx);
187
+ assertDirectiveValueEntryIsValid(directiveName, newElement);
188
+ directiveValue += " " + newElement;
189
+ } else {
190
+ directiveValue += " " + element;
191
+ }
192
+ }
193
+ if (directiveValue) {
194
+ assertDirectiveValueIsValid(directiveName, directiveValue);
195
+ result.push(`${directiveName}${directiveValue}`);
196
+ } else {
197
+ result.push(directiveName);
198
+ }
199
+ }
200
+ return result.join(";");
201
+ }
202
+ var contentSecurityPolicy = function contentSecurityPolicy2(options = {}) {
203
+ const headerName = options.reportOnly ? "Content-Security-Policy-Report-Only" : "Content-Security-Policy";
204
+ const normalizedDirectives = normalizeDirectives(options);
205
+ return async function contentSecurityPolicyMiddleware(ctx, next) {
206
+ const result = getHeaderValue(ctx, normalizedDirectives);
207
+ ctx.res.set(headerName, result);
208
+ await next();
209
+ };
210
+ };
211
+ contentSecurityPolicy.getDefaultDirectives = getDefaultDirectives;
212
+ contentSecurityPolicy.dangerouslyDisableDefaultSrc = dangerouslyDisableDefaultSrc;
213
+ var contentSecurityPolicy_default = contentSecurityPolicy;
214
+
215
+ // src/crossOriginEmbedderPolicy.ts
216
+ var ALLOWED_POLICIES = /* @__PURE__ */ new Set([
217
+ "require-corp",
218
+ "credentialless",
219
+ "unsafe-none"
220
+ ]);
221
+ function getHeaderValueFromOptions({
222
+ policy = "require-corp"
223
+ }) {
224
+ if (ALLOWED_POLICIES.has(policy)) {
225
+ return policy;
226
+ } else {
227
+ throw new Error(
228
+ `Cross-Origin-Embedder-Policy does not support the ${JSON.stringify(
229
+ policy
230
+ )} policy`
231
+ );
232
+ }
233
+ }
234
+ function crossOriginEmbedderPolicy(options = {}) {
235
+ const headerValue = getHeaderValueFromOptions(options);
236
+ return async function crossOriginEmbedderPolicyMiddleware(ctx, next) {
237
+ ctx.res.set("Cross-Origin-Embedder-Policy", headerValue);
238
+ await next();
239
+ };
240
+ }
241
+ var crossOriginEmbedderPolicy_default = crossOriginEmbedderPolicy;
242
+
243
+ // src/crossOriginOpenerPolicy.ts
244
+ var ALLOWED_POLICIES2 = /* @__PURE__ */ new Set(["same-origin", "same-origin-allow-popups", "unsafe-none"]);
245
+ function getHeaderValueFromOptions2({
246
+ policy = "same-origin"
247
+ }) {
248
+ if (ALLOWED_POLICIES2.has(policy)) {
249
+ return policy;
250
+ } else {
251
+ throw new Error(
252
+ `Cross-Origin-Opener-Policy does not support the ${JSON.stringify(
253
+ policy
254
+ )} policy`
255
+ );
256
+ }
257
+ }
258
+ function crossOriginOpenerPolicy(options = {}) {
259
+ const headerValue = getHeaderValueFromOptions2(options);
260
+ return async function crossOriginOpenerPolicyMiddleware(ctx, next) {
261
+ ctx.res.set("Cross-Origin-Opener-Policy", headerValue);
262
+ await next();
263
+ };
264
+ }
265
+ var crossOriginOpenerPolicy_default = crossOriginOpenerPolicy;
266
+
267
+ // src/crossOriginResourcePolicy.ts
268
+ var ALLOWED_POLICIES3 = /* @__PURE__ */ new Set(["same-origin", "same-site", "cross-origin"]);
269
+ function getHeaderValueFromOptions3({
270
+ policy = "same-origin"
271
+ }) {
272
+ if (ALLOWED_POLICIES3.has(policy)) {
273
+ return policy;
274
+ } else {
275
+ throw new Error(
276
+ `Cross-Origin-Resource-Policy does not support the ${JSON.stringify(
277
+ policy
278
+ )} policy`
279
+ );
280
+ }
281
+ }
282
+ function crossOriginResourcePolicy(options = {}) {
283
+ const headerValue = getHeaderValueFromOptions3(options);
284
+ return async function crossOriginResourcePolicyMiddleware(ctx, next) {
285
+ ctx.res.set("Cross-Origin-Resource-Policy", headerValue);
286
+ await next();
287
+ };
288
+ }
289
+ var crossOriginResourcePolicy_default = crossOriginResourcePolicy;
290
+
291
+ // src/originAgentCluster.ts
292
+ function originAgentCluster() {
293
+ return async function originAgentClusterMiddleware(ctx, next) {
294
+ ctx.res.set("Origin-Agent-Cluster", "?1");
295
+ await next();
296
+ };
297
+ }
298
+ var originAgentCluster_default = originAgentCluster;
299
+
300
+ // src/referrerPolicy.ts
301
+ var ALLOWED_TOKENS = /* @__PURE__ */ new Set([
302
+ "no-referrer",
303
+ "no-referrer-when-downgrade",
304
+ "same-origin",
305
+ "origin",
306
+ "strict-origin",
307
+ "origin-when-cross-origin",
308
+ "strict-origin-when-cross-origin",
309
+ "unsafe-url",
310
+ ""
311
+ ]);
312
+ function getHeaderValueFromOptions4({
313
+ policy = ["no-referrer"]
314
+ }) {
315
+ const tokens = typeof policy === "string" ? [policy] : policy;
316
+ if (tokens.length === 0) {
317
+ throw new Error("Referrer-Policy received no policy tokens");
318
+ }
319
+ const tokensSeen = /* @__PURE__ */ new Set();
320
+ tokens.forEach((token) => {
321
+ if (!ALLOWED_TOKENS.has(token)) {
322
+ throw new Error(
323
+ `Referrer-Policy received an unexpected policy token ${JSON.stringify(
324
+ token
325
+ )}`
326
+ );
327
+ } else if (tokensSeen.has(token)) {
328
+ throw new Error(
329
+ `Referrer-Policy received a duplicate policy token ${JSON.stringify(
330
+ token
331
+ )}`
332
+ );
333
+ }
334
+ tokensSeen.add(token);
335
+ });
336
+ return tokens.join(",");
337
+ }
338
+ function referrerPolicy(options = {}) {
339
+ const headerValue = getHeaderValueFromOptions4(options);
340
+ return async function referrerPolicyMiddleware(ctx, next) {
341
+ ctx.res.set("Referrer-Policy", headerValue);
342
+ await next();
343
+ };
344
+ }
345
+ var referrerPolicy_default = referrerPolicy;
346
+
347
+ // src/strictTransportSecurity.ts
348
+ var DEFAULT_MAX_AGE = 365 * 24 * 60 * 60;
349
+ function parseMaxAge(value = DEFAULT_MAX_AGE) {
350
+ if (value >= 0 && Number.isFinite(value)) {
351
+ return Math.floor(value);
352
+ } else {
353
+ throw new Error(
354
+ `Strict-Transport-Security: ${JSON.stringify(
355
+ value
356
+ )} is not a valid value for maxAge. Please choose a positive integer.`
357
+ );
358
+ }
359
+ }
360
+ function getHeaderValueFromOptions5(options) {
361
+ if ("maxage" in options) {
362
+ throw new Error(
363
+ "Strict-Transport-Security received an unsupported property, `maxage`. Did you mean to pass `maxAge`?"
364
+ );
365
+ }
366
+ if ("includeSubdomains" in options) {
367
+ throw new Error(
368
+ 'Strict-Transport-Security middleware should use `includeSubDomains` instead of `includeSubdomains`. (The correct one has an uppercase "D".)'
369
+ );
370
+ }
371
+ const directives = [`max-age=${parseMaxAge(options.maxAge)}`];
372
+ if (options.includeSubDomains === void 0 || options.includeSubDomains) {
373
+ directives.push("includeSubDomains");
374
+ }
375
+ if (options.preload) {
376
+ directives.push("preload");
377
+ }
378
+ return directives.join("; ");
379
+ }
380
+ function strictTransportSecurity(options = {}) {
381
+ const headerValue = getHeaderValueFromOptions5(options);
382
+ return async function strictTransportSecurityMiddleware(ctx, next) {
383
+ ctx.res.set("Strict-Transport-Security", headerValue);
384
+ await next();
385
+ };
386
+ }
387
+ var strictTransportSecurity_default = strictTransportSecurity;
388
+
389
+ // src/xContentTypeOptions.ts
390
+ function xContentTypeOptions() {
391
+ return async function xContentTypeOptionsMiddleware(ctx, next) {
392
+ ctx.res.set("X-Content-Type-Options", "nosniff");
393
+ await next();
394
+ };
395
+ }
396
+ var xContentTypeOptions_default = xContentTypeOptions;
397
+
398
+ // src/xDnsPrefetchControl.ts
399
+ function xDnsPrefetchControl(options = {}) {
400
+ const headerValue = options.allow ? "on" : "off";
401
+ return async function xDnsPrefetchControlMiddleware(ctx, next) {
402
+ ctx.res.set("X-DNS-Prefetch-Control", headerValue);
403
+ await next();
404
+ };
405
+ }
406
+ var xDnsPrefetchControl_default = xDnsPrefetchControl;
407
+
408
+ // src/xDownloadOptions.ts
409
+ function xDownloadOptions() {
410
+ return async function xDownloadOptionsMiddleware(ctx, next) {
411
+ ctx.res.set("X-Download-Options", "noopen");
412
+ await next();
413
+ };
414
+ }
415
+ var xDownloadOptions_default = xDownloadOptions;
416
+
417
+ // src/xFrameOptions.ts
418
+ function getHeaderValueFromOptions6({
419
+ action = "sameorigin"
420
+ }) {
421
+ const normalizedAction = typeof action === "string" ? action.toUpperCase() : action;
422
+ switch (normalizedAction) {
423
+ case "SAME-ORIGIN":
424
+ return "SAMEORIGIN";
425
+ case "DENY":
426
+ case "SAMEORIGIN":
427
+ return normalizedAction;
428
+ default:
429
+ throw new Error(
430
+ `X-Frame-Options received an invalid action ${JSON.stringify(action)}`
431
+ );
432
+ }
433
+ }
434
+ function xFrameOptions(options = {}) {
435
+ const headerValue = getHeaderValueFromOptions6(options);
436
+ return async function xFrameOptionsMiddleware(ctx, next) {
437
+ ctx.res.set("X-Frame-Options", headerValue);
438
+ await next();
439
+ };
440
+ }
441
+ var xFrameOptions_default = xFrameOptions;
442
+
443
+ // src/xPermittedCrossDomainPolicies.ts
444
+ var ALLOWED_PERMITTED_POLICIES = /* @__PURE__ */ new Set([
445
+ "none",
446
+ "master-only",
447
+ "by-content-type",
448
+ "all"
449
+ ]);
450
+ function getHeaderValueFromOptions7({
451
+ permittedPolicies = "none"
452
+ }) {
453
+ if (ALLOWED_PERMITTED_POLICIES.has(permittedPolicies)) {
454
+ return permittedPolicies;
455
+ } else {
456
+ throw new Error(
457
+ `X-Permitted-Cross-Domain-Policies does not support ${JSON.stringify(
458
+ permittedPolicies
459
+ )}`
460
+ );
461
+ }
462
+ }
463
+ function xPermittedCrossDomainPolicies(options = {}) {
464
+ const headerValue = getHeaderValueFromOptions7(options);
465
+ return async function xPermittedCrossDomainPoliciesMiddleware(ctx, next) {
466
+ ctx.res.set("X-Permitted-Cross-Domain-Policies", headerValue);
467
+ await next();
468
+ };
469
+ }
470
+ var xPermittedCrossDomainPolicies_default = xPermittedCrossDomainPolicies;
471
+
472
+ // src/xXssProtection.ts
473
+ function xXssProtection() {
474
+ return async function xXssProtectionMiddleware(ctx, next) {
475
+ ctx.res.set("X-XSS-Protection", "0");
476
+ await next();
477
+ };
478
+ }
479
+ var xXssProtection_default = xXssProtection;
480
+
481
+ // src/permissionPolicy.ts
482
+ function dashify2(str) {
483
+ return str.replace(/([a-z\d])([A-Z])/g, "$1-$2").toLowerCase();
484
+ }
485
+ var SHOULD_NOT_BE_QUOTED = /* @__PURE__ */ new Set(["*", "none", "self", "src"]);
486
+ function normalizeDirectives2(options) {
487
+ const directiveNamesSeen = /* @__PURE__ */ new Set();
488
+ const result = {};
489
+ for (const rawDirectiveName in options) {
490
+ const directiveName = dashify2(rawDirectiveName);
491
+ if (directiveNamesSeen.has(directiveName)) {
492
+ throw new Error(`Permission-Policy received a duplicate directive ${JSON.stringify(
493
+ directiveName
494
+ )}`);
495
+ }
496
+ directiveNamesSeen.add(directiveName);
497
+ const rawDirectiveValue = options[rawDirectiveName];
498
+ if (typeof rawDirectiveValue === "boolean") {
499
+ result[directiveName] = rawDirectiveValue ? "(*)" : "()";
500
+ } else if (Array.isArray(rawDirectiveValue)) {
501
+ if (rawDirectiveValue.length === 0) {
502
+ result[directiveName] = "()";
503
+ } else if (rawDirectiveValue.length === 1 && (rawDirectiveValue[0] === "*" || rawDirectiveValue[0] === "none")) {
504
+ result[directiveName] = `(${rawDirectiveValue[0]})`;
505
+ } else {
506
+ const allowList = rawDirectiveValue.map((v) => SHOULD_NOT_BE_QUOTED.has(v) ? v : `"${v}"`);
507
+ result[directiveName] = `(${allowList.join(" ")})`;
508
+ }
509
+ } else {
510
+ throw new Error(
511
+ `Permission-Policy received an invalid directive value for ${JSON.stringify(
512
+ directiveName
513
+ )}. ${JSON.stringify(rawDirectiveValue)} should be a boolean or an array of strings.`
514
+ );
515
+ }
516
+ }
517
+ if (Object.keys(result).length === 0) {
518
+ throw new Error("Permission-Policy has no directives. Either set some or disable the header");
519
+ }
520
+ return result;
521
+ }
522
+ function getHeaderValue2(normalizedDirectives) {
523
+ return Object.entries(normalizedDirectives).map(([k, v]) => `${k}=${v}`).join(", ");
524
+ }
525
+ function permissionPolicy(options = {}) {
526
+ const normalizedDirectives = normalizeDirectives2(options);
527
+ const headerValue = getHeaderValue2(normalizedDirectives);
528
+ return async function permissionPolicyMiddleware(ctx, next) {
529
+ ctx.res.set("Permissions-Policy", headerValue);
530
+ await next();
531
+ };
532
+ }
533
+ var permissionPolicy_default = permissionPolicy;
534
+
535
+ // src/index.ts
536
+ function getMiddlewareFunctionsFromOptions(options) {
537
+ const result = [];
538
+ switch (options.contentSecurityPolicy) {
539
+ case void 0:
540
+ case true:
541
+ result.push(contentSecurityPolicy_default());
542
+ break;
543
+ case false:
544
+ break;
545
+ default:
546
+ result.push(contentSecurityPolicy_default(options.contentSecurityPolicy));
547
+ break;
548
+ }
549
+ switch (options.crossOriginEmbedderPolicy) {
550
+ case void 0:
551
+ case false:
552
+ break;
553
+ case true:
554
+ result.push(crossOriginEmbedderPolicy_default());
555
+ break;
556
+ default:
557
+ result.push(crossOriginEmbedderPolicy_default(options.crossOriginEmbedderPolicy));
558
+ break;
559
+ }
560
+ switch (options.crossOriginOpenerPolicy) {
561
+ case void 0:
562
+ case true:
563
+ result.push(crossOriginOpenerPolicy_default());
564
+ break;
565
+ case false:
566
+ break;
567
+ default:
568
+ result.push(crossOriginOpenerPolicy_default(options.crossOriginOpenerPolicy));
569
+ break;
570
+ }
571
+ switch (options.crossOriginResourcePolicy) {
572
+ case void 0:
573
+ case true:
574
+ result.push(crossOriginResourcePolicy_default());
575
+ break;
576
+ case false:
577
+ break;
578
+ default:
579
+ result.push(crossOriginResourcePolicy_default(options.crossOriginResourcePolicy));
580
+ break;
581
+ }
582
+ switch (options.originAgentCluster) {
583
+ case void 0:
584
+ case true:
585
+ result.push(originAgentCluster_default());
586
+ break;
587
+ case false:
588
+ break;
589
+ default:
590
+ result.push(originAgentCluster_default());
591
+ break;
592
+ }
593
+ switch (options.referrerPolicy) {
594
+ case void 0:
595
+ case true:
596
+ result.push(referrerPolicy_default());
597
+ break;
598
+ case false:
599
+ break;
600
+ default:
601
+ result.push(referrerPolicy_default(options.referrerPolicy));
602
+ break;
603
+ }
604
+ if ("strictTransportSecurity" in options && "hsts" in options) {
605
+ throw new Error(
606
+ "Strict-Transport-Security option was specified twice. Remove the `hsts` option to fix this error."
607
+ );
608
+ }
609
+ const strictTransportSecurityOption = options.strictTransportSecurity ?? options.hsts;
610
+ switch (strictTransportSecurityOption) {
611
+ case void 0:
612
+ case true:
613
+ result.push(strictTransportSecurity_default());
614
+ break;
615
+ case false:
616
+ break;
617
+ default:
618
+ result.push(strictTransportSecurity_default(strictTransportSecurityOption));
619
+ break;
620
+ }
621
+ if ("xContentTypeOptions" in options && "noSniff" in options) {
622
+ throw new Error(
623
+ "X-Content-Type-Options option was specified twice. Remove the `noSniff` option to fix this error."
624
+ );
625
+ }
626
+ const xContentTypeOptionsOption = options.xContentTypeOptions ?? options.noSniff;
627
+ switch (xContentTypeOptionsOption) {
628
+ case void 0:
629
+ case true:
630
+ result.push(xContentTypeOptions_default());
631
+ break;
632
+ case false:
633
+ break;
634
+ default:
635
+ result.push(xContentTypeOptions_default());
636
+ break;
637
+ }
638
+ if ("xDnsPrefetchControl" in options && "dnsPrefetchControl" in options) {
639
+ throw new Error(
640
+ "X-DNS-Prefetch-Control option was specified twice. Remove the `dnsPrefetchControl` option to fix this error."
641
+ );
642
+ }
643
+ const xDnsPrefetchControlOption = options.xDnsPrefetchControl ?? options.dnsPrefetchControl;
644
+ switch (xDnsPrefetchControlOption) {
645
+ case void 0:
646
+ case true:
647
+ result.push(xDnsPrefetchControl_default());
648
+ break;
649
+ case false:
650
+ break;
651
+ default:
652
+ result.push(xDnsPrefetchControl_default(xDnsPrefetchControlOption));
653
+ break;
654
+ }
655
+ if ("xDownloadOptions" in options && "ieNoOpen" in options) {
656
+ throw new Error(
657
+ "X-Download-Options option was specified twice. Remove the `ieNoOpen` option to fix this error."
658
+ );
659
+ }
660
+ const xDownloadOptionsOption = options.xDownloadOptions ?? options.ieNoOpen;
661
+ switch (xDownloadOptionsOption) {
662
+ case void 0:
663
+ case true:
664
+ result.push(xDownloadOptions_default());
665
+ break;
666
+ case false:
667
+ break;
668
+ default:
669
+ result.push(xDownloadOptions_default());
670
+ break;
671
+ }
672
+ if ("xFrameOptions" in options && "frameguard" in options) {
673
+ throw new Error(
674
+ "X-Frame-Options option was specified twice. Remove the `frameguard` option to fix this error."
675
+ );
676
+ }
677
+ const xFrameOptionsOption = options.xFrameOptions ?? options.frameguard;
678
+ switch (xFrameOptionsOption) {
679
+ case void 0:
680
+ case true:
681
+ result.push(xFrameOptions_default());
682
+ break;
683
+ case false:
684
+ break;
685
+ default:
686
+ result.push(xFrameOptions_default(xFrameOptionsOption));
687
+ break;
688
+ }
689
+ if ("xPermittedCrossDomainPolicies" in options && "permittedCrossDomainPolicies" in options) {
690
+ throw new Error(
691
+ "X-Permitted-Cross-Domain-Policies option was specified twice. Remove the `permittedCrossDomainPolicies` option to fix this error."
692
+ );
693
+ }
694
+ const xPermittedCrossDomainPoliciesOption = options.xPermittedCrossDomainPolicies ?? options.permittedCrossDomainPolicies;
695
+ switch (xPermittedCrossDomainPoliciesOption) {
696
+ case void 0:
697
+ case true:
698
+ result.push(xPermittedCrossDomainPolicies_default());
699
+ break;
700
+ case false:
701
+ break;
702
+ default:
703
+ result.push(
704
+ xPermittedCrossDomainPolicies_default(xPermittedCrossDomainPoliciesOption)
705
+ );
706
+ break;
707
+ }
708
+ if ("xPoweredBy" in options && "hidePoweredBy" in options) {
709
+ throw new Error(
710
+ "X-Powered-By option was specified twice. Remove the `hidePoweredBy` option to fix this error."
711
+ );
712
+ }
713
+ const xPoweredByOption = options.xPoweredBy ?? options.hidePoweredBy;
714
+ const xPoweredBy = function xPoweredBy2() {
715
+ return async function xPoweredByMiddleware(ctx, next) {
716
+ ctx.res.delete("X-Powered-By");
717
+ await next();
718
+ };
719
+ };
720
+ switch (xPoweredByOption) {
721
+ case void 0:
722
+ case true:
723
+ result.push(xPoweredBy());
724
+ break;
725
+ case false:
726
+ break;
727
+ default:
728
+ result.push(xPoweredBy());
729
+ break;
730
+ }
731
+ if ("xXssProtection" in options && "xssFilter" in options) {
732
+ throw new Error(
733
+ "X-XSS-Protection option was specified twice. Remove the `xssFilter` option to fix this error."
734
+ );
735
+ }
736
+ const xXssProtectionOption = options.xXssProtection ?? options.xssFilter;
737
+ switch (xXssProtectionOption) {
738
+ case void 0:
739
+ case true:
740
+ result.push(xXssProtection_default());
741
+ break;
742
+ case false:
743
+ break;
744
+ default:
745
+ result.push(xXssProtection_default());
746
+ break;
747
+ }
748
+ if (options.permissionPolicy && typeof options.permissionPolicy === "object") {
749
+ result.push(permissionPolicy_default(options.permissionPolicy));
750
+ }
751
+ return result;
752
+ }
753
+ var secureHeaders = Object.assign(
754
+ function secureHeaders2(options = {}) {
755
+ const middlewareFunctions = getMiddlewareFunctionsFromOptions(options);
756
+ const secureHeadersHandler = (0, import_hoa.compose)(middlewareFunctions);
757
+ return async function secureHeadersMiddleware(ctx, next) {
758
+ await secureHeadersHandler(ctx, next);
759
+ };
760
+ },
761
+ {
762
+ contentSecurityPolicy: contentSecurityPolicy_default,
763
+ crossOriginEmbedderPolicy: crossOriginEmbedderPolicy_default,
764
+ crossOriginOpenerPolicy: crossOriginOpenerPolicy_default,
765
+ crossOriginResourcePolicy: crossOriginResourcePolicy_default,
766
+ originAgentCluster: originAgentCluster_default,
767
+ referrerPolicy: referrerPolicy_default,
768
+ strictTransportSecurity: strictTransportSecurity_default,
769
+ xContentTypeOptions: xContentTypeOptions_default,
770
+ xDnsPrefetchControl: xDnsPrefetchControl_default,
771
+ xDownloadOptions: xDownloadOptions_default,
772
+ xFrameOptions: xFrameOptions_default,
773
+ xPermittedCrossDomainPolicies: xPermittedCrossDomainPolicies_default,
774
+ xXssProtection: xXssProtection_default,
775
+ permissionPolicy: permissionPolicy_default,
776
+ // Legacy aliases
777
+ dnsPrefetchControl: xDnsPrefetchControl_default,
778
+ xssFilter: xXssProtection_default,
779
+ permittedCrossDomainPolicies: xPermittedCrossDomainPolicies_default,
780
+ ieNoOpen: xDownloadOptions_default,
781
+ noSniff: xContentTypeOptions_default,
782
+ frameguard: xFrameOptions_default,
783
+ hsts: strictTransportSecurity_default
784
+ }
785
+ );
786
+ var index_default = secureHeaders;
787
+ // Annotate the CommonJS export names for ESM import in node:
788
+ 0 && (module.exports = {
789
+ contentSecurityPolicy,
790
+ crossOriginEmbedderPolicy,
791
+ crossOriginOpenerPolicy,
792
+ crossOriginResourcePolicy,
793
+ dnsPrefetchControl,
794
+ frameguard,
795
+ hsts,
796
+ ieNoOpen,
797
+ noSniff,
798
+ originAgentCluster,
799
+ permissionPolicy,
800
+ permittedCrossDomainPolicies,
801
+ referrerPolicy,
802
+ secureHeaders,
803
+ strictTransportSecurity,
804
+ xContentTypeOptions,
805
+ xDnsPrefetchControl,
806
+ xDownloadOptions,
807
+ xFrameOptions,
808
+ xPermittedCrossDomainPolicies,
809
+ xXssProtection,
810
+ xssFilter
811
+ });