220 lines
7.7 KiB
JavaScript
220 lines
7.7 KiB
JavaScript
"use strict";
|
|
|
|
// F9 (trust-configuration escape hatches): CSRF_ALLOWED_HOSTS is an
|
|
// operator-owned trust allowlist — every accepted host may issue cross-site
|
|
// state-changing requests against the whole panel. Careless entries must fail
|
|
// SAFE: this parser drops invalid or over-broad entries (dropping an
|
|
// allowlist entry only ever *narrows* trust) and says exactly why, instead of
|
|
// trusting them silently. Benign formatting mistakes (scheme, port, path,
|
|
// case, trailing dot) are normalized so legitimate integrations keep working.
|
|
//
|
|
// Entries are plain hostnames or "*.domain" wildcards, normalized the same
|
|
// way index.js normalizes Origin/Referer hosts (lowercase, no port, no IPv6
|
|
// brackets) — matching itself stays in index.js `hostAllowed()`.
|
|
|
|
const net = require("node:net");
|
|
|
|
const MAX_ENTRIES = 25;
|
|
const MAX_HOST_LEN = 253;
|
|
|
|
// Multi-label public suffixes and shared-hosting zones where a wildcard would
|
|
// trust origins owned by unrelated parties. Not a full Public Suffix List —
|
|
// the structural single-label rule in parseCsrfAllowedHosts() catches bare
|
|
// TLDs ("*.com"); this set covers the popular multi-label careless cases.
|
|
const WILDCARD_FORBIDDEN_BASES = new Set([
|
|
"co.uk", "org.uk", "ac.uk", "gov.uk", "com.au", "net.au", "org.au",
|
|
"co.jp", "or.jp", "ne.jp", "com.br", "com.mx", "com.ar", "com.cn",
|
|
"co.in", "co.nz", "co.za", "com.tr", "com.pl", "com.ua",
|
|
"github.io", "gitlab.io", "pages.dev", "workers.dev", "vercel.app",
|
|
"netlify.app", "herokuapp.com", "azurewebsites.net", "cloudfront.net",
|
|
"amazonaws.com", "r2.dev", "web.app", "run.app", "firebaseapp.com",
|
|
"repl.co", "glitch.me", "onrender.com", "fly.dev", "railway.app",
|
|
"readthedocs.io", "wordpress.com", "blogspot.com",
|
|
]);
|
|
|
|
function labelOk(label) {
|
|
if (!label || label.length > 63) return false;
|
|
return /^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$/.test(label);
|
|
}
|
|
|
|
function hostOk(host) {
|
|
if (!host || host.length > MAX_HOST_LEN) return false;
|
|
if (host.includes("..")) return false;
|
|
return host.split(".").every(labelOk);
|
|
}
|
|
|
|
// "host" / "host:port" / "[v6]" / "[v6]:port" → { host, port }, or null when
|
|
// malformed (bare multi-colon IPv6, ":garbage" ports, missing "]").
|
|
function splitHostPort(entry) {
|
|
if (entry.startsWith("[")) {
|
|
const end = entry.indexOf("]");
|
|
if (end === -1) return null;
|
|
const host = entry.slice(1, end);
|
|
const rest = entry.slice(end + 1);
|
|
let port = null;
|
|
if (rest) {
|
|
if (!rest.startsWith(":")) return null;
|
|
port = rest.slice(1);
|
|
if (!/^\d{1,5}$/.test(port) || Number(port) < 1 || Number(port) > 65535) return null;
|
|
}
|
|
return { host, port };
|
|
}
|
|
const colonCount = (entry.match(/:/g) || []).length;
|
|
if (colonCount === 0) return { host: entry, port: null };
|
|
if (colonCount === 1) {
|
|
const idx = entry.indexOf(":");
|
|
const host = entry.slice(0, idx);
|
|
const port = entry.slice(idx + 1);
|
|
if (/^\d{1,5}$/.test(port) && Number(port) >= 1 && Number(port) <= 65535) {
|
|
return { host, port };
|
|
}
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Parse and validate a CSRF_ALLOWED_HOSTS value.
|
|
*
|
|
* @param {string} rawEnv raw env value (comma-separated)
|
|
* @param {{panelPublicHost?: string, allowWildcards?: boolean}} opts
|
|
* panelPublicHost — this panel's own public host (from PANEL_PUBLIC_URL);
|
|
* entries equal to it are classified as self (same-origin-equivalent,
|
|
* not a trust widening). allowWildcards — opt-in for "*.domain" entries,
|
|
* set from CSRF_ALLOWED_HOSTS_ALLOW_WILDCARDS=1 in index.js.
|
|
* @returns {{
|
|
* accepted: string[], rejected: {raw: string, reason: string}[],
|
|
* notes: {from: string, to: string, note: string}[],
|
|
* selfHosts: string[], externalHosts: string[]
|
|
* }}
|
|
*/
|
|
function parseCsrfAllowedHosts(rawEnv, opts = {}) {
|
|
const panelPublicHost = String((opts && opts.panelPublicHost) || "")
|
|
.toLowerCase().replace(/\.+$/, "");
|
|
const allowWildcards = opts && opts.allowWildcards === true;
|
|
|
|
const accepted = [];
|
|
const rejected = [];
|
|
const notes = [];
|
|
const selfHosts = [];
|
|
const externalHosts = [];
|
|
const seen = new Set();
|
|
|
|
const parts = String(rawEnv == null ? "" : rawEnv).split(",");
|
|
for (const rawPart of parts) {
|
|
const raw = String(rawPart).trim();
|
|
if (!raw) continue;
|
|
|
|
let working = raw.toLowerCase();
|
|
let note = null;
|
|
|
|
const schemeIdx = working.indexOf("://");
|
|
if (schemeIdx !== -1) {
|
|
working = working.slice(schemeIdx + 3);
|
|
note = "scheme";
|
|
}
|
|
const cut = working.search(/[/?#]/);
|
|
if (cut !== -1) {
|
|
working = working.slice(0, cut);
|
|
note = note ? `${note}/path` : "path";
|
|
}
|
|
if (!working) {
|
|
rejected.push({ raw, reason: "nothing left after removing scheme/path" });
|
|
continue;
|
|
}
|
|
if (working === "null" || working === "undefined") {
|
|
rejected.push({ raw, reason: "placeholder value" });
|
|
continue;
|
|
}
|
|
if (/\s/.test(working) || working.includes("@") || /[\u0000-\u001f\u007f]/.test(working)) {
|
|
rejected.push({ raw, reason: "whitespace, '@' or control characters are not allowed" });
|
|
continue;
|
|
}
|
|
working = working.replace(/\.+$/, "");
|
|
|
|
if (working === "*") {
|
|
rejected.push({ raw, reason: '"*" would trust every origin and is never allowed' });
|
|
continue;
|
|
}
|
|
|
|
const isWildcard = working.startsWith("*.") || working.startsWith(".");
|
|
const isBracketed = working.startsWith("[");
|
|
let base;
|
|
if (isWildcard) {
|
|
base = working.replace(/^\*\./, "").replace(/^\./, "");
|
|
} else if (isBracketed) {
|
|
const split = splitHostPort(working);
|
|
if (!split) {
|
|
rejected.push({ raw, reason: "malformed IPv6 literal" });
|
|
continue;
|
|
}
|
|
base = split.host;
|
|
if (split.port != null) note = note ? `${note}/port` : "port";
|
|
} else {
|
|
const split = splitHostPort(working);
|
|
if (!split) {
|
|
rejected.push({ raw, reason: "malformed host:port (wrap IPv6 literals in brackets)" });
|
|
continue;
|
|
}
|
|
base = split.host;
|
|
if (split.port != null) note = note ? `${note}/port` : "port";
|
|
}
|
|
|
|
if (isWildcard) {
|
|
if (!base) {
|
|
rejected.push({ raw, reason: "wildcard with empty base" });
|
|
continue;
|
|
}
|
|
if (!base.includes(".")) {
|
|
rejected.push({ raw, reason: `"*.${base}" would trust every host under the public suffix "${base}"` });
|
|
continue;
|
|
}
|
|
if (WILDCARD_FORBIDDEN_BASES.has(base)) {
|
|
rejected.push({ raw, reason: `"*.${base}" spans a public/shared zone of unrelated origins` });
|
|
continue;
|
|
}
|
|
if (!allowWildcards) {
|
|
rejected.push({ raw, reason: "wildcard entries require CSRF_ALLOWED_HOSTS_ALLOW_WILDCARDS=1" });
|
|
continue;
|
|
}
|
|
if (!hostOk(base)) {
|
|
rejected.push({ raw, reason: "not a valid hostname" });
|
|
continue;
|
|
}
|
|
} else if (isBracketed) {
|
|
// Stored bracket-less to match how Origin/Referer hosts are normalized
|
|
// in index.js (extractHostnameFromHeader strips the brackets).
|
|
if (net.isIP(base) !== 6) {
|
|
rejected.push({ raw, reason: "invalid IPv6 literal" });
|
|
continue;
|
|
}
|
|
} else if (!hostOk(base)) {
|
|
rejected.push({ raw, reason: "not a valid hostname" });
|
|
continue;
|
|
}
|
|
|
|
const entry = isWildcard ? `*.${base}` : base;
|
|
if (seen.has(entry)) continue;
|
|
if (accepted.length >= MAX_ENTRIES) {
|
|
rejected.push({ raw, reason: `entry limit reached (max ${MAX_ENTRIES})` });
|
|
continue;
|
|
}
|
|
seen.add(entry);
|
|
accepted.push(entry);
|
|
if (note) notes.push({ from: raw, to: entry, note });
|
|
|
|
if (!isWildcard && panelPublicHost && entry === panelPublicHost) {
|
|
selfHosts.push(entry);
|
|
} else {
|
|
externalHosts.push(entry);
|
|
}
|
|
}
|
|
|
|
return { accepted, rejected, notes, selfHosts, externalHosts };
|
|
}
|
|
|
|
module.exports = {
|
|
parseCsrfAllowedHosts,
|
|
MAX_ENTRIES,
|
|
};
|