61 lines
3.6 KiB
JavaScript
61 lines
3.6 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:session-account-binding — ONE definition of "this session still
|
|
// belongs to this account row", shared by the HTTP session middleware and the
|
|
// Socket.IO handshake so the two auth surfaces cannot drift. Sessions minted
|
|
// before accountId/accountEpoch existed pass (covered by the revocation sweep).
|
|
// A missing session or an unresolved user row can never PROVE the binding, so
|
|
// the predicate fails closed; both real call sites already resolve a session
|
|
// and a user row before calling — the guard exists so a future caller cannot
|
|
// turn an unresolved row into an implicit pass.
|
|
function sessionAccountBindingMismatch(session, user) {
|
|
if (!session || !user) return true;
|
|
const idMismatch = Number.isFinite(session.accountId) && Number(user.id) !== session.accountId;
|
|
const epochMismatch = Number.isFinite(session.accountEpoch)
|
|
&& Number.isFinite(user.session_epoch)
|
|
&& Number(user.session_epoch) !== session.accountEpoch;
|
|
return idMismatch || epochMismatch;
|
|
}
|
|
|
|
// adpanel:credential-generation — ONE strict comparison for "was this proof
|
|
// minted against the credential generation that is live now". A proof verified
|
|
// before a credential change must never be promoted onto the new generation:
|
|
// when both sides carry comparable epochs, any difference is a mismatch. An
|
|
// absent or NULL epoch on either side is UNCOMPARABLE (never a coercion to 0
|
|
// and never an implicit pass by itself), and so is any value Number() cannot
|
|
// render finite (NaN, Infinity, non-numeric strings). NUMERIC STRINGS ARE
|
|
// COMPARABLE — "3" equals 3 — deliberately matching sessionAccountBinding-
|
|
// Mismatch's documented tolerance for drivers that return numeric strings, and
|
|
// Number()'s grammar extends that tolerance to value-like coercions (" " and
|
|
// "" read as 0, booleans as 1/0, [3] as 3): every real epoch source is a
|
|
// server-side number (BIGINT rows via mapUserRow's Number(), or a minted
|
|
// Number()), so those coercions are unreachable, the deny direction holds
|
|
// regardless ("" vs any bumped generation still mismatches), and callers MUST
|
|
// pair this predicate with an unconditional credential-fingerprint re-check
|
|
// (password + secret), which stays authoritative even on a legacy read whose
|
|
// row lacks the epoch column. Proof records minted by current code (e.g. the
|
|
// pending captcha login record) enforce finiteness of their own recorded epoch
|
|
// at the call site before consulting this predicate; a record without one
|
|
// skips the epoch leg and rides the fingerprint leg alone.
|
|
function credentialGenerationMismatch(provedEpoch, liveEpoch) {
|
|
// null must not ride Number()'s null→0 coercion: an explicitly absent value
|
|
// is uncomparable, never epoch 0.
|
|
const proved = provedEpoch == null ? NaN : Number(provedEpoch);
|
|
const live = liveEpoch == null ? NaN : Number(liveEpoch);
|
|
if (!Number.isFinite(proved) || !Number.isFinite(live)) return false;
|
|
return proved !== live;
|
|
}
|
|
|
|
// adpanel:account-revocation-identity - an email can be reassigned; a known
|
|
// account ID takes precedence over email, which is only a legacy fallback.
|
|
function matchesAccountForRevocation(targetId, targetEmail, recordId, recordEmail, { unboundOnly = false } = {}) {
|
|
const expectedId = String(targetId ?? "").trim();
|
|
const actualId = String(recordId ?? "").trim();
|
|
if (unboundOnly && actualId) return false;
|
|
if (expectedId && actualId) return expectedId === actualId;
|
|
const email = String(targetEmail ?? "").trim().toLowerCase();
|
|
return !!email && email === String(recordEmail ?? "").trim().toLowerCase();
|
|
}
|
|
|
|
module.exports = { sessionAccountBindingMismatch, credentialGenerationMismatch, matchesAccountForRevocation };
|