Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

+167
View File
@@ -0,0 +1,167 @@
"use strict";
// adpanel:branding-load-sanitization — pins the load-time re-sanitization of
// data/branding.json. The save route sanitizes appName/logoUrl/
// loginBackgroundExternalUrl, but the file is operator-editable at rest
// (0600 operator file, same trust tier as .env): a hand-edited or co-opted
// config used to reach the pages raw — putting an arbitrary remote origin on
// the auth pages (the auth-page CSP derives img-src from the branding logo
// origin) and rendering an unsanitized appName. loadBrandingConfig now
// re-runs the module's OWN sanitizers (the exact ones the save route uses),
// so the load path must never give a config value wider acceptance than the
// admin API would, and the shipped config must round-trip byte-identically.
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 branding = require(path.join(ROOT, "utils", "branding.js"));
const CONFIG_PATH = path.join(ROOT, "data", "branding.json");
test("shipped data/branding.json loads byte-identically through the load sanitizers", () => {
const raw = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
// The identity precondition: every shipped value passes the module's own
// sanitizers unchanged. If a shipped value ever needed rewriting at load,
// the on-disk file and the served branding would diverge — that must stay
// impossible for the shipped artifact.
assert.strictEqual(branding.sanitizeAppName(raw.appName), raw.appName,
"shipped appName passes sanitizeAppName unchanged");
assert.strictEqual(branding.sanitizeLogoUrl(raw.logoUrl), raw.logoUrl,
"shipped logoUrl passes sanitizeLogoUrl unchanged (href re-serialization is identity for it)");
assert.strictEqual(branding.sanitizeLogoUrl(raw.loginBackgroundExternalUrl), raw.loginBackgroundExternalUrl,
"shipped loginBackgroundExternalUrl passes sanitizeLogoUrl unchanged");
// End-to-end through the real loader against the real shipped file.
branding.invalidateBrandingCache();
const loaded = branding.loadBrandingConfig();
for (const [key, value] of Object.entries(raw)) {
assert.deepStrictEqual(loaded[key], value, `loaded ${key} equals the on-disk value`);
}
assert.match(branding.resolveFaviconAssetUrl(loaded), /^\/images\/adpanel-dark\.svg(?:\?|$)/);
assert.strictEqual(branding.resolveFaviconMimeType(loaded), "image/svg+xml");
assert.match(branding.resolveLoginBackgroundAssetUrl(loaded), /^\/images\/shadow-lines\.avif(?:\?|$)/);
});
test("legacy bundled branding URLs use local files without a background mirror", () => {
const cdn = "https://cdn.jsdelivr.net/gh/antonndev/ADCDn@main/";
const config = branding.sanitizeLoadedBrandingConfig({
logoUrl: `${cdn}adpanel-dark.svg?v=old`,
loginBackgroundExternalUrl: `${cdn}shadow-lines.avif`,
localLoginBackgroundPath: "login-background-remote-old.avif",
});
assert.strictEqual(config.logoUrl, null);
assert.strictEqual(config.loginBackgroundExternalUrl, null);
assert.strictEqual(branding.resolveFaviconAssetUrl(config), "/images/adpanel-dark.svg");
assert.strictEqual(branding.resolveLoginBackgroundAssetUrl(config), "/images/shadow-lines.avif");
assert.strictEqual(config.loginBackgroundMimeType, "image/avif");
assert.strictEqual(branding.preferBundledMediaUrl(`${cdn}minecraft.avif`), "/images/minecraft.avif");
assert.strictEqual(branding.resolveBundledMediaFilename(`${cdn}../adpanel-dark.svg`), null);
assert.strictEqual(branding.resolveBundledMediaFilename(`${cdn}missing.webp`), null);
assert.strictEqual(branding.preferBundledMediaUrl("/images/adpanel-dark.svg?v=custom"), "/images/adpanel-dark.svg?v=custom");
});
test("load normalizer rejects every URL class the save route rejects", () => {
const { sanitizeLoadedBrandingConfig } = branding;
for (const url of [
"javascript:alert(1)",
"//evil.example/x.png",
"data:image/svg+xml;base64,AAAA",
"https://10.0.0.5/logo.png",
"http://127.0.0.1:8080/x.png",
"http://[::1]/x.png",
"https://169.254.169.254/latest/meta-data/",
"not a url at all",
""
]) {
const out = sanitizeLoadedBrandingConfig({ appName: "ADPanel", logoUrl: url, loginBackgroundExternalUrl: url });
assert.strictEqual(out.logoUrl, null, `logoUrl ${JSON.stringify(url)} is rejected at load`);
assert.strictEqual(out.loginBackgroundExternalUrl, null, `loginBackgroundExternalUrl ${JSON.stringify(url)} is rejected at load`);
}
});
test("load normalizer accepts exactly what the save route accepts", () => {
const { sanitizeLoadedBrandingConfig, sanitizeLogoUrl } = branding;
for (const url of [
"https://cdn.example.com/logos/brand.png",
"http://cdn.example.com/logos/brand.webp"
]) {
const out = sanitizeLoadedBrandingConfig({ logoUrl: url, loginBackgroundExternalUrl: url });
assert.strictEqual(out.logoUrl, sanitizeLogoUrl(url),
`public https?:// URL ${url} gets the save route's canonicalized href at load`);
assert.strictEqual(out.loginBackgroundExternalUrl, sanitizeLogoUrl(url));
}
});
test("appName is cut down to the save path's alphabet and budget at load", () => {
const { sanitizeLoadedBrandingConfig, DEFAULT_BRANDING } = branding;
// 10KB appName → trimmed to the save path's 50-char budget.
assert.strictEqual(sanitizeLoadedBrandingConfig({ appName: "A".repeat(10240) }).appName.length, 50,
"10KB appName is trimmed to 50 chars");
// Control characters and non-allowed glyphs are stripped (save-path filter).
assert.strictEqual(sanitizeLoadedBrandingConfig({ appName: "Bad\x00\x1F<Name>\u00e9!" }).appName, "BadName!",
"control chars and non-ASCII glyphs are stripped");
// Fully filtered-out appName → the documented default, never undefined.
assert.strictEqual(sanitizeLoadedBrandingConfig({ appName: "<script>alert(1)</script>" }).appName,
"scriptalert1script", "filtered appName keeps the surviving save-path alphabet");
assert.strictEqual(sanitizeLoadedBrandingConfig({ appName: "\x00\x1F" }).appName, DEFAULT_BRANDING.appName,
"appName with nothing surviving the filter falls back to the default");
assert.strictEqual(sanitizeLoadedBrandingConfig({}).appName, DEFAULT_BRANDING.appName,
"missing appName falls back to the default");
assert.strictEqual(sanitizeLoadedBrandingConfig({ appName: 42 }).appName, DEFAULT_BRANDING.appName,
"non-string appName falls back to the default");
});
test("a hand-edited branding.json is sanitized at load (real loader round-trip)", () => {
const original = fs.readFileSync(CONFIG_PATH);
// No other test reads data/branding.json (the branding module is the only
// reader and tests inject their own config elsewhere), so a guarded
// swap-with-restore here exercises the real load path end-to-end. The
// restore runs in finally so a failed assertion can never leave the
// adversarial file behind.
try {
fs.writeFileSync(CONFIG_PATH, JSON.stringify({
appName: "<img src=x onerror=alert(1)>",
logoUrl: "javascript:alert(1)",
loginBackgroundExternalUrl: "//evil.example/bg.png"
}), { mode: 0o600 });
branding.invalidateBrandingCache();
const loaded = branding.loadBrandingConfig();
assert.strictEqual(loaded.appName, "img srcx onerroralert1",
"hand-edited hostile appName is cut down to the save-path alphabet");
assert.strictEqual(loaded.logoUrl, null,
"hand-edited javascript: logoUrl is rejected at load (middleware falls back to the local default)");
assert.strictEqual(loaded.loginBackgroundExternalUrl, null,
"hand-edited protocol-relative background URL is rejected at load");
} finally {
fs.writeFileSync(CONFIG_PATH, original, { mode: 0o600 });
branding.invalidateBrandingCache();
}
// The restore must put the shipped config back byte-for-byte.
assert.deepStrictEqual(fs.readFileSync(CONFIG_PATH), original);
});
test("a hand-edited UNPARSEABLE branding.json falls back to the defaults at load", () => {
const original = fs.readFileSync(CONFIG_PATH);
try {
fs.writeFileSync(CONFIG_PATH, "{ this is not json", { mode: 0o600 });
branding.invalidateBrandingCache();
const loaded = branding.loadBrandingConfig();
assert.deepStrictEqual(loaded, { ...branding.DEFAULT_BRANDING },
"unparseable config yields the documented defaults (existing error path)");
} finally {
fs.writeFileSync(CONFIG_PATH, original, { mode: 0o600 });
branding.invalidateBrandingCache();
}
const restored = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
assert.strictEqual(restored.appName, "ADPanel", "shipped config restored");
});