140 lines
6.9 KiB
JavaScript
140 lines
6.9 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:captcha-hostname-pin — source pins for the OPTIONAL siteverify
|
|
// hostname allowlist (CAPTCHA_EXPECTED_HOSTNAMES, off unless configured).
|
|
//
|
|
// verifyExternalCaptcha has exactly three provider branches (Cloudflare
|
|
// Turnstile, hCaptcha, Google reCAPTCHA) inside ONE function. Each branch
|
|
// must consult the SHARED guard (utils/captcha-expected-hostnames.js) after
|
|
// its `success` check so a response minted for another site cannot satisfy
|
|
// this panel — and no branch may derive the allowlist from the request Host
|
|
// header. These pins hold the guarantees:
|
|
// 1. index.js wires the shared module (one require, one env-injected guard);
|
|
// 2. every provider branch calls the guard exactly once, after `success`,
|
|
// before returning ok;
|
|
// 3. the remoteip argument each provider receives is still the real client
|
|
// IP (the hostname check must not touch it);
|
|
// 4. no Host-header-derived allowlist exists in the function.
|
|
|
|
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 INDEX_JS = path.join(ROOT, "index.js");
|
|
const GUARD_JS = path.join(ROOT, "utils", "captcha-expected-hostnames.js");
|
|
|
|
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
|
const guardSource = fs.readFileSync(GUARD_JS, "utf8");
|
|
|
|
function countOccurrences(haystack, needle) {
|
|
let count = 0;
|
|
let at = haystack.indexOf(needle);
|
|
while (at >= 0) {
|
|
count += 1;
|
|
at = haystack.indexOf(needle, at + 1);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
function extractVerifyExternalCaptchaBody(source) {
|
|
const start = source.indexOf("async function verifyExternalCaptcha(token, remoteIp) {");
|
|
assert.ok(start >= 0, "verifyExternalCaptcha(token, remoteIp) must exist in index.js");
|
|
// The next top-level declaration after the function marks its end; the
|
|
// guard wiring must stay inside this body.
|
|
const end = source.indexOf("\nfunction cloneJsonPayload(", start);
|
|
assert.ok(end > start, "cloneJsonPayload must follow verifyExternalCaptcha (update this pin if the function moves)");
|
|
return source.slice(start, end);
|
|
}
|
|
|
|
test("pins: index.js wires the shared hostname guard module with an injected env", () => {
|
|
assert.ok(
|
|
indexSource.includes('require("./utils/captcha-expected-hostnames")'),
|
|
"index.js must require the shared captcha-expected-hostnames module (no inline per-branch implementations)"
|
|
);
|
|
// adpanel:captcha-hostname-default — the construction grew a derived
|
|
// default option; the pin's intent is unchanged and still holds: exactly
|
|
// ONE shared guard instance, env-injected, its default list sourced only
|
|
// from the PANEL_PUBLIC_URL derivation (never from request headers —
|
|
// pinned in detail by tests/captcha-hostname-default.test.js).
|
|
const wiring = countOccurrences(indexSource, "createCaptchaHostnameGuard({");
|
|
assert.strictEqual(
|
|
wiring,
|
|
1,
|
|
"exactly one shared guard instance, constructed from the environment (never from request headers)"
|
|
);
|
|
const constructionStart = indexSource.indexOf("createCaptchaHostnameGuard({");
|
|
const constructionEnd = indexSource.indexOf("});", constructionStart);
|
|
const construction = indexSource.slice(constructionStart, constructionEnd);
|
|
assert.ok(
|
|
construction.includes("env: process.env"),
|
|
"the single guard instance is constructed from the environment"
|
|
);
|
|
assert.ok(
|
|
construction.includes("defaultHostnames: _CAPTCHA_DEFAULT_HOSTNAME ? [_CAPTCHA_DEFAULT_HOSTNAME] : []"),
|
|
"the derived default list comes only from the _CAPTCHA_DEFAULT_HOSTNAME derivation"
|
|
);
|
|
assert.ok(
|
|
guardSource.includes("adpanel:captcha-hostname-pin"),
|
|
"the guard module carries the topic comment documenting the fail-closed contract"
|
|
);
|
|
assert.ok(
|
|
guardSource.includes("module.exports") && guardSource.includes("createCaptchaHostnameGuard"),
|
|
"the guard module exports its factory"
|
|
);
|
|
});
|
|
|
|
test("pins: every provider branch in verifyExternalCaptcha calls the shared guard", () => {
|
|
const body = extractVerifyExternalCaptchaBody(indexSource);
|
|
|
|
// adpanel:captcha-hostname-default — the hCaptcha branch's call grew the
|
|
// { useDefault: true } opt-in argument, so the exact call text differs per
|
|
// branch; the pin's intent is unchanged and now holds on the shared prefix:
|
|
// exactly three guard consultations, one per provider branch (2 byte-identical
|
|
// plain calls + 1 hCaptcha opt-in, pinned in detail by
|
|
// tests/captcha-hostname-default.test.js).
|
|
assert.strictEqual(
|
|
countOccurrences(body, "captchaHostnameGuard.check(res.json"),
|
|
3,
|
|
"each of the three provider branches (turnstile/hcaptcha/recaptcha) must call the shared guard exactly once"
|
|
);
|
|
assert.strictEqual(
|
|
countOccurrences(body, 'return { ok: false, error: "captcha-validation-failed" }'),
|
|
3,
|
|
"each branch keeps its own success check — the guard supplements it, it does not replace it"
|
|
);
|
|
|
|
// Each `success`-failure check must be followed by the guard call before
|
|
// the branch's success return, so no branch returns ok without the check.
|
|
const guardCall = "captchaHostnameGuard.check(res.json";
|
|
let searched = 0;
|
|
let at = body.indexOf('if (res.status !== 200 || !(res.json && res.json.success)) {');
|
|
while (at >= 0) {
|
|
searched += 1;
|
|
const okReturn = body.indexOf("return { ok: true };", at);
|
|
const guard = body.indexOf(guardCall, at);
|
|
assert.ok(guard >= 0, "guard call found after the success check");
|
|
assert.ok(okReturn < 0 || guard < okReturn, "guard call precedes the branch's success return");
|
|
at = body.indexOf('if (res.status !== 200 || !(res.json && res.json.success)) {', at + 1);
|
|
}
|
|
assert.strictEqual(searched, 3, "exactly three success checks (one per provider branch)");
|
|
});
|
|
|
|
test("pins: providers still receive the real client IP as remoteip", () => {
|
|
const body = extractVerifyExternalCaptchaBody(indexSource);
|
|
assert.ok(body.includes("remoteip: remoteIp || undefined"), "turnstile branch keeps remoteip: remoteIp");
|
|
assert.ok(body.includes("remoteip=${encodeURIComponent(remoteIp || \"\")}"), "hcaptcha branch keeps remoteip: remoteIp");
|
|
assert.ok(body.includes("remoteip=${encodeURIComponent(remoteIp)}"), "recaptcha branch keeps remoteip: remoteIp");
|
|
assert.ok(!body.includes("ipKeyGenerator"), "the bucketed login IP key must never reach the captcha provider");
|
|
});
|
|
|
|
test("pins: no Host-header-derived allowlist in the verify function", () => {
|
|
const body = extractVerifyExternalCaptchaBody(indexSource);
|
|
assert.ok(!/req\.headers/i.test(body), "verifyExternalCaptcha must not read request headers");
|
|
assert.ok(!/req\.get\(/i.test(body), "verifyExternalCaptcha must not read request headers via req.get");
|
|
const guardCode = guardSource.replace(/\/\/[^\n]*/g, "");
|
|
assert.ok(!/headers/i.test(guardCode), "the guard module derives nothing from headers (comments aside)");
|
|
assert.ok(!/req\b/.test(guardCode), "the guard module never sees the request at all");
|
|
});
|