|
1 | 1 | import { |
2 | | - type ContentSecurityPolicyOptions, |
3 | | - type CrossOriginEmbedderPolicyOptions, |
4 | | - type CrossOriginOpenerPolicyOptions, |
5 | | - type CrossOriginResourcePolicyOptions, |
6 | | - type ReferrerPolicyOptions, |
7 | | - type StrictTransportSecurityOptions, |
8 | | - type XDnsPrefetchControlOptions, |
9 | | - type XFrameOptionsOptions, |
10 | | - type XPermittedCrossDomainPoliciesOptions, |
11 | | - contentSecurityPolicy, |
12 | | - crossOriginEmbedderPolicy, |
13 | | - crossOriginOpenerPolicy, |
14 | | - crossOriginResourcePolicy, |
15 | | - originAgentCluster, |
16 | | - referrerPolicy, |
17 | | - strictTransportSecurity, |
18 | | - xContentTypeOptions, |
19 | | - xDnsPrefetchControl, |
20 | | - xDownloadOptions, |
21 | | - xFrameOptions, |
22 | | - xPermittedCrossDomainPolicies, |
23 | | - xXssProtection, |
24 | | -} from "./rules/index.js"; |
| 2 | + type ContentSecureOptions, |
| 3 | + contentSecurity, |
| 4 | +} from "./rules/content/index.js"; |
| 5 | +import { |
| 6 | + type GeneralSecureOptions, |
| 7 | + generalSecurity, |
| 8 | +} from "./rules/general/index.js"; |
| 9 | +import { |
| 10 | + type ResourceSharingSecureOptions, |
| 11 | + resourceSharing, |
| 12 | +} from "./rules/resourceSharing/index.js"; |
| 13 | + |
| 14 | +const headers = new Headers(); |
| 15 | +helmet(headers, { content: { contentSecurityPolicy: {} } }); |
| 16 | + |
| 17 | +helmet(headers, { resourceSharing: true }); |
25 | 18 |
|
26 | 19 | /** |
27 | | - * Sets sensible security headers onto a Headers instance. |
28 | | - * |
29 | | - * This utility function configures security headers based on the provided settings. |
| 20 | + * Applies security headers to a `Headers` instance with sensible defaults. |
| 21 | + * General security headers are always set, while content and resource-sharing headers can be opted in. |
30 | 22 | */ |
31 | | -export function helmet( |
32 | | - headers: Headers, |
33 | | - { options = {}, html = false, cors = false }: HelmetOptions = {}, |
34 | | -) { |
35 | | - switch (options.crossOriginResourcePolicy) { |
| 23 | +export function helmet(headers: Headers, options: HelmetOptions = {}) { |
| 24 | + switch (options.general) { |
36 | 25 | case undefined: |
37 | 26 | case true: |
38 | | - crossOriginResourcePolicy(headers, cors ? "cross-origin" : "same-origin"); |
| 27 | + generalSecurity(headers); |
39 | 28 | break; |
40 | 29 | case false: |
41 | 30 | break; |
42 | 31 | default: |
43 | | - crossOriginResourcePolicy(headers, options.crossOriginResourcePolicy); |
44 | | - } |
45 | | - |
46 | | - if (options.originAgentCluster ?? true) { |
47 | | - originAgentCluster(headers); |
| 32 | + generalSecurity(headers, options.general); |
48 | 33 | } |
49 | 34 |
|
50 | | - switch (options.referrerPolicy) { |
| 35 | + switch (options.content) { |
51 | 36 | case undefined: |
52 | | - case true: |
53 | | - referrerPolicy(headers); |
54 | | - break; |
55 | 37 | case false: |
56 | 38 | break; |
57 | | - default: |
58 | | - referrerPolicy(headers, options.referrerPolicy); |
59 | | - } |
60 | | - |
61 | | - switch (options.strictTransportSecurity) { |
62 | | - case undefined: |
63 | 39 | case true: |
64 | | - strictTransportSecurity(headers); |
65 | | - break; |
66 | | - case false: |
| 40 | + contentSecurity(headers); |
67 | 41 | break; |
68 | 42 | default: |
69 | | - strictTransportSecurity(headers, options.strictTransportSecurity); |
| 43 | + contentSecurity(headers, options.content); |
70 | 44 | } |
71 | 45 |
|
72 | | - if (options.xContentTypeOptions ?? true) { |
73 | | - xContentTypeOptions(headers); |
74 | | - } |
75 | | - |
76 | | - switch (options.xDnsPrefetchControl) { |
| 46 | + switch (options.resourceSharing) { |
77 | 47 | case undefined: |
78 | | - case true: |
79 | | - xDnsPrefetchControl(headers); |
80 | | - break; |
81 | 48 | case false: |
82 | 49 | break; |
83 | | - default: |
84 | | - xDnsPrefetchControl(headers, options.xDnsPrefetchControl); |
85 | | - } |
86 | | - |
87 | | - switch (options.xPermittedCrossDomainPolicies) { |
88 | | - case undefined: |
89 | 50 | case true: |
90 | | - xPermittedCrossDomainPolicies(headers); |
91 | | - break; |
92 | | - case false: |
| 51 | + resourceSharing(headers); |
93 | 52 | break; |
94 | 53 | default: |
95 | | - xPermittedCrossDomainPolicies( |
96 | | - headers, |
97 | | - options.xPermittedCrossDomainPolicies, |
98 | | - ); |
99 | | - } |
100 | | - |
101 | | - if (options.xXssProtection ?? true) { |
102 | | - xXssProtection(headers); |
103 | | - } |
104 | | - |
105 | | - if (html) { |
106 | | - switch (options.contentSecurityPolicy) { |
107 | | - case undefined: |
108 | | - case true: |
109 | | - contentSecurityPolicy(headers); |
110 | | - break; |
111 | | - case false: |
112 | | - break; |
113 | | - default: |
114 | | - contentSecurityPolicy(headers, options.contentSecurityPolicy); |
115 | | - } |
116 | | - |
117 | | - switch (options.crossOriginOpenerPolicy) { |
118 | | - case undefined: |
119 | | - case true: |
120 | | - crossOriginOpenerPolicy(headers); |
121 | | - break; |
122 | | - case false: |
123 | | - break; |
124 | | - default: |
125 | | - crossOriginOpenerPolicy(headers, options.crossOriginOpenerPolicy); |
126 | | - } |
127 | | - |
128 | | - switch (options.crossOriginEmbedderPolicy) { |
129 | | - case undefined: |
130 | | - case true: |
131 | | - crossOriginEmbedderPolicy(headers); |
132 | | - break; |
133 | | - case false: |
134 | | - break; |
135 | | - default: |
136 | | - crossOriginEmbedderPolicy(headers, options.crossOriginEmbedderPolicy); |
137 | | - } |
138 | | - |
139 | | - if (options.xDownloadOptions ?? true) { |
140 | | - xDownloadOptions(headers); |
141 | | - } |
142 | | - |
143 | | - switch (options.xFrameOptions) { |
144 | | - case undefined: |
145 | | - case true: |
146 | | - xFrameOptions(headers); |
147 | | - break; |
148 | | - case false: |
149 | | - break; |
150 | | - default: |
151 | | - xFrameOptions(headers, options.xFrameOptions); |
152 | | - } |
| 54 | + resourceSharing(headers, options.resourceSharing); |
153 | 55 | } |
154 | 56 | } |
155 | 57 |
|
156 | 58 | export type HelmetOptions = { |
157 | 59 | /** |
158 | | - * Options to enable, disable defaults, or customized security headers for your own need |
| 60 | + * Configures general security headers. |
| 61 | + * Enabled by default. |
159 | 62 | */ |
160 | | - options?: SecureOptions; |
| 63 | + general?: GeneralSecureOptions | boolean; |
161 | 64 | /** |
162 | | - * Whether the content type is html, this enables document specific security headers. |
163 | | - * @default {true} |
| 65 | + * Configures security headers relevant to content, typically for `text/html` responses. |
| 66 | + * Disabled by default. |
| 67 | + * |
| 68 | + * @see https://developer.mozilla.org/en-US/docs/Glossary/Browsing_context |
164 | 69 | */ |
165 | | - html?: boolean; |
| 70 | + content?: ContentSecureOptions | boolean; |
166 | 71 | /** |
167 | | - * Whether the content is shared cross-origin, this modifys defaults to support cross origin resource sharing. |
168 | | - * @default {false} |
| 72 | + * Configures security headers related to resource sharing (CORS). |
| 73 | + * Disabled by default. |
169 | 74 | */ |
170 | | - cors?: boolean; |
171 | | -}; |
172 | | - |
173 | | -export type SecureOptions = GeneralSecureOptions & HtmlSpecificSecureOptions; |
174 | | - |
175 | | -type GeneralSecureOptions = { |
176 | | - crossOriginResourcePolicy?: CrossOriginResourcePolicyOptions | boolean; |
177 | | - originAgentCluster?: boolean; |
178 | | - referrerPolicy?: ReferrerPolicyOptions | boolean; |
179 | | - strictTransportSecurity?: StrictTransportSecurityOptions | boolean; |
180 | | - xContentTypeOptions?: boolean; |
181 | | - xDnsPrefetchControl?: XDnsPrefetchControlOptions | boolean; |
182 | | - xPermittedCrossDomainPolicies?: |
183 | | - | XPermittedCrossDomainPoliciesOptions |
184 | | - | boolean; |
185 | | - xXssProtection?: boolean; |
186 | | -}; |
187 | | - |
188 | | -type HtmlSpecificSecureOptions = { |
189 | | - contentSecurityPolicy?: ContentSecurityPolicyOptions | boolean; |
190 | | - crossOriginOpenerPolicy?: CrossOriginOpenerPolicyOptions | boolean; |
191 | | - crossOriginEmbedderPolicy?: CrossOriginEmbedderPolicyOptions | boolean; |
192 | | - xDownloadOptions?: boolean; |
193 | | - xFrameOptions?: XFrameOptionsOptions | boolean; |
| 75 | + resourceSharing?: ResourceSharingSecureOptions | boolean; |
194 | 76 | }; |
0 commit comments