@c9up/warden 0.1.27 → 0.2.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/dist/mfa/OtpProvider.d.ts +39 -1
- package/dist/mfa/OtpProvider.d.ts.map +1 -1
- package/dist/mfa/OtpProvider.js +97 -5
- package/dist/mfa/OtpProvider.js.map +1 -1
- package/dist/mfa/WebauthnProvider.d.ts +60 -10
- package/dist/mfa/WebauthnProvider.d.ts.map +1 -1
- package/dist/mfa/WebauthnProvider.js +207 -47
- package/dist/mfa/WebauthnProvider.js.map +1 -1
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +1 -1
- package/src/mfa/OtpProvider.ts +133 -6
- package/src/mfa/WebauthnProvider.ts +289 -71
|
@@ -18,23 +18,104 @@
|
|
|
18
18
|
import { randomBytes } from "node:crypto";
|
|
19
19
|
import { WardenError } from "../errors.js";
|
|
20
20
|
import { base64urlToBuffer, bufferToBase64url, coseToKeyObject, decodeCbor, parseAuthenticatorData, sha256, verifyWebauthnSignature, } from "./webauthn-codec.js";
|
|
21
|
+
/**
|
|
22
|
+
* Run a parse of attacker-supplied bytes, turning a throw into "invalid".
|
|
23
|
+
*
|
|
24
|
+
* A truncated attestation, a malformed COSE key or authenticator data that
|
|
25
|
+
* stops mid-structure are INPUT errors — `CBOR: item starts past the end of the
|
|
26
|
+
* buffer` reaching the caller is a 500 for a bad request, and a 500 tells
|
|
27
|
+
* whoever sent it that they reached something which did not expect them.
|
|
28
|
+
*
|
|
29
|
+
* Deliberately narrow: only the decoding and the signature check go through
|
|
30
|
+
* here. A store that cannot be read is infrastructure, and reporting an outage
|
|
31
|
+
* as "not verified" would let a database failure read as a failed login.
|
|
32
|
+
*/
|
|
33
|
+
function parsePayload(parse) {
|
|
34
|
+
try {
|
|
35
|
+
return parse();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
21
41
|
export class MemoryWebauthnChallengeStore {
|
|
22
42
|
#store = new Map();
|
|
23
43
|
#ttlMs;
|
|
24
|
-
/**
|
|
25
|
-
|
|
44
|
+
/**
|
|
45
|
+
* @param ttlMs Server-side challenge lifetime. Default 5 min.
|
|
46
|
+
* @param maxEntries How many pending ceremonies this process will hold.
|
|
47
|
+
* Sweeping removes only EXPIRED entries, so a flood of still-valid ones
|
|
48
|
+
* grew the map without a ceiling. Default 10 000.
|
|
49
|
+
*/
|
|
50
|
+
constructor(ttlMs = 300_000, maxEntries = 10_000) {
|
|
51
|
+
// A TTL of Infinity or NaN is not a TTL: the challenge never expires,
|
|
52
|
+
// against a contract that says it MUST be time-bound — and an unconsumed
|
|
53
|
+
// challenge left live indefinitely is exactly the replay window the
|
|
54
|
+
// expiry exists to close.
|
|
55
|
+
if (!Number.isInteger(ttlMs) || ttlMs < 1) {
|
|
56
|
+
throw new WardenError("E_WARDEN_STORE_TTL_INVALID", `ttlMs must be a positive integer, got ${String(ttlMs)}.`, {
|
|
57
|
+
hint: "Give the ceremony a lifetime in milliseconds — the default is 300000 (5 min).",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
26
60
|
this.#ttlMs = ttlMs;
|
|
61
|
+
// A ceiling of 0, a negative, NaN or Infinity is not a ceiling: the
|
|
62
|
+
// first two refuse every write and the last two disable the bound this
|
|
63
|
+
// exists to provide, silently. A caller asking for one of those means
|
|
64
|
+
// something, and none of the meanings is what would have happened.
|
|
65
|
+
if (!Number.isInteger(maxEntries) || maxEntries < 1) {
|
|
66
|
+
throw new WardenError("E_WARDEN_STORE_LIMIT_INVALID", `maxEntries must be a positive integer, got ${String(maxEntries)}.`, {
|
|
67
|
+
hint: "Pass the number of pending entries this process may hold — the default is 10_000.",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
this.#maxEntries = maxEntries;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Size at which the next sweep runs, doubling each time.
|
|
74
|
+
*
|
|
75
|
+
* Entries only ever left when their state was reused or taken, so a
|
|
76
|
+
* ceremony nobody finished — a passkey prompt dismissed, which happens all
|
|
77
|
+
* day — stayed for the life of the process. Sweeping on write bounds it
|
|
78
|
+
* without a timer, which would hold the event loop open and need a disposal
|
|
79
|
+
* contract this store does not have.
|
|
80
|
+
*/
|
|
81
|
+
#sweepAt = 64;
|
|
82
|
+
#maxEntries;
|
|
83
|
+
async save(state, ceremony) {
|
|
84
|
+
if (this.#store.size >= this.#maxEntries && !this.#store.has(state)) {
|
|
85
|
+
this.#sweep();
|
|
86
|
+
}
|
|
87
|
+
if (this.#store.size >= this.#maxEntries && !this.#store.has(state)) {
|
|
88
|
+
// Refused rather than evicted: making room by dropping someone
|
|
89
|
+
// else's pending ceremony is how a flood locks a real user out.
|
|
90
|
+
throw new WardenError("E_WARDEN_WEBAUTHN_STORE_FULL", `The in-memory WebAuthn challenge store is full (${this.#maxEntries} pending ceremonies).`, {
|
|
91
|
+
hint: "Rate-limit the endpoint that starts a ceremony, or move to a persistent store with its own TTL.",
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
this.#store.set(state, {
|
|
95
|
+
...ceremony,
|
|
96
|
+
expiresAt: Date.now() + this.#ttlMs,
|
|
97
|
+
});
|
|
98
|
+
if (this.#store.size > this.#sweepAt)
|
|
99
|
+
this.#sweep();
|
|
27
100
|
}
|
|
28
|
-
|
|
29
|
-
|
|
101
|
+
/** Drop everything already expired. */
|
|
102
|
+
#sweep() {
|
|
103
|
+
const now = Date.now();
|
|
104
|
+
for (const [state, entry] of this.#store) {
|
|
105
|
+
if (entry.expiresAt < now)
|
|
106
|
+
this.#store.delete(state);
|
|
107
|
+
}
|
|
108
|
+
this.#sweepAt = Math.max(64, this.#store.size * 2);
|
|
30
109
|
}
|
|
31
110
|
async take(state) {
|
|
32
111
|
const entry = this.#store.get(state);
|
|
33
112
|
this.#store.delete(state); // single-use regardless of freshness
|
|
34
|
-
|
|
113
|
+
// `<=`, not `<`: an entry whose deadline is exactly now has expired.
|
|
114
|
+
// Strictly-less left it valid for the remainder of that millisecond.
|
|
115
|
+
if (!entry || entry.expiresAt <= Date.now()) {
|
|
35
116
|
return null;
|
|
36
117
|
}
|
|
37
|
-
return entry.challenge;
|
|
118
|
+
return { challenge: entry.challenge, userId: entry.userId };
|
|
38
119
|
}
|
|
39
120
|
}
|
|
40
121
|
export class MemoryWebauthnCredentialStore {
|
|
@@ -48,11 +129,24 @@ export class MemoryWebauthnCredentialStore {
|
|
|
48
129
|
async findByUser(userId) {
|
|
49
130
|
return [...this.#store.values()].filter((p) => p.userId === userId);
|
|
50
131
|
}
|
|
51
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Atomic here for free: a synchronous read, compare and write with no
|
|
134
|
+
* `await` between them cannot be interleaved on a single-threaded runtime.
|
|
135
|
+
* Written as one block on purpose — an `await` inside would silently
|
|
136
|
+
* reintroduce the race this exists to close.
|
|
137
|
+
*/
|
|
138
|
+
async advanceCounter(id, expected, next) {
|
|
52
139
|
const p = this.#store.get(id);
|
|
53
|
-
if (p)
|
|
54
|
-
|
|
55
|
-
|
|
140
|
+
if (!p || p.counter !== expected)
|
|
141
|
+
return false;
|
|
142
|
+
// Defence in depth: this method ADVANCES. A caller that has already
|
|
143
|
+
// decided the assertion is good can still have decided it wrongly, and
|
|
144
|
+
// a store that writes whatever it is handed turns one bad decision into
|
|
145
|
+
// a permanently disarmed counter.
|
|
146
|
+
if (next < expected)
|
|
147
|
+
return false;
|
|
148
|
+
this.#store.set(id, { ...p, counter: next });
|
|
149
|
+
return true;
|
|
56
150
|
}
|
|
57
151
|
}
|
|
58
152
|
const DEFAULT_ALGS = [-7, -257, -8];
|
|
@@ -116,7 +210,9 @@ export class WebauthnProvider {
|
|
|
116
210
|
},
|
|
117
211
|
};
|
|
118
212
|
const state = randomBytes(16).toString("hex");
|
|
119
|
-
|
|
213
|
+
// The identity travels WITH the challenge. Stored apart, `finish` had to
|
|
214
|
+
// trust whatever `userId` the caller handed back.
|
|
215
|
+
await this.#challenges.save(state, { challenge, userId: user.id });
|
|
120
216
|
return { options, state };
|
|
121
217
|
}
|
|
122
218
|
/**
|
|
@@ -124,35 +220,65 @@ export class WebauthnProvider {
|
|
|
124
220
|
* the stored challenge and persist the new passkey on success.
|
|
125
221
|
*/
|
|
126
222
|
async finishRegistration(state, userId, response) {
|
|
127
|
-
const
|
|
128
|
-
if (!
|
|
129
|
-
return { verified: false };
|
|
130
|
-
}
|
|
131
|
-
if (!this.#validClientData(response.response.clientDataJSON, "webauthn.create", expectedChallenge)) {
|
|
223
|
+
const ceremony = await this.#challenges.take(state);
|
|
224
|
+
if (!ceremony) {
|
|
132
225
|
return { verified: false };
|
|
133
226
|
}
|
|
134
|
-
|
|
135
|
-
|
|
227
|
+
// The passkey is registered for the user who STARTED the ceremony, not
|
|
228
|
+
// for whoever the caller names now. A mismatch is an integration reading
|
|
229
|
+
// the id back from the request, or a session that changed between the
|
|
230
|
+
// two steps; either way it would attach a working credential to the
|
|
231
|
+
// wrong account, so it is refused rather than resolved in someone's
|
|
232
|
+
// favour.
|
|
233
|
+
if (ceremony.userId !== undefined && ceremony.userId !== userId) {
|
|
136
234
|
return { verified: false };
|
|
137
235
|
}
|
|
138
|
-
|
|
139
|
-
if (!Buffer.isBuffer(authDataRaw)) {
|
|
236
|
+
if (!this.#validClientData(response.response.clientDataJSON, "webauthn.create", ceremony.challenge)) {
|
|
140
237
|
return { verified: false };
|
|
141
238
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
239
|
+
// The payload is attacker-supplied: a truncated attestation, a malformed
|
|
240
|
+
// COSE key or authenticator data that stops mid-structure are INPUT
|
|
241
|
+
// errors, and this method promises `{ verified: false }` for an invalid
|
|
242
|
+
// response. Letting the decoder's exception out turned a bad request
|
|
243
|
+
// into a 500 — and a 500 tells whoever sent it that they reached
|
|
244
|
+
// something that did not expect them.
|
|
245
|
+
//
|
|
246
|
+
// Only the parsing is wrapped: a store that cannot be read is
|
|
247
|
+
// infrastructure, and swallowing that would report "not verified" for an
|
|
248
|
+
// outage.
|
|
249
|
+
const parsed = parsePayload(() => {
|
|
250
|
+
const attestation = decodeCbor(base64urlToBuffer(response.response.attestationObject)).value;
|
|
251
|
+
if (!(attestation instanceof Map))
|
|
252
|
+
return null;
|
|
253
|
+
const authDataRaw = attestation.get("authData");
|
|
254
|
+
if (!Buffer.isBuffer(authDataRaw))
|
|
255
|
+
return null;
|
|
256
|
+
const authData = parseAuthenticatorData(authDataRaw);
|
|
257
|
+
if (!this.#validAuthenticator(authData) ||
|
|
258
|
+
!authData.cosePublicKey ||
|
|
259
|
+
!authData.cosePublicKeyBytes ||
|
|
260
|
+
!authData.credentialId) {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
// The narrowing is carried OUT of the closure: the guard above proved
|
|
264
|
+
// these three are present, and the caller cannot see that through an
|
|
265
|
+
// object literal typed from `authData` alone.
|
|
266
|
+
return {
|
|
267
|
+
credentialId: authData.credentialId,
|
|
268
|
+
publicKeyBytes: authData.cosePublicKeyBytes,
|
|
269
|
+
signCount: authData.signCount,
|
|
270
|
+
alg: coseToKeyObject(authData.cosePublicKey).alg,
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
if (parsed === null) {
|
|
147
274
|
return { verified: false };
|
|
148
275
|
}
|
|
149
|
-
const { alg } = coseToKeyObject(authData.cosePublicKey);
|
|
150
276
|
await this.#credentials.save({
|
|
151
|
-
id: bufferToBase64url(
|
|
277
|
+
id: bufferToBase64url(parsed.credentialId),
|
|
152
278
|
userId,
|
|
153
|
-
publicKey: bufferToBase64url(
|
|
154
|
-
alg,
|
|
155
|
-
counter:
|
|
279
|
+
publicKey: bufferToBase64url(parsed.publicKeyBytes),
|
|
280
|
+
alg: parsed.alg,
|
|
281
|
+
counter: parsed.signCount,
|
|
156
282
|
transports: response.response.transports,
|
|
157
283
|
});
|
|
158
284
|
return { verified: true };
|
|
@@ -178,7 +304,10 @@ export class WebauthnProvider {
|
|
|
178
304
|
: undefined,
|
|
179
305
|
};
|
|
180
306
|
const state = randomBytes(16).toString("hex");
|
|
181
|
-
|
|
307
|
+
// `userId` is optional here: a usernameless login is named by the
|
|
308
|
+
// credential itself. When it IS given, storing it lets `finish` refuse an
|
|
309
|
+
// assertion from someone else's passkey.
|
|
310
|
+
await this.#challenges.save(state, { challenge, userId });
|
|
182
311
|
return { options, state };
|
|
183
312
|
}
|
|
184
313
|
/**
|
|
@@ -187,36 +316,67 @@ export class WebauthnProvider {
|
|
|
187
316
|
* Returns the owning `userId` on success.
|
|
188
317
|
*/
|
|
189
318
|
async finishAuthentication(state, response) {
|
|
190
|
-
const
|
|
191
|
-
if (!
|
|
319
|
+
const ceremony = await this.#challenges.take(state);
|
|
320
|
+
if (!ceremony) {
|
|
192
321
|
return { verified: false };
|
|
193
322
|
}
|
|
194
|
-
if (!this.#validClientData(response.response.clientDataJSON, "webauthn.get",
|
|
323
|
+
if (!this.#validClientData(response.response.clientDataJSON, "webauthn.get", ceremony.challenge)) {
|
|
195
324
|
return { verified: false };
|
|
196
325
|
}
|
|
197
326
|
const stored = await this.#credentials.findById(response.id);
|
|
198
327
|
if (!stored) {
|
|
199
328
|
return { verified: false };
|
|
200
329
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
330
|
+
// When the ceremony named a user, the asserted credential has to be
|
|
331
|
+
// theirs. `allowCredentials` is a HINT the browser may ignore and an
|
|
332
|
+
// attacker simply will: without this, a login started for one account
|
|
333
|
+
// could be completed with a passkey belonging to another.
|
|
334
|
+
if (ceremony.userId !== undefined && stored.userId !== ceremony.userId) {
|
|
335
|
+
return { verified: false };
|
|
336
|
+
}
|
|
337
|
+
// Same reasoning as registration: the payload is attacker-supplied, and
|
|
338
|
+
// this method promises `{ verified: false }` for an invalid one. Only
|
|
339
|
+
// the parsing and the signature check are wrapped — a store failure is
|
|
340
|
+
// infrastructure and must not be reported as "not verified".
|
|
341
|
+
const checked = parsePayload(() => {
|
|
342
|
+
const authDataRaw = base64urlToBuffer(response.response.authenticatorData);
|
|
343
|
+
const authData = parseAuthenticatorData(authDataRaw);
|
|
344
|
+
if (!this.#validAuthenticator(authData))
|
|
345
|
+
return null;
|
|
346
|
+
// Signed payload = authenticatorData ‖ SHA-256(clientDataJSON).
|
|
347
|
+
const clientHash = sha256(base64urlToBuffer(response.response.clientDataJSON));
|
|
348
|
+
const signedData = Buffer.concat([authDataRaw, clientHash]);
|
|
349
|
+
const { key } = coseToKeyObject(expectCoseMap(base64urlToBuffer(stored.publicKey)));
|
|
350
|
+
const ok = verifyWebauthnSignature(stored.alg, key, signedData, base64urlToBuffer(response.response.signature));
|
|
351
|
+
return ok ? { authData } : null;
|
|
352
|
+
});
|
|
353
|
+
if (checked === null) {
|
|
204
354
|
return { verified: false };
|
|
205
355
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
356
|
+
const { authData } = checked;
|
|
357
|
+
// Replay guard, as the spec words it: run the comparison when EITHER
|
|
358
|
+
// counter is nonzero, then refuse anything that does not strictly
|
|
359
|
+
// advance. Authenticators that always report 0 — many platform passkeys
|
|
360
|
+
// — stay exempt, because that is the case where both sides are zero.
|
|
361
|
+
//
|
|
362
|
+
// The guard used to test the incoming counter alone, so a credential
|
|
363
|
+
// sitting at 42 accepted an assertion reporting 0 and then WROTE 0
|
|
364
|
+
// back. That is worse than admitting one assertion: it erases the
|
|
365
|
+
// stored counter, and with it the clone detection for every assertion
|
|
366
|
+
// after it.
|
|
367
|
+
if ((authData.signCount !== 0 || stored.counter !== 0) &&
|
|
368
|
+
authData.signCount <= stored.counter) {
|
|
212
369
|
return { verified: false };
|
|
213
370
|
}
|
|
214
|
-
//
|
|
215
|
-
// that
|
|
216
|
-
|
|
371
|
+
// The write is what decides. Comparing here and writing unconditionally
|
|
372
|
+
// let a second assertion that read the same old counter also pass, and
|
|
373
|
+
// whichever wrote last set the stored value — possibly backwards.
|
|
374
|
+
// Refusing when the counter has moved under us costs a legitimate user
|
|
375
|
+
// one retry and costs a clone the whole attack.
|
|
376
|
+
const advanced = await this.#credentials.advanceCounter(stored.id, stored.counter, authData.signCount);
|
|
377
|
+
if (!advanced) {
|
|
217
378
|
return { verified: false };
|
|
218
379
|
}
|
|
219
|
-
await this.#credentials.updateCounter(stored.id, authData.signCount);
|
|
220
380
|
return { verified: true, userId: stored.userId };
|
|
221
381
|
}
|
|
222
382
|
/** Validate clientDataJSON: type, challenge match, and origin allow-list. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebauthnProvider.js","sourceRoot":"","sources":["../../src/mfa/WebauthnProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EACN,iBAAiB,EACjB,iBAAiB,EAEjB,eAAe,EACf,UAAU,EACV,sBAAsB,EACtB,MAAM,EACN,uBAAuB,GACvB,MAAM,qBAAqB,CAAC;AA4F7B,MAAM,OAAO,4BAA4B;IACxC,MAAM,GAAG,IAAI,GAAG,EAAoD,CAAC;IAC5D,MAAM,CAAS;IAExB,kEAAkE;IAClE,YAAY,KAAK,GAAG,OAAO;QAC1B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,KAAa,EAAE,SAAiB;QAC1C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5E,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,KAAa;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,CAAC,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Q;YACjC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CACrC,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,QAGnB;QACA,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;QACjE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;YACvE,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,IAAI,CAAC,iBAAiB,KAAK,UAAU,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC;YACjE,OAAO,KAAK,CAAC;QACd,CAAC;QACD,OAAO,IAAI,CAAC;IACb,CAAC;CACD;AAED,SAAS,aAAa,CAAC,KAAa;IACnC,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC;IACxC,IAAI,CAAC,CAAC,OAAO,YAAY,GAAG,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,WAAW,CAAC,oBAAoB,EAAE,8BAA8B,CAAC,CAAC;IAC7E,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
|
Binary file
|
package/package.json
CHANGED
package/src/mfa/OtpProvider.ts
CHANGED
|
@@ -31,17 +31,105 @@ export interface OtpChallenge {
|
|
|
31
31
|
attempts: number;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Storage for pending challenges.
|
|
36
|
+
*
|
|
37
|
+
* {@link take} is what makes a one-time code one-time. Verification used to be
|
|
38
|
+
* `find` then compare then `delete` — three operations with `await` between
|
|
39
|
+
* them — so two requests carrying the same correct code both read the challenge
|
|
40
|
+
* before either removed it, and both were told `ok`. The attempt budget had the
|
|
41
|
+
* same shape: concurrent wrong guesses each read `attempts: 0` and wrote `1`,
|
|
42
|
+
* so twenty of them burned one attempt. A store backed by a network widens that
|
|
43
|
+
* window from microseconds to a round trip.
|
|
44
|
+
*
|
|
45
|
+
* A custom store MUST implement `take` as a single indivisible operation:
|
|
46
|
+
* `DELETE ... RETURNING` on SQL, `GETDEL` or a Lua script on Redis, a
|
|
47
|
+
* compare-and-set on anything else. Handing back a copy and deleting separately
|
|
48
|
+
* reopens the hole this exists to close.
|
|
49
|
+
*/
|
|
35
50
|
export interface OtpChallengeStore {
|
|
36
51
|
save(challenge: OtpChallenge): Promise<void>;
|
|
37
52
|
find(id: string): Promise<OtpChallenge | null>;
|
|
38
53
|
delete(id: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Atomically remove the challenge and return what was removed.
|
|
56
|
+
*
|
|
57
|
+
* At most one concurrent caller can be handed a given challenge; every
|
|
58
|
+
* other gets `null`. Whoever holds it decides what happens next — spend it,
|
|
59
|
+
* or put it back with one more attempt recorded.
|
|
60
|
+
*/
|
|
61
|
+
take(id: string): Promise<OtpChallenge | null>;
|
|
39
62
|
}
|
|
40
63
|
|
|
41
64
|
export class MemoryOtpChallengeStore implements OtpChallengeStore {
|
|
42
65
|
#store = new Map<string, OtpChallenge>();
|
|
66
|
+
/**
|
|
67
|
+
* Size at which the next sweep runs, doubling each time.
|
|
68
|
+
*
|
|
69
|
+
* Entries only ever left when their id was reused or taken, so a challenge
|
|
70
|
+
* nobody came back for — a code requested and never submitted, which is the
|
|
71
|
+
* ordinary abandoned login — stayed for the life of the process. Sweeping on
|
|
72
|
+
* write keeps it bounded without a timer: a timer would hold the event loop
|
|
73
|
+
* open and need a disposal contract this store does not have.
|
|
74
|
+
*
|
|
75
|
+
* Doubling makes the cost amortised O(1) per save rather than a scan on
|
|
76
|
+
* every one, and the floor keeps a small deployment from sweeping constantly.
|
|
77
|
+
*/
|
|
78
|
+
#sweepAt = 64;
|
|
79
|
+
readonly #maxEntries: number;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param maxEntries How many pending challenges this process will hold.
|
|
83
|
+
* Sweeping only removes EXPIRED entries, so a flood of still-valid ones —
|
|
84
|
+
* an unauthenticated endpoint being hammered — grew the map without any
|
|
85
|
+
* ceiling. Default 10 000, which is far above a real login rate and far
|
|
86
|
+
* below a memory problem.
|
|
87
|
+
*/
|
|
88
|
+
constructor(maxEntries = 10_000) {
|
|
89
|
+
// A ceiling of 0, a negative, NaN or Infinity is not a ceiling: the
|
|
90
|
+
// first two refuse every write and the last two disable the bound this
|
|
91
|
+
// exists to provide, silently. A caller asking for one of those means
|
|
92
|
+
// something, and none of the meanings is what would have happened.
|
|
93
|
+
if (!Number.isInteger(maxEntries) || maxEntries < 1) {
|
|
94
|
+
throw new WardenError(
|
|
95
|
+
"E_WARDEN_STORE_LIMIT_INVALID",
|
|
96
|
+
`maxEntries must be a positive integer, got ${String(maxEntries)}.`,
|
|
97
|
+
{
|
|
98
|
+
hint: "Pass the number of pending entries this process may hold — the default is 10_000.",
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
this.#maxEntries = maxEntries;
|
|
103
|
+
}
|
|
104
|
+
|
|
43
105
|
async save(c: OtpChallenge): Promise<void> {
|
|
106
|
+
if (this.#store.size >= this.#maxEntries && !this.#store.has(c.id)) {
|
|
107
|
+
// Swept first: the ceiling is about live challenges, not stale ones.
|
|
108
|
+
this.#sweep();
|
|
109
|
+
}
|
|
110
|
+
if (this.#store.size >= this.#maxEntries && !this.#store.has(c.id)) {
|
|
111
|
+
// REFUSED, not evicted. Evicting to make room lets whoever is
|
|
112
|
+
// flooding push a legitimate user's challenge out and lock them out;
|
|
113
|
+
// refusing the new one fails the attacker instead.
|
|
114
|
+
throw new WardenError(
|
|
115
|
+
"E_WARDEN_OTP_STORE_FULL",
|
|
116
|
+
`The in-memory OTP store is full (${this.#maxEntries} pending challenges).`,
|
|
117
|
+
{
|
|
118
|
+
hint: "Rate-limit the endpoint that mints codes, or move to a persistent store with its own TTL.",
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
}
|
|
44
122
|
this.#store.set(c.id, c);
|
|
123
|
+
if (this.#store.size > this.#sweepAt) this.#sweep();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Drop everything already expired. */
|
|
127
|
+
#sweep(): void {
|
|
128
|
+
const now = Date.now();
|
|
129
|
+
for (const [id, challenge] of this.#store) {
|
|
130
|
+
if (challenge.expiresAt < now) this.#store.delete(id);
|
|
131
|
+
}
|
|
132
|
+
this.#sweepAt = Math.max(64, this.#store.size * 2);
|
|
45
133
|
}
|
|
46
134
|
async find(id: string): Promise<OtpChallenge | null> {
|
|
47
135
|
return this.#store.get(id) ?? null;
|
|
@@ -49,6 +137,17 @@ export class MemoryOtpChallengeStore implements OtpChallengeStore {
|
|
|
49
137
|
async delete(id: string): Promise<void> {
|
|
50
138
|
this.#store.delete(id);
|
|
51
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Atomic here for free: a synchronous read-and-remove with no `await`
|
|
142
|
+
* between the two cannot be interleaved on a single-threaded runtime.
|
|
143
|
+
* Written as one statement pair on purpose — adding an `await` inside would
|
|
144
|
+
* silently reintroduce the race.
|
|
145
|
+
*/
|
|
146
|
+
async take(id: string): Promise<OtpChallenge | null> {
|
|
147
|
+
const challenge = this.#store.get(id) ?? null;
|
|
148
|
+
this.#store.delete(id);
|
|
149
|
+
return challenge;
|
|
150
|
+
}
|
|
52
151
|
}
|
|
53
152
|
|
|
54
153
|
export interface OtpConfig {
|
|
@@ -88,6 +187,21 @@ export class OtpProvider {
|
|
|
88
187
|
readonly #maxAttempts: number;
|
|
89
188
|
|
|
90
189
|
constructor(config: OtpConfig) {
|
|
190
|
+
// `take` became part of the contract in 0.2.0 because a one-time code
|
|
191
|
+
// cannot be one-time without it. A store written against the old
|
|
192
|
+
// interface would otherwise fail at the first verification with
|
|
193
|
+
// "take is not a function" — during a login, far from the cause — so it
|
|
194
|
+
// is refused here, where the fix is obvious.
|
|
195
|
+
const store = config.store;
|
|
196
|
+
if (store !== undefined && typeof store.take !== "function") {
|
|
197
|
+
throw new WardenError(
|
|
198
|
+
"E_WARDEN_OTP_STORE_CONTRACT",
|
|
199
|
+
"The OTP challenge store does not implement take().",
|
|
200
|
+
{
|
|
201
|
+
hint: "take(id) must remove the challenge AND return it in ONE indivisible operation — `DELETE ... RETURNING` on SQL, GETDEL or a Lua script on Redis. Reading then deleting separately lets two requests with the same correct code both succeed.",
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
}
|
|
91
205
|
if (!config?.channel) {
|
|
92
206
|
throw new WardenError(
|
|
93
207
|
"INVALID_CONFIG",
|
|
@@ -144,7 +258,16 @@ export class OtpProvider {
|
|
|
144
258
|
expiresAt,
|
|
145
259
|
attempts: 0,
|
|
146
260
|
});
|
|
147
|
-
|
|
261
|
+
// Saved BEFORE the send, so a code that arrives can always be verified —
|
|
262
|
+
// but a send that fails must not leave the challenge behind. It could
|
|
263
|
+
// never be used (nobody has the code) and never be swept before its TTL,
|
|
264
|
+
// so an SMS gateway having a bad hour quietly filled the store.
|
|
265
|
+
try {
|
|
266
|
+
await this.#channel.send(recipient, code);
|
|
267
|
+
} catch (error) {
|
|
268
|
+
await this.#store.delete(id);
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
148
271
|
return { challengeId: id, expiresAt };
|
|
149
272
|
}
|
|
150
273
|
|
|
@@ -157,23 +280,27 @@ export class OtpProvider {
|
|
|
157
280
|
code: string,
|
|
158
281
|
nowMs: number = Date.now(),
|
|
159
282
|
): Promise<OtpVerification> {
|
|
160
|
-
|
|
283
|
+
// TAKE, do not read. The challenge leaves the store before anything is
|
|
284
|
+
// compared, so a second request racing this one finds nothing and is
|
|
285
|
+
// refused — whatever the outcome here. Every path below either keeps it
|
|
286
|
+
// removed or puts it back deliberately.
|
|
287
|
+
const challenge = await this.#store.take(challengeId);
|
|
161
288
|
if (!challenge) {
|
|
162
289
|
return { ok: false, reason: "not_found" };
|
|
163
290
|
}
|
|
164
291
|
if (nowMs > challenge.expiresAt) {
|
|
165
|
-
await this.#store.delete(challengeId);
|
|
166
292
|
return { ok: false, reason: "expired" };
|
|
167
293
|
}
|
|
168
294
|
if (matchesStored(challenge.hash, code.replace(/\s/g, ""))) {
|
|
169
|
-
await this.#store.delete(challengeId);
|
|
170
295
|
return { ok: true };
|
|
171
296
|
}
|
|
172
297
|
const attempts = challenge.attempts + 1;
|
|
173
298
|
if (attempts >= this.#maxAttempts) {
|
|
174
|
-
await this.#store.delete(challengeId);
|
|
175
299
|
return { ok: false, reason: "too_many_attempts" };
|
|
176
300
|
}
|
|
301
|
+
// Wrong, but budget remains: put it back with the attempt recorded.
|
|
302
|
+
// Holding it for the length of the comparison is also what makes the
|
|
303
|
+
// counter reliable — no two callers can be incrementing the same value.
|
|
177
304
|
await this.#store.save({ ...challenge, attempts });
|
|
178
305
|
return { ok: false, reason: "mismatch" };
|
|
179
306
|
}
|