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

+84
View File
@@ -0,0 +1,84 @@
"use strict";
// adpanel:captcha-csp-scoping — the GLOBAL helmet CSP must not allow every
// captcha provider's hosts on every authenticated page: those hosts are known
// CSP-bypass footholds (script/JSONP endpoints on www.google.com et al.), and
// the only page that ever renders a captcha widget is the pre-auth login page,
// which builds its own provider-scoped CSP (buildAuthPageCsp). The global
// config therefore carries the captcha origins ONLY as per-request conditional
// entries created here: each returns its host when the LIVE runtime captcha
// flags say that provider is the configured one, and "" otherwise (helmet
// joins directive entries with spaces and the CSP grammar ignores extra
// whitespace, so an empty entry contributes nothing). Flags are read at
// REQUEST time so Settings' live captcha reconfiguration (applyCaptchaConfig,
// no restart) keeps the header in step — a snapshot at middleware-creation
// time would break the hot-swapped provider until the next restart. The
// per-provider, per-directive origin sets below mirror buildAuthPageCsp's
// captcha branch EXACTLY — one definition of "which hosts does this provider
// need on this directive", never a wider copy. This module never touches
// helmet's nonce directive functions and never derives anything from request
// headers.
const CAPTCHA_PROVIDER_ORIGINS = Object.freeze({
turnstile: Object.freeze({
script: Object.freeze(["https://challenges.cloudflare.com"]),
style: Object.freeze([]),
connect: Object.freeze([]),
frame: Object.freeze(["https://challenges.cloudflare.com"]),
}),
hcaptcha: Object.freeze({
script: Object.freeze(["https://hcaptcha.com", "https://*.hcaptcha.com"]),
style: Object.freeze(["https://hcaptcha.com", "https://*.hcaptcha.com"]),
connect: Object.freeze(["https://hcaptcha.com", "https://*.hcaptcha.com"]),
frame: Object.freeze(["https://hcaptcha.com", "https://*.hcaptcha.com"]),
}),
recaptcha: Object.freeze({
script: Object.freeze(["https://www.google.com", "https://www.gstatic.com"]),
style: Object.freeze([]),
connect: Object.freeze(["https://www.google.com", "https://www.gstatic.com"]),
frame: Object.freeze(["https://www.google.com", "https://www.gstatic.com"]),
}),
});
// Maps the panel's live captcha flags to the provider key. captcha disabled →
// null (no captcha origin is emitted anywhere). The recaptcha default matches
// deriveCaptchaProvider/applyCaptchaConfig in index.js.
function resolveCaptchaProvider({ enabled, isCf, isHcaptcha } = {}) {
if (!enabled) return null;
if (isCf) return "turnstile";
if (isHcaptcha) return "hcaptcha";
return "recaptcha";
}
function createCaptchaCspOriginSource({ readFlags } = {}) {
if (typeof readFlags !== "function") {
throw new Error("createCaptchaCspOriginSource requires a readFlags() accessor");
}
const originFor = (directiveKey, origin) => {
const provider = resolveCaptchaProvider(readFlags() || {});
if (!provider) return "";
return CAPTCHA_PROVIDER_ORIGINS[provider][directiveKey].includes(origin) ? origin : "";
};
return {
// Returns a helmet directive-array element: a per-request function that
// yields the origin when the configured provider needs it on that
// directive group, or "" when not. helmet calls each function element
// with (req, res) at header-build time; this source ignores the request
// by design (the allowlist is operator config, never request data).
cspOrigin(directive, origin) {
const directiveKey = String(directive || "").toLowerCase();
if (!Object.prototype.hasOwnProperty.call(CAPTCHA_PROVIDER_ORIGINS.turnstile, directiveKey)) {
throw new Error(`captcha-csp-origins: unknown directive group ${JSON.stringify(directive)}`);
}
const host = String(origin || "");
if (!host) throw new Error("captcha-csp-origins: cspOrigin requires a non-empty origin");
return () => originFor(directiveKey, host);
},
};
}
module.exports = {
CAPTCHA_PROVIDER_ORIGINS,
resolveCaptchaProvider,
createCaptchaCspOriginSource,
};