Files
Kelx/utils/login-page-errors.test.js
T
2026-11-30 16:32:37 +00:00

96 lines
4.3 KiB
JavaScript

"use strict";
// Tests for utils/login-page-errors.js — remediation for Finding #5,
// reflected ?error= text on the login page (content-spoofing on the most
// phishing-relevant page).
// Run: node --test utils/login-page-errors.test.js
//
// The suite proves: the resolver only ever returns server-owned messages,
// selected by exact opaque code; arbitrary attacker-controlled query text,
// non-string query values (qs arrays/objects — `?error[0]=…`,
// `?error[__proto__]=…`), the empty string, and non-String string objects all
// resolve to null, so nothing from the URL is ever echoed back.
const test = require("node:test");
const assert = require("node:assert");
const {
LOGIN_ERROR_CODE_SUSPENDED,
resolveLoginErrorMessage,
} = require("./login-page-errors");
test("the known code maps to its server-owned message", () => {
assert.strictEqual(resolveLoginErrorMessage("suspended"), "Your account is suspended");
});
test("the exported code constant is the suspended key itself", () => {
assert.strictEqual(LOGIN_ERROR_CODE_SUSPENDED, "suspended");
assert.strictEqual(
resolveLoginErrorMessage(LOGIN_ERROR_CODE_SUSPENDED),
"Your account is suspended"
);
});
test("arbitrary attacker-controlled text resolves to null (never echoed)", () => {
assert.strictEqual(resolveLoginErrorMessage("Your%20account%20is%20suspended"), null);
assert.strictEqual(resolveLoginErrorMessage("Free Bitcoin — click here"), null);
assert.strictEqual(resolveLoginErrorMessage("anything"), null);
// Exact-match on the code: near-misses must not resolve.
assert.strictEqual(resolveLoginErrorMessage("Suspended"), null);
assert.strictEqual(resolveLoginErrorMessage("SUSPENDED"), null);
assert.strictEqual(resolveLoginErrorMessage("suspended "), null);
assert.strictEqual(resolveLoginErrorMessage(" suspended"), null);
assert.strictEqual(resolveLoginErrorMessage("suspended\n"), null);
});
test("the empty string resolves to null", () => {
assert.strictEqual(resolveLoginErrorMessage(""), null);
});
test("qs arrays resolve to null — including an array holding the code itself", () => {
// ?error=suspended&error=suspended parses to an array; ?error[0]=suspended too.
assert.strictEqual(resolveLoginErrorMessage([]), null);
assert.strictEqual(resolveLoginErrorMessage(["suspended"]), null);
assert.strictEqual(resolveLoginErrorMessage(["Your account is suspended"]), null);
assert.strictEqual(resolveLoginErrorMessage([0]), null);
});
test("qs objects resolve to null", () => {
// ?error[__proto__]=x and ?error[0]=suspended parse to plain objects.
assert.strictEqual(resolveLoginErrorMessage({}), null);
assert.strictEqual(resolveLoginErrorMessage({ 0: "suspended" }), null);
assert.strictEqual(resolveLoginErrorMessage({ message: "suspended" }), null);
});
test("__proto__ resolves to null in both senses", () => {
// The literal query value ?error=__proto__ is the string key "__proto__":
// the lookup is a Map get, so it must not hit Object.prototype.
assert.strictEqual(resolveLoginErrorMessage("__proto__"), null);
// ?error[__proto__]=x parses to an object with a __proto__ data property
// (or affects the prototype in legacy parsers) — still non-string → null.
const crafted = JSON.parse('{"__proto__": "suspended"}');
assert.strictEqual(typeof crafted, "object");
assert.strictEqual(resolveLoginErrorMessage(crafted), null);
assert.strictEqual(resolveLoginErrorMessage(Object.create(null)), null);
});
test("other non-string query values resolve to null", () => {
assert.strictEqual(resolveLoginErrorMessage(undefined), null);
assert.strictEqual(resolveLoginErrorMessage(null), null);
assert.strictEqual(resolveLoginErrorMessage(0), null);
assert.strictEqual(resolveLoginErrorMessage(1), null);
assert.strictEqual(resolveLoginErrorMessage(true), null);
assert.strictEqual(resolveLoginErrorMessage(false), null);
// A String object is typeof "object", not "string" — ignored.
assert.strictEqual(resolveLoginErrorMessage(new String("suspended")), null);
});
test("the resolver exposes no way to register new messages from query input", () => {
// The module surface is intentionally tiny: a resolver plus the constant.
const exported = require("./login-page-errors");
assert.deepStrictEqual(Object.keys(exported).sort(), [
"LOGIN_ERROR_CODE_SUSPENDED",
"resolveLoginErrorMessage",
]);
});