Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/portal-domain-controller/src/contracts.test.ts
T

88 lines
3.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
canonicalJson,
createSignedDomainCommand,
createSignedDomainResult,
normalizeDomainHostname,
sha256Hex,
verifySignedDomainCommand,
verifySignedDomainResult,
} from "./contracts.js";
const SECRET = "test-command-secret-that-is-at-least-thirty-two-bytes";
const command = {
claimId: "claim_12345678",
tenantId: "tenant_12345678",
generation: 3,
hostname: "portal.example.com",
action: "provision" as const,
};
describe("portal-domain signed contracts", () => {
it("produces a deterministic hash and domain-separated signature", () => {
const first = createSignedDomainCommand(command, SECRET);
const second = createSignedDomainCommand({ ...command }, SECRET);
expect(first).toEqual(second);
expect(first.contentHash).toBe(sha256Hex(canonicalJson(first.payload)));
expect(verifySignedDomainCommand(first, SECRET)).toEqual(first.payload);
const result = createSignedDomainResult({
...command,
status: "active",
attempt: 1,
completedAt: "2026-07-16T10:00:00.000Z",
configHash: "a".repeat(64),
certificateExpiresAt: "2026-10-01T00:00:00.000Z",
}, SECRET);
expect(result.signature).not.toBe(first.signature);
expect(verifySignedDomainResult(result, SECRET).status).toBe("active");
});
it("rejects payload, hash, signature, algorithm and unknown-field tampering", () => {
const signed = createSignedDomainCommand(command, SECRET);
expect(() => verifySignedDomainCommand({ ...signed, payload: { ...signed.payload, generation: 4 } }, SECRET)).toThrow("content hash");
expect(() => verifySignedDomainCommand({ ...signed, contentHash: "0".repeat(64) }, SECRET)).toThrow("content hash");
expect(() => verifySignedDomainCommand({ ...signed, signature: "0".repeat(64) }, SECRET)).toThrow("signature");
expect(() => verifySignedDomainCommand({ ...signed, algorithm: "none" }, SECRET)).toThrow("algorithm");
expect(() => verifySignedDomainCommand({ ...signed, injected: true }, SECRET)).toThrow("unknown or missing");
expect(() => verifySignedDomainCommand({ ...signed, payload: { ...signed.payload, injected: true } }, SECRET)).toThrow("unknown or missing");
expect(() => verifySignedDomainCommand(signed, `${SECRET}!`)).toThrow("signature");
});
it("accepts only canonical, concrete fully qualified hostnames and bounded identifiers", () => {
expect(normalizeDomainHostname("Portal.Example.com.")).toBe("portal.example.com");
for (const hostname of ["*.example.com", "127.0.0.1", "localhost", "-bad.example.com", "bad_.example.com", "[::1]"]) {
expect(() => normalizeDomainHostname(hostname)).toThrow();
}
expect(() => createSignedDomainCommand({ ...command, hostname: "Portal.Example.com" }, SECRET)).toThrow("canonical");
expect(() => createSignedDomainCommand({ ...command, claimId: "../x" }, SECRET)).toThrow("claim id");
expect(() => createSignedDomainCommand({ ...command, generation: 0 }, SECRET)).toThrow("generation");
});
it("rejects undersized signing secrets and non-canonical result timestamps", () => {
expect(() => createSignedDomainCommand(command, "short")).toThrow("32");
expect(() => createSignedDomainResult({
...command,
status: "active",
attempt: 1,
completedAt: "2026-07-16",
}, SECRET)).toThrow("timestamp");
expect(() => createSignedDomainResult({
...command,
status: "active",
attempt: 1,
completedAt: "2026-07-16T10:00:00.000Z",
configHash: "not-a-sha256",
certificateExpiresAt: "2026-10-01T00:00:00.000Z",
}, SECRET)).toThrow("configHash");
expect(() => createSignedDomainResult({
...command,
status: "active",
attempt: 1,
completedAt: "2026-07-16T10:00:00.000Z",
configHash: "a".repeat(64),
certificateExpiresAt: "2026-07-16T09:00:00.000Z",
}, SECRET)).toThrow("expire after observation");
});
});