Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,111 @@
|
||||
"use strict";
|
||||
|
||||
// adpanel:captcha-hostname-pin — CAPTCHA_EXPECTED_HOSTNAMES is an OPTIONAL
|
||||
// operator allowlist (comma-separated hostnames) for the external captcha
|
||||
// siteverify responses. It is OFF unless the env var is set to at least one
|
||||
// non-empty entry; unset/blank keeps the historical behavior exactly (only
|
||||
// `success` is checked). When configured, a provider response must carry a
|
||||
// `hostname` field that exactly matches one allowlisted entry after
|
||||
// trim + lowercase; a response without a usable hostname FAILS CLOSED — a
|
||||
// token verified for some other site (a leaked/replayed siteverify response)
|
||||
// must not satisfy this panel. The allowlist comes ONLY from the environment;
|
||||
// it is never derived from the request Host header (an attacker-controlled
|
||||
// Host must not be able to widen the allowlist). The check runs AFTER the
|
||||
// provider's `success` check and never touches the remoteip argument.
|
||||
//
|
||||
// adpanel:captcha-hostname-default — the factory also accepts
|
||||
// `defaultHostnames` (already-deployment-derived hostnames; index.js derives
|
||||
// them from PANEL_PUBLIC_URL, the codebase's canonical serving-origin setting,
|
||||
// never from a request header). The default list participates ONLY when the
|
||||
// env var is entirely ABSENT (undefined): unset = the derived default pin,
|
||||
// which a check() call must explicitly opt into via { useDefault: true };
|
||||
// an explicitly SET value — non-empty (that list wins, byte-identical to the
|
||||
// pre-default behavior) or blank (feature off, as documented above) — always
|
||||
// overrides the default, and blank doubles as the kill switch that disables
|
||||
// both. The opt-in exists because only hCaptcha needs the default: hCaptcha
|
||||
// does not bind tokens to the sitekey's domain at issuance (the response's
|
||||
// `hostname` is just where the widget ran), while Turnstile and reCAPTCHA are
|
||||
// domain-bound at issuance, so a derived pin would add no security there and
|
||||
// would only tighten deployment shapes those providers already accept.
|
||||
|
||||
const CAPTCHA_EXPECTED_HOSTNAMES_ENV = "CAPTCHA_EXPECTED_HOSTNAMES";
|
||||
const CAPTCHA_HOSTNAME_MISMATCH_ERROR = "captcha-hostname-not-allowed";
|
||||
|
||||
// Parses the raw env value into the allowlist. Returns null when the feature
|
||||
// is off (unset, non-string, or no non-empty entries after split/trim) and an
|
||||
// array of lowercased, trimmed hostnames otherwise. Empty entries (trailing
|
||||
// commas, stray spaces) are dropped rather than creating an unmatchable "".
|
||||
function parseCaptchaExpectedHostnames(raw) {
|
||||
if (typeof raw !== "string") return null;
|
||||
const entries = raw
|
||||
.split(",")
|
||||
.map((entry) => entry.trim().toLowerCase())
|
||||
.filter(Boolean);
|
||||
return entries.length ? entries : null;
|
||||
}
|
||||
|
||||
// adpanel:captcha-hostname-default — normalizes the caller-provided default
|
||||
// list with the exact semantics of the env list (trim + lowercase, drop
|
||||
// empties, plus dedupe) so both pin sets match identically. Non-string
|
||||
// entries and non-array input yield [] (no default).
|
||||
function normalizeCaptchaDefaultHostnames(entries) {
|
||||
if (!Array.isArray(entries)) return [];
|
||||
const seen = new Set();
|
||||
const out = [];
|
||||
for (const entry of entries) {
|
||||
if (typeof entry !== "string") continue;
|
||||
const host = entry.trim().toLowerCase();
|
||||
if (!host || seen.has(host)) continue;
|
||||
seen.add(host);
|
||||
out.push(host);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function createCaptchaHostnameGuard({ env = process.env, defaultHostnames = [] } = {}) {
|
||||
const envRawValue = env ? env[CAPTCHA_EXPECTED_HOSTNAMES_ENV] : undefined;
|
||||
const expectedHostnames = parseCaptchaExpectedHostnames(envRawValue);
|
||||
const defaults = normalizeCaptchaDefaultHostnames(defaultHostnames);
|
||||
// The default pin is eligible ONLY when the env var is entirely absent —
|
||||
// this preserves the documented env contract byte-identically (non-empty
|
||||
// env = that list; blank env = off) and leaves a blank value as the
|
||||
// operator's explicit kill switch for both pin sources.
|
||||
const defaultApplies = envRawValue === undefined && defaults.length > 0;
|
||||
return {
|
||||
enabled: expectedHostnames !== null,
|
||||
defaultEnabled: defaultApplies,
|
||||
expectedHostnames,
|
||||
defaultHostnames: defaults,
|
||||
// True when ANY allowlist is effective (env-configured or derived
|
||||
// default) — the one-time unpinned warning keys off this.
|
||||
pinActive: expectedHostnames !== null || defaultApplies,
|
||||
// Returns null when the response is acceptable (no pin effective for
|
||||
// this call, or hostname present and allowlisted), or the shared failure
|
||||
// shape otherwise — the caller returns it verbatim. A non-string, empty,
|
||||
// or missing hostname is a mismatch (fail closed), not a skip. The
|
||||
// derived default participates only for calls that pass
|
||||
// { useDefault: true } (the hCaptcha branch; see the header comment).
|
||||
check(verifyJson, opts = {}) {
|
||||
const hostnames = expectedHostnames !== null
|
||||
? expectedHostnames
|
||||
: (opts && opts.useDefault === true && defaultApplies ? defaults : null);
|
||||
if (hostnames === null || hostnames.length === 0) return null;
|
||||
const hostname =
|
||||
verifyJson && typeof verifyJson.hostname === "string"
|
||||
? verifyJson.hostname.trim().toLowerCase()
|
||||
: "";
|
||||
if (!hostname || !hostnames.includes(hostname)) {
|
||||
return { ok: false, error: CAPTCHA_HOSTNAME_MISMATCH_ERROR };
|
||||
}
|
||||
return null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CAPTCHA_EXPECTED_HOSTNAMES_ENV,
|
||||
CAPTCHA_HOSTNAME_MISMATCH_ERROR,
|
||||
parseCaptchaExpectedHostnames,
|
||||
normalizeCaptchaDefaultHostnames,
|
||||
createCaptchaHostnameGuard,
|
||||
};
|
||||
Reference in new issue
Block a user