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,96 @@
|
||||
"use strict";
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
|
||||
const {
|
||||
CAPTCHA_EXPECTED_HOSTNAMES_ENV,
|
||||
CAPTCHA_HOSTNAME_MISMATCH_ERROR,
|
||||
parseCaptchaExpectedHostnames,
|
||||
createCaptchaHostnameGuard,
|
||||
} = require("./captcha-expected-hostnames");
|
||||
|
||||
// adpanel:captcha-hostname-pin — unit coverage for the OPTIONAL siteverify
|
||||
// hostname allowlist: off by default, list parsing (spaces/commas/case),
|
||||
// fail-closed on a missing/blank/non-string hostname, exact match semantics.
|
||||
|
||||
function failure(result) {
|
||||
assert.ok(result, "expected a failure object, got null (allowed)");
|
||||
assert.strictEqual(result.ok, false);
|
||||
assert.strictEqual(result.error, CAPTCHA_HOSTNAME_MISMATCH_ERROR);
|
||||
assert.strictEqual(Object.keys(result).length, 2, "failure shape is exactly { ok, error }");
|
||||
}
|
||||
|
||||
test("env unset means the guard is off and everything passes", () => {
|
||||
const guard = createCaptchaHostnameGuard({ env: {} });
|
||||
assert.strictEqual(guard.enabled, false);
|
||||
assert.strictEqual(guard.expectedHostnames, null);
|
||||
assert.strictEqual(guard.check({ success: true }), null);
|
||||
assert.strictEqual(guard.check({ success: true, hostname: "evil.example" }), null);
|
||||
assert.strictEqual(guard.check({ success: true }), null);
|
||||
assert.strictEqual(guard.check(undefined), null, "no hostname field still passes while off");
|
||||
assert.strictEqual(guard.check(null), null);
|
||||
});
|
||||
|
||||
test("blank or comma-only env values behave like unset (feature off)", () => {
|
||||
for (const raw of ["", " ", ",", " , , ", "\t"]) {
|
||||
const guard = createCaptchaHostnameGuard({ env: { [CAPTCHA_EXPECTED_HOSTNAMES_ENV]: raw } });
|
||||
assert.strictEqual(guard.enabled, false, `raw=${JSON.stringify(raw)} must be off`);
|
||||
assert.strictEqual(guard.expectedHostnames, null);
|
||||
assert.strictEqual(guard.check({ success: true }), null);
|
||||
}
|
||||
assert.strictEqual(parseCaptchaExpectedHostnames(undefined), null);
|
||||
assert.strictEqual(parseCaptchaExpectedHostnames(null), null);
|
||||
assert.strictEqual(parseCaptchaExpectedHostnames(42), null);
|
||||
});
|
||||
|
||||
test("list parsing trims entries, lowercases, and drops empties", () => {
|
||||
assert.deepStrictEqual(parseCaptchaExpectedHostnames(" panel.example.com ,Foo.Bar.ORG,baz"), [
|
||||
"panel.example.com",
|
||||
"foo.bar.org",
|
||||
"baz",
|
||||
]);
|
||||
assert.deepStrictEqual(parseCaptchaExpectedHostnames("A.com,,B.com,"), ["a.com", "b.com"]);
|
||||
assert.deepStrictEqual(parseCaptchaExpectedHostnames("Only.One"), ["only.one"]);
|
||||
});
|
||||
|
||||
test("configured guard requires a hostname field (fail closed when absent)", () => {
|
||||
const guard = createCaptchaHostnameGuard({ env: { [CAPTCHA_EXPECTED_HOSTNAMES_ENV]: "panel.example.com" } });
|
||||
assert.strictEqual(guard.enabled, true);
|
||||
failure(guard.check({ success: true }), "success but no hostname");
|
||||
failure(guard.check({ success: true, hostname: "" }));
|
||||
failure(guard.check({ success: true, hostname: " " }));
|
||||
failure(guard.check({ success: true, hostname: 123 }), "non-string hostname");
|
||||
failure(guard.check({ success: true, hostname: null }));
|
||||
failure(guard.check(null));
|
||||
failure(guard.check(undefined));
|
||||
});
|
||||
|
||||
test("hostname matching is exact after trim + lowercase", () => {
|
||||
const guard = createCaptchaHostnameGuard({
|
||||
env: { [CAPTCHA_EXPECTED_HOSTNAMES_ENV]: "Panel.Example.COM, alt.example.org" },
|
||||
});
|
||||
assert.strictEqual(guard.check({ success: true, hostname: "panel.example.com" }), null);
|
||||
assert.strictEqual(guard.check({ success: true, hostname: "PANEL.EXAMPLE.COM" }), null);
|
||||
assert.strictEqual(guard.check({ success: true, hostname: " panel.example.com " }), null);
|
||||
assert.strictEqual(guard.check({ success: true, hostname: "alt.example.org" }), null);
|
||||
failure(guard.check({ success: true, hostname: "evil.example.com" }));
|
||||
failure(guard.check({ success: true, hostname: "panel.example.com.evil.test" }));
|
||||
failure(guard.check({ success: true, hostname: "sub.panel.example.com" }), "subdomains are not implied");
|
||||
failure(guard.check({ success: true, hostname: "panel.example.com,alt.example.org" }), "no list smuggling");
|
||||
});
|
||||
|
||||
test("guard is constructed once with a fixed env snapshot", () => {
|
||||
const env = { [CAPTCHA_EXPECTED_HOSTNAMES_ENV]: "a.example" };
|
||||
const guard = createCaptchaHostnameGuard({ env });
|
||||
env[CAPTCHA_EXPECTED_HOSTNAMES_ENV] = "b.example";
|
||||
assert.deepStrictEqual(guard.expectedHostnames, ["a.example"], "list does not mutate after construction");
|
||||
});
|
||||
|
||||
test("default env source is process.env and the module exports its constants", () => {
|
||||
assert.strictEqual(CAPTCHA_EXPECTED_HOSTNAMES_ENV, "CAPTCHA_EXPECTED_HOSTNAMES");
|
||||
const guard = createCaptchaHostnameGuard();
|
||||
assert.strictEqual(guard.enabled, process.env.CAPTCHA_EXPECTED_HOSTNAMES !== undefined
|
||||
&& String(process.env.CAPTCHA_EXPECTED_HOSTNAMES).trim() !== ""
|
||||
&& parseCaptchaExpectedHostnames(process.env.CAPTCHA_EXPECTED_HOSTNAMES) !== null);
|
||||
});
|
||||
Reference in new issue
Block a user