157 lines
6.9 KiB
JavaScript
157 lines
6.9 KiB
JavaScript
"use strict";
|
|
|
|
// Tests for the server-rendered 2FA gating of the dashboard's Account >
|
|
// Credentials tab.
|
|
//
|
|
// A user without 2FA must not see the "Recovery Codes" row or the "Change
|
|
// 2FA" management row: recovery-code management is meaningless without 2FA,
|
|
// so the non-2FA state gets a single activation entry ("Enable 2FA") that
|
|
// starts the existing enrollment flow instead. The gating is rendered
|
|
// server-side (EJS) rather than hidden via client JS, so a tampered client
|
|
// finds no recovery-code DOM at all; defense in depth comes from the
|
|
// regenerate API, which already rejects accounts without 2FA.
|
|
//
|
|
// index.js boots servers on require, so the render-locals contract is locked
|
|
// with source assertions (same technique as
|
|
// tests/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 INDEX_EJS = path.join(ROOT, "views", "index.ejs");
|
|
|
|
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
|
const indexEjsSource = fs.readFileSync(INDEX_EJS, "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);
|
|
}
|
|
|
|
// The credentials block under test, bounded by the list container and the
|
|
// next top-level modal, so the recovery modals further down the file (part
|
|
// of the regen/enrollment flows, not the credentials list) never leak into
|
|
// these assertions.
|
|
function credentialsListSource() {
|
|
return sliceBetween(
|
|
indexEjsSource,
|
|
'<div class="account-credentials-list">',
|
|
'<div id="dashboardAssistantDeleteModal"'
|
|
);
|
|
}
|
|
|
|
function conditionalBranches() {
|
|
const list = credentialsListSource();
|
|
const openMarker = "<% if (typeof dashboardHas2FA !== 'undefined' && dashboardHas2FA) { %>";
|
|
const openAt = list.indexOf(openMarker);
|
|
assert.ok(openAt >= 0, "the credentials rows must sit behind the dashboardHas2FA conditional");
|
|
const elseMarker = "<% } else { %>";
|
|
const elseAt = list.indexOf(elseMarker, openAt);
|
|
assert.ok(elseAt > openAt, "the conditional must have a falsy branch");
|
|
const closeMarker = "<% } %>";
|
|
const closeAt = list.indexOf(closeMarker, elseAt);
|
|
assert.ok(closeAt > elseAt, "the conditional must be closed");
|
|
return {
|
|
truthy: list.slice(openAt + openMarker.length, elseAt),
|
|
falsy: list.slice(elseAt + elseMarker.length, closeAt)
|
|
};
|
|
}
|
|
|
|
test("the credentials 2FA/recovery rows are gated behind a server-rendered dashboardHas2FA conditional", () => {
|
|
const list = credentialsListSource();
|
|
assert.ok(
|
|
list.includes("<% if (typeof dashboardHas2FA !== 'undefined' && dashboardHas2FA) { %>"),
|
|
"the guard must tolerate a missing local AND be falsy for non-2FA accounts"
|
|
);
|
|
assert.ok(list.includes("<% } else { %>"), "a falsy branch must exist for non-2FA accounts");
|
|
});
|
|
|
|
test("the Recovery Codes row only exists inside the truthy branch", () => {
|
|
const list = credentialsListSource();
|
|
const openAt = list.indexOf("<% if (typeof dashboardHas2FA");
|
|
const elseAt = list.indexOf("<% } else { %>", openAt);
|
|
assert.ok(openAt >= 0 && elseAt > openAt, "conditional structure must be intact");
|
|
const truthy = list.slice(openAt, elseAt);
|
|
const falsy = list.slice(elseAt, list.indexOf("<% } %>", elseAt));
|
|
|
|
assert.equal(
|
|
(list.match(/id="regenerateRecoveryBtn"/g) || []).length,
|
|
1,
|
|
"the recovery button must be rendered from exactly one place"
|
|
);
|
|
assert.ok(truthy.includes('id="regenerateRecoveryBtn"'), "the truthy branch keeps the Recovery Codes row");
|
|
assert.ok(!falsy.includes("regenerateRecoveryBtn"), "the falsy branch must not render the recovery button");
|
|
// Position check: the recovery row may only appear after the conditional opens.
|
|
const btnAt = list.indexOf('id="regenerateRecoveryBtn"');
|
|
assert.ok(btnAt > openAt, "the recovery row must sit inside the conditional, not before it");
|
|
});
|
|
|
|
test("the falsy branch renders a single Enable 2FA activation row and no recovery entry points", () => {
|
|
const { falsy } = conditionalBranches();
|
|
assert.ok(
|
|
falsy.includes('<button class="credential-action-btn" id="change2faBtn" type="button">Enable 2FA</button>'),
|
|
"the falsy branch must render the Enable 2FA activation button"
|
|
);
|
|
assert.ok(falsy.includes("#icon-shield-icon"), "the activation row keeps the 2FA shield icon");
|
|
assert.ok(!falsy.includes("regenerateRecoveryBtn"), "no recovery button may exist for non-2FA accounts");
|
|
assert.ok(!falsy.includes("Recovery Codes"), "no recovery-codes text may exist for non-2FA accounts");
|
|
assert.ok(
|
|
falsy.includes("Protect your account with an authenticator app"),
|
|
"the activation row carries the enrollment-oriented description"
|
|
);
|
|
});
|
|
|
|
test("the truthy branch keeps today's 2FA management rows unchanged", () => {
|
|
const { truthy } = conditionalBranches();
|
|
assert.ok(
|
|
truthy.includes('<button class="credential-action-btn" id="change2faBtn" type="button">Change 2FA</button>'),
|
|
"2FA users keep the Change 2FA button"
|
|
);
|
|
assert.ok(
|
|
truthy.includes("View and regenerate your one-time recovery codes"),
|
|
"2FA users keep the Recovery Codes description"
|
|
);
|
|
assert.ok(
|
|
truthy.includes("Change your authentication app 2FA Code"),
|
|
"2FA users keep the 2FA row description"
|
|
);
|
|
});
|
|
|
|
test("the Change Password and Change Email rows stay outside the conditional", () => {
|
|
const list = credentialsListSource();
|
|
const { truthy, falsy } = conditionalBranches();
|
|
assert.ok(!truthy.includes('id="changePasswordBtn"'), "the password row is shared, not duplicated per branch");
|
|
assert.ok(!falsy.includes('id="changePasswordBtn"'), "the password row is shared, not duplicated per branch");
|
|
assert.ok(!truthy.includes('id="changeEmailBtn"'), "the email row is shared, not duplicated per branch");
|
|
assert.ok(!falsy.includes('id="changeEmailBtn"'), "the email row is shared, not duplicated per branch");
|
|
assert.equal(
|
|
(list.match(/id="changePasswordBtn"/g) || []).length,
|
|
1,
|
|
"exactly one Change Password row may exist"
|
|
);
|
|
assert.equal(
|
|
(list.match(/id="changeEmailBtn"/g) || []).length,
|
|
1,
|
|
"exactly one Change Email row may exist"
|
|
);
|
|
});
|
|
|
|
test("GET / passes dashboardHas2FA computed via hasTwoFactorEnabled(userObj) to the template", () => {
|
|
const renderCall = sliceBetween(indexSource, 'return res.render("index", {', "});");
|
|
assert.ok(
|
|
renderCall.includes("dashboardHas2FA: userObj ? hasTwoFactorEnabled(userObj) : false,"),
|
|
"the render locals must carry the boolean from the same evaluation GET /api/account uses"
|
|
);
|
|
assert.ok(
|
|
!renderCall.includes("dashboardHas2FA: {"),
|
|
"only the boolean may be exposed, not any richer 2FA state"
|
|
);
|
|
});
|