@middy/http-security-headers 7.3.0 → 7.3.2

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.
Files changed (2) hide show
  1. package/index.js +89 -21
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -4,29 +4,97 @@ import { normalizeHttpResponse, validateOptions } from "@middy/util";
4
4
 
5
5
  // Each policy option accepts either a config object or a boolean (true = use
6
6
  // defaults, false = disable). See middleware body for enable/disable logic.
7
- const objectOrBoolean = (v) =>
8
- typeof v === "boolean" ||
9
- (v !== null && typeof v === "object" && !Array.isArray(v));
7
+
8
+ const booleanOr = (objectSchema) => ({
9
+ oneOf: [{ type: "boolean" }, objectSchema],
10
+ });
11
+
12
+ const policyObject = (policyEnum) => ({
13
+ type: "object",
14
+ properties: {
15
+ policy: policyEnum
16
+ ? { type: "string", enum: policyEnum }
17
+ : { type: "string" },
18
+ },
19
+ additionalProperties: false,
20
+ });
21
+
22
+ const actionObject = {
23
+ type: "object",
24
+ properties: {
25
+ action: { type: "string" },
26
+ },
27
+ additionalProperties: false,
28
+ };
29
+
30
+ const referrerPolicyValues = [
31
+ "no-referrer",
32
+ "no-referrer-when-downgrade",
33
+ "origin",
34
+ "origin-when-cross-origin",
35
+ "same-origin",
36
+ "strict-origin",
37
+ "strict-origin-when-cross-origin",
38
+ "unsafe-url",
39
+ ];
40
+
41
+ const permittedCrossDomainPoliciesValues = [
42
+ "none",
43
+ "master-only",
44
+ "by-content-type",
45
+ "by-ftp-filename",
46
+ "all",
47
+ ];
10
48
 
11
49
  const optionSchema = {
12
- contentSecurityPolicy: objectOrBoolean,
13
- contentSecurityPolicyReportOnly: "boolean?",
14
- contentTypeOptions: objectOrBoolean,
15
- crossOriginEmbedderPolicy: objectOrBoolean,
16
- crossOriginOpenerPolicy: objectOrBoolean,
17
- crossOriginResourcePolicy: objectOrBoolean,
18
- dnsPrefetchControl: objectOrBoolean,
19
- downloadOptions: objectOrBoolean,
20
- frameOptions: objectOrBoolean,
21
- originAgentCluster: objectOrBoolean,
22
- permissionsPolicy: objectOrBoolean,
23
- permittedCrossDomainPolicies: objectOrBoolean,
24
- poweredBy: "boolean?",
25
- referrerPolicy: objectOrBoolean,
26
- reportingEndpoints: objectOrBoolean,
27
- reportTo: objectOrBoolean,
28
- strictTransportSecurity: objectOrBoolean,
29
- xssProtection: objectOrBoolean,
50
+ type: "object",
51
+ properties: {
52
+ // CSP directives are an open-ended string map; keep permissive.
53
+ contentSecurityPolicy: booleanOr({ type: "object" }),
54
+ contentSecurityPolicyReportOnly: { type: "boolean" },
55
+ contentTypeOptions: booleanOr(actionObject),
56
+ crossOriginEmbedderPolicy: booleanOr(policyObject()),
57
+ crossOriginOpenerPolicy: booleanOr(policyObject()),
58
+ crossOriginResourcePolicy: booleanOr(policyObject()),
59
+ dnsPrefetchControl: booleanOr({
60
+ type: "object",
61
+ properties: {
62
+ allow: { type: "boolean" },
63
+ },
64
+ additionalProperties: false,
65
+ }),
66
+ downloadOptions: booleanOr(actionObject),
67
+ frameOptions: booleanOr(actionObject),
68
+ originAgentCluster: booleanOr({
69
+ type: "object",
70
+ additionalProperties: false,
71
+ }),
72
+ // Permissions-Policy features are an open-ended string map.
73
+ permissionsPolicy: booleanOr({ type: "object" }),
74
+ permittedCrossDomainPolicies: booleanOr(
75
+ policyObject(permittedCrossDomainPoliciesValues),
76
+ ),
77
+ poweredBy: { type: "boolean" },
78
+ referrerPolicy: booleanOr(policyObject(referrerPolicyValues)),
79
+ // Reporting-Endpoints is an open-ended group->url map.
80
+ reportingEndpoints: booleanOr({ type: "object" }),
81
+ // Report-To groups are open-ended; maxAge/includeSubdomains are known.
82
+ reportTo: booleanOr({ type: "object" }),
83
+ strictTransportSecurity: booleanOr({
84
+ type: "object",
85
+ properties: {
86
+ maxAge: { type: "number", minimum: 0 },
87
+ includeSubDomains: { type: "boolean" },
88
+ preload: { type: "boolean" },
89
+ },
90
+ additionalProperties: false,
91
+ }),
92
+ xssProtection: booleanOr({
93
+ type: "object",
94
+ additionalProperties: false,
95
+ }),
96
+ },
97
+ additionalProperties: false,
30
98
  };
31
99
 
32
100
  export const httpSecurityHeadersValidateOptions = (options) =>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-security-headers",
3
- "version": "7.3.0",
3
+ "version": "7.3.2",
4
4
  "description": "Applies best practice security headers to responses. It's a simplified port of HelmetJS",
5
5
  "type": "module",
6
6
  "engines": {
@@ -65,10 +65,10 @@
65
65
  "url": "https://github.com/sponsors/willfarrell"
66
66
  },
67
67
  "dependencies": {
68
- "@middy/util": "7.3.0"
68
+ "@middy/util": "7.3.2"
69
69
  },
70
70
  "devDependencies": {
71
- "@middy/core": "7.3.0",
71
+ "@middy/core": "7.3.2",
72
72
  "@types/aws-lambda": "^8.0.0",
73
73
  "@types/node": "^22.0.0"
74
74
  }