"use strict"; // adpanel:env-int-clamp — ONE home for "read an integer from the environment // and clamp it into a documented band" across the login-hardening modules // (preauth-session, login-throttle, bcrypt-rounds). // The modules each carried a private 5-line copy of this resolver and // the copies had already drifted semantically (BCRYPT_ROUNDS="0" fell back // to the default 12 while PREAUTH_SESSION_MAX_AGE_MS="0" clamped to its // floor), which is exactly the divergence AGENTS.md forbids. The zero/negative // policy is therefore an EXPLICIT parameter so each variable's documented // semantics cannot be silently changed by copying another module's pattern: // - zeroPolicy "floor" (default): every finite parse clamps into // [min, max] — a parsed "0" (or negative) is nonsense for a // timeout/budget/count and lands on the documented floor, so it can // never disable the control. // - zeroPolicy "default": a non-positive parse means "not configured" and // yields the fallback (BCRYPT_ROUNDS: the pre-consolidation resolver // treated 0 and negatives as unusable and fell back to the documented // default, never to the band floor). // The lenient parseInt semantics are preserved verbatim from the copies this // helper replaced: radix 10, prefix parsing ("12abc" → 12), decimal // truncation ("12.7" → 12), leading whitespace tolerated, and any // non-finite result (unset, empty, garbage) yields the fallback. function resolveClampedInt(env, name, { fallback, min, max, zeroPolicy = "floor" } = {}) { const n = parseInt(String(env[name] ?? ""), 10); if (!Number.isFinite(n)) return fallback; if (zeroPolicy === "default" && n <= 0) return fallback; return Math.min(Math.max(n, min), max); } module.exports = { resolveClampedInt };