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,106 @@
|
||||
"use strict";
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
|
||||
const {
|
||||
CAPTCHA_PROVIDER_ORIGINS,
|
||||
resolveCaptchaProvider,
|
||||
createCaptchaCspOriginSource,
|
||||
} = require("./captcha-csp-origins");
|
||||
|
||||
// adpanel:captcha-csp-scoping — unit coverage for the provider-scoped global
|
||||
// CSP captcha origins: provider resolution from the live flags, per-directive
|
||||
// origin emission, hot-swap re-reads, and fail-fast on wiring mistakes.
|
||||
|
||||
test("resolveCaptchaProvider maps the runtime flags to the provider key", () => {
|
||||
assert.strictEqual(resolveCaptchaProvider({ enabled: false, isCf: true, isHcaptcha: true }), null);
|
||||
assert.strictEqual(resolveCaptchaProvider({ enabled: true, isCf: true, isHcaptcha: false }), "turnstile");
|
||||
assert.strictEqual(resolveCaptchaProvider({ enabled: true, isCf: false, isHcaptcha: true }), "hcaptcha");
|
||||
assert.strictEqual(resolveCaptchaProvider({ enabled: true, isCf: false, isHcaptcha: false }), "recaptcha");
|
||||
assert.strictEqual(resolveCaptchaProvider({ enabled: true, isCf: true, isHcaptcha: true }), "turnstile");
|
||||
assert.strictEqual(resolveCaptchaProvider(undefined), null);
|
||||
assert.strictEqual(resolveCaptchaProvider({}), null);
|
||||
});
|
||||
|
||||
test("cspOrigin returns the host only when the configured provider needs it on that directive", () => {
|
||||
const source = createCaptchaCspOriginSource({ readFlags: () => ({ enabled: true, isCf: false, isHcaptcha: false }) });
|
||||
const scriptGoogle = source.cspOrigin("script", "https://www.google.com");
|
||||
assert.strictEqual(typeof scriptGoogle, "function", "cspOrigin must return a helmet directive function");
|
||||
assert.strictEqual(scriptGoogle({}), "https://www.google.com", "recaptcha needs www.google.com on script-src");
|
||||
assert.strictEqual(source.cspOrigin("frame", "https://www.gstatic.com")({}), "https://www.gstatic.com");
|
||||
assert.strictEqual(source.cspOrigin("connect", "https://www.google.com")({}), "https://www.google.com");
|
||||
// recaptcha needs no hcaptcha host anywhere
|
||||
assert.strictEqual(source.cspOrigin("script", "https://hcaptcha.com")({}), "");
|
||||
assert.strictEqual(source.cspOrigin("style", "https://*.hcaptcha.com")({}), "");
|
||||
// turnstile needs no connect-src host (mirrors buildAuthPageCsp)
|
||||
const cfSource = createCaptchaCspOriginSource({ readFlags: () => ({ enabled: true, isCf: true }) });
|
||||
assert.strictEqual(cfSource.cspOrigin("script", "https://challenges.cloudflare.com")({}), "https://challenges.cloudflare.com");
|
||||
assert.strictEqual(cfSource.cspOrigin("frame", "https://challenges.cloudflare.com")({}), "https://challenges.cloudflare.com");
|
||||
assert.strictEqual(cfSource.cspOrigin("connect", "https://challenges.cloudflare.com")({}), "");
|
||||
assert.strictEqual(cfSource.cspOrigin("script", "https://www.google.com")({}), "");
|
||||
// hCaptcha needs its hosts on script/style/connect/frame
|
||||
const hSource = createCaptchaCspOriginSource({ readFlags: () => ({ enabled: true, isHcaptcha: true }) });
|
||||
for (const directive of ["script", "style", "connect", "frame"]) {
|
||||
assert.strictEqual(hSource.cspOrigin(directive, "https://hcaptcha.com")({}), "https://hcaptcha.com", directive);
|
||||
assert.strictEqual(hSource.cspOrigin(directive, "https://*.hcaptcha.com")({}), "https://*.hcaptcha.com", directive);
|
||||
}
|
||||
assert.strictEqual(hSource.cspOrigin("script", "https://www.gstatic.com")({}), "");
|
||||
});
|
||||
|
||||
test("cspOrigin returns an empty string for every origin when captcha is disabled", () => {
|
||||
const source = createCaptchaCspOriginSource({ readFlags: () => ({ enabled: false, isCf: false, isHcaptcha: false }) });
|
||||
for (const [directive, hosts] of Object.entries({
|
||||
script: ["https://challenges.cloudflare.com", "https://www.google.com", "https://www.gstatic.com", "https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
style: ["https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
connect: ["https://www.google.com", "https://www.gstatic.com", "https://challenges.cloudflare.com", "https://hcaptcha.com"],
|
||||
frame: ["https://www.google.com", "https://www.gstatic.com", "https://challenges.cloudflare.com", "https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
})) {
|
||||
for (const host of hosts) {
|
||||
assert.strictEqual(source.cspOrigin(directive, host)({}), "", `${host} on ${directive} while disabled`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("flags are re-read on every evaluation (live hot-swap keeps the header in step)", () => {
|
||||
let flags = { enabled: true, isCf: false, isHcaptcha: false };
|
||||
const source = createCaptchaCspOriginSource({ readFlags: () => flags });
|
||||
const turnstileScript = source.cspOrigin("script", "https://challenges.cloudflare.com");
|
||||
assert.strictEqual(turnstileScript({}), "", "recaptcha configured: no turnstile script host");
|
||||
flags = { enabled: true, isCf: true, isHcaptcha: false };
|
||||
assert.strictEqual(turnstileScript({}), "https://challenges.cloudflare.com", "hot-swapped to turnstile");
|
||||
flags = { enabled: false };
|
||||
assert.strictEqual(turnstileScript({}), "", "captcha disabled entirely");
|
||||
});
|
||||
|
||||
test("cspOrigin fails fast on wiring mistakes", () => {
|
||||
const source = createCaptchaCspOriginSource({ readFlags: () => ({ enabled: false }) });
|
||||
assert.throws(() => source.cspOrigin("imgSrc", "https://www.google.com"), /unknown directive group/);
|
||||
assert.throws(() => source.cspOrigin("script", ""), /non-empty origin/);
|
||||
assert.throws(() => createCaptchaCspOriginSource({}), /readFlags/);
|
||||
assert.throws(() => createCaptchaCspOriginSource({ readFlags: "nope" }), /readFlags/);
|
||||
});
|
||||
|
||||
test("the origin table mirrors buildAuthPageCsp's per-provider pushes", () => {
|
||||
// buildAuthPageCsp (index.js) is the model: turnstile — script+frame only;
|
||||
// hcaptcha — script, style, connect, frame; recaptcha — script+connect+frame
|
||||
// with google+gstatic. This pins the table against silent widening.
|
||||
assert.deepStrictEqual(CAPTCHA_PROVIDER_ORIGINS.turnstile, {
|
||||
script: ["https://challenges.cloudflare.com"],
|
||||
style: [],
|
||||
connect: [],
|
||||
frame: ["https://challenges.cloudflare.com"],
|
||||
});
|
||||
assert.deepStrictEqual(CAPTCHA_PROVIDER_ORIGINS.hcaptcha, {
|
||||
script: ["https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
style: ["https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
connect: ["https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
frame: ["https://hcaptcha.com", "https://*.hcaptcha.com"],
|
||||
});
|
||||
assert.deepStrictEqual(CAPTCHA_PROVIDER_ORIGINS.recaptcha, {
|
||||
script: ["https://www.google.com", "https://www.gstatic.com"],
|
||||
style: [],
|
||||
connect: ["https://www.google.com", "https://www.gstatic.com"],
|
||||
frame: ["https://www.google.com", "https://www.gstatic.com"],
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user