@softistx/alxia-clavix 0.2.0 → 0.3.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/README.md +39 -2
- package/dist/clavix.d.ts +10 -4
- package/dist/clavix.d.ts.map +1 -1
- package/dist/dpop/htu.d.ts +11 -0
- package/dist/dpop/htu.d.ts.map +1 -0
- package/dist/dpop/index.d.ts +6 -0
- package/dist/dpop/index.d.ts.map +1 -0
- package/dist/dpop/jwk.d.ts +20 -0
- package/dist/dpop/jwk.d.ts.map +1 -0
- package/dist/dpop/nonce.d.ts +29 -0
- package/dist/dpop/nonce.d.ts.map +1 -0
- package/dist/dpop/options.d.ts +33 -0
- package/dist/dpop/options.d.ts.map +1 -0
- package/dist/dpop/proof.d.ts +32 -0
- package/dist/dpop/proof.d.ts.map +1 -0
- package/dist/dpop/replay-store.d.ts +24 -0
- package/dist/dpop/replay-store.d.ts.map +1 -0
- package/dist/guard.d.ts +19 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +350 -18
- package/dist/index.js.map +11 -4
- package/docs/guide/principal-and-scopes.md +5 -1
- package/docs/guide/security.md +89 -3
- package/docs/roadmap.md +7 -0
- package/docs/troubleshooting.md +88 -1
- package/package.json +5 -5
package/docs/guide/security.md
CHANGED
|
@@ -32,9 +32,95 @@ Mitigations:
|
|
|
32
32
|
|
|
33
33
|
## DPoP
|
|
34
34
|
|
|
35
|
-
A token bound to a key carries `cnf.jkt` (RFC 9449)
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
A token bound to a key carries `cnf.jkt` (RFC 9449): a thief of the token cannot use it without the
|
|
36
|
+
private key. Turn the check on with `dpop`:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { connectRedis } from '@softistx/redis';
|
|
40
|
+
import { clavix, hmacDpopNonce } from '@softistx/alxia-clavix';
|
|
41
|
+
import { redisDpopReplayStore } from '@softistx/alxia-redis';
|
|
42
|
+
|
|
43
|
+
const connection = await connectRedis(Bun.env['REDIS_URL']!);
|
|
44
|
+
|
|
45
|
+
const auth = clavix({
|
|
46
|
+
domain: 'clavix.example.com',
|
|
47
|
+
audience: 'https://api.example.com',
|
|
48
|
+
dpop: {
|
|
49
|
+
origin: 'https://api.example.com', // the URL clients call, not the proxy's
|
|
50
|
+
replay: redisDpopReplayStore(connection.client, { name: 'dpop' }),
|
|
51
|
+
nonce: hmacDpopNonce({ secret: Bun.env['DPOP_NONCE_SECRET']! }), // optional, 16 characters or more
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`@softistx/alxia-redis` needs no peer on `@softistx/alxia-clavix`: its store is typed by shape, and the
|
|
57
|
+
two are kept in step by a type-level spec in clavix.
|
|
58
|
+
|
|
59
|
+
A client sends `Authorization: DPoP <token>` and a `DPoP: <proof>` header. The guard is
|
|
60
|
+
`@softistx/alxia-jwt`'s `bearer()` with the DPoP half plugged in through its extension point, so
|
|
61
|
+
the 401 bodies, the cookie mode and `alxia({ errors: 'problem' })` are the same. On every call
|
|
62
|
+
`auth.bearer()` checks, in this order:
|
|
63
|
+
|
|
64
|
+
1. the token verifies as before, and carries `cnf.jkt` (else `dpop_unbound`; `dpop_unsupported` when
|
|
65
|
+
`dpop` is not configured at all);
|
|
66
|
+
2. the proof is one JWT, header `typ` exactly `dpop+jwt` (compared case-sensitively), no `crit`
|
|
67
|
+
header (an extension this server cannot understand, refused as `dpop_proof_malformed`), `alg`
|
|
68
|
+
`ES256` or `EdDSA` (`dpop.algorithms`), and a public `jwk` (`EC` P-256 or `OKP` Ed25519, no
|
|
69
|
+
private member), with a valid signature by that key;
|
|
70
|
+
3. `htm` is the request method, `htu` is `dpop.origin` plus the request path (query and fragment
|
|
71
|
+
ignored; scheme and host compared without case, `:443` and `:80` equal to none, the path exact,
|
|
72
|
+
userinfo refused);
|
|
73
|
+
4. `iat` is at most `maxAgeSec` (60) old and at most `clockSkewSec` (5) ahead;
|
|
74
|
+
5. `jti` is present (at most 256 characters), `ath` is the base64url SHA-256 of the access token,
|
|
75
|
+
and the JWK thumbprint (RFC 7638) of the proof key equals `cnf.jkt`;
|
|
76
|
+
6. with `nonce`, the proof carries a nonce the server issued;
|
|
77
|
+
7. only then the `jti` is recorded in `dpop.replay`: a refused proof is never recorded, and one
|
|
78
|
+
already recorded is `dpop_proof_replay`.
|
|
79
|
+
|
|
80
|
+
A token with `cnf.jkt` sent as `Bearer`, or in the cookie, is always refused (`dpop_required`), also
|
|
81
|
+
when `dpop` is not configured: without the proof it is a stolen token. A token without `cnf.jkt` keeps
|
|
82
|
+
working as `Bearer`.
|
|
83
|
+
|
|
84
|
+
### Migrating a deployment
|
|
85
|
+
|
|
86
|
+
This is a behaviour change. Until now a token with `cnf.jkt` passed as a bearer token; now it is a 401
|
|
87
|
+
`dpop_required`. Before upgrading an API that receives bound tokens:
|
|
88
|
+
|
|
89
|
+
1. add `dpop` to `clavix()` (a replay store, and the public `origin`);
|
|
90
|
+
2. have the clients send `Authorization: DPoP <token>` and a proof header, instead of
|
|
91
|
+
`Authorization: Bearer <token>`;
|
|
92
|
+
3. tokens without `cnf.jkt` need nothing: they keep working as `Bearer`.
|
|
93
|
+
|
|
94
|
+
The `WWW-Authenticate` of a request with no token also changes with `dpop`: `Bearer` becomes
|
|
95
|
+
`Bearer, DPoP algs="ES256 EdDSA"` (the algorithms you accept).
|
|
96
|
+
|
|
97
|
+
### Multi-instance
|
|
98
|
+
|
|
99
|
+
The replay store must be shared. `memoryDpopReplayStore()` is for tests and one instance: with two
|
|
100
|
+
instances, a proof replayed on the other one is accepted. It also keeps at most `maxEntries` live
|
|
101
|
+
proofs (10 000 by default) and throws past that, rather than growing without bound or forgetting one.
|
|
102
|
+
Use `redisDpopReplayStore` from `@softistx/alxia-redis`, or a store of your own (`claim(key, ttlMs)`
|
|
103
|
+
answering `true` once, atomically). A store that throws makes the request fail with a 500: a proof is
|
|
104
|
+
never accepted unchecked. The store holds proofs under `<jkt>:<jti>`, for `maxAgeSec + clockSkewSec`
|
|
105
|
+
plus a 5 second margin. The access token's own `jti` is not the proof's.
|
|
106
|
+
|
|
107
|
+
### Behind a proxy (`htu`)
|
|
108
|
+
|
|
109
|
+
`origin` is yours, never a header. Behind a proxy the request URL is the internal one, and `Host` or
|
|
110
|
+
`X-Forwarded-*` are what an attacker sends. The proof's `htu` is compared with the public origin you
|
|
111
|
+
configure and the path the API receives, so a proxy that strips a path prefix breaks `htu`
|
|
112
|
+
(`dpop_proof_url`). Giving a public base with a prefix is not built yet:
|
|
113
|
+
[softistx/alxia#993](https://github.com/softistx/alxia/issues/993).
|
|
114
|
+
|
|
115
|
+
### Nonces
|
|
116
|
+
|
|
117
|
+
With `nonce`, a proof without a valid one is a 401 `use_dpop_nonce` carrying a fresh `DPoP-Nonce`
|
|
118
|
+
header: the client retries once with it in the proof. A response to an accepted proof also carries a
|
|
119
|
+
fresh `DPoP-Nonce` once the nonce used is past half its life (RFC 9449 section 8.2), so the next proof
|
|
120
|
+
does not meet a refusal. `hmacDpopNonce` is stateless (every instance with the same secret accepts the
|
|
121
|
+
nonces of the others), derives its HMAC key from the secret with a label of its own, and expires nonces
|
|
122
|
+
after `ttlMs` (5 minutes by default; a finite number above 0, else a `TypeError`). A nonce limits how long a proof can be pre-made; the `jti` check still stops
|
|
123
|
+
replays.
|
|
38
124
|
|
|
39
125
|
## Step-up
|
|
40
126
|
|
package/docs/roadmap.md
CHANGED
|
@@ -24,6 +24,13 @@ Nothing scheduled yet.
|
|
|
24
24
|
|
|
25
25
|
## Shipped
|
|
26
26
|
|
|
27
|
+
### Unreleased
|
|
28
|
+
|
|
29
|
+
- **DPoP proofs checked.** `clavix({ dpop })` accepts the `DPoP` scheme and verifies each proof
|
|
30
|
+
(`typ`, ES256 or EdDSA, `htm`, `htu` from the public origin, fresh `iat`, single-use `jti`, `ath`,
|
|
31
|
+
key thumbprint equal to `cnf.jkt`), with an optional `DPoP-Nonce` and `WWW-Authenticate: DPoP`
|
|
32
|
+
answers. A token with `cnf.jkt` sent as `Bearer` is now refused (behaviour change: bound-token clients move to the `DPoP` scheme). Known limit: [a proxy that strips a path prefix](https://github.com/softistx/alxia/issues/993).
|
|
33
|
+
|
|
27
34
|
### 0.1.0
|
|
28
35
|
|
|
29
36
|
- **Verify Clavix access tokens in an API.** A multi-tenant issuer allow-list
|
package/docs/troubleshooting.md
CHANGED
|
@@ -14,6 +14,13 @@ startup (`verify` never throws), or the body of a 401 or 403.
|
|
|
14
14
|
- [`TypeError: clavix: probation must be a positive integer`](#typeerror-clavix-probation-must-be-a-positive-integer)
|
|
15
15
|
- [`TypeError: clavix: the tenant of … is empty`](#typeerror-clavix-the-tenant-of--is-empty)
|
|
16
16
|
- [`TypeError: clavix: warm issuer … is not allowed`](#typeerror-clavix-warm-issuer--is-not-allowed)
|
|
17
|
+
- [`TypeError: clavix: dpop.origin must be an origin such as "https://api.example.com", got …`](#typeerror-clavix-dpoporigin-must-be-an-origin-such-as-httpsapiexamplecom-got-)
|
|
18
|
+
- [`TypeError: clavix: dpop.replay is required`](#typeerror-clavix-dpopreplay-is-required)
|
|
19
|
+
- [`TypeError: clavix: dpop.algorithms takes ES256, EdDSA or both`](#typeerror-clavix-dpopalgorithms-takes-es256-eddsa-or-both)
|
|
20
|
+
- [`TypeError: clavix: dpop.maxAgeSec must be a finite number above 0`](#typeerror-clavix-dpopmaxagesec-must-be-a-finite-number-above-0)
|
|
21
|
+
- [`TypeError: clavix: dpop.clockSkewSec must be a finite number, 0 or more`](#typeerror-clavix-dpopclockskewsec-must-be-a-finite-number-0-or-more)
|
|
22
|
+
- [`TypeError: clavix: the dpop nonce secret needs 16 characters`](#typeerror-clavix-the-dpop-nonce-secret-needs-16-characters)
|
|
23
|
+
- [`TypeError: clavix: the dpop nonce ttlMs must be a finite number above 0`](#typeerror-clavix-the-dpop-nonce-ttlms-must-be-a-finite-number-above-0)
|
|
17
24
|
- [`TypeError: requireScope: give scopes, each a non-empty token without spaces or quotes`](#typeerror-requirescope-give-scopes-each-a-non-empty-token-without-spaces-or-quotes)
|
|
18
25
|
|
|
19
26
|
**Responses**
|
|
@@ -27,6 +34,10 @@ startup (`verify` never throws), or the body of a 401 or 403.
|
|
|
27
34
|
- [`401 … "reason": "claims"`](#401-reason-claims)
|
|
28
35
|
- [`401 … "reason": "expired"` or `"not_yet_valid"`](#401-reason-expired-or-not_yet_valid)
|
|
29
36
|
- [`401 … "reason": "key"` or `"keys_unavailable"`](#401-reason-key-or-keys_unavailable)
|
|
37
|
+
- [`401 … "reason": "dpop_required"`, `"dpop_unsupported"`, `"dpop_malformed"` or `"dpop_unbound"`](#401-reason-dpop_required-dpop_unsupported-dpop_malformed-or-dpop_unbound)
|
|
38
|
+
- [`401 … "reason": "dpop_proof_*"`](#401-reason-dpop_proof_)
|
|
39
|
+
- [`401 … "reason": "dpop_nonce"`](#401-reason-dpop_nonce)
|
|
40
|
+
- [`500` on a request with a DPoP proof](#500-on-a-request-with-a-dpop-proof)
|
|
30
41
|
- [`403 { "error": "forbidden", "reason": "insufficient_scope" }`](#403-insufficient_scope)
|
|
31
42
|
|
|
32
43
|
**Traps**
|
|
@@ -106,6 +117,37 @@ clavix({
|
|
|
106
117
|
});
|
|
107
118
|
```
|
|
108
119
|
|
|
120
|
+
### `TypeError: clavix: dpop.origin must be an origin such as "https://api.example.com", got …`
|
|
121
|
+
|
|
122
|
+
`dpop.origin` is a scheme, host and optional port, no path, query, fragment or userinfo
|
|
123
|
+
(`https://api.example.com`).
|
|
124
|
+
|
|
125
|
+
### `TypeError: clavix: dpop.replay is required`
|
|
126
|
+
|
|
127
|
+
Give `dpop.replay`: `redisDpopReplayStore(connection.client, { name: 'dpop' })` from
|
|
128
|
+
`@softistx/alxia-redis`, or `memoryDpopReplayStore()` for tests and one instance.
|
|
129
|
+
|
|
130
|
+
### `TypeError: clavix: dpop.algorithms takes ES256, EdDSA or both`
|
|
131
|
+
|
|
132
|
+
`dpop.algorithms` is a non-empty list of `'ES256'` and `'EdDSA'`.
|
|
133
|
+
|
|
134
|
+
### `TypeError: clavix: dpop.maxAgeSec must be a finite number above 0`
|
|
135
|
+
|
|
136
|
+
`maxAgeSec` is in seconds, finite and above zero (60 by default).
|
|
137
|
+
|
|
138
|
+
### `TypeError: clavix: dpop.clockSkewSec must be a finite number, 0 or more`
|
|
139
|
+
|
|
140
|
+
`clockSkewSec` is in seconds, finite, zero or more (5 by default).
|
|
141
|
+
|
|
142
|
+
### `TypeError: clavix: the dpop nonce secret needs 16 characters`
|
|
143
|
+
|
|
144
|
+
`hmacDpopNonce({ secret })` takes a secret of at least 16 characters, shared by every instance
|
|
145
|
+
(`Bun.env['DPOP_NONCE_SECRET']!`).
|
|
146
|
+
|
|
147
|
+
### `TypeError: clavix: the dpop nonce ttlMs must be a finite number above 0`
|
|
148
|
+
|
|
149
|
+
`hmacDpopNonce({ ttlMs })` takes a finite number of milliseconds above 0 (default 300000, 5 minutes).
|
|
150
|
+
|
|
109
151
|
### `TypeError: requireScope: give scopes, each a non-empty token without spaces or quotes`
|
|
110
152
|
|
|
111
153
|
`requireScope()` got no scope, or one with a space, a quote or a backslash. One argument per scope:
|
|
@@ -134,7 +176,7 @@ access token. The client must send the access token, not the ID token.
|
|
|
134
176
|
|
|
135
177
|
### 401 reason `missing`
|
|
136
178
|
|
|
137
|
-
No `Authorization: Bearer <token>` header (and no cookie, when `bearer({ cookie })` is set). The client must send the access token.
|
|
179
|
+
No `Authorization: Bearer <token>` header (and no cookie, when `bearer({ cookie })` is set), or an `Authorization: Bearer`/`DPoP` header with no parsable token (the cookie is then not read). The client must send the access token.
|
|
138
180
|
|
|
139
181
|
### 401 reason `signature` or `algorithm`
|
|
140
182
|
|
|
@@ -165,6 +207,51 @@ keys could not be fetched and none is cached. Check that the issuer's discovery
|
|
|
165
207
|
from the API (egress, DNS, `timeoutMs`). See
|
|
166
208
|
[`@softistx/alxia-jwt`'s JWKS guide](https://github.com/softistx/alxia/blob/develop/packages/jwt/docs/guide/jwks.md).
|
|
167
209
|
|
|
210
|
+
### 401 reason `dpop_required`, `dpop_unsupported`, `dpop_malformed` or `dpop_unbound`
|
|
211
|
+
|
|
212
|
+
`WWW-Authenticate: DPoP error="invalid_token"` (with `dpop`). `dpop_required`: the token carries
|
|
213
|
+
`cnf.jkt` and was sent as `Bearer` or in the cookie (no proof), also when `clavix()` has no `dpop`
|
|
214
|
+
option. Send `Authorization: DPoP <token>` with a `DPoP` proof header, and give `clavix()` its `dpop`
|
|
215
|
+
option. `dpop_unsupported`: the `DPoP` scheme was used while `clavix()` has no `dpop` option; add it.
|
|
216
|
+
`dpop_malformed`: the token's `cnf` is not an object, or its `jkt` is present but not a non-empty string; ask Clavix for a new token.
|
|
217
|
+
`dpop_unbound`: the `DPoP` scheme with a token that has no `cnf.jkt`: ask Clavix for a DPoP-bound
|
|
218
|
+
token (send the proof at the token endpoint) or use `Bearer`.
|
|
219
|
+
|
|
220
|
+
### 401 reason `dpop_proof_*`
|
|
221
|
+
|
|
222
|
+
`WWW-Authenticate: DPoP error="invalid_dpop_proof"`. The suffix names the check that failed:
|
|
223
|
+
|
|
224
|
+
| Reason | Cause and fix |
|
|
225
|
+
| --- | --- |
|
|
226
|
+
| `dpop_proof_missing` | no `DPoP` header: send one proof per request |
|
|
227
|
+
| `dpop_proof_malformed` | not three dot-separated parts of JSON, several proofs in one header, or a `crit` header |
|
|
228
|
+
| `dpop_proof_type` | header `typ` is not exactly `dpop+jwt` (case matters) |
|
|
229
|
+
| `dpop_proof_algorithm` | `alg` is not in `dpop.algorithms` (`ES256`, `EdDSA` by default) |
|
|
230
|
+
| `dpop_proof_key` | the header `jwk` is absent, not a P-256 or Ed25519 public key, or has a private member |
|
|
231
|
+
| `dpop_proof_signature` | the proof is not signed by its own `jwk` |
|
|
232
|
+
| `dpop_proof_method` | `htm` is not the request's method |
|
|
233
|
+
| `dpop_proof_url` | `htu` is not `dpop.origin` plus the path (userinfo, another host or a trailing slash differ); check `origin`, and a proxy that rewrites the path ([#993](https://github.com/softistx/alxia/issues/993)) |
|
|
234
|
+
| `dpop_proof_iat` | `iat` is more than `maxAgeSec` old or more than `clockSkewSec` ahead: fix the client's clock, or make one proof per request |
|
|
235
|
+
| `dpop_proof_jti` | no `jti`, or one over 256 characters |
|
|
236
|
+
| `dpop_proof_ath` | `ath` is not the SHA-256 of this access token: the proof was made for another token, or for none |
|
|
237
|
+
| `dpop_proof_thumbprint` | the proof key is not the key the token is bound to (`cnf.jkt`) |
|
|
238
|
+
| `dpop_proof_replay` | this proof was already used: sign a new one (new `jti`) for every request, retries included |
|
|
239
|
+
|
|
240
|
+
### 401 reason `dpop_nonce`
|
|
241
|
+
|
|
242
|
+
`WWW-Authenticate: DPoP error="use_dpop_nonce"` and a `DPoP-Nonce` header. `dpop.nonce` is set and the
|
|
243
|
+
proof carries no valid nonce. Retry once with a new proof whose `nonce` claim is that header's value;
|
|
244
|
+
keep the latest `DPoP-Nonce` a response gives (a successful response carries one too when the nonce is
|
|
245
|
+
near its end). An expired nonce (older than `ttlMs`) is answered the same way with a fresh one.
|
|
246
|
+
|
|
247
|
+
### 500 on a request with a DPoP proof
|
|
248
|
+
|
|
249
|
+
The replay store threw (`claim` rejected): Redis is down, or `redisDpopReplayStore` has no connection.
|
|
250
|
+
Every DPoP request fails with a 500 until it answers, on purpose: a proof is never accepted unchecked.
|
|
251
|
+
Check the Redis connection (`/ready`, the `connectRedis` handle); `Bearer` tokens without `cnf.jkt`
|
|
252
|
+
are not affected. `memoryDpopReplayStore` throws `memoryDpopReplayStore: full (use a shared store)`
|
|
253
|
+
past `maxEntries` live proofs: use a shared store.
|
|
254
|
+
|
|
168
255
|
### 403 `insufficient_scope`
|
|
169
256
|
|
|
170
257
|
```json
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softistx/alxia-clavix",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Verify the access tokens Clavix issues in an alxia API: a multi-tenant issuer allow-list on the RFC 9068 profile of @softistx/alxia-jwt",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"registry": "https://registry.npmjs.org",
|
|
31
|
-
"access": "
|
|
31
|
+
"access": "restricted"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "bun run ../../build.ts",
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@softistx/alxia": "^0.28.0",
|
|
45
|
-
"@softistx/alxia-jwt": "^0.
|
|
46
|
-
"@types/bun": "^1.4.
|
|
45
|
+
"@softistx/alxia-jwt": "^0.10.0",
|
|
46
|
+
"@types/bun": "^1.4.5"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"@softistx/alxia": "^0.28.0",
|
|
50
|
-
"@softistx/alxia-jwt": "^0.
|
|
50
|
+
"@softistx/alxia-jwt": "^0.10.0",
|
|
51
51
|
"typescript": "^6.0.3 || ^7.0.0"
|
|
52
52
|
}
|
|
53
53
|
}
|