Files
Kelx/utils/step-up-proof-strength.js
2026-11-30 16:32:37 +00:00

96 lines
4.9 KiB
JavaScript

"use strict";
// adpanel:stepup-proof-strength — a destructive-action grace grant must never
// satisfy a STRONGER step-up verification requirement than the proof that
// minted it. Grants used to record no proof strength, so a grant minted by the
// legacy either/or verification (TOTP alone — no password ever checked) could
// silently unlock a credentialFlow "password-then-totp" gate, whose entire
// security property is that the password is proven FIRST. This module is the
// ONE home of the strength vocabulary, of the mapping from the verification
// branch actually taken to the strength it achieved, and of the conservative
// satisfaction rule used at consumption time. index.js records the achieved
// strength on every grant (DB column + memory mirror + session state, DB row
// being the source of truth) and enforces the rule before consuming a grant
// for a password-then-totp gate.
//
// Value space (DB column destructive_action_grants.proof_strength, NULL = unknown):
// "password+totp" — password-then-totp flow: password verified, then TOTP verified
// "password" — password verified, no TOTP involved (either/or with 2FA off,
// or password-then-totp for a 2FA-less admin)
// "totp" — legacy either/or TOTP-only proof (2FA-enabled admin, no password)
// NULL / unknown — legacy rows, degraded DB, or an unrecorded proof: NEVER
// satisfies a password-then-totp requirement (conservative;
// a 5-minute TTL bounds how long such an in-flight grant lives)
const STEP_UP_PROOF_STRENGTHS = Object.freeze({
PASSWORD_TOTP: "password+totp",
PASSWORD: "password",
TOTP: "totp",
});
// Rank = how much a strength can satisfy. The order is exactly the subset
// relation over satisfiable requirements: "totp" satisfies no
// password-then-totp requirement, "password" satisfies only the 2FA-less one,
// "password+totp" satisfies both — so a plain rank comparison implements the
// satisfaction table below without a second mechanism to drift.
const STEP_UP_PROOF_STRENGTH_RANK = Object.freeze({
[STEP_UP_PROOF_STRENGTHS.TOTP]: 1,
[STEP_UP_PROOF_STRENGTHS.PASSWORD]: 2,
[STEP_UP_PROOF_STRENGTHS.PASSWORD_TOTP]: 3,
});
// NULL/unknown/anything-unrecognized collapses to null: consumers treat it as
// the weakest possible proof and fail closed.
function normalizeStepUpProofStrength(value) {
const raw = String(value == null ? "" : value).trim();
return STEP_UP_PROOF_STRENGTH_RANK[raw] ? raw : null;
}
// The strength ACHIEVED by a completed verifyDestructiveActionStepUp, computed
// from the branch actually taken (flow = options.credentialFlow passed to that
// verification, authMethod = its success return). Unknown flow/method values
// yield null — an unrecordable proof must never be recorded as a strong one.
function achievedStepUpProofStrength(flow, authMethod) {
const method = String(authMethod || "");
if (method !== "2fa" && method !== "password") return null;
if (flow === "password-then-totp") {
// Password-first: the password was ALWAYS verified; the TOTP step ran
// only for a 2FA-enabled admin.
return method === "2fa" ? STEP_UP_PROOF_STRENGTHS.PASSWORD_TOTP : STEP_UP_PROOF_STRENGTHS.PASSWORD;
}
// Legacy either/or: a 2FA-enabled admin proves the TOTP code ALONE — no
// password was checked, so the grant must never satisfy a password-first
// requirement. A 2FA-less admin's proof is the password itself.
return method === "2fa" ? STEP_UP_PROOF_STRENGTHS.TOTP : STEP_UP_PROOF_STRENGTHS.PASSWORD;
}
// The strength a gate REQUIRES of a grace grant before the grant may satisfy
// it. Only credentialFlow "password-then-totp" gates impose one (a 2FA-enabled
// admin demands the full password+TOTP proof, a 2FA-less admin the password);
// every either/or gate returns null — no requirement, grants keep working for
// them exactly as before (per-surface contracts preserved).
function requiredStepUpProofStrength(flow, twoFactorEnabled) {
if (flow !== "password-then-totp") return null;
return twoFactorEnabled ? STEP_UP_PROOF_STRENGTHS.PASSWORD_TOTP : STEP_UP_PROOF_STRENGTHS.PASSWORD;
}
// May a grant with the achieved strength satisfy a gate requiring
// requiredFlowStrength? Deliberately conservative: "totp" and NULL/unknown
// NEVER satisfy; "password+totp" satisfies everything; "password" only the
// password-level requirement.
function proofStrengthSatisfies(achieved, requiredFlowStrength) {
const achievedStrength = normalizeStepUpProofStrength(achieved);
const required = normalizeStepUpProofStrength(requiredFlowStrength);
if (!achievedStrength || !required) return false;
return STEP_UP_PROOF_STRENGTH_RANK[achievedStrength] >= STEP_UP_PROOF_STRENGTH_RANK[required];
}
module.exports = {
STEP_UP_PROOF_STRENGTHS,
STEP_UP_PROOF_STRENGTH_RANK,
normalizeStepUpProofStrength,
achievedStepUpProofStrength,
requiredStepUpProofStrength,
proofStrengthSatisfies,
};