"use strict"; 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 BRANDING_JS = path.join(ROOT, "utils", "branding.js"); const SETTINGS_EJS = path.join(ROOT, "views", "settings.ejs"); const LOGIN_EJS = path.join(ROOT, "views", "login.ejs"); const STATUS_EJS = path.join(ROOT, "views", "status.ejs"); const indexSource = fs.readFileSync(INDEX_JS, "utf8"); const brandingSource = fs.readFileSync(BRANDING_JS, "utf8"); const settingsEjsSource = fs.readFileSync(SETTINGS_EJS, "utf8"); const loginEjsSource = fs.readFileSync(LOGIN_EJS, "utf8"); const statusEjsSource = fs.readFileSync(STATUS_EJS, "utf8"); function endIndexOfFunction(source, startMarker) { const start = source.indexOf(startMarker); assert.ok(start >= 0, `marker ${startMarker} not found`); const bodyStart = source.indexOf("{", start); let depth = 0; for (let i = bodyStart; i < source.length; i += 1) { if (source[i] === "{") depth += 1; else if (source[i] === "}") { depth -= 1; if (depth === 0) return i + 1; } } assert.fail(`unbalanced braces after marker ${startMarker}`); } test("logo consistency: /auth-assets/logo checks remote URL before default fallback", () => { const start = indexSource.indexOf('app.get("/auth-assets/logo"'); const end = endIndexOfFunction(indexSource.slice(start), "async (req, res) => {") + start; const handler = indexSource.slice(start, end); const localLogoAt = handler.indexOf("branding.localLogoPath"); const remoteLogoAt = handler.indexOf("branding.logoUrl"); const defaultPathAt = handler.indexOf("getDefaultLogoPath()"); assert.ok(localLogoAt >= 0, "admin-uploaded logo must be checked first"); assert.ok(remoteLogoAt > localLogoAt, "configured remote logo URL must come before built-in default"); assert.ok(defaultPathAt > remoteLogoAt, "built-in default must come after configured remote logo URL"); assert.ok(handler.includes("sendPrivateRemoteAsset"), "remote logo must be proxied via sendPrivateRemoteAsset"); }); test("logo consistency: app.get('/settings') does not overwrite res.locals.branding", () => { const start = indexSource.indexOf('app.get("/settings"'); const end = endIndexOfFunction(indexSource.slice(start), "async (req, res) => {") + start; const handler = indexSource.slice(start, end); assert.ok(!handler.includes("branding: loadBrandingConfig()"), "settings route must not overwrite res.locals.branding with raw disk JSON"); }); test("logo consistency: app.get('/status') does not overwrite res.locals.branding", () => { const start = indexSource.indexOf('app.get("/status"'); const end = endIndexOfFunction(indexSource.slice(start), "async (req, res) => {") + start; const handler = indexSource.slice(start, end); assert.ok(!handler.includes("branding: brandingConfig"), "status route must not overwrite res.locals.branding with raw disk JSON"); }); test("logo consistency: GET /api/admin/branding returns local logo indicators", () => { const start = indexSource.indexOf('app.get("/api/admin/branding"'); const end = endIndexOfFunction(indexSource.slice(start), "async (req, res) => {") + start; const handler = indexSource.slice(start, end); assert.ok(handler.includes("hasLocalLogo"), "branding endpoint must indicate if a local logo exists"); assert.ok(handler.includes("localLogoPath"), "branding endpoint must return localLogoPath"); }); test("logo consistency: settings sidebar watermark displays configured logo in both full and collapsed states", () => { const watermarkStart = settingsEjsSource.indexOf('class="sidebar-watermark"'); assert.ok(watermarkStart >= 0, "sidebar-watermark must exist in settings.ejs"); const watermarkSection = settingsEjsSource.slice(watermarkStart, watermarkStart + 800); assert.ok(watermarkSection.includes('class="watermark-full"'), "watermark-full must exist"); assert.ok(watermarkSection.includes('class="watermark-collapsed"'), "watermark-collapsed must exist"); // Both full and collapsed watermarks should render the configured logoUrl assert.ok( watermarkSection.includes("branding.logoUrl"), "sidebar watermark must reference branding.logoUrl" ); assert.ok( watermarkSection.includes("onerror=\"this.onerror=null;this.src='/auth-assets/logo';\""), "both watermarks must carry graceful fallback to /auth-assets/logo" ); }); test("logo consistency: login page access button references configured logoUrl with /auth-assets/logo fallback", () => { // Removed 2026-11: this region previously also carried the img's inline // onerror fallback (an earlier round pinned it as staying deliberately). // That attribute is gone — dead under script-src-attr 'none' (it could // never fire) — so the inline-handler surface stays empty and the // /auth-assets/logo fallback lives in the src ternary below, which is what // these assertions still pin. Do not re-add an inline handler here. const btnStart = loginEjsSource.indexOf('class="access-panel-btn"'); assert.ok(btnStart >= 0, "access-panel-btn must exist in login.ejs"); const btnSection = loginEjsSource.slice(btnStart, btnStart + 400); assert.ok(btnSection.includes("class=\"access-btn-logo\""), "access-btn-logo must exist"); assert.ok(btnSection.includes("branding.logoUrl"), "access-btn-logo must reference branding.logoUrl"); assert.ok(btnSection.includes("/auth-assets/logo"), "access-btn-logo must fallback to /auth-assets/logo"); }); test("logo consistency: brandingMiddleware verifies disk existence before resolving /images/ path", () => { assert.ok(brandingSource.includes("fs.existsSync"), "brandingMiddleware must check file existence on disk"); assert.ok(brandingSource.includes("localLogoExists"), "brandingMiddleware must guard local logo resolution"); }); test("favicon hardening: applySvgContentSecurityHeaders omits sandbox for favicons", () => { assert.ok(indexSource.includes("function applySvgContentSecurityHeaders(res, isFavicon = false)"), "applySvgContentSecurityHeaders accepts isFavicon parameter"); assert.ok(indexSource.includes("res.setHeader(\"Content-Security-Policy\", \"default-src 'none'\");"), "favicon SVG CSP must omit sandbox so browser tab chrome can render it"); assert.ok(indexSource.includes("res.setHeader(\"Content-Security-Policy\", \"default-src 'none'; sandbox\");"), "non-favicon SVG CSP must keep sandbox for security"); }); test("favicon hardening: favicons are served with public cache control, not no-store", () => { assert.ok(indexSource.includes('res.setHeader("Cache-Control", "public, max-age=86400, must-revalidate");'), "favicons must be cacheable so browser tab strip retains them"); }); test("favicon hardening: SVG favicons are enhanced with light-theme contrast contour", () => { assert.ok(indexSource.includes("function enhanceSvgForFavicon"), "enhanceSvgForFavicon helper must exist"); assert.ok(indexSource.includes("prefers-color-scheme:light"), "enhanceSvgForFavicon must provide light-theme contour for white glyphs"); }); test("favicon hardening: views configure sizes=any for SVG and alternate icon fallbacks", () => { const views = [ { name: "settings.ejs", src: settingsEjsSource }, { name: "login.ejs", src: loginEjsSource }, { name: "status.ejs", src: statusEjsSource }, { name: "index.ejs", src: fs.readFileSync(path.join(ROOT, "views", "index.ejs"), "utf8") }, { name: "bot.ejs", src: fs.readFileSync(path.join(ROOT, "views", "bot.ejs"), "utf8") }, ]; for (const v of views) { assert.ok(v.src.includes('sizes="<%= (branding && branding.faviconMimeType === \'image/svg+xml\') ? \'any\' : \'16x16\' %>"'), `${v.name} must specify sizes=any for SVG favicons`); assert.ok(v.src.includes('rel="alternate icon"'), `${v.name} must supply an alternate icon fallback`); } });