@blamejs/core 0.6.12 → 0.6.20

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 (46) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/NOTICE +16 -0
  3. package/README.md +9 -8
  4. package/index.js +12 -0
  5. package/lib/api-key.js +2 -3
  6. package/lib/audit.js +4 -0
  7. package/lib/auth/password.js +449 -4
  8. package/lib/cache.js +3 -7
  9. package/lib/cli.js +598 -4
  10. package/lib/config-drift.js +309 -0
  11. package/lib/crypto-field.js +37 -0
  12. package/lib/crypto.js +8 -0
  13. package/lib/db.js +17 -2
  14. package/lib/dual-control.js +475 -0
  15. package/lib/file-type.js +265 -0
  16. package/lib/http-client.js +77 -0
  17. package/lib/internal-sha1-hibp.js +34 -0
  18. package/lib/middleware/csp-nonce.js +7 -4
  19. package/lib/middleware/index.js +2 -0
  20. package/lib/middleware/network-allowlist.js +199 -0
  21. package/lib/network-dns.js +469 -0
  22. package/lib/network-heartbeat.js +290 -0
  23. package/lib/network-nts.js +552 -0
  24. package/lib/network-proxy.js +246 -0
  25. package/lib/network-tls.js +326 -0
  26. package/lib/network.js +233 -0
  27. package/lib/notify.js +2 -3
  28. package/lib/ntp-check.js +50 -4
  29. package/lib/numeric-checks.js +40 -0
  30. package/lib/object-store/azure-blob.js +16 -42
  31. package/lib/permissions.js +223 -9
  32. package/lib/pqc-agent.js +4 -4
  33. package/lib/queue.js +5 -5
  34. package/lib/restore.js +5 -3
  35. package/lib/retention.js +439 -0
  36. package/lib/retry.js +3 -6
  37. package/lib/security-assert.js +368 -0
  38. package/lib/session.js +138 -8
  39. package/lib/slug.js +2 -3
  40. package/lib/ssrf-guard.js +9 -0
  41. package/lib/testing.js +3 -7
  42. package/lib/vendor/MANIFEST.json +12 -0
  43. package/lib/vendor/common-passwords-top-10000.txt +10000 -0
  44. package/lib/webhook.js +3 -6
  45. package/package.json +3 -2
  46. package/sbom.cyclonedx.json +61 -0
@@ -47,6 +47,10 @@
47
47
  * "the call shape was wrong" (empty plain, oversize plain).
48
48
  */
49
49
  var argon2 = require("../vendor/argon2");
50
+ var httpClient = require("../http-client");
51
+ var internalSha1Hibp = require("../internal-sha1-hibp");
52
+ var safeUrl = require("../safe-url");
53
+ var timingSafeEqual = require("../crypto").timingSafeEqual;
50
54
  var { AuthError } = require("../framework-error");
51
55
 
52
56
  // Tuning targets ~250–500ms on commodity 2026 hardware. memoryCost
