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

+121
View File
@@ -0,0 +1,121 @@
"use strict";
// adpanel:safecompare-length-cap — behavior + posture pins for the length cap
// inside safeCompare.
//
// safeCompare is the panel's ONE timing-safe string compare (ledger
// botproof-timing-safe owns its typeof/strict-string discipline — this file
// must not weaken it, only cover the cap that sits AFTER that gate). Every
// legitimate compared value is a fixed-size token; census of all call sites
// (index.js):
// verifyActionToken signature HMAC-sha256 base64url 43
// login CSRF token randomBytes(32) hex 64
// remember-login signature HMAC-sha256 base64url 43
// remember-login fingerprint sha256 hex 64
// session-revoke grace hashes (x8) HMAC-sha256 hex 64
// stealth gate signature HMAC-sha256 base64url 43
// stealth bootstrap state id randomBytes(18) base64url 24
// panel probe nonce randomBytes(16) hex 32
// captcha request fingerprint sha256 hex 64
// pending-login user fingerprint sha256 hex 64
// node/bootstrap tokens operator env config (well under cap)
// botProof seed randomBytes(18) hex 36
// The cap (16384 = Node's default max-http-header-size) is strictly above every
// one of them, so it can never reject a genuine comparison; it only bounds
// worst-case buffer allocation inside the helper regardless of upstream
// body/header-limit changes. The early-false path leaks only that an
// ATTACKER-SUPPLIED length exceeded the cap — attacker-known metadata, never
// secret-derived — while sub-cap comparisons keep the full length-mismatch
// equalization.
const test = require("node:test");
const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const crypto = require("node:crypto");
const ROOT = path.join(__dirname, "..");
const INDEX_JS = path.join(ROOT, "index.js");
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
const START_MARKER = "function safeCompare(a, b) {";
const END_MARKER = "\nconst ACTION_TOKEN_SECRET =";
const start = indexSource.indexOf(START_MARKER);
const end = start >= 0 ? indexSource.indexOf(END_MARKER, start) : -1;
assert.ok(start >= 0, "safeCompare must still exist in index.js");
assert.ok(end > start, "safeCompare block end marker not found");
const safeCompareSrc = indexSource.slice(start, end);
// Execute the REAL extracted source so the behavior pins cannot drift from
// what index.js actually ships (same shape as botproof-timing-safe.test.js —
// safeCompare is module-internal, so source extraction is the reach-in).
function buildSafeCompare() {
const sandbox = { crypto, Buffer, console };
vm.createContext(sandbox);
vm.runInContext(`${safeCompareSrc}\nsafeCompare;`, sandbox);
return sandbox.safeCompare;
}
test("pins: gate order is typeof rejection -> length cap -> buffer build", () => {
const typeofIdx = safeCompareSrc.indexOf('typeof a !== "string"');
const capIdx = safeCompareSrc.indexOf("16384");
const bufIdx = safeCompareSrc.indexOf("Buffer.from(a)");
assert.ok(typeofIdx >= 0, "the strict typeof gate must remain (ledger botproof-timing-safe)");
assert.ok(capIdx > typeofIdx, "the cap must sit AFTER the typeof gate");
assert.ok(bufIdx > capIdx, "the cap must run BEFORE the buffer build");
assert.ok(
safeCompareSrc.includes("a.length > 16384 || b.length > 16384"),
"the cap must check BOTH sides at 16384"
);
});
test("pins: length-mismatch equalization posture is intact for sub-cap inputs", () => {
assert.ok(
safeCompareSrc.includes("if (bufA.length !== bufB.length)") &&
safeCompareSrc.includes("crypto.timingSafeEqual(bufA, bufA)") &&
safeCompareSrc.includes("return false;\n }\n\n return crypto.timingSafeEqual(bufA, bufB);"),
"sub-cap length mismatches must keep burning timingSafeEqual(bufA, bufA) so mismatched length is not length-leaked"
);
});
test("behavior: equal values under the cap -> true, mismatched -> false", () => {
const safeCompare = buildSafeCompare();
assert.strictEqual(safeCompare("abc", "abc"), true);
const token = crypto.randomBytes(32).toString("hex");
assert.strictEqual(safeCompare(token, token), true);
assert.strictEqual(safeCompare(token, token + "0"), false);
assert.strictEqual(safeCompare(token.slice(0, -1), token), false);
assert.strictEqual(safeCompare("", ""), true);
});
test("behavior: overlong input (either side, > 16384) fails closed immediately", () => {
const safeCompare = buildSafeCompare();
// Equal overlong strings would compare true without the cap — this is the
// discriminating case that the cap itself rejects.
assert.strictEqual(safeCompare("a".repeat(16385), "a".repeat(16385)), false);
assert.strictEqual(safeCompare("x", "a".repeat(16385)), false);
assert.strictEqual(safeCompare("a".repeat(16385), "x"), false);
assert.strictEqual(safeCompare("a".repeat(100000), "a".repeat(100000)), false);
});
test("behavior: exactly-at-cap boundary behaves as under-cap", () => {
const safeCompare = buildSafeCompare();
assert.strictEqual(safeCompare("a".repeat(16384), "a".repeat(16384)), true);
assert.strictEqual(safeCompare("a".repeat(16384), "a".repeat(16383) + "b"), false);
assert.strictEqual(safeCompare("a".repeat(16384), "a".repeat(16385)), false);
});
test("behavior: typeof/Array rejections still fail closed BEFORE the cap", () => {
const safeCompare = buildSafeCompare();
assert.strictEqual(safeCompare(["token"], ["token"]), false);
assert.strictEqual(safeCompare(["token"], "token"), false);
assert.strictEqual(safeCompare("token", ["token"]), false);
assert.strictEqual(safeCompare(null, null), false);
assert.strictEqual(safeCompare(undefined, "token"), false);
assert.strictEqual(safeCompare(123, 123), false);
assert.strictEqual(safeCompare({ toString: () => "token" }, "token"), false);
assert.strictEqual(safeCompare(new String("token"), new String("token")), false);
});