Public: anyone can read this; the crew can change it.
A test pins that the proxy serves only verified domains: `getVerifiedDomain`, `primaryDomain`, `removeDomain` #6799
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/6791-a-test-pins-that-the-proxy-serves-only-v → main 1 commit fork 1 thread · 1 unresolved
1 file
changed +83
−0
test/core-domains.test.ts added +83
| @@ -0,0 +1,83 @@ | ||
| 1 | /** | |
| 2 | * The proxy serves only verified domains (#6791): `getVerifiedDomain` is | |
| 3 | * the one lookup it trusts, so an unverified host, a near-miss spelling and | |
| 4 | * a subdomain of a verified host are all refused, and a removed host goes. | |
| 5 | */ | |
| 6 | import { expect, test } from "bun:test"; | |
| 7 | import { | |
| 8 | addDomain, | |
| 9 | getVerifiedDomain, | |
| 10 | listDomains, | |
| 11 | primaryDomain, | |
| 12 | recordCheck, | |
| 13 | removeDomain, | |
| 14 | type DomainRules, | |
| 15 | } from "../src/core/domains.ts"; | |
| 16 | import { migrations } from "../src/core/schema.ts"; | |
| 17 | import { openDb } from "../src/kernel/db.ts"; | |
| 18 | ||
| 19 | const RULES: DomainRules = { forgeHost: "forge.test" }; | |
| 20 | ||
| 21 | function fresh() { | |
| 22 | const db = openDb(":memory:", migrations); | |
| 23 | // Domains are rows of their own; the app they belong to is not under test. | |
| 24 | db.run("PRAGMA foreign_keys = OFF"); | |
| 25 | return db; | |
| 26 | } | |
| 27 | ||
| 28 | const add = (db: ReturnType<typeof fresh>, host: string, appId = "a1") => { | |
| 29 | const r = addDomain(db, { host, appId }, RULES); | |
| 30 | if (!r.ok) throw new Error(`add ${host}: ${r.error}`); | |
| 31 | return r.value; | |
| 32 | }; | |
| 33 | ||
| 34 | test("an unverified host is listed but not served; a passing check serves it", () => { | |
| 35 | const db = fresh(); | |
| 36 | add(db, "app.example.com"); | |
| 37 | expect(listDomains(db, "a1").map((d) => d.host)).toEqual(["app.example.com"]); | |
| 38 | expect(getVerifiedDomain(db, "app.example.com")).toBeUndefined(); | |
| 39 | ||
| 40 | recordCheck(db, "app.example.com", { verified: false, said: "no record" }); | |
| 41 | expect(getVerifiedDomain(db, "app.example.com")).toBeUndefined(); | |
| 42 | ||
| 43 | recordCheck(db, "app.example.com", { verified: true, said: "ok" }); | |
| 44 | expect(getVerifiedDomain(db, "app.example.com")?.host).toBe("app.example.com"); | |
| 45 | }); | |
| 46 | ||
| 47 | test("a failing check after verification does not unserve the host", () => { | |
| 48 | const db = fresh(); | |
| 49 | add(db, "app.example.com"); | |
| 50 | recordCheck(db, "app.example.com", { verified: true, said: "ok" }); | |
| 51 | const at = getVerifiedDomain(db, "app.example.com")!.verifiedAt; | |
| 52 | recordCheck(db, "app.example.com", { verified: false, said: "gone" }); | |
| 53 | const still = getVerifiedDomain(db, "app.example.com"); | |
| 54 | expect(still?.verifiedAt).toBe(at); | |
| 55 | expect(still?.said).toBe("gone"); | |
| 56 | }); | |
| 57 | ||
| 58 | test("the lookup is exact: case and a trailing dot fold, a subdomain does not", () => { | |
| 59 | const db = fresh(); | |
| 60 | add(db, "example.com"); | |
| 61 | recordCheck(db, "example.com", { verified: true, said: "ok" }); | |
| 62 | expect(getVerifiedDomain(db, "EXAMPLE.com")?.host).toBe("example.com"); | |
| 63 | expect(getVerifiedDomain(db, "example.com.")?.host).toBe("example.com"); | |
| 64 | expect(getVerifiedDomain(db, "www.example.com")).toBeUndefined(); | |
| 65 | expect(getVerifiedDomain(db, "a.b.example.com")).toBeUndefined(); | |
| 66 | expect(getVerifiedDomain(db, "com")).toBeUndefined(); | |
| 67 | expect(getVerifiedDomain(db, "xexample.com")).toBeUndefined(); | |
| 68 | }); | |
| 69 | ||
| 70 | test("primaryDomain names the verified one and removeDomain unserves it", () => { | |
| 71 | const db = fresh(); | |
| 72 | add(db, "app.example.com"); | |
| 73 | expect(primaryDomain(db, "a1")).toBeUndefined(); | |
| 74 | recordCheck(db, "app.example.com", { verified: true, said: "ok" }); | |
| 75 | expect(primaryDomain(db, "a1")?.host).toBe("app.example.com"); | |
| 76 | expect(primaryDomain(db, "other")).toBeUndefined(); | |
| 77 | ||
| 78 | expect(removeDomain(db, "app.example.com")).toBe(true); | |
| 79 | expect(getVerifiedDomain(db, "app.example.com")).toBeUndefined(); | |
| 80 | expect(primaryDomain(db, "a1")).toBeUndefined(); | |
| 81 | expect(listDomains(db, "a1")).toEqual([]); | |
| 82 | expect(removeDomain(db, "app.example.com")).toBe(false); | |
| 83 | }); |
new above
offline
Nit: add a second verified host here and assert primaryDomain returns the older one. That is the documented contract and nothing pins it yet.