import { describe, expect, it } from "bun:test";
import { readFileSync } from "fs";
const SRC = readFileSync("src/routes/saml-sso.tsx", "utf8");
describe("verification is mandatory (fail-closed)", () => {
it("refuses when no IdP certificate is configured", () => {
expect(SRC).toContain("if (!idpCertPem || !idpCertPem.trim())");
expect(SRC).toContain("Refusing to accept an unverified assertion");
});
it("no longer makes verification conditional on the cert being present", () => {
expect(SRC).not.toMatch(/if \(idpCertPem && idpCertPem\.trim\(\)\) \{\s*\n\s*const sigValid/);
});
});
describe("anti-wrapping structural checks", () => {
it("runs before signature verification", () => {
const coverage = SRC.indexOf("assertSignatureCoversAssertion(xml)");
const verify = SRC.indexOf("if (!verifySamlSignature(xml, idpCertPem))");
expect(coverage).toBeGreaterThan(-1);
expect(verify).toBeGreaterThan(-1);
expect(coverage).toBeLessThan(verify);
});
it("rejects multiple Assertions", () => {
expect(SRC).toContain("assertionIds.length > 1");
expect(SRC).toContain("signature-wrapping attempt");
});
it("rejects a Reference that covers neither the Response nor the Assertion", () => {
expect(SRC).toContain("referenced !== assertionIds[0] && referenced !== responseId");
});
it("rejects a response with no Assertion at all", () => {
expect(SRC).toContain("assertionIds.length === 0");
});
});
describe("signature algorithm selection", () => {
it("reads Algorithm from SignatureMethod, not the first match in the doc", () => {
expect(SRC).toContain("SignatureMethod[^>]*\\bAlgorithm=");
expect(SRC).not.toMatch(/const algMatch = xml\.match\(\/Algorithm=/);
});
it("refuses an unrecognised algorithm instead of defaulting to SHA1", () => {
expect(SRC).toContain("unrecognised SignatureMethod");
});
});
describe("the incompleteness is documented, not hidden", () => {
it("verifySamlSignature is marked as not proving assertion integrity", () => {
expect(SRC).toContain("INCOMPLETE BY DESIGN");
});
});
|