@softistx/alxia-secure-headers 0.0.0-stage → 0.6.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.
- package/LICENSE +21 -0
- package/README.md +231 -2
- package/dist/clear-site-data.d.ts +49 -0
- package/dist/clear-site-data.d.ts.map +1 -0
- package/dist/csp.d.ts +36 -0
- package/dist/csp.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +292 -0
- package/dist/index.js.map +14 -0
- package/dist/nonce.d.ts +34 -0
- package/dist/nonce.d.ts.map +1 -0
- package/dist/page.d.ts +58 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/secure-headers.d.ts +64 -0
- package/dist/secure-headers.d.ts.map +1 -0
- package/docs/README.md +11 -0
- package/docs/guide.md +749 -0
- package/docs/roadmap.md +77 -0
- package/docs/troubleshooting.md +959 -0
- package/package.json +51 -5
|
@@ -0,0 +1,959 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
Each entry is headed by the text you see: a TypeScript error, an exception,
|
|
4
|
+
a browser console message, or a header that is or is not on a response.
|
|
5
|
+
`secureHeaders`, `csp()`, `pageHeaders()` and `clearSiteData()` throw only
|
|
6
|
+
at startup, on options they cannot send; `cacheControl` is an option of
|
|
7
|
+
`secureHeaders`. What goes wrong otherwise is a type, a browser refusing what a header forbids,
|
|
8
|
+
or a header in the wrong place.
|
|
9
|
+
|
|
10
|
+
**Types**
|
|
11
|
+
|
|
12
|
+
- [`Type 'true' is not assignable to type 'Setting | undefined'`](#type-true-is-not-assignable-to-type-setting--undefined)
|
|
13
|
+
- [`No overload matches this call` … `is not assignable to type 'SecureHeadersOptions' with 'exactOptionalPropertyTypes: true'`](#no-overload-matches-this-call--is-not-assignable-to-type-secureheadersoptions-with-exactoptionalpropertytypes-true)
|
|
14
|
+
- [`No overload matches this call` … `Object literal may only specify known properties, and '…' does not exist in type 'SecureHeadersOptions & …'`](#no-overload-matches-this-call--object-literal-may-only-specify-known-properties-and--does-not-exist-in-type-secureheadersoptions--)
|
|
15
|
+
- [`Type 'undefined' is not assignable to type 'SecureHeadersOptions & { readonly nonce: true; }'`](#type-undefined-is-not-assignable-to-type-secureheadersoptions---readonly-nonce-true-)
|
|
16
|
+
- [`Property 'nonce' does not exist on type 'RouteBase<…>'`](#property-nonce-does-not-exist-on-type-routebase)
|
|
17
|
+
- [`No overload matches this call` … `Type 'boolean' is not assignable to type 'true'`](#no-overload-matches-this-call--type-boolean-is-not-assignable-to-type-true)
|
|
18
|
+
- [`Type 'string' is not assignable to type 'CspValue | undefined'`](#type-string-is-not-assignable-to-type-cspvalue--undefined)
|
|
19
|
+
- [`Object literal may only specify known properties, and 'contentSecurityPolicy' does not exist in type 'PageHeadersOptions'`](#object-literal-may-only-specify-known-properties-and-contentsecuritypolicy-does-not-exist-in-type-pageheadersoptions)
|
|
20
|
+
- [`Property 'https' is missing in type '{}' but required in type 'PageHeadersOptions'`](#property-https-is-missing-in-type--but-required-in-type-pageheadersoptions)
|
|
21
|
+
- [`Type '"cash"' is not assignable to type '"cookies" | "*" | "cache" | "storage" | "executionContexts" | "clientHints" | "prefetchCache" | "prerenderCache"'`](#type-cash-is-not-assignable-to-type-cookies----cache--storage--executioncontexts--clienthints--prefetchcache--prerendercache)
|
|
22
|
+
|
|
23
|
+
**Runtime**
|
|
24
|
+
|
|
25
|
+
- [`TypeError: secureHeaders: … is empty; give false to leave the … header out`](#typeerror-secureheaders--is-empty-give-false-to-leave-the--header-out)
|
|
26
|
+
- [`use(): argument 1 looks like a factory (<name>): call it, use(<name>())`](#use-argument-1-looks-like-a-factory-name-call-it-usename)
|
|
27
|
+
- [`TypeError: secureHeaders: nonce is on, but the content-security-policy has no script-src to add it to: …`](#typeerror-secureheaders-nonce-is-on-but-the-content-security-policy-has-no-script-src-to-add-it-to-)
|
|
28
|
+
- [`TypeError: secureHeaders: nonce is on, but contentSecurityPolicy is false: …`](#typeerror-secureheaders-nonce-is-on-but-contentsecuritypolicy-is-false-)
|
|
29
|
+
- [`TypeError: secureHeaders: contentSecurityPolicy names NONCE, but nonce is off: give nonce: true`](#typeerror-secureheaders-contentsecuritypolicy-names-nonce-but-nonce-is-off-give-nonce-true)
|
|
30
|
+
- [`TypeError: csp(): <name> has no source: write ["'none'"] to allow nothing, or false to leave it out`](#typeerror-csp-name-has-no-source-write-none-to-allow-nothing-or-false-to-leave-it-out)
|
|
31
|
+
- [`TypeError: csp(): "<name>" is not a directive name: write lowercase letters, digits and hyphens, starting with a letter, as in "script-src"`](#typeerror-csp-name-is-not-a-directive-name-write-lowercase-letters-digits-and-hyphens-starting-with-a-letter-as-in-script-src)
|
|
32
|
+
- [`TypeError: csp(): <name> has the source "<source>", which is empty or holds ";", "," or a space: a source is one token, so split a list into several sources, and write a comma inside a URL as %2C`](#typeerror-csp-name-has-the-source-source-which-is-empty-or-holds---or-a-space-a-source-is-one-token-so-split-a-list-into-several-sources-and-write-a-comma-inside-a-url-as-2c)
|
|
33
|
+
- [`TypeError: csp(): no directive is left: give at least one, such as { "default-src": ["'none'"] }`](#typeerror-csp-no-directive-is-left-give-at-least-one-such-as--default-src-none-)
|
|
34
|
+
- [`TypeError: clearSiteData(): no type is given: write at least one, such as ["cache"]`](#typeerror-clearsitedata-no-type-is-given-write-at-least-one-such-as-cache)
|
|
35
|
+
- [`TypeError: clearSiteData(): "<type>" is not a type: write one of "cache", "cookies", "storage", "executionContexts", "clientHints", "prefetchCache", "prerenderCache", "*"`](#typeerror-clearsitedata-type-is-not-a-type-write-one-of-cache-cookies-storage-executioncontexts-clienthints-prefetchcache-prerendercache-)
|
|
36
|
+
- [`TypeError: pageHeaders(): script-src has no NONCE: a page script runs only with the response's nonce, so write script-src with NONCE in directives, such as ["'self'", NONCE, "'strict-dynamic'"]`](#typeerror-pageheaders-script-src-has-no-nonce-a-page-script-runs-only-with-the-responses-nonce-so-write-script-src-with-nonce-in-directives-such-as-self-nonce-strict-dynamic)
|
|
37
|
+
- [`TypeError: pageHeaders(): script-src-elem has no NONCE: script elements follow it instead of script-src, so add NONCE to it, or give false to remove it`](#typeerror-pageheaders-script-src-elem-has-no-nonce-script-elements-follow-it-instead-of-script-src-so-add-nonce-to-it-or-give-false-to-remove-it)
|
|
38
|
+
- [`TypeError: pageHeaders(): <name> cannot be removed from a page policy: …`](#typeerror-pageheaders-name-cannot-be-removed-from-a-page-policy-)
|
|
39
|
+
- [`TypeError: csp(): <value> in <directive> is a keyword: write it quoted, '<value>'`](#typeerror-csp-value-in-directive-is-a-keyword-write-it-quoted-value)
|
|
40
|
+
- [`TypeError: clearSiteData(): methods is empty or holds an empty method: write at least one, such as ["POST"], or leave methods out`](#typeerror-clearsitedata-methods-is-empty-or-holds-an-empty-method-write-at-least-one-such-as-post-or-leave-methods-out)
|
|
41
|
+
|
|
42
|
+
**In the browser**
|
|
43
|
+
|
|
44
|
+
- [`Refused to load the … because it violates the following Content Security Policy directive: "default-src 'none'"`](#refused-to-load-the--because-it-violates-the-following-content-security-policy-directive-default-src-none)
|
|
45
|
+
- [`Refused to send form data to '…' because it violates the following Content Security Policy directive: "form-action 'none'"`](#refused-to-send-form-data-to--because-it-violates-the-following-content-security-policy-directive-form-action-none)
|
|
46
|
+
- [`Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-…'"`](#refused-to-execute-inline-script-because-it-violates-the-following-content-security-policy-directive-script-src-self-nonce-)
|
|
47
|
+
- [Inline style attributes stop applying once `style-src` has the nonce](#inline-style-attributes-stop-applying-once-style-src-has-the-nonce)
|
|
48
|
+
- [A third-party script or font is refused by a page policy](#a-third-party-script-or-font-is-refused-by-a-page-policy)
|
|
49
|
+
- [`Refused to frame '…' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'none'"`](#refused-to-frame--because-an-ancestor-violates-the-following-content-security-policy-directive-frame-ancestors-none)
|
|
50
|
+
- [`net::ERR_BLOCKED_BY_RESPONSE.NotSameOrigin`](#neterr_blocked_by_responsenotsameorigin)
|
|
51
|
+
- [`window.opener` is `null` in a sign-in popup](#windowopener-is-null-in-a-sign-in-popup)
|
|
52
|
+
- [A subdomain on plain HTTP no longer opens](#a-subdomain-on-plain-http-no-longer-opens)
|
|
53
|
+
|
|
54
|
+
**Headers**
|
|
55
|
+
|
|
56
|
+
- [An option has no effect on a response](#an-option-has-no-effect-on-a-response)
|
|
57
|
+
- [A CDN still caches a page, though `cacheControl` is set](#a-cdn-still-caches-a-page-though-cachecontrol-is-set)
|
|
58
|
+
- [`X-Powered-By` or `Server` is still sent](#x-powered-by-or-server-is-still-sent)
|
|
59
|
+
- [A response arrives without the headers](#a-response-arrives-without-the-headers)
|
|
60
|
+
- [A 404 or a route outside a group has no headers](#a-404-or-a-route-outside-a-group-has-no-headers)
|
|
61
|
+
- [A `try`/`catch` middleware's reply has no headers](#a-trycatch-middlewares-reply-has-no-headers)
|
|
62
|
+
- [A route has no headers, though the others do](#a-route-has-no-headers-though-the-others-do)
|
|
63
|
+
- [`Clear-Site-Data` is missing on a client-side sign-out](#clear-site-data-is-missing-on-a-client-side-sign-out)
|
|
64
|
+
|
|
65
|
+
## Types
|
|
66
|
+
|
|
67
|
+
### `Type 'true' is not assignable to type 'Setting | undefined'`
|
|
68
|
+
|
|
69
|
+
**When:** a header option is given `true`, an object or an array.
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
error TS2769: No overload matches this call.
|
|
73
|
+
Overload 1 of 2, '(options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware', gave the following error.
|
|
74
|
+
Type 'true' is not assignable to type 'Setting'.
|
|
75
|
+
Overload 2 of 2, '(options?: (SecureHeadersOptions & { readonly nonce?: false; }) | undefined): SecureHeaders', gave the following error.
|
|
76
|
+
Type 'true' is not assignable to type 'Setting | undefined'.
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
An object, `{ defaultSrc: [...] }`, reads the same with its own type in
|
|
80
|
+
place of `'true'`.
|
|
81
|
+
|
|
82
|
+
`hidePoweredBy` given anything but a boolean is the same error, against
|
|
83
|
+
`'boolean | undefined'`.
|
|
84
|
+
|
|
85
|
+
**Why:** a header option is the header's value as a string, or `false` to
|
|
86
|
+
leave it out (`Setting`, exported, is `string | false`). There is no
|
|
87
|
+
`true` — leaving the key out is the default. The option takes a string,
|
|
88
|
+
and `csp()` builds that string from directives
|
|
89
|
+
([guide](guide.md#writing-the-policy-as-directives)).
|
|
90
|
+
|
|
91
|
+
**Fix:** write the header as it goes out, or leave the key out:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
app.use(
|
|
95
|
+
secureHeaders({
|
|
96
|
+
contentSecurityPolicy: "default-src 'self'; img-src 'self' data:",
|
|
97
|
+
// xFrameOptions: true → leave it out for 'DENY'
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### `No overload matches this call` … `is not assignable to type 'SecureHeadersOptions' with 'exactOptionalPropertyTypes: true'`
|
|
103
|
+
|
|
104
|
+
**When:** an option is set to `undefined`, often from a condition, with
|
|
105
|
+
`exactOptionalPropertyTypes` on.
|
|
106
|
+
|
|
107
|
+
```text
|
|
108
|
+
error TS2769: No overload matches this call.
|
|
109
|
+
Overload 1 of 2, '(options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware', gave the following error.
|
|
110
|
+
Argument of type '{ xFrameOptions: undefined; }' is not assignable to parameter of type 'SecureHeadersOptions & { readonly nonce: true; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
|
|
111
|
+
Type '{ xFrameOptions: undefined; }' is not assignable to type 'SecureHeadersOptions' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
|
|
112
|
+
Overload 2 of 2, '(options?: (SecureHeadersOptions & { readonly nonce?: false; }) | undefined): SecureHeaders', gave the following error.
|
|
113
|
+
Argument of type '{ xFrameOptions: undefined; }' is not assignable to parameter of type 'SecureHeadersOptions & { readonly nonce?: false; }' with 'exactOptionalPropertyTypes: true'. …
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Why:** the options are optional, not `undefined`-able. Under that flag a
|
|
117
|
+
key present with `undefined` is not a key left out.
|
|
118
|
+
|
|
119
|
+
**Fix:** spread the key in only when it has a value:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const production = Bun.env.NODE_ENV === 'production';
|
|
123
|
+
|
|
124
|
+
app.use(secureHeaders({ ...(production ? {} : { strictTransportSecurity: false }) }));
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### `No overload matches this call` … `Object literal may only specify known properties, and '…' does not exist in type 'SecureHeadersOptions & …'`
|
|
128
|
+
|
|
129
|
+
**When:** an option names a header the plugin does not send, or uses
|
|
130
|
+
another library's name for one.
|
|
131
|
+
|
|
132
|
+
```text
|
|
133
|
+
error TS2769: No overload matches this call.
|
|
134
|
+
Overload 1 of 2, '(options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware', gave the following error.
|
|
135
|
+
Object literal may only specify known properties, and 'xPoweredBy' does not exist in type 'SecureHeadersOptions & { readonly nonce: true; }'.
|
|
136
|
+
Overload 2 of 2, '(options?: (SecureHeadersOptions & { readonly nonce?: false; }) | undefined): SecureHeaders', gave the following error.
|
|
137
|
+
Object literal may only specify known properties, and 'xPoweredBy' does not exist in type 'SecureHeadersOptions & { readonly nonce?: false; }'.
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Why:** the options are the twelve headers in the
|
|
141
|
+
[guide](guide.md#the-options), plus `hidePoweredBy` and `nonce`. `X-Powered-By` and
|
|
142
|
+
`Server` are governed by `hidePoweredBy`; anything else —
|
|
143
|
+
`Content-Security-Policy-Report-Only`, `X-XSS-Protection` — is not sent by
|
|
144
|
+
the middleware.
|
|
145
|
+
|
|
146
|
+
**Fix:** `hidePoweredBy` for those two; a middleware of your own for another
|
|
147
|
+
header:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { alxia, defineMiddleware, settle, withHeaders } from '@softistx/alxia';
|
|
151
|
+
import { secureHeaders } from '@softistx/alxia-secure-headers';
|
|
152
|
+
|
|
153
|
+
const reportOnly = defineMiddleware(async (ctx, next) =>
|
|
154
|
+
withHeaders(await settle(ctx, next()), (headers) =>
|
|
155
|
+
headers.set('content-security-policy-report-only', "default-src 'self'"),
|
|
156
|
+
),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const app = alxia()
|
|
160
|
+
.use(secureHeaders({ hidePoweredBy: true }))
|
|
161
|
+
.use(reportOnly);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### `Type 'undefined' is not assignable to type 'SecureHeadersOptions & { readonly nonce: true; }'`
|
|
165
|
+
|
|
166
|
+
**When:** `secureHeaders` is given to `app.use` without being called.
|
|
167
|
+
|
|
168
|
+
```text
|
|
169
|
+
error TS2345: Argument of type '{ (options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware; (options?: (SecureHeadersOptions & { ...; }) | undefined): SecureHeaders; }' is not assignable to parameter of type '{ (options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware; (options?: (SecureHeadersOptions & { ...; }) | undefined): SecureHeaders; } & Step<...>'.
|
|
170
|
+
Type '{ (options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware; (options?: (SecureHeadersOptions & { ...; }) | undefined): SecureHeaders; }' is not assignable to type 'Step<BaseContext & Empty, (SecureHeadersOptions & { readonly nonce?: false; }) | undefined, MiddlewareReturn>'.
|
|
171
|
+
Types of parameters 'options' and 'ctx' are incompatible.
|
|
172
|
+
Type '(SecureHeadersOptions & { readonly nonce?: false; }) | undefined' is not assignable to type 'SecureHeadersOptions & { readonly nonce: true; }'.
|
|
173
|
+
Type 'undefined' is not assignable to type 'SecureHeadersOptions & { readonly nonce: true; }'.
|
|
174
|
+
Type 'undefined' is not assignable to type 'SecureHeadersOptions'.
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**Why:** `secureHeaders` makes the middleware from its options; the
|
|
178
|
+
middleware is what it returns. Given to `use` as it is, it is read as a
|
|
179
|
+
middleware whose context is its options, and TypeScript says those do not
|
|
180
|
+
fit its first overload, the nonce one.
|
|
181
|
+
|
|
182
|
+
**Fix:** call it, with or without options:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
app.use(secureHeaders());
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### `Property 'nonce' does not exist on type 'RouteBase<…>'`
|
|
189
|
+
|
|
190
|
+
**When:** a route or a middleware reads `nonce` from its context.
|
|
191
|
+
|
|
192
|
+
```text
|
|
193
|
+
error TS2339: Property 'nonce' does not exist on type 'RouteBase<RouteApp<Method, Empty, "">, "/">'.
|
|
194
|
+
error TS2339: Property 'nonce' does not exist on type 'BaseContext & Empty'.
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The second is a `derive` or a middleware.
|
|
198
|
+
|
|
199
|
+
**Why:** only `secureHeaders({ nonce: true })` adds it, and only to the
|
|
200
|
+
routes declared after its `app.use`. Without `nonce: true`, or on a route
|
|
201
|
+
declared before it, there is none.
|
|
202
|
+
|
|
203
|
+
**Fix:** turn it on, and declare the routes that read it after it:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
alxia()
|
|
207
|
+
.use(secureHeaders({ nonce: true, contentSecurityPolicy: "script-src 'self'" }))
|
|
208
|
+
.get('/', ({ nonce, reply }) => reply(200, nonce));
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### `No overload matches this call` … `Type 'boolean' is not assignable to type 'true'`
|
|
212
|
+
|
|
213
|
+
**When:** `nonce` is given a `boolean` that is not a literal: a flag from
|
|
214
|
+
the environment, say.
|
|
215
|
+
|
|
216
|
+
```text
|
|
217
|
+
error TS2769: No overload matches this call.
|
|
218
|
+
Overload 1 of 2, '(options: SecureHeadersOptions & { readonly nonce: true; }): NonceMiddleware', gave the following error.
|
|
219
|
+
Type 'boolean' is not assignable to type 'true'.
|
|
220
|
+
Overload 2 of 2, '(options?: (SecureHeadersOptions & { readonly nonce?: false; }) | undefined): SecureHeaders', gave the following error.
|
|
221
|
+
Type 'boolean' is not assignable to type 'false'.
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Why:** `nonce: true` changes the middleware's type, since it adds `nonce`
|
|
225
|
+
to the context. A `boolean` leaves TypeScript unable to say whether the
|
|
226
|
+
routes read one.
|
|
227
|
+
|
|
228
|
+
**Fix:** pick the call by the flag, so each has a literal:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const policy = "default-src 'self'; script-src 'self'";
|
|
232
|
+
const app = useNonce
|
|
233
|
+
? alxia().use(secureHeaders({ nonce: true, contentSecurityPolicy: policy }))
|
|
234
|
+
: alxia().use(secureHeaders({ contentSecurityPolicy: policy }));
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Or keep it on everywhere: the nonce costs one random draw per request.
|
|
238
|
+
|
|
239
|
+
### `Type 'string' is not assignable to type 'CspValue | undefined'`
|
|
240
|
+
|
|
241
|
+
**When:** a directive of `csp()`, or of `pageHeaders({ directives })`, is given a bare string instead of a list.
|
|
242
|
+
|
|
243
|
+
```text
|
|
244
|
+
error TS2322: Type 'string' is not assignable to type 'CspValue | undefined'.
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Why:** a directive's value is a list of sources (`readonly string[]`), `true` for a bare directive, or `false` to leave it out. One string would be one source, and a space inside it is refused at startup.
|
|
248
|
+
|
|
249
|
+
**Fix:** write a list, even for one source:
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
csp({ 'script-src': ["'self'"] });
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### `Object literal may only specify known properties, and 'contentSecurityPolicy' does not exist in type 'PageHeadersOptions'`
|
|
256
|
+
|
|
257
|
+
**When:** `pageHeaders()` is given `contentSecurityPolicy`. `'nonce'` gives the same message, with the name changed.
|
|
258
|
+
|
|
259
|
+
```text
|
|
260
|
+
error TS2353: Object literal may only specify known properties, and 'contentSecurityPolicy' does not exist in type 'PageHeadersOptions'.
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**Why:** a page policy is written through `directives`, merged over `PAGE_DIRECTIVES`, and always carries a nonce, so neither `contentSecurityPolicy` nor `nonce` is an option of `pageHeaders()`.
|
|
264
|
+
|
|
265
|
+
**Fix:** use `directives`, and drop `nonce`:
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
pageHeaders({ https: true, directives: { 'img-src': ["'self'", 'https://img.example'] } });
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
For a whole policy of your own, use `secureHeaders({ nonce: true, contentSecurityPolicy: csp(…) })`.
|
|
272
|
+
|
|
273
|
+
### `Property 'https' is missing in type '{}' but required in type 'PageHeadersOptions'`
|
|
274
|
+
|
|
275
|
+
**When:** `pageHeaders({})`, or an options object without `https`.
|
|
276
|
+
|
|
277
|
+
```text
|
|
278
|
+
error TS2345: Argument of type '{}' is not assignable to parameter of type 'PageHeadersOptions'.
|
|
279
|
+
Property 'https' is missing in type '{}' but required in type 'PageHeadersOptions'.
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
**Why:** `https` switches HSTS and `upgrade-insecure-requests`, and the package never reads the environment to guess it.
|
|
283
|
+
|
|
284
|
+
**Fix:** pass your own flag:
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
pageHeaders({ https: import.meta.env.PROD });
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### `Type '"cash"' is not assignable to type '"cookies" | "*" | "cache" | "storage" | "executionContexts" | "clientHints" | "prefetchCache" | "prerenderCache"'`
|
|
291
|
+
|
|
292
|
+
**When:** `clearSiteData(['cash'])`, a type the header does not know, or a type of `ClearSiteDataType` misspelled.
|
|
293
|
+
|
|
294
|
+
```text
|
|
295
|
+
error TS2322: Type '"cash"' is not assignable to type '"cookies" | "*" | "cache" | "storage" | "executionContexts" | "clientHints" | "prefetchCache" | "prerenderCache"'.
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
The union is `ClearSiteDataType`, written out.
|
|
299
|
+
|
|
300
|
+
**Why:** the browser ignores a type it does not know, so the cache would stay as it was without a word.
|
|
301
|
+
|
|
302
|
+
**Fix:** use one of the listed types, in a list:
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
clearSiteData(['cache']);
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Runtime
|
|
309
|
+
|
|
310
|
+
### `TypeError: secureHeaders: … is empty; give false to leave the … header out`
|
|
311
|
+
|
|
312
|
+
**When:** `secureHeaders()` is called with a header option set to `''`, or
|
|
313
|
+
to spaces only, usually to turn a header off. It throws at once, so the app
|
|
314
|
+
fails at startup:
|
|
315
|
+
|
|
316
|
+
```text
|
|
317
|
+
TypeError: secureHeaders: xFrameOptions is empty; give false to leave the x-frame-options header out
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
**Why:** a header with an empty value says nothing a browser can act on.
|
|
321
|
+
Only `false` leaves a header out.
|
|
322
|
+
|
|
323
|
+
**Fix:**
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
app.use(secureHeaders({ xFrameOptions: false }));
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### `use(): argument 1 looks like a factory (<name>): call it, use(<name>())`
|
|
330
|
+
|
|
331
|
+
**When:** `alxia().use(secureHeaders)`, `use(pageHeaders)` or
|
|
332
|
+
`use(clearSiteData)`, a factory given uncalled, in JavaScript or past a
|
|
333
|
+
cast. `<name>` is the factory's. It throws where the app is declared, since
|
|
334
|
+
alxia 0.5; before, each request was answered with a 500 and the log said
|
|
335
|
+
`GET /: a middleware (secureHeaders) returned function: …`. TypeScript
|
|
336
|
+
refuses it first, with `"this looks like a factory given uncalled: …"` in
|
|
337
|
+
the error.
|
|
338
|
+
|
|
339
|
+
**Why:** `use` takes a middleware, a `(ctx, next)` function; `secureHeaders`,
|
|
340
|
+
`pageHeaders` and `clearSiteData` make one, and are marked as factories, so
|
|
341
|
+
`use`, a route and `plugin` refuse them.
|
|
342
|
+
|
|
343
|
+
**Fix:** call it:
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
const app = alxia()
|
|
347
|
+
.use(secureHeaders())
|
|
348
|
+
.get('/', ({ reply }) => reply(200, 'ok'));
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### `TypeError: secureHeaders: nonce is on, but the content-security-policy has no script-src to add it to: …`
|
|
352
|
+
|
|
353
|
+
**When:** `nonce: true`, with a policy that has neither a `script-src` (or
|
|
354
|
+
`script-src-elem`) nor `NONCE` — the default policy among them. It throws
|
|
355
|
+
at startup:
|
|
356
|
+
|
|
357
|
+
```text
|
|
358
|
+
TypeError: secureHeaders: nonce is on, but the content-security-policy has no script-src to add it to: write one, or place NONCE where the nonce goes (the policy is "default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'")
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
**Why:** the nonce goes in a directive. Without `script-src`, scripts fall
|
|
362
|
+
back on `default-src`, and adding a `script-src` of the middleware's own would
|
|
363
|
+
change what else they may load.
|
|
364
|
+
|
|
365
|
+
**Fix:** write the `script-src` you want, or say where the nonce goes:
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
import { NONCE, secureHeaders } from '@softistx/alxia-secure-headers';
|
|
369
|
+
|
|
370
|
+
app.use(secureHeaders({ nonce: true, contentSecurityPolicy: "default-src 'self'; script-src 'self'" }));
|
|
371
|
+
app.use(secureHeaders({ nonce: true, contentSecurityPolicy: `default-src 'self' ${NONCE}` }));
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### `TypeError: secureHeaders: nonce is on, but contentSecurityPolicy is false: …`
|
|
375
|
+
|
|
376
|
+
**When:** `nonce: true` and `contentSecurityPolicy: false` together.
|
|
377
|
+
|
|
378
|
+
```text
|
|
379
|
+
TypeError: secureHeaders: nonce is on, but contentSecurityPolicy is false: a nonce is only read through the content-security-policy header
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
**Why:** a browser only honours a nonce the response's policy names; with
|
|
383
|
+
no policy there is nothing for it to allow.
|
|
384
|
+
|
|
385
|
+
**Fix:** give a policy, or drop `nonce`. A route that writes its own policy
|
|
386
|
+
with `ctx.nonce` still needs the middleware's: give the app one, and let the
|
|
387
|
+
route's replace it ([guide](guide.md#who-reads-it)).
|
|
388
|
+
|
|
389
|
+
### `TypeError: secureHeaders: contentSecurityPolicy names NONCE, but nonce is off: give nonce: true`
|
|
390
|
+
|
|
391
|
+
**When:** the policy holds `NONCE`, and `nonce` is left out or `false`.
|
|
392
|
+
It throws at startup:
|
|
393
|
+
|
|
394
|
+
```text
|
|
395
|
+
TypeError: secureHeaders: contentSecurityPolicy names NONCE, but nonce is off: give nonce: true
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
**Why:** without `nonce: true` the placeholder would go out as it is,
|
|
399
|
+
`'nonce-{alxia}'`, which allows nothing and reads as a mistake in a
|
|
400
|
+
browser's console.
|
|
401
|
+
|
|
402
|
+
**Fix:**
|
|
403
|
+
|
|
404
|
+
```ts
|
|
405
|
+
import { NONCE, secureHeaders } from '@softistx/alxia-secure-headers';
|
|
406
|
+
|
|
407
|
+
app.use(secureHeaders({ nonce: true, contentSecurityPolicy: `script-src 'self' ${NONCE}` }));
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### `TypeError: csp(): <name> has no source: write ["'none'"] to allow nothing, or false to leave it out`
|
|
411
|
+
|
|
412
|
+
**When:** `csp()` is given an empty array for a directive. It throws at once, so the app fails at startup:
|
|
413
|
+
|
|
414
|
+
```text
|
|
415
|
+
TypeError: csp(): <name> has no source: write ["'none'"] to allow nothing, or false to leave it out
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
**Why:** an empty list says nothing a browser can use, and omitting the directive is not the same as forbidding everything.
|
|
419
|
+
|
|
420
|
+
**Fix:**
|
|
421
|
+
|
|
422
|
+
```ts
|
|
423
|
+
csp({ 'img-src': ["'none'"] }); // allow nothing
|
|
424
|
+
csp({ 'img-src': false }); // leave the directive out
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### `TypeError: csp(): "<name>" is not a directive name: write lowercase letters, digits and hyphens, starting with a letter, as in "script-src"`
|
|
428
|
+
|
|
429
|
+
**When:** a key of the object is not lowercase letters, digits and hyphens starting with a letter, such as `scriptSrc`, `Script-Src` or `-x`. It throws at once, so the app fails at startup:
|
|
430
|
+
|
|
431
|
+
```text
|
|
432
|
+
TypeError: csp(): "<name>" is not a directive name: write lowercase letters, digits and hyphens, starting with a letter, as in "script-src"
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
**Why:** directive names are written as in the header; a camel-cased key would name a directive no browser knows, so it is refused, not sent.
|
|
436
|
+
|
|
437
|
+
**Fix:**
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
csp({ 'script-src': ["'self'"] });
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
### `TypeError: csp(): <name> has the source "<source>", which is empty or holds ";", "," or a space: a source is one token, so split a list into several sources, and write a comma inside a URL as %2C`
|
|
444
|
+
|
|
445
|
+
**When:** a source is an empty string, or holds `;`, `,` or whitespace, usually a whole list or a second directive in one string. It throws at once, so the app fails at startup:
|
|
446
|
+
|
|
447
|
+
```text
|
|
448
|
+
TypeError: csp(): <name> has the source "<source>", which is empty or holds ";", "," or a space: a source is one token, so split a list into several sources, and write a comma inside a URL as %2C
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
**Why:** a `;` would end the directive and start another, so a value could smuggle in a directive; a space would pass two sources as one; a `,` would split the header in two for a proxy. A comma inside a URL is legitimate, so percent-encode it as `%2C`.
|
|
452
|
+
|
|
453
|
+
**Fix:**
|
|
454
|
+
|
|
455
|
+
```ts
|
|
456
|
+
csp({ 'img-src': ["'self'", 'data:'] }); // not ["'self' data:"]
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### `TypeError: csp(): no directive is left: give at least one, such as { "default-src": ["'none'"] }`
|
|
460
|
+
|
|
461
|
+
**When:** every directive is `false` or `undefined`, or the object is empty. It throws at once, so the app fails at startup:
|
|
462
|
+
|
|
463
|
+
```text
|
|
464
|
+
TypeError: csp(): no directive is left: give at least one, such as { "default-src": ["'none'"] }
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
**Why:** an empty policy is no policy; it would be sent as an empty header.
|
|
468
|
+
|
|
469
|
+
**Fix:**
|
|
470
|
+
|
|
471
|
+
```ts
|
|
472
|
+
csp({ 'default-src': ["'none'"] });
|
|
473
|
+
// or leave the header out: secureHeaders({ contentSecurityPolicy: false })
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### `TypeError: clearSiteData(): no type is given: write at least one, such as ["cache"]`
|
|
477
|
+
|
|
478
|
+
**When:** `clearSiteData([])`, called with no type, throws where the app is declared.
|
|
479
|
+
|
|
480
|
+
**Why:** a `Clear-Site-Data` header with no value tells the browser nothing.
|
|
481
|
+
|
|
482
|
+
**Fix:** name what to clear:
|
|
483
|
+
|
|
484
|
+
```ts
|
|
485
|
+
app.use('/sign-out', clearSiteData(['cache']));
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
### `TypeError: clearSiteData(): "<type>" is not a type: write one of "cache", "cookies", "storage", "executionContexts", "clientHints", "prefetchCache", "prerenderCache", "*"`
|
|
489
|
+
|
|
490
|
+
**When:** a type is misspelled (`'cash'`) or is not one the header knows, past a cast or in JavaScript. TypeScript refuses it first.
|
|
491
|
+
|
|
492
|
+
**Why:** the browser ignores a type it does not know, so the cache would stay as it was without a word.
|
|
493
|
+
|
|
494
|
+
**Fix:** use one of the types the message lists:
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
app.use('/sign-out', clearSiteData(['cache', 'cookies']));
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
### `TypeError: pageHeaders(): script-src has no NONCE: a page script runs only with the response's nonce, so write script-src with NONCE in directives, such as ["'self'", NONCE, "'strict-dynamic'"]`
|
|
501
|
+
|
|
502
|
+
**When:** `pageHeaders({ directives: { 'script-src': [...] } })` gives a `script-src` without `NONCE`, or `false`, and the app is declared.
|
|
503
|
+
|
|
504
|
+
**Why:** a page script runs only with the response's nonce. A `script-src` that lacks `NONCE` would leave every script of the page refused, and the page would not hydrate.
|
|
505
|
+
|
|
506
|
+
**Fix:** keep `NONCE` in the list you give, with `'strict-dynamic'` after it:
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
import { NONCE, pageHeaders } from '@softistx/alxia-secure-headers';
|
|
510
|
+
|
|
511
|
+
pageHeaders({
|
|
512
|
+
https: true,
|
|
513
|
+
directives: { 'script-src': ["'self'", NONCE, "'strict-dynamic'"] },
|
|
514
|
+
});
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
### `TypeError: pageHeaders(): script-src-elem has no NONCE: script elements follow it instead of script-src, so add NONCE to it, or give false to remove it`
|
|
518
|
+
|
|
519
|
+
**When:** `pageHeaders({ directives: { 'script-src-elem': [...] } })` gives a `script-src-elem` list without `NONCE`.
|
|
520
|
+
|
|
521
|
+
**Why:** browsers that support CSP level 3 check `<script>` elements against `script-src-elem` when it is present, and ignore `script-src` for them. A list without `NONCE` would leave every script of the page refused, and the page would not hydrate.
|
|
522
|
+
|
|
523
|
+
**Fix:** add `NONCE` to the list, or give `false` to remove the directive so `script-src` governs again:
|
|
524
|
+
|
|
525
|
+
```ts
|
|
526
|
+
import { NONCE, pageHeaders } from '@softistx/alxia-secure-headers';
|
|
527
|
+
|
|
528
|
+
pageHeaders({
|
|
529
|
+
https: true,
|
|
530
|
+
directives: { 'script-src-elem': ["'self'", NONCE, "'strict-dynamic'"] },
|
|
531
|
+
});
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
### `TypeError: pageHeaders(): <name> cannot be removed from a page policy: …`
|
|
535
|
+
|
|
536
|
+
**When:** `pageHeaders({ directives: { 'default-src': false } })`, or `false` or `true` for `base-uri` or `object-src`. The app fails where it is declared.
|
|
537
|
+
|
|
538
|
+
```text
|
|
539
|
+
TypeError: pageHeaders(): base-uri cannot be removed from a page policy: it has no fallback, so an injected <base> would redirect nonced relative scripts, so give it a list of sources
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
**Why:** `default-src` covers frame, media and child sources, and `base-uri` has no fallback, so without it an injected `<base>` would redirect the nonced relative scripts. `object-src` blocks plugin content.
|
|
543
|
+
|
|
544
|
+
**Fix:** give the directive a list, or leave it out of `directives` to keep the preset's:
|
|
545
|
+
|
|
546
|
+
```ts
|
|
547
|
+
pageHeaders({ https: true, directives: { 'base-uri': ["'self'"] } });
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### `TypeError: csp(): <value> in <directive> is a keyword: write it quoted, '<value>'`
|
|
551
|
+
|
|
552
|
+
**When:** a source of `csp()` is `self`, `none`, `unsafe-inline`, `unsafe-eval`, `unsafe-hashes`, `strict-dynamic`, `wasm-unsafe-eval`, `report-sample` or `inline-speculation-rules`, or starts `nonce-`, `sha256-`, `sha384-` or `sha512-`, written without its single quotes (in any case). The same goes for `pageHeaders({ directives })`.
|
|
553
|
+
|
|
554
|
+
```text
|
|
555
|
+
TypeError: csp(): self in script-src is a keyword: write it quoted, 'self'
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
**Why:** a browser reads `self` as a host called `self`, not as this site, and ignores the keyword it meant. The policy would be looser or stricter than written, without a word.
|
|
559
|
+
|
|
560
|
+
**Fix:** quote it inside the string:
|
|
561
|
+
|
|
562
|
+
```ts
|
|
563
|
+
csp({ 'script-src': ["'self'", NONCE] });
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
### `TypeError: clearSiteData(): methods is empty or holds an empty method: write at least one, such as ["POST"], or leave methods out`
|
|
567
|
+
|
|
568
|
+
**When:** `clearSiteData(['cache'], { methods: [] })`, or a `methods` list that holds `''`. It throws where the app is declared.
|
|
569
|
+
|
|
570
|
+
**Why:** with no method, the middleware would never send the header, and the sign-out would leave the cache as it was.
|
|
571
|
+
|
|
572
|
+
**Fix:** name the methods, or leave `methods` out for `['POST']`:
|
|
573
|
+
|
|
574
|
+
```ts
|
|
575
|
+
clearSiteData(['cache'], { methods: ['POST', 'DELETE'] });
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
## In the browser
|
|
579
|
+
|
|
580
|
+
These messages are Chrome's wording; Firefox and Safari report the same
|
|
581
|
+
refusal in their own words.
|
|
582
|
+
|
|
583
|
+
### `Refused to load the … because it violates the following Content Security Policy directive: "default-src 'none'"`
|
|
584
|
+
|
|
585
|
+
**When:** the app serves an HTML page, and the page's styles, scripts,
|
|
586
|
+
images or fonts do not load. Chrome adds `Note that 'style-src-elem' was
|
|
587
|
+
not explicitly set, so 'default-src' is used as a fallback.` or similar.
|
|
588
|
+
Inline code reads `Refused to execute inline script …` or
|
|
589
|
+
`Refused to apply inline style …`.
|
|
590
|
+
|
|
591
|
+
**Why:** the default `Content-Security-Policy` is for an API:
|
|
592
|
+
`default-src 'none'` lets a response load nothing.
|
|
593
|
+
|
|
594
|
+
**Fix:** give the pages a policy on their reply, and keep the strict one for
|
|
595
|
+
the API:
|
|
596
|
+
|
|
597
|
+
```ts
|
|
598
|
+
app.get('/', ({ reply }) =>
|
|
599
|
+
reply(200, html, {
|
|
600
|
+
headers: {
|
|
601
|
+
'content-type': 'text/html; charset=utf-8',
|
|
602
|
+
'content-security-policy': "default-src 'self'; img-src 'self' data:; frame-ancestors 'none'",
|
|
603
|
+
},
|
|
604
|
+
}),
|
|
605
|
+
);
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
or set `contentSecurityPolicy` for the whole app. See the
|
|
609
|
+
[guide](guide.md#an-app-that-serves-html-too).
|
|
610
|
+
|
|
611
|
+
### `Refused to send form data to '…' because it violates the following Content Security Policy directive: "form-action 'none'"`
|
|
612
|
+
|
|
613
|
+
**When:** a `<form>` on a page the app serves is submitted, and nothing
|
|
614
|
+
happens.
|
|
615
|
+
|
|
616
|
+
**Why:** the default policy has `form-action 'none'`: the page may post no
|
|
617
|
+
form, even to itself.
|
|
618
|
+
|
|
619
|
+
**Fix:** allow your origin in that page's policy:
|
|
620
|
+
|
|
621
|
+
```ts
|
|
622
|
+
const PAGE_POLICY = "default-src 'self'; form-action 'self'; base-uri 'self'; frame-ancestors 'none'";
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
### `Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-…'"`
|
|
626
|
+
|
|
627
|
+
**When:** `nonce: true` is on, and a page's inline script does not run.
|
|
628
|
+
Chrome adds `Either the 'unsafe-inline' keyword, a hash ('sha256-…'), or a
|
|
629
|
+
nonce ('nonce-...') is required to enable inline execution.`
|
|
630
|
+
|
|
631
|
+
**Why:** the script carries no nonce, or another request's: one cached with
|
|
632
|
+
the page, or written into the HTML at build time. Each response allows only
|
|
633
|
+
its own.
|
|
634
|
+
|
|
635
|
+
**Fix:** write the request's `ctx.nonce` on each `<script>`, and never
|
|
636
|
+
cache a page whose HTML holds one:
|
|
637
|
+
|
|
638
|
+
```ts
|
|
639
|
+
.get('/', ({ nonce, reply }) =>
|
|
640
|
+
reply(200, `<script nonce="${nonce}">…</script>`, {
|
|
641
|
+
headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' },
|
|
642
|
+
}),
|
|
643
|
+
)
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
With React Router, `nonceOf(loadContext)` in `entry.server.tsx` does it for
|
|
647
|
+
every script it renders; see
|
|
648
|
+
[its troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/react-router/docs/troubleshooting.md#a-script-of-the-page-has-no-nonce).
|
|
649
|
+
|
|
650
|
+
### Inline style attributes stop applying once `style-src` has the nonce
|
|
651
|
+
|
|
652
|
+
**When:** `NONCE` is put in `style-src`, which also has `'unsafe-inline'`,
|
|
653
|
+
and every `style="…"` attribute is ignored: Chrome prints `Refused to apply
|
|
654
|
+
inline style because it violates the following Content Security Policy
|
|
655
|
+
directive: "style-src 'self' 'unsafe-inline' 'nonce-…'". Note that
|
|
656
|
+
'unsafe-inline' is ignored if either a hash or nonce value is present in
|
|
657
|
+
the source list.`
|
|
658
|
+
|
|
659
|
+
**Why:** a nonce in a directive turns its `'unsafe-inline'` off, and an
|
|
660
|
+
attribute cannot carry a nonce. That is why `nonce: true` alone leaves
|
|
661
|
+
`style-src` as written.
|
|
662
|
+
|
|
663
|
+
**Fix:** keep the nonce out of `style-src`, or allow the attributes in a
|
|
664
|
+
directive of their own:
|
|
665
|
+
|
|
666
|
+
```ts
|
|
667
|
+
contentSecurityPolicy: `default-src 'self'; script-src 'self'; style-src-elem 'self' ${NONCE}; style-src-attr 'unsafe-inline'`,
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
### A third-party script or font is refused by a page policy
|
|
671
|
+
|
|
672
|
+
**When:** a page behind `pageHeaders()` loads a script, a stylesheet or a font from another origin, and the console reads `Refused to load the script 'https://…' because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-…' 'strict-dynamic'"` (or `font-src 'self'`).
|
|
673
|
+
|
|
674
|
+
**Why:** `PAGE_DIRECTIVES` allows this site only. A script another script adds is allowed through `'strict-dynamic'`; one named in the HTML needs the nonce, and a font needs its origin in `font-src`.
|
|
675
|
+
|
|
676
|
+
**Fix:** extend the directive that loads it through `directives`, which replaces that directive's list, so repeat what it already holds:
|
|
677
|
+
|
|
678
|
+
```ts
|
|
679
|
+
pageHeaders({
|
|
680
|
+
https: true,
|
|
681
|
+
directives: {
|
|
682
|
+
'font-src': ["'self'", 'https://fonts.example'],
|
|
683
|
+
'style-src-elem': ["'self'", NONCE, 'https://fonts.example'],
|
|
684
|
+
},
|
|
685
|
+
});
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
Never put the origin in `default-src`. Under `'strict-dynamic'` a third-party script is loaded by a nonced script, not by allowlisting its host: a script tag from another origin takes the page's `nonce` attribute, and a host in `script-src` is ignored by browsers that support `'strict-dynamic'`. To allow a host for images, extend `img-src` the same way, as in `directives: { 'img-src': ["'self'", 'https://img.example'] }`.
|
|
689
|
+
|
|
690
|
+
### `Refused to frame '…' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'none'"`
|
|
691
|
+
|
|
692
|
+
**When:** a page puts a response of the app in an `<iframe>`. With the
|
|
693
|
+
policy turned off, the message becomes `Refused to display '…' in a frame
|
|
694
|
+
because it set 'X-Frame-Options' to 'deny'.`
|
|
695
|
+
|
|
696
|
+
**Why:** two headers refuse framing: `frame-ancestors 'none'` in the
|
|
697
|
+
default policy, and `X-Frame-Options: DENY`. Turning one off leaves the
|
|
698
|
+
other.
|
|
699
|
+
|
|
700
|
+
**Fix:** change both:
|
|
701
|
+
|
|
702
|
+
```ts
|
|
703
|
+
app.use(
|
|
704
|
+
secureHeaders({
|
|
705
|
+
contentSecurityPolicy: "default-src 'none'; frame-ancestors 'self'",
|
|
706
|
+
xFrameOptions: 'SAMEORIGIN',
|
|
707
|
+
}),
|
|
708
|
+
);
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
### `net::ERR_BLOCKED_BY_RESPONSE.NotSameOrigin`
|
|
712
|
+
|
|
713
|
+
**When:** a page on another origin embeds a response of the app — an
|
|
714
|
+
`<img>`, a `<script>`, a `no-cors` fetch — and the network panel shows it
|
|
715
|
+
blocked, although it answered 200.
|
|
716
|
+
|
|
717
|
+
**Why:** `Cross-Origin-Resource-Policy: same-origin` lets only the app's own
|
|
718
|
+
origin embed its responses. A CORS `fetch` is not concerned.
|
|
719
|
+
|
|
720
|
+
**Fix:** open it for the app when it serves public assets, or for the
|
|
721
|
+
routes that need it, on their reply:
|
|
722
|
+
|
|
723
|
+
```ts
|
|
724
|
+
app.use(secureHeaders({ crossOriginResourcePolicy: 'cross-origin' }));
|
|
725
|
+
```
|
|
726
|
+
|
|
727
|
+
### `window.opener` is `null` in a sign-in popup
|
|
728
|
+
|
|
729
|
+
**When:** a page of the app opens a popup to another origin — a sign-in,
|
|
730
|
+
a payment — and the popup cannot report back, or the page sees the popup
|
|
731
|
+
as closed at once.
|
|
732
|
+
|
|
733
|
+
**Why:** `Cross-Origin-Opener-Policy: same-origin` puts the page in a
|
|
734
|
+
browsing context group of its own, cut from any window of another origin.
|
|
735
|
+
|
|
736
|
+
**Fix:**
|
|
737
|
+
|
|
738
|
+
```ts
|
|
739
|
+
app.use(secureHeaders({ crossOriginOpenerPolicy: 'same-origin-allow-popups' }));
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
### A subdomain on plain HTTP no longer opens
|
|
743
|
+
|
|
744
|
+
**When:** after a browser has visited the app over HTTPS, another host
|
|
745
|
+
under the same domain, served over plain HTTP, is forced to HTTPS and fails.
|
|
746
|
+
|
|
747
|
+
**Why:** the default `Strict-Transport-Security` is
|
|
748
|
+
`max-age=31536000; includeSubDomains`: the browser remembers, for a year,
|
|
749
|
+
to use HTTPS for the host **and every subdomain**. A browser ignores the
|
|
750
|
+
header on a plain-HTTP response, so it bites only once the app is reached
|
|
751
|
+
over HTTPS.
|
|
752
|
+
|
|
753
|
+
**Fix:** drop `includeSubDomains` when a subdomain is still on HTTP, or the
|
|
754
|
+
header altogether where the app is not served over HTTPS:
|
|
755
|
+
|
|
756
|
+
```ts
|
|
757
|
+
app.use(secureHeaders({ strictTransportSecurity: 'max-age=31536000' }));
|
|
758
|
+
```
|
|
759
|
+
|
|
760
|
+
A browser that already stored the policy keeps it until it expires, or
|
|
761
|
+
until it reads `max-age=0` from the host over HTTPS.
|
|
762
|
+
|
|
763
|
+
## Headers
|
|
764
|
+
|
|
765
|
+
### An option has no effect on a response
|
|
766
|
+
|
|
767
|
+
**When:** an option is set, and some responses still carry another value.
|
|
768
|
+
|
|
769
|
+
**Why:** the middleware never overwrites a header the response already has.
|
|
770
|
+
A route set it on its reply or on `set.headers`, a middleware declared
|
|
771
|
+
after `secureHeaders` set it on the way out, or one declared before it
|
|
772
|
+
overwrote it.
|
|
773
|
+
`@softistx/alxia-graphql`'s IDE sets its own `Content-Security-Policy` on purpose.
|
|
774
|
+
|
|
775
|
+
**Fix:** find what sets it on that route, and change it there:
|
|
776
|
+
|
|
777
|
+
```ts
|
|
778
|
+
const response = await app.request('/the-route');
|
|
779
|
+
console.log(response.headers.get('content-security-policy'));
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
### A CDN still caches a page, though `cacheControl` is set
|
|
783
|
+
|
|
784
|
+
**When:** `cacheControl: 'private, no-store'` is set, and some pages are still
|
|
785
|
+
stored by a cache.
|
|
786
|
+
|
|
787
|
+
**Why:** a route that sets its own `Cache-Control` (a `reply` with
|
|
788
|
+
`cache-control: public, max-age=…`, or a middleware after `secureHeaders`)
|
|
789
|
+
keeps it: the option only fills a header the response lacks.
|
|
790
|
+
|
|
791
|
+
**Fix:** drop the route's header, or give it the same value. Before, the
|
|
792
|
+
route's own header wins:
|
|
793
|
+
|
|
794
|
+
```ts
|
|
795
|
+
app.get('/the-page', ({ reply }) =>
|
|
796
|
+
reply(200, html, { headers: { 'cache-control': 'public, max-age=60' } }),
|
|
797
|
+
);
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
After, the route leaves it to `secureHeaders` (or says `private, no-store`
|
|
801
|
+
itself):
|
|
802
|
+
|
|
803
|
+
```ts
|
|
804
|
+
app.get('/the-page', ({ reply }) => reply(200, html));
|
|
805
|
+
|
|
806
|
+
const response = await app.request('/the-page');
|
|
807
|
+
console.log(response.headers.get('cache-control')); // private, no-store
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
### `X-Powered-By` or `Server` is still sent
|
|
811
|
+
|
|
812
|
+
**When:** `hidePoweredBy` is on (the default), and one of them is on the
|
|
813
|
+
response.
|
|
814
|
+
|
|
815
|
+
**Why:** the middleware deletes them when the response comes back through
|
|
816
|
+
it. A middleware declared **before** `secureHeaders` that adds one sees the
|
|
817
|
+
response after that, and keeps it. A proxy in front of the app may add its
|
|
818
|
+
own `Server` too.
|
|
819
|
+
|
|
820
|
+
**Fix:** declare `secureHeaders` before the middleware that adds the header,
|
|
821
|
+
or stop that middleware adding it:
|
|
822
|
+
|
|
823
|
+
```ts
|
|
824
|
+
import { alxia, defineMiddleware, settle, withHeaders } from '@softistx/alxia';
|
|
825
|
+
import { secureHeaders } from '@softistx/alxia-secure-headers';
|
|
826
|
+
|
|
827
|
+
const poweredBy = defineMiddleware(async (ctx, next) =>
|
|
828
|
+
withHeaders(await settle(ctx, next()), (headers) => headers.set('x-powered-by', 'my-app')),
|
|
829
|
+
);
|
|
830
|
+
|
|
831
|
+
const app = alxia()
|
|
832
|
+
.use(secureHeaders()) // outermost: deletes the header on the way out
|
|
833
|
+
.use(poweredBy);
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
### A response arrives without the headers
|
|
837
|
+
|
|
838
|
+
**When:** a response has none of the headers, though `secureHeaders` is
|
|
839
|
+
in use.
|
|
840
|
+
|
|
841
|
+
**Why:** the middleware decorates what comes back through it, and two
|
|
842
|
+
responses do not: a WebSocket upgrade that succeeds (the `101`), and a page
|
|
843
|
+
served with `page()`, which `Bun.serve` answers itself.
|
|
844
|
+
|
|
845
|
+
**Fix:** serve a page that needs the headers through a route, `file` or
|
|
846
|
+
`static` rather than `page()` — or give it its policy in its HTML with
|
|
847
|
+
`<meta http-equiv="Content-Security-Policy" content="…">`, where browsers
|
|
848
|
+
ignore `frame-ancestors`. A middleware declared before `secureHeaders`,
|
|
849
|
+
whose own work may fail, catches it and still returns what `next()`
|
|
850
|
+
resolved to, which already carries the headers:
|
|
851
|
+
|
|
852
|
+
```ts
|
|
853
|
+
const timing = defineMiddleware(async (ctx, next) => {
|
|
854
|
+
const response = await next();
|
|
855
|
+
try {
|
|
856
|
+
recordTiming(response); // your own work
|
|
857
|
+
} catch (error) {
|
|
858
|
+
console.error(error);
|
|
859
|
+
}
|
|
860
|
+
return response;
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
const app = alxia().use(timing).use(secureHeaders());
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
### A 404 or a route outside a group has no headers
|
|
867
|
+
|
|
868
|
+
**When:** `secureHeaders` is used inside a `group`, and a route outside the
|
|
869
|
+
group, or a request that matches no route, comes back without them.
|
|
870
|
+
|
|
871
|
+
**Why:** a middleware declared in a `group` stays inside the group: it runs
|
|
872
|
+
on its routes and on a request no route matches under its prefix, and on
|
|
873
|
+
nothing else: not on a route outside the group, nor declared after it.
|
|
874
|
+
|
|
875
|
+
**Fix:** use it on the app, and give the routes that differ their own
|
|
876
|
+
values on their replies:
|
|
877
|
+
|
|
878
|
+
```ts
|
|
879
|
+
import { alxia } from '@softistx/alxia';
|
|
880
|
+
import { secureHeaders } from '@softistx/alxia-secure-headers';
|
|
881
|
+
|
|
882
|
+
const app = alxia()
|
|
883
|
+
.use(secureHeaders())
|
|
884
|
+
.get('/embed', ({ reply }) =>
|
|
885
|
+
reply(200, 'ok', {
|
|
886
|
+
headers: {
|
|
887
|
+
'content-security-policy': "default-src 'none'; frame-ancestors 'self'",
|
|
888
|
+
'x-frame-options': 'SAMEORIGIN',
|
|
889
|
+
},
|
|
890
|
+
}),
|
|
891
|
+
);
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
### A `try`/`catch` middleware's reply has no headers
|
|
895
|
+
|
|
896
|
+
**When:** a middleware wraps `next()` in a `try`/`catch` to answer errors in
|
|
897
|
+
its own format, is declared before `secureHeaders()`, and its reply comes
|
|
898
|
+
back without the security headers.
|
|
899
|
+
|
|
900
|
+
**Why:** `secureHeaders` settles `next()`: it sets the headers on the response
|
|
901
|
+
the route boundary would give (the `HttpError`'s status, or a 500), then the error goes on to the middlewares around it. A `try`/`catch`
|
|
902
|
+
declared **before** it catches the error, but its reply is made outside
|
|
903
|
+
`secureHeaders`, so it carries no headers; the 500 inside did.
|
|
904
|
+
|
|
905
|
+
**Fix:** declare the error-handling middleware after `secureHeaders`, so
|
|
906
|
+
the headers are set on its reply:
|
|
907
|
+
|
|
908
|
+
```ts
|
|
909
|
+
import { alxia, defineMiddleware } from '@softistx/alxia';
|
|
910
|
+
import { secureHeaders } from '@softistx/alxia-secure-headers';
|
|
911
|
+
|
|
912
|
+
const errors = defineMiddleware(async (_ctx, next) => {
|
|
913
|
+
try {
|
|
914
|
+
return await next();
|
|
915
|
+
} catch (error) {
|
|
916
|
+
return Response.json({ error: String(error) }, { status: 500 });
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
const app = alxia()
|
|
921
|
+
.use(secureHeaders()) // first
|
|
922
|
+
.use(errors)
|
|
923
|
+
.get('/boom', () => {
|
|
924
|
+
throw new Error('boom');
|
|
925
|
+
});
|
|
926
|
+
```
|
|
927
|
+
|
|
928
|
+
### A route has no headers, though the others do
|
|
929
|
+
|
|
930
|
+
**When:** one route's responses lack the headers, while a `404` or another
|
|
931
|
+
route's have them.
|
|
932
|
+
|
|
933
|
+
**Why:** a route declared **before** `app.use(secureHeaders())` does not run
|
|
934
|
+
it. A request no route matches runs every top-level middleware wherever it
|
|
935
|
+
is declared, which is why its `404` is covered.
|
|
936
|
+
|
|
937
|
+
**Fix:** declare `secureHeaders` before the routes:
|
|
938
|
+
|
|
939
|
+
```ts
|
|
940
|
+
const app = alxia()
|
|
941
|
+
.use(secureHeaders())
|
|
942
|
+
.get('/early', ({ reply }) => reply(200, 'now covered'));
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
### `Clear-Site-Data` is missing on a client-side sign-out
|
|
946
|
+
|
|
947
|
+
**When:** a form sign-out empties the cache, but a client-side one (the button, a navigation) does not. `Clear-Site-Data` is on the response of `POST /sign-out` and absent from `POST /sign-out.data`.
|
|
948
|
+
|
|
949
|
+
**Why:** React Router's single fetch posts the route's `.data` URL, a path `use('/sign-out', …)` does not cover. A route cannot set the header there either: that answer carries a redirect's cookies, not its other headers.
|
|
950
|
+
|
|
951
|
+
**Fix:** mount the middleware on both paths:
|
|
952
|
+
|
|
953
|
+
```ts
|
|
954
|
+
const cleared = clearSiteData(['cache']);
|
|
955
|
+
|
|
956
|
+
const app = alxia()
|
|
957
|
+
.use('/sign-out', cleared)
|
|
958
|
+
.use('/sign-out.data', cleared);
|
|
959
|
+
```
|