Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,68 @@
|
||||
"use strict";
|
||||
|
||||
// adpanel:credential-generation — the ONE home of session-epoch generation.
|
||||
//
|
||||
// users.session_epoch is the credential-generation discriminator: every
|
||||
// browser session binds it at login/rebind (req.session.accountEpoch) and the
|
||||
// HTTP session middleware plus the Socket.IO handshake re-check that binding
|
||||
// against the live row. THE CONSTRAINT THIS MODULE ENFORCES: an epoch change
|
||||
// is only meaningful in the SAME SQL statement as the credential it
|
||||
// invalidates for. Bumped in a separate statement, a crash or a concurrent
|
||||
// writer landing between the two commits leaves the row's credential and its
|
||||
// generation out of step — a stale credential could authenticate under the
|
||||
// new generation, or a fresh credential under the old one — so every
|
||||
// interactive credential writer (2FA confirm, self-service password changes,
|
||||
// email change, admin password reset, admin 2FA reset) and account suspension
|
||||
// must fold
|
||||
// `session_epoch = ?` into its EXISTING compare-and-swap UPDATE. Never issue
|
||||
// a second UPDATE for the epoch, and never bump an unrelated writer
|
||||
// (a lost final compare-and-swap must not have already bumped:
|
||||
// the recovery-code burn, the TOTP counter claim and the login bcrypt rehash
|
||||
// stay epoch-free).
|
||||
//
|
||||
// Generation is CSPRNG in the established range — crypto.randomInt(1,
|
||||
// 2147483647) — the exact convention the panel's user-provisioning INSERT and
|
||||
// the CLI have always minted with (pinned by
|
||||
// scripts/verify-user-lifecycle-session-revocation.js). Never 0: the column
|
||||
// default 0 means "no generation" (legacy and boot-migrated rows), so a real
|
||||
// generation must never collide with it. The value is security-relevant
|
||||
// discriminator material, not operation detail: never a predictable
|
||||
// increment, never logged, never returned in a response body.
|
||||
|
||||
const crypto = require("node:crypto");
|
||||
|
||||
// The established generation range, shared by every epoch write since the
|
||||
// column was introduced: [1, 2147483647] — positive, INT-safe, and never the
|
||||
// 0 default.
|
||||
const SESSION_EPOCH_MIN = 1;
|
||||
const SESSION_EPOCH_MAX = 2147483647;
|
||||
|
||||
// Fresh unpredictable generation. crypto.randomInt is rejection-sampled
|
||||
// internally and uniformly distributed over the range.
|
||||
function newSessionEpoch() {
|
||||
return crypto.randomInt(SESSION_EPOCH_MIN, SESSION_EPOCH_MAX);
|
||||
}
|
||||
|
||||
// Fresh generation guaranteed different from `currentEpoch`: a bump that
|
||||
// landed on the row's PRESENT generation would re-validate every session the
|
||||
// bump was meant to tear down — their bound epoch would still match the row.
|
||||
// Absent/NULL/malformed currents coerce to the column default 0, which a real
|
||||
// generation can never equal, so legacy rows need no special case. Bounded
|
||||
// retry (8 attempts): a collision is a ~2^-31 event per draw, so the retry
|
||||
// cannot realistically exhaust; the generator is CSPRNG, never a
|
||||
// deterministic "current + 1" fallback.
|
||||
function newSessionEpochDifferentFrom(currentEpoch) {
|
||||
const current = Number(currentEpoch);
|
||||
let candidate = newSessionEpoch();
|
||||
for (let attempt = 0; attempt < 8 && candidate === current; attempt += 1) {
|
||||
candidate = newSessionEpoch();
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
newSessionEpoch,
|
||||
newSessionEpochDifferentFrom,
|
||||
SESSION_EPOCH_MIN,
|
||||
SESSION_EPOCH_MAX,
|
||||
};
|
||||
Reference in new issue
Block a user