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,263 @@
|
||||
"use strict";
|
||||
|
||||
// Pins the password-first step-up contract for admin actions performed ON
|
||||
// ANOTHER USER from the settings Accounts tab (product decision: the
|
||||
// "peer-admin target proof" is removed; the step-up becomes the ACTING
|
||||
// admin's current password first, then the ACTING admin's own 2FA code only
|
||||
// when they have 2FA enabled).
|
||||
//
|
||||
// Contract locked here:
|
||||
// - The nine opted-in gates pass credentialFlow: "password-then-totp".
|
||||
// (2026-11: the admin 2FA reset gate moved with its handler into
|
||||
// services/admin-2fa-reset.js — the pre-flight guard consolidation — so
|
||||
// its source is pinned there; the other eight remain in index.js.)
|
||||
// - In that flow the password is verified BEFORE any TOTP check (textual
|
||||
// order), the challenge always asks for the password, and the challenge
|
||||
// payload tells the client whether a TOTP step follows
|
||||
// (requiresTwoFactor/twoFactorEnabled) without changing the
|
||||
// revokeSecurity shape.
|
||||
// - The peer-admin target proof (requireTargetAdminCredentialProof /
|
||||
// hasTargetCredentialPayload / targetPassword / targetTwoFactorCode) is
|
||||
// gone from index.js entirely.
|
||||
// - Every other step-up gate (nodes, webhooks, maintenance, ...) keeps
|
||||
// today's either/or behavior.
|
||||
//
|
||||
// index.js boots servers on require, so route-level behavior is locked with
|
||||
// source assertions (same technique as audit-l1-l2-l4-l6-hardening.test.js).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const ROOT = path.join(__dirname, "..");
|
||||
const INDEX_JS = path.join(ROOT, "index.js");
|
||||
const RESET_MODULE_JS = path.join(ROOT, "services", "admin-2fa-reset.js");
|
||||
|
||||
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
||||
const resetModuleSource = fs.readFileSync(RESET_MODULE_JS, "utf8");
|
||||
|
||||
function sliceBetween(source, startMarker, endMarker) {
|
||||
const start = source.indexOf(startMarker);
|
||||
assert.ok(start >= 0, `start marker not found: ${startMarker}`);
|
||||
const end = source.indexOf(endMarker, start + startMarker.length);
|
||||
assert.ok(end > start, `end marker not found after start: ${endMarker}`);
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
function countOccurrences(haystack, needle) {
|
||||
return haystack.split(needle).length - 1;
|
||||
}
|
||||
|
||||
// Slice covering one step-up gate: from the start of its
|
||||
// requireDestructiveActionStepUp(...) call (the subject string is an argument
|
||||
// of that call) through the gate's `if (!stepUp.ok) return;`.
|
||||
function stepUpGateSlice(source, subject) {
|
||||
const subjectAt = source.indexOf(subject);
|
||||
assert.ok(subjectAt >= 0, `step-up subject not found: ${subject}`);
|
||||
const callStart = source.lastIndexOf("requireDestructiveActionStepUp(", subjectAt);
|
||||
assert.ok(
|
||||
callStart >= 0 && subjectAt - callStart < 400,
|
||||
`the subject must be an argument of a requireDestructiveActionStepUp call: ${subject}`
|
||||
);
|
||||
const gateEnd = source.indexOf("if (!stepUp.ok) return;", subjectAt);
|
||||
assert.ok(gateEnd > subjectAt, `the step-up call for ${subject} must be followed by its ok check`);
|
||||
return source.slice(callStart, gateEnd);
|
||||
}
|
||||
|
||||
const CREDENTIAL_FLOW = 'credentialFlow: "password-then-totp"';
|
||||
|
||||
// The nine Accounts-tab gates, each anchored on its unique step-up subject
|
||||
// string, plus the source each gate lives in. The window between the subject
|
||||
// and the gate's `if (!stepUp.ok)` must opt into the password-first flow.
|
||||
// The admin 2FA reset gate lives in services/admin-2fa-reset.js (it moved
|
||||
// there with the shared handler); every other gate stays in index.js.
|
||||
const OPTED_IN_STEP_UP_SUBJECTS = [
|
||||
['"Admin 2FA reset"', "admin 2FA reset (reset-2fa / confirm-2fa-reset)", resetModuleSource],
|
||||
['"Admin recovery codes reset"', "recovery codes reset", indexSource],
|
||||
['Access grant "${emailLower}" on "${server}"', "accounts/:email/add", indexSource],
|
||||
['Access revoke "${emailLower}" on "${server}"', "accounts/:email/remove", indexSource],
|
||||
['Agent access ${enabled ? "enable" : "disable"} "${email}"', "accounts/:email/agent-access", indexSource],
|
||||
['Permission grant "${email}" on "${canonicalServer}"', "accounts/:email/grant-perms", indexSource],
|
||||
['nextSuspended ? "Account suspend" : "Account unsuspend"', "accounts/:email/suspension", indexSource],
|
||||
['Account delete "${targetEmailLower}"', "DELETE accounts/:email", indexSource],
|
||||
['Password reset for "${targetEmail}"', "accounts/:email/change-password", indexSource],
|
||||
];
|
||||
|
||||
test("all nine Accounts-tab step-up gates opt into credentialFlow password-then-totp", () => {
|
||||
for (const [subject, label, source] of OPTED_IN_STEP_UP_SUBJECTS) {
|
||||
const gate = stepUpGateSlice(source, subject);
|
||||
assert.ok(
|
||||
gate.includes(CREDENTIAL_FLOW),
|
||||
`${label} must pass ${CREDENTIAL_FLOW}`
|
||||
);
|
||||
}
|
||||
|
||||
// Exactly nine opt-ins across the two sources: no other gate may silently
|
||||
// join the flow. (8 in index.js + the moved 2FA-reset gate in the module.)
|
||||
assert.equal(
|
||||
countOccurrences(indexSource, CREDENTIAL_FLOW),
|
||||
8,
|
||||
"the eight index.js Accounts-tab gates may pass credentialFlow password-then-totp"
|
||||
);
|
||||
assert.equal(
|
||||
countOccurrences(resetModuleSource, CREDENTIAL_FLOW),
|
||||
1,
|
||||
"the moved admin 2FA reset gate opts in exactly once in services/admin-2fa-reset.js"
|
||||
);
|
||||
});
|
||||
|
||||
test("verifyDestructiveActionStepUp checks the current password BEFORE the TOTP in the password-first branch", () => {
|
||||
const fn = sliceBetween(
|
||||
indexSource,
|
||||
"async function verifyDestructiveActionStepUp(",
|
||||
"function buildStepUpRequiredBody("
|
||||
);
|
||||
|
||||
const branchStart = fn.indexOf('options.credentialFlow === "password-then-totp"');
|
||||
assert.ok(branchStart >= 0, "the password-first branch must exist");
|
||||
const branch = fn.slice(branchStart);
|
||||
|
||||
assert.ok(fn.includes("parseDestructiveActionCredentials(req.body)"), "both credential fields must be strictly parsed before verification");
|
||||
const passwordRead = branch.indexOf("credentials.currentPassword");
|
||||
const passwordVerify = branch.indexOf("verifyPasswordForUser(currentUser, currentPassword");
|
||||
const totpVerify = branch.indexOf("verifyTotpForUser(currentUser, credentials.twoFactorCode)");
|
||||
assert.ok(passwordRead >= 0, "the password-first branch must read the strictly parsed current password");
|
||||
assert.ok(passwordVerify >= 0, "the password-first branch must verify the password");
|
||||
assert.ok(totpVerify >= 0, "the password-first branch must verify the TOTP code");
|
||||
assert.ok(
|
||||
passwordRead < passwordVerify && passwordVerify < totpVerify,
|
||||
"textual order must be: read password -> verify password -> verify TOTP"
|
||||
);
|
||||
|
||||
// The 403 ladder follows the same order: password challenge, password
|
||||
// rejection, then the TOTP challenge and rejection.
|
||||
const passwordRequired = branch.indexOf('"password-required"');
|
||||
const invalidPassword = branch.indexOf('"invalid-password"');
|
||||
const twoFactorRequired = branch.indexOf('"2fa-required"');
|
||||
const invalidTwoFactor = branch.indexOf('"invalid-2fa"');
|
||||
assert.ok(passwordRequired >= 0 && invalidPassword >= 0 && twoFactorRequired >= 0 && invalidTwoFactor >= 0,
|
||||
"the password-first branch must emit all four credential error codes");
|
||||
assert.ok(
|
||||
passwordRequired < invalidPassword && invalidPassword < twoFactorRequired && twoFactorRequired < invalidTwoFactor,
|
||||
"the error ladder must be password-required, invalid-password, 2fa-required, invalid-2fa"
|
||||
);
|
||||
|
||||
// Success consumes the code (verifyTotpForUser default) — no pre-checking.
|
||||
assert.ok(
|
||||
!branch.includes("consume: false"),
|
||||
"the password-first branch must not pass consume:false; a successful code is consumed"
|
||||
);
|
||||
|
||||
// Downstream authMethod consumers keep working.
|
||||
assert.ok(branch.includes('authMethod: "2fa"'), "a 2FA-enabled admin verifies via 2fa");
|
||||
assert.ok(branch.includes('authMethod: "password"'), "a 2FA-less admin verifies via password");
|
||||
});
|
||||
|
||||
test("buildStepUpRequiredBody challenges with the password first in password-first mode", () => {
|
||||
const fn = sliceBetween(
|
||||
indexSource,
|
||||
"function buildStepUpRequiredBody(",
|
||||
"async function persistDestructiveActionGrace("
|
||||
);
|
||||
|
||||
assert.ok(
|
||||
fn.includes('options.credentialFlow === "password-then-totp"'),
|
||||
"the challenge must detect the password-first flow"
|
||||
);
|
||||
// In password-first mode the challenge asks for the password regardless of
|
||||
// the admin's 2FA state — the flow check must not be gated on !twoFactorEnabled.
|
||||
assert.match(
|
||||
fn,
|
||||
/const passwordMode = options\.credentialFlow === "password-then-totp" \|\| !twoFactorEnabled;/,
|
||||
"password-first mode must force the password challenge for every 2FA state"
|
||||
);
|
||||
assert.ok(fn.includes('"Current password is required"'), "the password challenge text must stay");
|
||||
assert.ok(fn.includes('"password-required"'), "the password challenge code must stay");
|
||||
|
||||
// The revokeSecurity payload shape is unchanged; requiresTwoFactor keeps
|
||||
// reflecting the admin's own 2FA state so the client can key the TOTP step.
|
||||
const security = sliceBetween(fn, "const security = {", "return {");
|
||||
for (const field of [
|
||||
"twoFactorEnabled",
|
||||
"authMethod",
|
||||
"requiresVerification",
|
||||
"requiresTwoFactor",
|
||||
"graceExpiresAt",
|
||||
"graceUsesRemaining",
|
||||
"graceUsesLimit",
|
||||
]) {
|
||||
assert.ok(security.includes(field), `revokeSecurity must keep the ${field} field`);
|
||||
}
|
||||
assert.ok(
|
||||
security.includes("requiresTwoFactor: twoFactorEnabled"),
|
||||
"requiresTwoFactor must reflect the acting admin's 2FA state"
|
||||
);
|
||||
});
|
||||
|
||||
test("requireDestructiveActionStepUp threads the options into both the challenge and the verification", () => {
|
||||
const fn = sliceBetween(
|
||||
indexSource,
|
||||
"async function requireDestructiveActionStepUp(",
|
||||
"async function requireDatabaseInstallStepUp("
|
||||
);
|
||||
assert.ok(
|
||||
fn.includes("buildStepUpRequiredBody(req, currentUser, stepUpOptions)"),
|
||||
"the no-payload challenge must receive the step-up options"
|
||||
);
|
||||
assert.ok(
|
||||
fn.includes("verifyDestructiveActionStepUp(req, currentUser, subject, stepUpOptions)"),
|
||||
"the credential verification must receive the step-up options"
|
||||
);
|
||||
// Challenge and verification failures keep attaching revokeSecurity + the
|
||||
// caller's failureResponseExtras.
|
||||
assert.match(fn, /revokeSecurity: await getDestructiveActionSecurityAsync\(req, currentUser\)/);
|
||||
assert.match(fn, /buildStepUpFailureResponseExtras\(stepUpOptions\)/);
|
||||
|
||||
// The database-install gate keeps its legacy behavior: no options passed.
|
||||
const installFn = sliceBetween(
|
||||
indexSource,
|
||||
"async function requireDatabaseInstallStepUp(",
|
||||
"const SESSION_DISPLAY_MIN_TRUNCATABLE_LENGTH = 22;"
|
||||
);
|
||||
assert.ok(
|
||||
installFn.includes("verifyDestructiveActionStepUp(req, currentUser, subject)"),
|
||||
"requireDatabaseInstallStepUp must keep the legacy either/or verification"
|
||||
);
|
||||
assert.ok(!installFn.includes("credentialFlow"), "requireDatabaseInstallStepUp must not opt into the password-first flow");
|
||||
});
|
||||
|
||||
test("the peer-admin target proof is gone from index.js entirely", () => {
|
||||
for (const banned of [
|
||||
"requireTargetAdminCredentialProof",
|
||||
"hasTargetCredentialPayload",
|
||||
"targetTwoFactorCode",
|
||||
"targetPassword",
|
||||
"targetProofRequired",
|
||||
"target-2fa-required",
|
||||
"invalid-target-2fa",
|
||||
"target-password-required",
|
||||
"invalid-target-password",
|
||||
"Target proof",
|
||||
"bcrypt-target-proof-timeout",
|
||||
]) {
|
||||
assert.ok(
|
||||
!indexSource.includes(banned),
|
||||
`index.js must no longer contain "${banned}"`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("the non-opted step-up gates keep today's either/or behavior", () => {
|
||||
for (const [subject, label] of [
|
||||
['Node delete "${node.name || nodeId}"', "node delete"],
|
||||
['Webhook delete "${category}"', "webhook delete"],
|
||||
['"Maintenance activation"', "maintenance activation"],
|
||||
]) {
|
||||
const gate = stepUpGateSlice(indexSource, subject);
|
||||
assert.ok(
|
||||
!gate.includes("credentialFlow"),
|
||||
`${label} must NOT pass credentialFlow (legacy either/or flow)`
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user