@@ -59,6 +63,444 @@ var DEFAULT_PARAMS = Object.freeze({
59
63
 
60
64
  var MAX_PLAINTEXT_BYTES = 4096;
61
65
 
66
+ // ---- Policy primitive ----------------------------------------------
67
+ //
68
+ // Argon2id covers the OFFLINE attack model: even with the DB stolen,
69
+ // each guess costs ~250ms of CPU + 64 MiB of RAM. It does NOT cover:
70
+ // - Online weak-credential attacks (operator phishes / reuses)
71
+ // - Periodic rotation requirements (PCI 8.3, NYDFS, some HIPAA)
72
+ // - History reuse (PCI 8.3 last-4 floor)
73
+ // - Operator-tunable composition rules (HIPAA / industry-specific
74
+ // where AAL2-equivalent posture is mandated)
75
+ //
76
+ // b.auth.password.policy(opts) returns:
77
+ // - check(plaintext, context?) → presentation-time gate
78
+ // - shouldRotate(passwordSetAt) → per-account rotation check
79
+ // - reuseProhibited(plaintext, history)→ history-reuse check
80
+ //
81
+ // Standards mapped:
82
+ // - NIST 800-63B §5.1.1.2: 8-char min floor, 64-char min max,
83
+ // breach check, NO MANDATORY composition. (Default posture.)
84
+ // - PCI-DSS 8.3.6 / 8.3.7: 12-char min, 90-day rotation, history
85
+ // of 4. (Operator opts in via { profile: "pci-8.3" }.)
86
+ // - HIPAA 164.308(a)(5)(ii)(D): "procedures for creating, changing,
87
+ // and safeguarding passwords" — addressed via composition opts.
88
+ // - GDPR Art. 32: storage shape (sealed via Argon2id); no
89
+ // additional policy requirement here.
90
+ // - NYDFS 23 NYCRR 500.12 / NIST AAL2: rotation + breach + length.
91
+ //
92
+ // The defaults follow NIST 800-63B (no mandatory composition,
93
+ // length-and-breach over rules). Every other regime layers on
94
+ // opt-in opts; the framework refuses to surprise an operator who
95
+ // followed the defaults.
96
+ //
97
+ // var policy = b.auth.password.policy({
98
+ // minLength: 12,
99
+ // breachCheck: "haveibeenpwned",
100
+ // mustRotateAfterMs: C.TIME.days(90), // PCI 8.3.9
101
+ // historyMinDistance: 4, // PCI 8.3.7
102
+ // complexity: {
103
+ // minCategories: 0, // NIST: don't enforce. opt in for HIPAA-flavoured.
104
+ // categories: ["lower", "upper", "digit", "special"],
105
+ // minRunRepeat: 3, // reject "aaaa…"
106
+ // minSequenceLength: 3, // reject "abcd"/"1234"
107
+ // },
108
+ // dictionary: ["companyName", "productName"],
109
+ // });
110
+ //
111
+ // await policy.check(plain, { email, username, deny: [...], passwordSetAt });
112
+ // policy.shouldRotate(passwordSetAt);
113
+ // await policy.reuseProhibited(plain, [oldHash1, oldHash2, oldHash3]);
114
+ //
115
+ // breachCheck:"haveibeenpwned" uses the HIBP k-anonymity API; the
116
+ // SHA-1 hash is computed in-process via lib/internal-sha1-hibp.js
117
+ // (NOT exported on b.crypto — see comment in lib/crypto.js). Only
118
+ // the first 5 hex chars cross the wire. Rate-limit and failure-mode
119
+ // are operator's call: an HIBP outage returns
120
+ // { ok: true, breachCheckSkipped: true } by default; failClosed:true
121
+ // rejects.
122
+ var DEFAULT_POLICY = Object.freeze({
123
+ minLength: 8, // NIST floor
124
+ maxLength: MAX_PLAINTEXT_BYTES,
125
+ forbidCommon: [],
126
+ // The bundled top-10000 list ships in lib/vendor/common-passwords-top-10000.txt
127
+ // (SecLists, CC-BY-3.0). Set false to skip — operators with a richer
128
+ // breach-list (HIBP downloads, NCSC 100k) layered via forbidCommon
129
+ // typically leave this on; it's additive.
130
+ useBundledCommon: true,
131
+ denyContextSubstrings: true,
132
+ breachCheck: null, // null | "haveibeenpwned"
133
+ breachThreshold: 1,
134
+ failClosed: false,
135
+ hibpEndpoint: "https://api.pwnedpasswords.com",
136
+ hibpTimeoutMs: 1500,
137
+ // Rotation policy (PCI 8.3.9 / NYDFS / industry-specific). null = no rotation.
138
+ mustRotateAfterMs: null,
139
+ // History reuse (PCI 8.3.7 floor: last-4). 0 = disabled.
140
+ // Operator passes the actual stored hash list to reuseProhibited().
141
+ historyMinDistance: 0,
142
+ // Composition rules (NIST 800-63B explicitly says NOT to enforce
143
+ // these; HIPAA / older standards still ask for them. Default is OFF
144
+ // so the NIST-aligned posture is the default; operators opt in.)
145
+ complexity: null,
146
+ // Dictionary terms forbidden as substrings (operator brand names,
147
+ // product names, etc.). Substring match, case-insensitive. Empty
148
+ // by default.
149
+ dictionary: [],
150
+ });
151
+
152
+ var COMPLEXITY_DEFAULT = Object.freeze({
153
+ minCategories: 0, // NIST default off; HIPAA-flavoured ops set 3 or 4
154
+ categories: ["lower", "upper", "digit", "special"],
155
+ minRunRepeat: 0, // reject N+ identical chars in a row; 0 = off
156
+ minSequenceLength: 0, // reject N+ ascending or descending chars; 0 = off
157
+ });
158
+
159
+ // Predefined profiles operators can opt into. Each spreads onto the
160
+ // policy opts so the operator can still override individual fields.
161
+ var POLICY_PROFILES = Object.freeze({
162
+ // NIST 800-63B AAL2 baseline — length + breach, no composition.
163
+ "nist-aal2": Object.freeze({
164
+ minLength: 8,
165
+ breachCheck: "haveibeenpwned",
166
+ }),
167
+ // PCI-DSS 4.0 §8.3 — 12-char min, 90-day rotation, history of 4.
168
+ // Composition is NOT required by PCI 4.0 (it dropped the older
169
+ // version's composition rule); breach check + length covers it.
170
+ "pci-4.0": Object.freeze({
171
+ minLength: 12,
172
+ breachCheck: "haveibeenpwned",
173
+ mustRotateAfterMs: 90 * 24 * 60 * 60 * 1000,
174
+ historyMinDistance: 4,
175
+ }),
176
+ // HIPAA 164.308 — "procedures for creating, changing, and
177
+ // safeguarding". The standard is intentionally vague; the
178
+ // commonly-implemented profile pairs length + composition +
179
+ // rotation + lockout (lockout is b.auth.lockout, separate
180
+ // primitive).
181
+ "hipaa-aal2": Object.freeze({
182
+ minLength: 12,
183
+ breachCheck: "haveibeenpwned",
184
+ mustRotateAfterMs: 180 * 24 * 60 * 60 * 1000,
185
+ historyMinDistance: 4,
186
+ complexity: {
187
+ minCategories: 3,
188
+ minRunRepeat: 3,
189
+ minSequenceLength: 3,
190
+ },
191
+ }),
192
+ });
193
+
194
+ // Top-10000 common-password set vendored from SecLists
195
+ // (CC-BY-3.0 by Daniel Miessler). Loaded lazily on first policy.check
196
+ // call — keeps boot fast for apps that never invoke the dictionary.
197
+ // Operators wanting deeper enforcement supply opts.forbidCommon (set
198
+ // of additional plaintexts) and/or opts.forbidCommonExtra (operator's
199
+ // own breach list); both layer additively on top of the bundled set.
200
+ var path = require("node:path");
201
+ var fs = require("node:fs");
202
+ var _bundledCommonPasswords = null;
203
+ function _loadBundledCommon() {
204
+ if (_bundledCommonPasswords) return _bundledCommonPasswords;
205
+ var p = path.join(__dirname, "..", "vendor", "common-passwords-top-10000.txt");
206
+ var text = fs.readFileSync(p, "utf8");
207
+ var set = new Set();
208
+ var lines = text.split(/\r?\n/);
209
+ for (var i = 0; i < lines.length; i++) {
210
+ var line = lines[i].trim();
211
+ if (line.length > 0) set.add(line.toLowerCase());
212
+ }
213
+ _bundledCommonPasswords = set;
214
+ return _bundledCommonPasswords;
215
+ }
216
+ function _commonPasswordsSize() {
217
+ return _loadBundledCommon().size;
218
+ }
219
+
220
+ function _ok(extra) { return Object.assign({ ok: true }, extra || {}); }
221
+ function _fail(code, message) {
222
+ return { ok: false, code: "policy/" + code, message: message };
223
+ }
224
+
225
+ // Argon2id verify with a known stored hash — used by reuseProhibited
226
+ // to compare a candidate plaintext against history entries without
227
+ // the operator having to wire verify() per row.
228
+ async function _argon2Verify(stored, plaintext) {
229
+ if (typeof stored !== "string" || stored.indexOf("$argon2id$") !== 0) return false;
230
+ try { return await argon2.verify(stored, plaintext); }
231
+ catch (_e) { return false; }
232
+ }
233
+
234
+ function _hasCategory(plaintext, category) {
235
+ if (category === "lower") return /[a-z]/.test(plaintext);
236
+ if (category === "upper") return /[A-Z]/.test(plaintext);
237
+ if (category === "digit") return /[0-9]/.test(plaintext);
238
+ if (category === "special") return /[^A-Za-z0-9]/.test(plaintext);
239
+ return false;
240
+ }
241
+
242
+ function _hasRunOfLength(plaintext, n) {
243
+ if (n < 2) return false;
244
+ for (var i = 0; i + n <= plaintext.length; i++) {
245
+ var c = plaintext.charCodeAt(i);
246
+ var allSame = true;
247
+ for (var j = 1; j < n; j++) {
248
+ if (plaintext.charCodeAt(i + j) !== c) { allSame = false; break; }
249
+ }
250
+ if (allSame) return true;
251
+ }
252
+ return false;
253
+ }
254
+
255
+ function _hasSequenceOfLength(plaintext, n) {
256
+ if (n < 3) return false;
257
+ for (var i = 0; i + n <= plaintext.length; i++) {
258
+ var ascending = true, descending = true;
259
+ for (var j = 1; j < n; j++) {
260
+ var diff = plaintext.charCodeAt(i + j) - plaintext.charCodeAt(i + j - 1);
261
+ if (diff !== 1) ascending = false;
262
+ if (diff !== -1) descending = false;
263
+ }
264
+ if (ascending || descending) return true;
265
+ }
266
+ return false;
267
+ }
268
+
269
+ function policy(opts) {
270
+ opts = opts || {};
271
+ // Apply named profile FIRST, then operator opts on top so the
272
+ // operator can override profile defaults per-field.
273
+ if (typeof opts.profile === "string" && opts.profile.length > 0) {
274
+ if (!POLICY_PROFILES[opts.profile]) {
275
+ throw new AuthError("auth-password/bad-policy",
276
+ "policy.profile must be one of " + Object.keys(POLICY_PROFILES).join("/") +
277
+ ", got " + JSON.stringify(opts.profile));
278
+ }
279
+ opts = Object.assign({}, POLICY_PROFILES[opts.profile], opts);
280
+ delete opts.profile;
281
+ }
282
+ var p = Object.assign({}, DEFAULT_POLICY, opts);
283
+ if (typeof p.minLength !== "number" || p.minLength < 1 || p.minLength > MAX_PLAINTEXT_BYTES) {
284
+ throw new AuthError("auth-password/bad-policy",
285
+ "policy.minLength must be in [1, " + MAX_PLAINTEXT_BYTES + "]");
286
+ }
287
+ if (typeof p.maxLength !== "number" || p.maxLength < p.minLength || p.maxLength > MAX_PLAINTEXT_BYTES) {
288
+ throw new AuthError("auth-password/bad-policy",
289
+ "policy.maxLength must be in [minLength, " + MAX_PLAINTEXT_BYTES + "]");
290
+ }
291
+ if (p.breachCheck !== null && p.breachCheck !== "haveibeenpwned") {
292
+ throw new AuthError("auth-password/bad-policy",
293
+ "policy.breachCheck must be null or 'haveibeenpwned', got " + JSON.stringify(p.breachCheck));
294
+ }
295
+ if (p.hibpEndpoint) {
296
+ safeUrl.parse(p.hibpEndpoint, { allowedProtocols: safeUrl.ALLOW_HTTP_TLS, errorClass: AuthError });
297
+ }
298
+ if (p.mustRotateAfterMs !== null &&
299
+ (typeof p.mustRotateAfterMs !== "number" || !isFinite(p.mustRotateAfterMs) || p.mustRotateAfterMs <= 0)) {
300
+ throw new AuthError("auth-password/bad-policy",
301
+ "policy.mustRotateAfterMs must be a positive finite number or null");
302
+ }
303
+ if (typeof p.historyMinDistance !== "number" || !isFinite(p.historyMinDistance) ||
304
+ p.historyMinDistance < 0 || Math.floor(p.historyMinDistance) !== p.historyMinDistance) {
305
+ throw new AuthError("auth-password/bad-policy",
306
+ "policy.historyMinDistance must be a non-negative integer");
307
+ }
308
+ if (p.complexity !== null && typeof p.complexity !== "object") {
309
+ throw new AuthError("auth-password/bad-policy",
310
+ "policy.complexity must be null or an object");
311
+ }
312
+ var complexity = p.complexity ? Object.assign({}, COMPLEXITY_DEFAULT, p.complexity) : null;
313
+ if (complexity) {
314
+ if (typeof complexity.minCategories !== "number" || complexity.minCategories < 0 ||
315
+ complexity.minCategories > complexity.categories.length) {
316
+ throw new AuthError("auth-password/bad-policy",
317
+ "policy.complexity.minCategories must be in [0, " + complexity.categories.length + "]");
318
+ }
319
+ for (var ci = 0; ci < complexity.categories.length; ci++) {
320
+ if (["lower", "upper", "digit", "special"].indexOf(complexity.categories[ci]) === -1) {
321
+ throw new AuthError("auth-password/bad-policy",
322
+ "policy.complexity.categories[" + ci + "] must be lower / upper / digit / special, got " +
323
+ JSON.stringify(complexity.categories[ci]));
324
+ }
325
+ }
326
+ }
327
+ var forbidLower = (Array.isArray(p.forbidCommon) ? p.forbidCommon : [])
328
+ .map(function (s) { return String(s).toLowerCase(); });
329
+ var bundledSet = p.useBundledCommon === false ? null : _loadBundledCommon();
330
+ var dictionaryLower = (Array.isArray(p.dictionary) ? p.dictionary : [])
331
+ .filter(function (s) { return typeof s === "string" && s.length >= 3; })
332
+ .map(function (s) { return s.toLowerCase(); });
333
+
334
+ async function check(plaintext, context) {
335
+ if (typeof plaintext !== "string") {
336
+ return _fail("bad-input", "plaintext must be a string");
337
+ }
338
+ var byteLen = Buffer.byteLength(plaintext, "utf8");
339
+ if (byteLen < p.minLength) {
340
+ return _fail("too-short", "plaintext is shorter than " + p.minLength + " bytes");
341
+ }
342
+ if (byteLen > p.maxLength) {
343
+ return _fail("too-long", "plaintext exceeds " + p.maxLength + " bytes");
344
+ }
345
+ var lower = plaintext.toLowerCase();
346
+ if (bundledSet && bundledSet.has(lower)) {
347
+ return _fail("forbidden-common", "plaintext matches a known breached / common password (bundled top-10000)");
348
+ }
349
+ for (var i = 0; i < forbidLower.length; i++) {
350
+ if (lower === forbidLower[i]) {
351
+ return _fail("forbidden-common", "plaintext matches a known weak / common password");
352
+ }
353
+ }
354
+ for (var di2 = 0; di2 < dictionaryLower.length; di2++) {
355
+ if (lower.indexOf(dictionaryLower[di2]) !== -1) {
356
+ return _fail("forbidden-dictionary",
357
+ "plaintext contains a forbidden dictionary term");
358
+ }
359
+ }
360
+ if (p.denyContextSubstrings && context) {
361
+ var deny = [];
362
+ if (typeof context.email === "string" && context.email.length > 0) {
363
+ deny.push(context.email.toLowerCase());
364
+ var at = context.email.indexOf("@");
365
+ if (at > 0) deny.push(context.email.slice(0, at).toLowerCase());
366
+ }
367
+ if (typeof context.username === "string" && context.username.length > 0) {
368
+ deny.push(context.username.toLowerCase());
369
+ }
370
+ if (Array.isArray(context.deny)) {
371
+ for (var di = 0; di < context.deny.length; di++) {
372
+ if (typeof context.deny[di] === "string" && context.deny[di].length >= 3) {
373
+ deny.push(context.deny[di].toLowerCase());
374
+ }
375
+ }
376
+ }
377
+ for (var dj = 0; dj < deny.length; dj++) {
378
+ if (deny[dj].length >= 3 && lower.indexOf(deny[dj]) !== -1) {
379
+ return _fail("contains-context",
380
+ "plaintext contains a forbidden context substring (account identifier or operator-supplied deny string)");
381
+ }
382
+ }
383
+ }
384
+ if (complexity) {
385
+ if (complexity.minCategories > 0) {
386
+ var hits = 0;
387
+ for (var cc = 0; cc < complexity.categories.length; cc++) {
388
+ if (_hasCategory(plaintext, complexity.categories[cc])) hits++;
389
+ }
390
+ if (hits < complexity.minCategories) {
391
+ return _fail("complexity-categories",
392
+ "plaintext uses " + hits + " character categories; policy requires at least " +
393
+ complexity.minCategories + " of [" + complexity.categories.join(", ") + "]");
394
+ }
395
+ }
396
+ if (complexity.minRunRepeat >= 2 && _hasRunOfLength(plaintext, complexity.minRunRepeat)) {
397
+ return _fail("complexity-run",
398
+ "plaintext contains " + complexity.minRunRepeat + "+ identical consecutive characters");
399
+ }
400
+ if (complexity.minSequenceLength >= 3 && _hasSequenceOfLength(plaintext, complexity.minSequenceLength)) {
401
+ return _fail("complexity-sequence",
402
+ "plaintext contains a " + complexity.minSequenceLength + "+-char ascending or descending sequence");
403
+ }
404
+ }
405
+ if (p.breachCheck === "haveibeenpwned") {
406
+ // HIBP k-anonymity: send the first 5 hex chars of the SHA-1
407
+ // hash, scan the returned suffix list. The framework's only
408
+ // SHA-1 usage; HIBP requires it. (See lib/internal-sha1-hibp.js
409
+ // for the restriction rationale.)
410
+ var sha1Full = internalSha1Hibp.sha1Hex(plaintext).toUpperCase();
411
+ var prefix = sha1Full.slice(0, 5);
412
+ var suffix = sha1Full.slice(5);
413
+ var url = p.hibpEndpoint.replace(/\/+$/, "") + "/range/" + prefix;
414
+ var resp;
415
+ try {
416
+ resp = await httpClient.request({
417
+ method: "GET",
418
+ url: url,
419
+ headers: { "User-Agent": "blamejs-password-policy/1" },
420
+ idleTimeoutMs: p.hibpTimeoutMs,
421
+ errorClass: AuthError,
422
+ });
423
+ } catch (e) {
424
+ if (p.failClosed) {
425
+ return _fail("breach-check-failed",
426
+ "HIBP lookup failed and policy is fail-closed: " + ((e && e.message) || String(e)));
427
+ }
428
+ return _ok({ breachCheckSkipped: true,
429
+ breachCheckSkipReason: (e && e.message) || String(e) });
430
+ }
431
+ if (resp.statusCode !== 200 || !resp.body) {
432
+ if (p.failClosed) {
433
+ return _fail("breach-check-failed",
434
+ "HIBP returned status " + resp.statusCode + " with no body");
435
+ }
436
+ return _ok({ breachCheckSkipped: true,
437
+ breachCheckSkipReason: "hibp-status-" + resp.statusCode });
438
+ }
439
+ var bodyText = Buffer.isBuffer(resp.body) ? resp.body.toString("utf8") : String(resp.body);
440
+ var lines = bodyText.split(/\r?\n/);
441
+ for (var li = 0; li < lines.length; li++) {
442
+ var line = lines[li].trim();
443
+ if (line.length === 0) continue;
444
+ var colon = line.indexOf(":");
445
+ if (colon < 0) continue;
446
+ var hashSuffix = line.slice(0, colon).toUpperCase();
447
+ var count = parseInt(line.slice(colon + 1), 10);
448
+ if (timingSafeEqual(Buffer.from(hashSuffix, "utf8"), Buffer.from(suffix, "utf8")) &&
449
+ isFinite(count) && count >= p.breachThreshold) {
450
+ return _fail("breached",
451
+ "plaintext appears in HaveIBeenPwned with count " + count +
452
+ " (threshold " + p.breachThreshold + ")");
453
+ }
454
+ }
455
+ return _ok({ breachCheckCount: 0 });
456
+ }
457
+ return _ok();
458
+ }
459
+
460
+ function shouldRotate(passwordSetAt, now) {
461
+ if (p.mustRotateAfterMs === null) return false;
462
+ if (typeof passwordSetAt !== "number" || !isFinite(passwordSetAt)) {
463
+ throw new AuthError("auth-password/bad-input",
464
+ "shouldRotate: passwordSetAt must be a numeric ms-epoch timestamp");
465
+ }
466
+ var nowMs = typeof now === "number" ? now : Date.now();
467
+ return (nowMs - passwordSetAt) >= p.mustRotateAfterMs;
468
+ }
469
+
470
+ async function reuseProhibited(plaintext, history) {
471
+ if (typeof plaintext !== "string" || plaintext.length === 0) return false;
472
+ if (p.historyMinDistance <= 0) return false;
473
+ if (!Array.isArray(history) || history.length === 0) return false;
474
+ // Check the most-recent N entries (history-min-distance bound).
475
+ var checkCount = Math.min(history.length, p.historyMinDistance);
476
+ for (var i = 0; i < checkCount; i++) {
477
+ if (await _argon2Verify(history[i], plaintext)) return true;
478
+ }
479
+ return false;
480
+ }
481
+
482
+ return {
483
+ check: check,
484
+ shouldRotate: shouldRotate,
485
+ reuseProhibited: reuseProhibited,
486
+ // Operator introspection — handy when an admin tool wants to
487
+ // surface "your policy requires X" to end users.
488
+ describe: function () {
489
+ return {
490
+ minLength: p.minLength,
491
+ maxLength: p.maxLength,
492
+ breachCheck: p.breachCheck,
493
+ mustRotateAfterMs: p.mustRotateAfterMs,
494
+ historyMinDistance: p.historyMinDistance,
495
+ complexity: complexity ? Object.assign({}, complexity) : null,
496
+ dictionaryCount: dictionaryLower.length,
497
+ forbidCommonCount: forbidLower.length,
498
+ bundledCommonCount: bundledSet ? bundledSet.size : 0,
499
+ };
500
+ },
501
+ };
502
+ }
503
+
62
504
  function _validatePlain(plain) {
63
505
  if (typeof plain !== "string" || plain.length === 0) {
64
506
  throw new AuthError("auth-password/invalid-plain",
@@ -132,8 +574,11 @@ function needsRehash(stored, opts) {
132
574
  }
133
575
 
134
576
  module.exports = {
135
- hash: hash,
136
- verify: verify,
137
- needsRehash: needsRehash,
138
- DEFAULT_PARAMS: DEFAULT_PARAMS,
577
+ hash: hash,
578
+ verify: verify,
579
+ needsRehash: needsRehash,
580
+ policy: policy,
581
+ DEFAULT_PARAMS: DEFAULT_PARAMS,
582
+ DEFAULT_POLICY: DEFAULT_POLICY,
583
+ POLICY_PROFILES: POLICY_PROFILES,
139
584
  };
package/lib/cache.js CHANGED
@@ -93,6 +93,7 @@
93
93
  var clusterStorage = require("./cluster-storage");
94
94
  var C = require("./constants");
95
95
  var lazyRequire = require("./lazy-require");
96
+ var numericChecks = require("./numeric-checks");
96
97
  var requestHelpers = require("./request-helpers");
97
98
  var safeAsync = require("./safe-async");
98
99
  var validateOpts = require("./validate-opts");
@@ -116,13 +117,8 @@ var DEFAULTS = Object.freeze({
116
117
 
117
118
  // ---- Config-time validation helpers (throw on bad input) ----
118
119
 
119
- function _isFiniteNonNegative(n) {
120
- return typeof n === "number" && isFinite(n) && n >= 0;
121
- }
122
-
123
- function _isPositiveInt(n) {
124
- return typeof n === "number" && isFinite(n) && n >= 1 && Math.floor(n) === n;
125
- }
120
+ var _isFiniteNonNegative = numericChecks.isFiniteNonNegative;
121
+ var _isPositiveInt = numericChecks.isPositiveInt;
126
122
 
127
123
  // ttlMs accepts: any non-negative finite number OR Infinity. NaN, negative,
128
124
  // or non-number is rejected.