@proteinjs/user-server 1.22.1 → 1.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/generated/index.js +1 -1
  3. package/dist/generated/index.js.map +1 -1
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +9 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/src/authentication/PasswordHasher.d.ts +9 -0
  9. package/dist/src/authentication/PasswordHasher.d.ts.map +1 -1
  10. package/dist/src/authentication/PasswordHasher.js +25 -0
  11. package/dist/src/authentication/PasswordHasher.js.map +1 -1
  12. package/dist/src/authentication/authenticate.d.ts +15 -0
  13. package/dist/src/authentication/authenticate.d.ts.map +1 -1
  14. package/dist/src/authentication/authenticate.js +55 -20
  15. package/dist/src/authentication/authenticate.js.map +1 -1
  16. package/dist/src/routes/executePasswordReset.d.ts +4 -0
  17. package/dist/src/routes/executePasswordReset.d.ts.map +1 -1
  18. package/dist/src/routes/executePasswordReset.js +7 -0
  19. package/dist/src/routes/executePasswordReset.js.map +1 -1
  20. package/dist/src/routes/initiatePasswordReset.d.ts +7 -0
  21. package/dist/src/routes/initiatePasswordReset.d.ts.map +1 -1
  22. package/dist/src/routes/initiatePasswordReset.js +28 -10
  23. package/dist/src/routes/initiatePasswordReset.js.map +1 -1
  24. package/dist/src/routes/login.d.ts +8 -0
  25. package/dist/src/routes/login.d.ts.map +1 -1
  26. package/dist/src/routes/login.js +63 -22
  27. package/dist/src/routes/login.js.map +1 -1
  28. package/dist/src/throttle/ClientAddress.d.ts +35 -0
  29. package/dist/src/throttle/ClientAddress.d.ts.map +1 -0
  30. package/dist/src/throttle/ClientAddress.js +48 -0
  31. package/dist/src/throttle/ClientAddress.js.map +1 -0
  32. package/dist/src/throttle/PasswordResetThrottle.d.ts +39 -0
  33. package/dist/src/throttle/PasswordResetThrottle.d.ts.map +1 -0
  34. package/dist/src/throttle/PasswordResetThrottle.js +59 -0
  35. package/dist/src/throttle/PasswordResetThrottle.js.map +1 -0
  36. package/dist/src/throttle/RequestDigests.d.ts +42 -0
  37. package/dist/src/throttle/RequestDigests.d.ts.map +1 -0
  38. package/dist/src/throttle/RequestDigests.js +96 -0
  39. package/dist/src/throttle/RequestDigests.js.map +1 -0
  40. package/dist/src/throttle/SignInThrottle.d.ts +67 -0
  41. package/dist/src/throttle/SignInThrottle.d.ts.map +1 -0
  42. package/dist/src/throttle/SignInThrottle.js +91 -0
  43. package/dist/src/throttle/SignInThrottle.js.map +1 -0
  44. package/dist/src/throttle/SlidingWindow.d.ts +51 -0
  45. package/dist/src/throttle/SlidingWindow.d.ts.map +1 -0
  46. package/dist/src/throttle/SlidingWindow.js +85 -0
  47. package/dist/src/throttle/SlidingWindow.js.map +1 -0
  48. package/dist/test/ClientAddress.test.d.ts +2 -0
  49. package/dist/test/ClientAddress.test.d.ts.map +1 -0
  50. package/dist/test/ClientAddress.test.js +51 -0
  51. package/dist/test/ClientAddress.test.js.map +1 -0
  52. package/dist/test/InitiatePasswordReset.test.js +373 -5
  53. package/dist/test/InitiatePasswordReset.test.js.map +1 -1
  54. package/dist/test/LoginThrottle.integration.test.d.ts +2 -0
  55. package/dist/test/LoginThrottle.integration.test.d.ts.map +1 -0
  56. package/dist/test/LoginThrottle.integration.test.js +616 -0
  57. package/dist/test/LoginThrottle.integration.test.js.map +1 -0
  58. package/dist/test/RequestDigests.test.d.ts +2 -0
  59. package/dist/test/RequestDigests.test.d.ts.map +1 -0
  60. package/dist/test/RequestDigests.test.js +73 -0
  61. package/dist/test/RequestDigests.test.js.map +1 -0
  62. package/dist/test/SlidingWindow.test.d.ts +2 -0
  63. package/dist/test/SlidingWindow.test.d.ts.map +1 -0
  64. package/dist/test/SlidingWindow.test.js +72 -0
  65. package/dist/test/SlidingWindow.test.js.map +1 -0
  66. package/generated/index.ts +1 -1
  67. package/index.ts +5 -0
  68. package/package.json +2 -2
  69. package/src/authentication/PasswordHasher.ts +18 -1
  70. package/src/authentication/authenticate.ts +36 -9
  71. package/src/routes/executePasswordReset.ts +7 -0
  72. package/src/routes/initiatePasswordReset.ts +29 -11
  73. package/src/routes/login.ts +39 -8
  74. package/src/throttle/ClientAddress.ts +47 -0
  75. package/src/throttle/PasswordResetThrottle.ts +62 -0
  76. package/src/throttle/RequestDigests.ts +97 -0
  77. package/src/throttle/SignInThrottle.ts +98 -0
  78. package/src/throttle/SlidingWindow.ts +90 -0
  79. package/test/ClientAddress.test.ts +54 -0
  80. package/test/InitiatePasswordReset.test.ts +212 -6
  81. package/test/LoginThrottle.integration.test.ts +339 -0
  82. package/test/RequestDigests.test.ts +81 -0
  83. package/test/SlidingWindow.test.ts +85 -0
  84. package/test/setup.js +2 -0
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientAddress = void 0;
4
+ /**
5
+ * The client's address, as a throttle should key on it.
6
+ *
7
+ * Behind Google's external Application Load Balancer the `X-Forwarded-For` header ENDS with the
8
+ * two entries the balancer appends — `<client-ip>,<load-balancer-ip>` — and everything before
9
+ * them is whatever the client itself sent. So the client is the second entry from the right:
10
+ * - never the LAST entry: that is the balancer's own forwarding-rule address, the same for every
11
+ * visitor. It is what express's `request.ip` resolves to under the server's `trust proxy 1`
12
+ * (one trusted hop: the Google front end that opened the connection), so a throttle keyed on
13
+ * `request.ip` there is one bucket for the whole world;
14
+ * - never the FIRST entry: a client can send its own header, and the balancer keeps it.
15
+ *
16
+ * Only when the server trusts a proxy (the deployed shape; `trust proxy` is unset in
17
+ * development) is the header read at all. A request that did not come through the balancer —
18
+ * no appended pair: an in-cluster call, a probe — and every request in development key on the
19
+ * connection's own address.
20
+ */
21
+ var ClientAddress = /** @class */ (function () {
22
+ function ClientAddress() {
23
+ }
24
+ ClientAddress.prototype.of = function (request) {
25
+ var _a, _b, _c, _d;
26
+ if ((_b = (_a = request.app) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(_a, 'trust proxy')) {
27
+ var entries = this.forwardedFor(request);
28
+ if (entries.length >= ClientAddress.BALANCER_APPENDED_ENTRIES) {
29
+ return entries[entries.length - ClientAddress.BALANCER_APPENDED_ENTRIES];
30
+ }
31
+ }
32
+ return (_d = (_c = request.socket) === null || _c === void 0 ? void 0 : _c.remoteAddress) !== null && _d !== void 0 ? _d : '';
33
+ };
34
+ ClientAddress.prototype.forwardedFor = function (request) {
35
+ var _a;
36
+ var header = (_a = request.headers) === null || _a === void 0 ? void 0 : _a['x-forwarded-for'];
37
+ var value = Array.isArray(header) ? header.join(',') : header !== null && header !== void 0 ? header : '';
38
+ return value
39
+ .split(',')
40
+ .map(function (entry) { return entry.trim(); })
41
+ .filter(function (entry) { return entry.length > 0; });
42
+ };
43
+ /** How many entries the load balancer appends to the header: the client, then itself. */
44
+ ClientAddress.BALANCER_APPENDED_ENTRIES = 2;
45
+ return ClientAddress;
46
+ }());
47
+ exports.ClientAddress = ClientAddress;
48
+ //# sourceMappingURL=ClientAddress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClientAddress.js","sourceRoot":"","sources":["../../../src/throttle/ClientAddress.ts"],"names":[],"mappings":";;;AAOA;;;;;;;;;;;;;;;;GAgBG;AACH;IAAA;IAsBA,CAAC;IAlBC,0BAAE,GAAF,UAAG,OAAsB;;QACvB,IAAI,MAAA,MAAA,OAAO,CAAC,GAAG,0CAAE,GAAG,mDAAG,aAAa,CAAC,EAAE;YACrC,IAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;YAC3C,IAAI,OAAO,CAAC,MAAM,IAAI,aAAa,CAAC,yBAAyB,EAAE;gBAC7D,OAAO,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,aAAa,CAAC,yBAAyB,CAAC,CAAC;aAC1E;SACF;QACD,OAAO,MAAA,MAAA,OAAO,CAAC,MAAM,0CAAE,aAAa,mCAAI,EAAE,CAAC;IAC7C,CAAC;IAEO,oCAAY,GAApB,UAAqB,OAAsB;;QACzC,IAAM,MAAM,GAAG,MAAA,OAAO,CAAC,OAAO,0CAAG,iBAAiB,CAAC,CAAC;QACpD,IAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,EAAE,CAAC;QACtE,OAAO,KAAK;aACT,KAAK,CAAC,GAAG,CAAC;aACV,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,IAAI,EAAE,EAAZ,CAAY,CAAC;aAC5B,MAAM,CAAC,UAAC,KAAK,IAAK,OAAA,KAAK,CAAC,MAAM,GAAG,CAAC,EAAhB,CAAgB,CAAC,CAAC;IACzC,CAAC;IApBD,yFAAyF;IACjE,uCAAyB,GAAG,CAAC,CAAC;IAoBxD,oBAAC;CAAA,AAtBD,IAsBC;AAtBY,sCAAa"}
@@ -0,0 +1,39 @@
1
+ import { ThrottleWindow } from './SignInThrottle';
2
+ /**
3
+ * The password-reset door's two windows (`POST /user/initiate-password-reset`), keyed by digests
4
+ * (`RequestDigests`), never by an address. Both count every request, whether or not the address
5
+ * has an account — a window that only counted real accounts would itself say which addresses
6
+ * have one. Past either window nothing is looked up, minted or mailed; the door's answer is the
7
+ * same sentence as always (see the route).
8
+ *
9
+ * These sit on top of the account row's own five-minute gap between links, which spans replicas
10
+ * (it is stored); the windows are in process memory (`SlidingWindow`): per replica. A
11
+ * deployment of three replicas behind a load balancer (up to ten under load) keeps three sets
12
+ * of windows, so its effective ceiling is ~3× the numbers below (up to ~10×) — for one address
13
+ * still at most one mail per five minutes. Friction, not the wall — a shared store is a
14
+ * separate step.
15
+ */
16
+ export declare class PasswordResetThrottle {
17
+ /**
18
+ * Per client: 10 requests an hour. Someone asking for their own address again, or a household
19
+ * asking for a few addresses, passes; a script sweeping addresses stops at ten.
20
+ */
21
+ private static readonly CLIENT_LIMIT;
22
+ private static readonly CLIENT_WINDOW_MS;
23
+ /**
24
+ * Per address: 3 requests an hour. A person whose mail is slow asks once or twice more; beyond
25
+ * that it is mail aimed at somebody's inbox.
26
+ */
27
+ private static readonly ACCOUNT_LIMIT;
28
+ private static readonly ACCOUNT_WINDOW_MS;
29
+ private readonly clients;
30
+ private readonly accounts;
31
+ constructor(options?: {
32
+ now?: () => number;
33
+ });
34
+ /** Count this request and answer which window refuses it, if any. */
35
+ admit(client: string, account: string): ThrottleWindow | undefined;
36
+ }
37
+ /** The process-wide windows the reset door shares — they must span requests. */
38
+ export declare const passwordResetThrottle: PasswordResetThrottle;
39
+ //# sourceMappingURL=PasswordResetThrottle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PasswordResetThrottle.d.ts","sourceRoot":"","sources":["../../../src/throttle/PasswordResetThrottle.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;;GAaG;AACH,qBAAa,qBAAqB;IAChC;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAM;IAC1C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IAE1D;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAK;IAC1C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAkB;IAE3D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IACxC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;gBAE7B,OAAO,CAAC,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;KAAE;IAa5C,qEAAqE;IACrE,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;CASnE;AAED,gFAAgF;AAChF,eAAO,MAAM,qBAAqB,uBAA8B,CAAC"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.passwordResetThrottle = exports.PasswordResetThrottle = void 0;
4
+ var SlidingWindow_1 = require("./SlidingWindow");
5
+ /**
6
+ * The password-reset door's two windows (`POST /user/initiate-password-reset`), keyed by digests
7
+ * (`RequestDigests`), never by an address. Both count every request, whether or not the address
8
+ * has an account — a window that only counted real accounts would itself say which addresses
9
+ * have one. Past either window nothing is looked up, minted or mailed; the door's answer is the
10
+ * same sentence as always (see the route).
11
+ *
12
+ * These sit on top of the account row's own five-minute gap between links, which spans replicas
13
+ * (it is stored); the windows are in process memory (`SlidingWindow`): per replica. A
14
+ * deployment of three replicas behind a load balancer (up to ten under load) keeps three sets
15
+ * of windows, so its effective ceiling is ~3× the numbers below (up to ~10×) — for one address
16
+ * still at most one mail per five minutes. Friction, not the wall — a shared store is a
17
+ * separate step.
18
+ */
19
+ var PasswordResetThrottle = /** @class */ (function () {
20
+ function PasswordResetThrottle(options) {
21
+ this.clients = new SlidingWindow_1.SlidingWindow({
22
+ windowMs: PasswordResetThrottle.CLIENT_WINDOW_MS,
23
+ limit: PasswordResetThrottle.CLIENT_LIMIT,
24
+ now: options === null || options === void 0 ? void 0 : options.now,
25
+ });
26
+ this.accounts = new SlidingWindow_1.SlidingWindow({
27
+ windowMs: PasswordResetThrottle.ACCOUNT_WINDOW_MS,
28
+ limit: PasswordResetThrottle.ACCOUNT_LIMIT,
29
+ now: options === null || options === void 0 ? void 0 : options.now,
30
+ });
31
+ }
32
+ /** Count this request and answer which window refuses it, if any. */
33
+ PasswordResetThrottle.prototype.admit = function (client, account) {
34
+ if (this.clients.hit(client)) {
35
+ return 'client';
36
+ }
37
+ if (this.accounts.hit(account)) {
38
+ return 'account';
39
+ }
40
+ return undefined;
41
+ };
42
+ /**
43
+ * Per client: 10 requests an hour. Someone asking for their own address again, or a household
44
+ * asking for a few addresses, passes; a script sweeping addresses stops at ten.
45
+ */
46
+ PasswordResetThrottle.CLIENT_LIMIT = 10;
47
+ PasswordResetThrottle.CLIENT_WINDOW_MS = 60 * 60 * 1000;
48
+ /**
49
+ * Per address: 3 requests an hour. A person whose mail is slow asks once or twice more; beyond
50
+ * that it is mail aimed at somebody's inbox.
51
+ */
52
+ PasswordResetThrottle.ACCOUNT_LIMIT = 3;
53
+ PasswordResetThrottle.ACCOUNT_WINDOW_MS = 60 * 60 * 1000;
54
+ return PasswordResetThrottle;
55
+ }());
56
+ exports.PasswordResetThrottle = PasswordResetThrottle;
57
+ /** The process-wide windows the reset door shares — they must span requests. */
58
+ exports.passwordResetThrottle = new PasswordResetThrottle();
59
+ //# sourceMappingURL=PasswordResetThrottle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PasswordResetThrottle.js","sourceRoot":"","sources":["../../../src/throttle/PasswordResetThrottle.ts"],"names":[],"mappings":";;;AAAA,iDAAgD;AAGhD;;;;;;;;;;;;;GAaG;AACH;IAkBE,+BAAY,OAAgC;QAC1C,IAAI,CAAC,OAAO,GAAG,IAAI,6BAAa,CAAC;YAC/B,QAAQ,EAAE,qBAAqB,CAAC,gBAAgB;YAChD,KAAK,EAAE,qBAAqB,CAAC,YAAY;YACzC,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,GAAG,IAAI,6BAAa,CAAC;YAChC,QAAQ,EAAE,qBAAqB,CAAC,iBAAiB;YACjD,KAAK,EAAE,qBAAqB,CAAC,aAAa;YAC1C,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG;SAClB,CAAC,CAAC;IACL,CAAC;IAED,qEAAqE;IACrE,qCAAK,GAAL,UAAM,MAAc,EAAE,OAAe;QACnC,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;YAC5B,OAAO,QAAQ,CAAC;SACjB;QACD,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YAC9B,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAvCD;;;OAGG;IACqB,kCAAY,GAAG,EAAE,CAAC;IAClB,sCAAgB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAE1D;;;OAGG;IACqB,mCAAa,GAAG,CAAC,CAAC;IAClB,uCAAiB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IA4B7D,4BAAC;CAAA,AAzCD,IAyCC;AAzCY,sDAAqB;AA2ClC,gFAAgF;AACnE,QAAA,qBAAqB,GAAG,IAAI,qBAAqB,EAAE,CAAC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The two keyed digests the throttled doors key their windows on and write on their log lines,
3
+ * in place of what they stand for:
4
+ * - `account(email)` — the ACCOUNT DIGEST: a keyed hash of the trimmed, lowercased address.
5
+ * One address gives one digest however it was typed, on every replica and across restarts, so
6
+ * an operator can say "one account" across lines without the address.
7
+ * - `coarseIp(address)` — the COARSE IP HASH: a keyed hash of the client address at the grain
8
+ * one device holds (an IPv4 address; an IPv6 /64, inside which a device rotates freely), so
9
+ * an operator can say "one device" and a throttle cannot be dodged by rotating inside a /64.
10
+ *
11
+ * Keyed with HMAC-SHA256 under a key DERIVED from the session secret — the one secret every
12
+ * replica already shares, the same keying the invite-request door's IP hash uses; derived per
13
+ * purpose, never used raw, so neither digest weakens the session's own use of it, and the two
14
+ * digests never collide in meaning. Truncated to 64 bits: enough to tell accounts and devices
15
+ * apart, useless to anyone without the key — never the address, never a plain hash a list of
16
+ * addresses could reverse.
17
+ *
18
+ * The key is the deployment's `SESSION_SECRET` unless the constructor is given one (the tests).
19
+ * Without either the digests refuse to run: a server with no `SESSION_SECRET` has no sessions
20
+ * either (the session middleware refuses to start), so nothing ever runs unkeyed — never a plain
21
+ * hash a list of addresses could reverse, never a per-process key that quietly stops matching
22
+ * across replicas.
23
+ */
24
+ export declare class RequestDigests {
25
+ private readonly options?;
26
+ /** Hex characters kept from the HMAC: 64 bits. */
27
+ private static readonly DIGEST_HEX_LENGTH;
28
+ constructor(options?: {
29
+ secret?: string | undefined;
30
+ } | undefined);
31
+ /** The account digest of an address, however it was typed. */
32
+ account(email: string): string;
33
+ /** The coarse IP hash of a client address (see `ClientAddress`). */
34
+ coarseIp(address: string): string;
35
+ private digest;
36
+ private secret;
37
+ /** An IPv4 address as itself (an IPv4-mapped IPv6 address as its IPv4); an IPv6 address as its /64. */
38
+ private coarsen;
39
+ /** The first four groups (the /64) of an IPv6 address, each without leading zeros; undefined when it does not parse. */
40
+ private ipv6Groups;
41
+ }
42
+ //# sourceMappingURL=RequestDigests.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RequestDigests.d.ts","sourceRoot":"","sources":["../../../src/throttle/RequestDigests.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,cAAc;IAIb,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;IAHrC,kDAAkD;IAClD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAM;gBAElB,OAAO,CAAC;;iBAAqB;IAE1D,8DAA8D;IAC9D,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI9B,oEAAoE;IACpE,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAIjC,OAAO,CAAC,MAAM;IAOd,OAAO,CAAC,MAAM;IAUd,uGAAuG;IACvG,OAAO,CAAC,OAAO;IAYf,wHAAwH;IACxH,OAAO,CAAC,UAAU;CAwBnB"}
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestDigests = void 0;
4
+ var crypto_1 = require("crypto");
5
+ /**
6
+ * The two keyed digests the throttled doors key their windows on and write on their log lines,
7
+ * in place of what they stand for:
8
+ * - `account(email)` — the ACCOUNT DIGEST: a keyed hash of the trimmed, lowercased address.
9
+ * One address gives one digest however it was typed, on every replica and across restarts, so
10
+ * an operator can say "one account" across lines without the address.
11
+ * - `coarseIp(address)` — the COARSE IP HASH: a keyed hash of the client address at the grain
12
+ * one device holds (an IPv4 address; an IPv6 /64, inside which a device rotates freely), so
13
+ * an operator can say "one device" and a throttle cannot be dodged by rotating inside a /64.
14
+ *
15
+ * Keyed with HMAC-SHA256 under a key DERIVED from the session secret — the one secret every
16
+ * replica already shares, the same keying the invite-request door's IP hash uses; derived per
17
+ * purpose, never used raw, so neither digest weakens the session's own use of it, and the two
18
+ * digests never collide in meaning. Truncated to 64 bits: enough to tell accounts and devices
19
+ * apart, useless to anyone without the key — never the address, never a plain hash a list of
20
+ * addresses could reverse.
21
+ *
22
+ * The key is the deployment's `SESSION_SECRET` unless the constructor is given one (the tests).
23
+ * Without either the digests refuse to run: a server with no `SESSION_SECRET` has no sessions
24
+ * either (the session middleware refuses to start), so nothing ever runs unkeyed — never a plain
25
+ * hash a list of addresses could reverse, never a per-process key that quietly stops matching
26
+ * across replicas.
27
+ */
28
+ var RequestDigests = /** @class */ (function () {
29
+ function RequestDigests(options) {
30
+ this.options = options;
31
+ }
32
+ /** The account digest of an address, however it was typed. */
33
+ RequestDigests.prototype.account = function (email) {
34
+ return this.digest('account-digest', email.trim().toLowerCase());
35
+ };
36
+ /** The coarse IP hash of a client address (see `ClientAddress`). */
37
+ RequestDigests.prototype.coarseIp = function (address) {
38
+ return this.digest('coarse-ip', this.coarsen(address.trim().toLowerCase()));
39
+ };
40
+ RequestDigests.prototype.digest = function (purpose, value) {
41
+ return (0, crypto_1.createHmac)('sha256', "".concat(purpose, ":").concat(this.secret()))
42
+ .update(value)
43
+ .digest('hex')
44
+ .slice(0, RequestDigests.DIGEST_HEX_LENGTH);
45
+ };
46
+ RequestDigests.prototype.secret = function () {
47
+ var _a, _b;
48
+ var configured = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.secret) !== null && _b !== void 0 ? _b : process.env.SESSION_SECRET;
49
+ if (!configured) {
50
+ throw new Error('SESSION_SECRET is not set: the account digest and the coarse IP hash need the key every replica shares');
51
+ }
52
+ return configured;
53
+ };
54
+ /** An IPv4 address as itself (an IPv4-mapped IPv6 address as its IPv4); an IPv6 address as its /64. */
55
+ RequestDigests.prototype.coarsen = function (address) {
56
+ var mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/.exec(address);
57
+ if (mapped) {
58
+ return mapped[1];
59
+ }
60
+ if (!address.includes(':')) {
61
+ return address;
62
+ }
63
+ var groups = this.ipv6Groups(address.split('%')[0]);
64
+ return groups ? "".concat(groups.slice(0, 4).join(':'), "::/64") : address;
65
+ };
66
+ /** The first four groups (the /64) of an IPv6 address, each without leading zeros; undefined when it does not parse. */
67
+ RequestDigests.prototype.ipv6Groups = function (address) {
68
+ var halves = address.split('::');
69
+ if (halves.length > 2) {
70
+ return undefined;
71
+ }
72
+ var parts = function (half) { return (half ? half.split(':') : []); };
73
+ var head = parts(halves[0]);
74
+ var tail = parts(halves[1]);
75
+ // A trailing dotted IPv4 part stands for two groups.
76
+ var groupsIn = function (list) { return list.reduce(function (count, part) { return count + (part.includes('.') ? 2 : 1); }, 0); };
77
+ var missing = 8 - groupsIn(head) - groupsIn(tail);
78
+ if (missing < 0 || (halves.length === 1 && missing !== 0)) {
79
+ return undefined;
80
+ }
81
+ var zeros = [];
82
+ for (var i = 0; i < missing; i++) {
83
+ zeros.push('0');
84
+ }
85
+ var groups = head.concat(zeros, tail).slice(0, 4);
86
+ if (groups.some(function (group) { return !/^[0-9a-f]{1,4}$/.test(group); })) {
87
+ return undefined;
88
+ }
89
+ return groups.map(function (group) { return parseInt(group, 16).toString(16); });
90
+ };
91
+ /** Hex characters kept from the HMAC: 64 bits. */
92
+ RequestDigests.DIGEST_HEX_LENGTH = 16;
93
+ return RequestDigests;
94
+ }());
95
+ exports.RequestDigests = RequestDigests;
96
+ //# sourceMappingURL=RequestDigests.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RequestDigests.js","sourceRoot":"","sources":["../../../src/throttle/RequestDigests.ts"],"names":[],"mappings":";;;AAAA,iCAAoC;AAEpC;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH;IAIE,wBAA6B,OAA6B;QAA7B,YAAO,GAAP,OAAO,CAAsB;IAAG,CAAC;IAE9D,8DAA8D;IAC9D,gCAAO,GAAP,UAAQ,KAAa;QACnB,OAAO,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC;IACnE,CAAC;IAED,oEAAoE;IACpE,iCAAQ,GAAR,UAAS,OAAe;QACtB,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;IAC9E,CAAC;IAEO,+BAAM,GAAd,UAAe,OAAe,EAAE,KAAa;QAC3C,OAAO,IAAA,mBAAU,EAAC,QAAQ,EAAE,UAAG,OAAO,cAAI,IAAI,CAAC,MAAM,EAAE,CAAE,CAAC;aACvD,MAAM,CAAC,KAAK,CAAC;aACb,MAAM,CAAC,KAAK,CAAC;aACb,KAAK,CAAC,CAAC,EAAE,cAAc,CAAC,iBAAiB,CAAC,CAAC;IAChD,CAAC;IAEO,+BAAM,GAAd;;QACE,IAAM,UAAU,GAAG,MAAA,MAAA,IAAI,CAAC,OAAO,0CAAE,MAAM,mCAAI,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QACtE,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,IAAI,KAAK,CACb,wGAAwG,CACzG,CAAC;SACH;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,uGAAuG;IAC/F,gCAAO,GAAf,UAAgB,OAAe;QAC7B,IAAM,MAAM,GAAG,oCAAoC,CAAC,IAAI,CA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C,EAArE,CAAqE,CAAC;QAC3G,IAAM,OAAO,GAAG,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QACpD,IAAI,OAAO,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,CAAC,EAAE;YACzD,OAAO,SAAS,CAAC;SAClB;QACD,IAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,EAAE,CAAC,EAAE,EAAE;YAChC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SACjB;QACD,IAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACpD,IAAI,MAAM,CAAC,IAAI,CAAC,UAAC,KAAK,IAAK,OAAA,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,EAA9B,CAA8B,CAAC,EAAE;YAC1D,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAhC,CAAgC,CAAC,CAAC;IACjE,CAAC;IArED,kDAAkD;IAC1B,gCAAiB,GAAG,EAAE,CAAC;IAqEjD,qBAAC;CAAA,AAvED,IAuEC;AAvEY,wCAAc"}
@@ -0,0 +1,67 @@
1
+ /** Which window refused a try: the client's (its coarse IP hash) or the account's (its digest). */
2
+ export type ThrottleWindow = 'client' | 'account';
3
+ /**
4
+ * The sign-in door's two windows (`POST /user/login`), keyed by digests (`RequestDigests`),
5
+ * never by an address:
6
+ * - per CLIENT: every try that is not a success counts — wrong passwords, blank ones too, the
7
+ * client made them — so one device cannot sweep many accounts; a successful sign-in never
8
+ * counts (it is forgiven the moment it succeeds), so the people behind one shared address
9
+ * signing in do not spend the window on each other;
10
+ * - per ACCOUNT: every try that carries a password counts (a blank submission judges no
11
+ * password, so it never counts), so guesses spread over many devices still stop; a success
12
+ * clears the account's count. A try is counted as it ARRIVES, before it is judged: tries in
13
+ * flight at the same moment cannot all pass the window together.
14
+ *
15
+ * A throttled try is told `ANSWER` whichever window refused it and whether or not the address
16
+ * has an account, in the same time a refused password takes (the door runs the same password
17
+ * check and discards its verdict).
18
+ *
19
+ * The windows are in process memory (`SlidingWindow`): per replica. A deployment of three
20
+ * replicas behind a load balancer (up to ten under load) keeps three sets of windows, so its
21
+ * effective ceiling is ~3× the numbers below (up to ~10×). Friction, not the wall — a shared
22
+ * store is a separate step.
23
+ */
24
+ export declare class SignInThrottle {
25
+ /** What a throttled try is told — in plain words, the same for every address, known or not. */
26
+ static readonly ANSWER = "Too many attempts. Try again in a few minutes.";
27
+ /**
28
+ * Per client: 50 tries that are not successes in 10 minutes (the founder's number, 2026-09-23).
29
+ * Successes never count, so an office or a school behind one address signing in at nine o'clock
30
+ * never spends this window on itself — only its mistypes do, and fifty wrong or blank tries in
31
+ * ten minutes is beyond any human population behind one address; a guessing script from one
32
+ * device is held to ~300 tries an hour per replica.
33
+ */
34
+ private static readonly CLIENT_LIMIT;
35
+ private static readonly CLIENT_WINDOW_MS;
36
+ /**
37
+ * Per account: 10 tries in 15 minutes (a success clears them, so only wrong ones ever add up).
38
+ * Someone who has forgotten a password tries a handful and asks for a reset link; ten wrong in
39
+ * a quarter of an hour is guessing, and counting per account holds however many devices the
40
+ * guesses come from. "A few minutes" in the answer is honest: the oldest try leaves the window
41
+ * within 15.
42
+ */
43
+ private static readonly ACCOUNT_LIMIT;
44
+ private static readonly ACCOUNT_WINDOW_MS;
45
+ private readonly clients;
46
+ private readonly accounts;
47
+ constructor(options?: {
48
+ now?: () => number;
49
+ });
50
+ /**
51
+ * Count this try against the client and against the account, and answer which window refuses
52
+ * it, if any. `account` is the digest of the address tried, given only when a password came
53
+ * with it (a blank submission judges no password, so it never counts toward an account). The
54
+ * count is taken here, before the try is judged, so tries in flight at once cannot all pass
55
+ * the window; a success clears it (`recordSuccess`).
56
+ */
57
+ admit(client: string, account?: string): ThrottleWindow | undefined;
58
+ /**
59
+ * The account proved itself: its window opens again. When it was a sign-in try from `client`
60
+ * (rather than a reset link redeemed), that try is forgiven — a success never counts against
61
+ * the device.
62
+ */
63
+ recordSuccess(account: string, client?: string): void;
64
+ }
65
+ /** The process-wide windows the sign-in door shares — they must span requests. */
66
+ export declare const signInThrottle: SignInThrottle;
67
+ //# sourceMappingURL=SignInThrottle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SignInThrottle.d.ts","sourceRoot":"","sources":["../../../src/throttle/SignInThrottle.ts"],"names":[],"mappings":"AAEA,mGAAmG;AACnG,MAAM,MAAM,cAAc,GAAG,QAAQ,GAAG,SAAS,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,cAAc;IACzB,+FAA+F;IAC/F,MAAM,CAAC,QAAQ,CAAC,MAAM,oDAAoD;IAE1E;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAM;IAC1C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAkB;IAE1D;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAM;IAC3C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAkB;IAE3D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IACxC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;gBAE7B,OAAO,CAAC,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;KAAE;IAa5C;;;;;;OAMG;IACH,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAUnE;;;;OAIG;IACH,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;CAMtD;AAED,kFAAkF;AAClF,eAAO,MAAM,cAAc,gBAAuB,CAAC"}
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signInThrottle = exports.SignInThrottle = void 0;
4
+ var SlidingWindow_1 = require("./SlidingWindow");
5
+ /**
6
+ * The sign-in door's two windows (`POST /user/login`), keyed by digests (`RequestDigests`),
7
+ * never by an address:
8
+ * - per CLIENT: every try that is not a success counts — wrong passwords, blank ones too, the
9
+ * client made them — so one device cannot sweep many accounts; a successful sign-in never
10
+ * counts (it is forgiven the moment it succeeds), so the people behind one shared address
11
+ * signing in do not spend the window on each other;
12
+ * - per ACCOUNT: every try that carries a password counts (a blank submission judges no
13
+ * password, so it never counts), so guesses spread over many devices still stop; a success
14
+ * clears the account's count. A try is counted as it ARRIVES, before it is judged: tries in
15
+ * flight at the same moment cannot all pass the window together.
16
+ *
17
+ * A throttled try is told `ANSWER` whichever window refused it and whether or not the address
18
+ * has an account, in the same time a refused password takes (the door runs the same password
19
+ * check and discards its verdict).
20
+ *
21
+ * The windows are in process memory (`SlidingWindow`): per replica. A deployment of three
22
+ * replicas behind a load balancer (up to ten under load) keeps three sets of windows, so its
23
+ * effective ceiling is ~3× the numbers below (up to ~10×). Friction, not the wall — a shared
24
+ * store is a separate step.
25
+ */
26
+ var SignInThrottle = /** @class */ (function () {
27
+ function SignInThrottle(options) {
28
+ this.clients = new SlidingWindow_1.SlidingWindow({
29
+ windowMs: SignInThrottle.CLIENT_WINDOW_MS,
30
+ limit: SignInThrottle.CLIENT_LIMIT,
31
+ now: options === null || options === void 0 ? void 0 : options.now,
32
+ });
33
+ this.accounts = new SlidingWindow_1.SlidingWindow({
34
+ windowMs: SignInThrottle.ACCOUNT_WINDOW_MS,
35
+ limit: SignInThrottle.ACCOUNT_LIMIT,
36
+ now: options === null || options === void 0 ? void 0 : options.now,
37
+ });
38
+ }
39
+ /**
40
+ * Count this try against the client and against the account, and answer which window refuses
41
+ * it, if any. `account` is the digest of the address tried, given only when a password came
42
+ * with it (a blank submission judges no password, so it never counts toward an account). The
43
+ * count is taken here, before the try is judged, so tries in flight at once cannot all pass
44
+ * the window; a success clears it (`recordSuccess`).
45
+ */
46
+ SignInThrottle.prototype.admit = function (client, account) {
47
+ if (this.clients.hit(client)) {
48
+ return 'client';
49
+ }
50
+ if (account !== undefined && this.accounts.hit(account)) {
51
+ return 'account';
52
+ }
53
+ return undefined;
54
+ };
55
+ /**
56
+ * The account proved itself: its window opens again. When it was a sign-in try from `client`
57
+ * (rather than a reset link redeemed), that try is forgiven — a success never counts against
58
+ * the device.
59
+ */
60
+ SignInThrottle.prototype.recordSuccess = function (account, client) {
61
+ this.accounts.clear(account);
62
+ if (client !== undefined) {
63
+ this.clients.forgive(client);
64
+ }
65
+ };
66
+ /** What a throttled try is told — in plain words, the same for every address, known or not. */
67
+ SignInThrottle.ANSWER = 'Too many attempts. Try again in a few minutes.';
68
+ /**
69
+ * Per client: 50 tries that are not successes in 10 minutes (the founder's number, 2026-09-23).
70
+ * Successes never count, so an office or a school behind one address signing in at nine o'clock
71
+ * never spends this window on itself — only its mistypes do, and fifty wrong or blank tries in
72
+ * ten minutes is beyond any human population behind one address; a guessing script from one
73
+ * device is held to ~300 tries an hour per replica.
74
+ */
75
+ SignInThrottle.CLIENT_LIMIT = 50;
76
+ SignInThrottle.CLIENT_WINDOW_MS = 10 * 60 * 1000;
77
+ /**
78
+ * Per account: 10 tries in 15 minutes (a success clears them, so only wrong ones ever add up).
79
+ * Someone who has forgotten a password tries a handful and asks for a reset link; ten wrong in
80
+ * a quarter of an hour is guessing, and counting per account holds however many devices the
81
+ * guesses come from. "A few minutes" in the answer is honest: the oldest try leaves the window
82
+ * within 15.
83
+ */
84
+ SignInThrottle.ACCOUNT_LIMIT = 10;
85
+ SignInThrottle.ACCOUNT_WINDOW_MS = 15 * 60 * 1000;
86
+ return SignInThrottle;
87
+ }());
88
+ exports.SignInThrottle = SignInThrottle;
89
+ /** The process-wide windows the sign-in door shares — they must span requests. */
90
+ exports.signInThrottle = new SignInThrottle();
91
+ //# sourceMappingURL=SignInThrottle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SignInThrottle.js","sourceRoot":"","sources":["../../../src/throttle/SignInThrottle.ts"],"names":[],"mappings":";;;AAAA,iDAAgD;AAKhD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH;IA2BE,wBAAY,OAAgC;QAC1C,IAAI,CAAC,OAAO,GAAG,IAAI,6BAAa,CAAC;YAC/B,QAAQ,EAAE,cAAc,CAAC,gBAAgB;YACzC,KAAK,EAAE,cAAc,CAAC,YAAY;YAClC,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,GAAG,IAAI,6BAAa,CAAC;YAChC,QAAQ,EAAE,cAAc,CAAC,iBAAiB;YAC1C,KAAK,EAAE,cAAc,CAAC,aAAa;YACnC,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG;SAClB,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,8BAAK,GAAL,UAAM,MAAc,EAAE,OAAgB;QACpC,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;YAC5B,OAAO,QAAQ,CAAC;SACjB;QACD,IAAI,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YACvD,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED;;;;OAIG;IACH,sCAAa,GAAb,UAAc,OAAe,EAAE,MAAe;QAC5C,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC7B,IAAI,MAAM,KAAK,SAAS,EAAE;YACxB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;SAC9B;IACH,CAAC;IAlED,+FAA+F;IAC/E,qBAAM,GAAG,gDAAgD,CAAC;IAE1E;;;;;;OAMG;IACqB,2BAAY,GAAG,EAAE,CAAC;IAClB,+BAAgB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAE1D;;;;;;OAMG;IACqB,4BAAa,GAAG,EAAE,CAAC;IACnB,gCAAiB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IA8C7D,qBAAC;CAAA,AApED,IAoEC;AApEY,wCAAc;AAsE3B,kFAAkF;AACrE,QAAA,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * A per-key sliding window: how many attempts a key made in the last `windowMs`, against a
3
+ * `limit`. The one window every throttled door shares — the sign-in and password-reset doors in
4
+ * this package, and any consumer door throttling per client (lifted from the invite-request
5
+ * door's per-IP throttle, which it replaces).
6
+ *
7
+ * Held in process memory only, which is what makes it cheap and what bounds it:
8
+ * - PER PROCESS. Each replica keeps its own windows, so behind a load balancer spreading a
9
+ * client over N replicas the effective ceiling is ~N× the limit; a restart forgets them.
10
+ * A throttle built on this is friction, not the wall. A shared store is a separate step.
11
+ * - BOUNDED. At most `maxKeys` keys are tracked; at the bound the oldest-touched key is
12
+ * forgotten first, so a flood of fresh keys degrades the friction before it grows memory.
13
+ *
14
+ * Keys are whatever the caller passes — callers pass digests (see `RequestDigests`), never raw
15
+ * addresses, so nothing identifying is held here either.
16
+ */
17
+ export declare class SlidingWindow {
18
+ private static readonly DEFAULT_MAX_KEYS;
19
+ private readonly windowMs;
20
+ private readonly limit;
21
+ private readonly maxKeys;
22
+ private readonly now;
23
+ /** key → the counted attempt times inside the window (pruned on touch), in touch order. */
24
+ private readonly attempts;
25
+ constructor(options: {
26
+ windowMs: number;
27
+ limit: number;
28
+ maxKeys?: number;
29
+ now?: () => number;
30
+ });
31
+ /**
32
+ * Record an attempt for `key` and answer whether it was over the window — `true` means refuse.
33
+ * A refused attempt is not counted, so a key drains one window after its last counted attempt.
34
+ */
35
+ hit(key: string): boolean;
36
+ /** Whether `key` has used up its window, without counting anything. */
37
+ isOver(key: string): boolean;
38
+ /** Count an attempt for `key` without answering (for doors that judge the attempt first). */
39
+ record(key: string): void;
40
+ /** Forget `key`'s attempts. */
41
+ clear(key: string): void;
42
+ /**
43
+ * Uncount `key`'s latest attempt (for doors that count a try as it arrives and learn afterwards
44
+ * that it should never have counted — a sign-in that succeeded).
45
+ */
46
+ forgive(key: string): void;
47
+ private inWindow;
48
+ /** Store `key`'s attempts as the most recently touched, evicting the oldest-touched key at the bound. */
49
+ private touch;
50
+ }
51
+ //# sourceMappingURL=SlidingWindow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SlidingWindow.d.ts","sourceRoot":"","sources":["../../../src/throttle/SlidingWindow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAU;IAElD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,2FAA2F;IAC3F,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA+B;gBAE5C,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;KAAE;IAO9F;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IASzB,uEAAuE;IACvE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAI5B,6FAA6F;IAC7F,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAMzB,+BAA+B;IAC/B,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIxB;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAM1B,OAAO,CAAC,QAAQ;IAKhB,yGAAyG;IACzG,OAAO,CAAC,KAAK;CAUd"}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SlidingWindow = void 0;
4
+ /**
5
+ * A per-key sliding window: how many attempts a key made in the last `windowMs`, against a
6
+ * `limit`. The one window every throttled door shares — the sign-in and password-reset doors in
7
+ * this package, and any consumer door throttling per client (lifted from the invite-request
8
+ * door's per-IP throttle, which it replaces).
9
+ *
10
+ * Held in process memory only, which is what makes it cheap and what bounds it:
11
+ * - PER PROCESS. Each replica keeps its own windows, so behind a load balancer spreading a
12
+ * client over N replicas the effective ceiling is ~N× the limit; a restart forgets them.
13
+ * A throttle built on this is friction, not the wall. A shared store is a separate step.
14
+ * - BOUNDED. At most `maxKeys` keys are tracked; at the bound the oldest-touched key is
15
+ * forgotten first, so a flood of fresh keys degrades the friction before it grows memory.
16
+ *
17
+ * Keys are whatever the caller passes — callers pass digests (see `RequestDigests`), never raw
18
+ * addresses, so nothing identifying is held here either.
19
+ */
20
+ var SlidingWindow = /** @class */ (function () {
21
+ function SlidingWindow(options) {
22
+ var _a, _b;
23
+ /** key → the counted attempt times inside the window (pruned on touch), in touch order. */
24
+ this.attempts = new Map();
25
+ this.windowMs = options.windowMs;
26
+ this.limit = options.limit;
27
+ this.maxKeys = (_a = options.maxKeys) !== null && _a !== void 0 ? _a : SlidingWindow.DEFAULT_MAX_KEYS;
28
+ this.now = (_b = options.now) !== null && _b !== void 0 ? _b : Date.now;
29
+ }
30
+ /**
31
+ * Record an attempt for `key` and answer whether it was over the window — `true` means refuse.
32
+ * A refused attempt is not counted, so a key drains one window after its last counted attempt.
33
+ */
34
+ SlidingWindow.prototype.hit = function (key) {
35
+ if (this.isOver(key)) {
36
+ this.touch(key, this.inWindow(key));
37
+ return true;
38
+ }
39
+ this.record(key);
40
+ return false;
41
+ };
42
+ /** Whether `key` has used up its window, without counting anything. */
43
+ SlidingWindow.prototype.isOver = function (key) {
44
+ return this.inWindow(key).length >= this.limit;
45
+ };
46
+ /** Count an attempt for `key` without answering (for doors that judge the attempt first). */
47
+ SlidingWindow.prototype.record = function (key) {
48
+ var attempts = this.inWindow(key);
49
+ attempts.push(this.now());
50
+ this.touch(key, attempts);
51
+ };
52
+ /** Forget `key`'s attempts. */
53
+ SlidingWindow.prototype.clear = function (key) {
54
+ this.attempts.delete(key);
55
+ };
56
+ /**
57
+ * Uncount `key`'s latest attempt (for doors that count a try as it arrives and learn afterwards
58
+ * that it should never have counted — a sign-in that succeeded).
59
+ */
60
+ SlidingWindow.prototype.forgive = function (key) {
61
+ var attempts = this.inWindow(key);
62
+ attempts.pop();
63
+ this.touch(key, attempts);
64
+ };
65
+ SlidingWindow.prototype.inWindow = function (key) {
66
+ var _a;
67
+ var cutoff = this.now() - this.windowMs;
68
+ return ((_a = this.attempts.get(key)) !== null && _a !== void 0 ? _a : []).filter(function (time) { return time > cutoff; });
69
+ };
70
+ /** Store `key`'s attempts as the most recently touched, evicting the oldest-touched key at the bound. */
71
+ SlidingWindow.prototype.touch = function (key, attempts) {
72
+ if (!this.attempts.has(key) && this.attempts.size >= this.maxKeys) {
73
+ var oldest = this.attempts.keys().next().value;
74
+ if (oldest !== undefined) {
75
+ this.attempts.delete(oldest);
76
+ }
77
+ }
78
+ this.attempts.delete(key);
79
+ this.attempts.set(key, attempts);
80
+ };
81
+ SlidingWindow.DEFAULT_MAX_KEYS = 10000;
82
+ return SlidingWindow;
83
+ }());
84
+ exports.SlidingWindow = SlidingWindow;
85
+ //# sourceMappingURL=SlidingWindow.js.map
@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