"use strict"; const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const ejs = require("ejs"); const ROOT = path.join(__dirname, ".."); const VIEW_SRC = fs.readFileSync(path.join(ROOT, "views", "maintenance.ejs"), "utf8"); const INDEX_SRC = fs.readFileSync(path.join(ROOT, "index.js"), "utf8"); const MAINTENANCE_EJS = path.join(ROOT, "views", "maintenance.ejs"); // The pre-gate fingerprints the view must never re-gain. The remote branding // references are the leak class: data/branding.json ships a jsDelivr // ADPanel asset URL, and the preconnect/dns-prefetch hints leaked the host // string even when the href pointed elsewhere. test("maintenance.ejs carries no remote/ADPanel-identifying head fingerprints", () => { for (const forbidden of [ "jsdelivr", "preconnect", "dns-prefetch", "faviconUrl", "logoUrl", "adpanel-dark", ]) { assert.ok( !VIEW_SRC.toLowerCase().includes(forbidden.toLowerCase()), `maintenance.ejs must not contain "${forbidden}" — the pre-auth page cannot carry remote ADPanel asset URLs or host hints` ); } }); test("the maintenance page keeps its recovery poll and explanatory copy", () => { // The page must keep serving its purpose during the lockdown: the recovery // poller, the reason banner and the visible copy stay. assert.ok(VIEW_SRC.includes("fetch('/api/maintenance/status'"), "the recovery poller stays"); assert.ok(VIEW_SRC.includes("Under Maintenance"), "the visible title stays"); assert.ok(VIEW_SRC.includes("scheduled maintenance"), "the visible message stays"); }); test("rendered maintenance page does not expose the operator's name or icon", async () => { const remoteBranding = { appName: "Fixture ADPanel", faviconMimeType: "image/webp", faviconUrl: "https://cdn.jsdelivr.net/gh/adpanel/assets@main/favicon.webp", logoUrl: "https://cdn.jsdelivr.net/gh/adpanel/assets@main/logo.webp", }; for (const branding of [remoteBranding, null]) { const rendered = await ejs.renderFile(MAINTENANCE_EJS, { branding, reason: "Database upgrade", cspNonce: "pin-nonce", }); const low = rendered.toLowerCase(); assert.ok(!low.includes("jsdelivr"), "no jsDelivr string survives the render"); assert.ok(!low.includes("preconnect") && !low.includes("dns-prefetch"), "no host-hint link tags survive the render"); assert.doesNotMatch(low, /adpanel|\/auth-assets\/favicon|rel="(?:alternate )?icon"/); assert.match(rendered, /Under Maintenance<\/title>/); assert.ok(rendered.includes("Database upgrade"), "the reason banner still renders"); assert.ok(rendered.includes('nonce="pin-nonce"'), "the CSP nonce still renders"); } }); // adpanel:maintenance-503-no-store — the terminator's two 503 exits (the // /api/ JSON and the HTML render) must set their own no-store: the global // HTML no-store middleware and the /api/ CDN middleware mount AFTER the // terminator, so a terminated request never reaches them. test("both maintenance 503 exits set Cache-Control: no-store before responding", () => { const start = INDEX_SRC.indexOf("app.use(async (req, res, next) => {"); const terminator = INDEX_SRC.indexOf("MAINTENANCE_BYPASS_PATHS", start); assert.notStrictEqual(terminator, -1, "the maintenance terminator exists"); const bodyStart = INDEX_SRC.indexOf("const method = String(req.method || \"\").toUpperCase();", terminator); assert.notStrictEqual(bodyStart, -1, "the terminator body anchor exists"); const body = INDEX_SRC.slice(bodyStart, bodyStart + 6000); assert.ok(body.includes("MAINTENANCE_BYPASS_PREFIXES") && body.includes('render("maintenance"'), "the window is the maintenance terminator"); const apiExit = body.indexOf('res.status(503).json({'); const renderExit = body.indexOf('res.status(503).render("maintenance"'); assert.notStrictEqual(apiExit, -1, "the /api/ 503 JSON exit exists"); assert.notStrictEqual(renderExit, -1, "the HTML 503 render exit exists"); assert.ok(body.includes("adpanel:maintenance-503-no-store"), "the exits carry the convention comment"); const apiHeaderAt = body.lastIndexOf('res.setHeader("Cache-Control", "no-store");', apiExit); assert.notStrictEqual(apiHeaderAt, -1, "the API 503 sets no-store"); assert.ok(apiHeaderAt < apiExit, "the API 503 sets no-store before responding"); const renderHeaderAt = body.lastIndexOf('res.setHeader("Cache-Control", "no-store");', renderExit); assert.notStrictEqual(renderHeaderAt, -1, "the HTML 503 sets no-store"); assert.ok(renderHeaderAt < renderExit, "the HTML 503 sets no-store before responding"); }); test("maintenance allows CAPTCHA sign-in completion without unlocking account routes", async () => { const start = INDEX_SRC.indexOf("const MAINTENANCE_BYPASS_PATHS ="); const end = INDEX_SRC.indexOf("\n});", start); assert.ok(start >= 0 && end > start, "the mounted maintenance middleware exists"); let maintenanceGate; vm.runInNewContext(INDEX_SRC.slice(start, end + "\n});".length), { app: { use(handler) { maintenanceGate = handler; } }, isOpenNodeRoute: async () => false, isMaintenanceActive: () => true, }); for (const [method, pathname, expected] of [ ["POST", "/login", "next"], ["POST", "/api/captcha/verify", "next"], ["POST", "/API/Captcha/Verify/", "next"], ["GET", "/api/captcha/verify", 503], ["POST", "/api/captcha/verify/extra", 503], ["POST", "/api/account/password/change", 503], ]) { let outcome; const res = { setHeader() {}, status(status) { outcome = status; return this; }, json() {}, }; await maintenanceGate({ method, path: pathname, isAdmin: false }, res, (err) => { assert.ifError(err); outcome = "next"; }); assert.strictEqual(outcome, expected, `${method} ${pathname}`); } });