Public: anyone can read this; the crew can change it.

Domain claims: proved names should lapse, so a re-registered domain can be reclaimed #14892

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14888-domain-claims-proved-names-should-lapse → main 4 commits fork

  • Merged. scout/14888-domain-claims-proved-names-should-lapse was merged into main as e83da2e.

Review

Sign in to review this change.
10 files changed +337 −35
docs/features/app-own-domain.md +24
@@ -83,3 +83,27 @@ All in `test/app-domains.test.ts`:
8383 with the domain";
8484 - removal makes the host unknown again and the ask 404: "removal makes the
8585 host unknown again".
86
87 ## Claiming a name someone else holds
88
89 If a domain lapsed and you registered it, the old holder's row does not
90 block you. `POST /api/v1/repos/:owner/:name/app/domains/:host/claim` (admin
91 scope) looks up `_forge-verify.<host>`. When your claim record
92 (`forge-verify=<claim token>`) is published and the holder's own record is
93 not, the name moves to your app, already verified. Otherwise the answer is
94 422 `name_held` with the `publish` record to add. The decision is the proof's,
95 not the clock's: a holder whose record still stands keeps the name. A
96 resolver failure counts as "still held".
97
98 The claimed row is the claimant's: production's environment, a fresh
99 claim token, and no old records, which were the holder's and go with the
100 holder's row. The `publish` record names the lowercased host the check
101 reads, however the request spelled it. (`held` is another error's word on
102 this API, so the claim's is `name_held`.)
103
104 In the terminal, the domains screen (`n` from the deploys screen) has
105 `t`, "take a name another app holds": type the name and it runs the same
106 action. A held name answers with the TXT record to publish; claim again
107 once it is there. Tests: "a name moves to the app that proves it once the
108 holder's record is gone" in `test/app-domains.test.ts` and the `t` key in
109 `test/tui-deploys.test.ts`.
src/actions/domains.ts +91 −33
@@ -7,13 +7,17 @@
77
88 import type { App } from "../app.ts";
99 import { certificateSaid, probeCertificate } from "../apps/certificate.ts";
10 import { checkDomain, recordsFor, type DnsRecord } from "../apps/domains.ts";
10 import { checkClaim, checkDomain, recordsFor, verifyName, verifyValue, type DnsRecord } from "../apps/domains.ts";
1111 import { appHostSuffix } from "../apps/host.ts";
1212 import { appsRuntime, type AppsRuntime } from "../apps/runtime.ts";
1313 import { getAppByRepo, type AppRecord } from "../core/apps.ts";
1414 import {
1515 addDomain,
16 claimToken,
1617 getDomain,
18 normalizeHost,
19 takeOverDomain,
20 type TakeOverError,
1721 listDomains,
1822 parseRecordsInput,
1923 setOldRecords,
@@ -64,6 +68,19 @@ function serving(app: App): Result<AppsRuntime, "no_runtime"> {
6468 return runtime === undefined ? err("no_runtime") : ok(runtime);
6569 }
6670
71 /** The admin, their app and the runtime that serves it, or why not. */
72 function admittedServing(
73 app: App,
74 actor: User | undefined,
75 repo: Repo,
76 ): Result<{ admin: User; record: AppRecord; runtime: AppsRuntime }, DomainError> {
77 const gate = admitted(app, actor, repo);
78 if (!gate.ok) return gate;
79 const runtime = serving(app);
80 if (!runtime.ok) return runtime;
81 return ok({ ...gate.value, runtime: runtime.value });
82 }
83
6784 /** The forge's own hostname — the one name no app may take. */
6885 function forgeHost(app: App): string {
6986 return app.config.url !== undefined
@@ -107,15 +124,18 @@ export function listAppDomains(
107124 actor: User | undefined,
108125 repo: Repo,
109126 ): Result<AppDomainView[], DomainError> {
110 const gate = admitted(app, actor, repo);
127 const gate = admittedServing(app, actor, repo);
111128 if (!gate.ok) return gate;
112 const runtime = serving(app);
113 if (!runtime.ok) return runtime;
114 return ok(
115 listDomains(app.db, gate.value.record.id).map((domain) =>
116 view(runtime.value, gate.value.record, domain),
117 ),
118 );
129 const { record, runtime } = gate.value;
130 return ok(listDomains(app.db, record.id).map((domain) => view(runtime, record, domain)));
131 }
132
133 /** What names are the forge's own to hand out, for this app's runtime. */
134 function domainRules(app: App, runtime: { options: { domain: string } }) {
135 return {
136 forgeHost: forgeHost(app),
137 appHostSuffix: appHostSuffix(runtime.options.domain.toLowerCase().replace(/:\d+$/, "")),
138 };
119139 }
120140
121141 /** Add a domain; the answer carries the two records to publish. */
@@ -126,22 +146,15 @@ export function addAppDomain(
126146 host: string,
127147 oldRecords?: unknown,
128148 ): Result<AppDomainView, DomainError | AddDomainError | "invalid_records"> {
129 const gate = admitted(app, actor, repo);
149 const gate = admittedServing(app, actor, repo);
130150 if (!gate.ok) return gate;
131 const runtime = serving(app);
132 if (!runtime.ok) return runtime;
133 const { admin, record } = gate.value;
151 const { admin, record, runtime } = gate.value;
134152 const kept = oldRecords === undefined ? ok<OldRecord[]>([]) : parseRecordsInput(oldRecords);
135153 if (!kept.ok) return kept;
136154 const added = addDomain(
137155 app.db,
138156 { host, appId: record.id, createdBy: admin.id },
139 {
140 forgeHost: forgeHost(app),
141 appHostSuffix: appHostSuffix(
142 runtime.value.options.domain.toLowerCase().replace(/:\d+$/, ""),
143 ),
144 },
157 domainRules(app, runtime),
145158 );
146159 if (!added.ok) return added;
147160 if (kept.value.length > 0) setOldRecords(app.db, added.value.host, kept.value);
@@ -151,7 +164,56 @@ export function addAppDomain(
151164 actorId: admin.id,
152165 payload: { host: added.value.host, slug: record.slug },
153166 });
154 return ok(view(runtime.value, record, getDomain(app.db, added.value.host) ?? added.value));
167 return ok(view(runtime, record, getDomain(app.db, added.value.host) ?? added.value));
168 }
169
170 /**
171 * Claim a name another app holds, by proof: publish `claimProofRecord`'s
172 * TXT and, once the holder's own record is gone (the domain lapsed and was
173 * registered again), the name moves here, verified. A live, proved holder
174 * keeps it. A name nobody holds is added as usual.
175 */
176 export async function claimAppDomain(
177 app: App,
178 actor: User | undefined,
179 repo: Repo,
180 host: string,
181 ): Promise<Result<AppDomainView, DomainError | TakeOverError | "invalid_records">> {
182 const gate = admittedServing(app, actor, repo);
183 if (!gate.ok) return gate;
184 const { admin, record, runtime } = gate.value;
185 const holder = getDomain(app.db, normalizeHost(host));
186 if (holder === undefined) return addAppDomain(app, actor, repo, host);
187 if (holder.appId === record.id) return err("own_domain");
188 const proof = await checkClaim(
189 holder.host,
190 holder.token,
191 claimToken(holder.host, record.id),
192 runtime.options.dns,
193 );
194 const taken = takeOverDomain(
195 app.db,
196 { host, appId: record.id, createdBy: admin.id },
197 domainRules(app, runtime),
198 proof,
199 );
200 if (!taken.ok) return taken;
201 app.bus.append({
202 kind: "app.domain.claimed",
203 repoId: repo.id,
204 actorId: admin.id,
205 payload: { host: taken.value.host, slug: record.slug, from: holder.appId },
206 });
207 return ok(view(runtime, record, taken.value));
208 }
209
210 /** The TXT record an app publishes to claim `host` from whoever holds it. */
211 export function claimProofRecord(
212 host: string,
213 appId: string,
214 ): { type: "TXT"; name: string; value: string } {
215 const name = normalizeHost(host);
216 return { type: "TXT", name: verifyName(name), value: verifyValue(claimToken(name, appId)) };
155217 }
156218
157219 /**
@@ -165,11 +227,9 @@ export function keepOldRecords(
165227 host: string,
166228 records: unknown,
167229 ): Result<AppDomainView, DomainError | "no_such_domain" | "invalid_records"> {
168 const gate = admitted(app, actor, repo);
230 const gate = admittedServing(app, actor, repo);
169231 if (!gate.ok) return gate;
170 const runtime = serving(app);
171 if (!runtime.ok) return runtime;
172 const { admin, record } = gate.value;
232 const { admin, record, runtime } = gate.value;
173233 const domain = ownedDomain(app, record, host);
174234 if (domain === undefined) return err("no_such_domain");
175235 const parsed = parseRecordsInput(records);
@@ -181,7 +241,7 @@ export function keepOldRecords(
181241 actorId: admin.id,
182242 payload: { host: domain.host, slug: record.slug, count: parsed.value.length },
183243 });
184 return ok(view(runtime.value, record, { ...domain, oldRecords: parsed.value }));
244 return ok(view(runtime, record, { ...domain, oldRecords: parsed.value }));
185245 }
186246
187247 /**
@@ -194,24 +254,22 @@ export async function checkAppDomain(
194254 repo: Repo,
195255 host: string,
196256 ): Promise<Result<AppDomainView, DomainError | "no_such_domain">> {
197 const gate = admitted(app, actor, repo);
257 const gate = admittedServing(app, actor, repo);
198258 if (!gate.ok) return gate;
199 const runtime = serving(app);
200 if (!runtime.ok) return runtime;
201 const { admin, record } = gate.value;
259 const { admin, record, runtime } = gate.value;
202260 const domain = ownedDomain(app, record, host);
203261 if (domain === undefined) return err("no_such_domain");
204262 const found = await checkDomain(
205263 domain.host,
206264 domain.token,
207 runtime.value.hostFor(record.slug, domain.env),
208 runtime.value.options.dns,
265 runtime.hostFor(record.slug, domain.env),
266 runtime.options.dns,
209267 );
210268 // A verified name is served, so its certificate is worth reading: a failed
211269 // renewal shows here instead of on the day it expires.
212270 const said = found.verified
213271 ? `${found.said} ${certificateSaid(
214 await (runtime.value.options.certificate ?? ((h) => probeCertificate(h)))(
272 await (runtime.options.certificate ?? ((h) => probeCertificate(h)))(
215273 domain.host,
216274 ),
217275 new Date(),
@@ -228,7 +286,7 @@ export async function checkAppDomain(
228286 payload: { host: domain.host, slug: record.slug },
229287 });
230288 }
231 return ok(view(runtime.value, record, updated));
289 return ok(view(runtime, record, updated));
232290 }
233291
234292 /** Take a domain away. The forge stops answering there at once. */
src/apps/domains.ts +19
@@ -195,6 +195,25 @@ export async function checkDomain(
195195 };
196196 }
197197
198 /**
199 * Is a claim proved? The claimant's token is in the TXT set and the
200 * holder's is not. A lookup that failed proves nothing either way.
201 */
202 export async function checkClaim(
203 host: string,
204 holderToken: string,
205 claimantToken: string,
206 resolver: DomainResolver = systemResolver,
207 ): Promise<{ claimantPublished: boolean; holderPublished: boolean }> {
208 const proof = await lookup(() => resolver.resolveTxt(verifyName(host)));
209 if (proof.kind === "failed") return { claimantPublished: false, holderPublished: true };
210 const values = proof.kind === "none" ? [] : proof.value.map((chunks) => chunks.join(""));
211 return {
212 claimantPublished: values.includes(verifyValue(claimantToken)),
213 holderPublished: values.includes(verifyValue(holderToken)),
214 };
215 }
216
198217 /** Where the name sends traffic, as one clause of the verified sentence. */
199218 async function pointing(
200219 host: string,
src/core/domains.ts +53
@@ -186,6 +186,59 @@ function newToken(): string {
186186 return out;
187187 }
188188
189 /**
190 * The token an app publishes to claim a name another app holds. Derived from
191 * host and app, so the claimant can read it before any row is theirs; like
192 * every token here it proves nothing until it is in the zone's DNS.
193 */
194 export function claimToken(host: string, appId: string): string {
195 const bytes = new Bun.CryptoHasher("sha256").update(`forge-claim\0${normalizeHost(host)}\0${appId}`).digest();
196 let out = "";
197 for (let i = 0; i < TOKEN_LENGTH; i++) out += TOKEN_ALPHABET[bytes[i]! & 31];
198 return out;
199 }
200
201 export type TakeOverError = AddDomainError | "not_held" | "own_domain" | "name_held";
202
203 /**
204 * Give a held name to the app that proved it. The decision is the proof's,
205 * not the clock's: the claimant's `claimToken` must be in DNS and the
206 * holder's own token must not be. A holder whose record still stands keeps
207 * the name, and so does one the resolver could not be asked about.
208 */
209 export function takeOverDomain(
210 db: Database,
211 input: { host: string; appId: string; createdBy?: string | undefined },
212 rules: DomainRules,
213 proof: { claimantPublished: boolean; holderPublished: boolean },
214 ): Result<AppDomain, TakeOverError> {
215 const host = normalizeHost(input.host);
216 if (!isHostname(host)) return err("invalid_host");
217 if (isReserved(host, rules)) return err("reserved_host");
218 const holder = getDomain(db, host);
219 if (!holder) return err("not_held");
220 if (holder.appId === input.appId) return err("own_domain");
221 if (!proof.claimantPublished || proof.holderPublished) return err("name_held");
222 const at = now();
223 db.transaction(() => {
224 db.query("DELETE FROM app_domain WHERE host = ?1").run(host);
225 // Like `addDomain`, the claimant's row is production's; the holder's
226 // old records were the holder's and go with the row.
227 db.query(
228 "INSERT INTO app_domain (host, app_id, env, token, created_by, created_at, verified_at, checked_at, said)" +
229 " VALUES (?1, ?2, 'production', ?3, ?4, ?5, ?5, ?5, ?6)",
230 ).run(
231 host,
232 input.appId,
233 claimToken(host, input.appId),
234 input.createdBy ?? null,
235 at,
236 "Claimed by proof: the TXT record is published.",
237 );
238 })();
239 return ok(getDomain(db, host)!);
240 }
241
189242 export function addDomain(
190243 db: Database,
191244 input: { host: string; appId: string; createdBy?: string | undefined },
src/design/activity.ts +1
@@ -99,6 +99,7 @@ export function eventVerb(event: ForgeEvent): string {
9999 if (kind.startsWith("app.domain.")) {
100100 const host = payload["host"];
101101 const which = typeof host === "string" ? host : "a domain";
102 if (kind === "app.domain.claimed") return `claimed the domain ${which} by proof`;
102103 if (kind === "app.domain.added") return `gave the app the domain ${which}`;
103104 if (kind === "app.domain.verified") return `verified the domain ${which}`;
104105 if (kind === "app.domain.old_records") return `wrote down the old DNS records of ${which}`;
src/http/api/app.ts +20 −1
@@ -9,7 +9,7 @@
99 import { readAppMembers, setAppMembersThrough, type AppMembersError } from "../../actions/app_members.ts";
1010 import { appView, appVisitors, deleteAppEnvVar, disableApp, enableApp, listAppEnv, openEnvUrl, redeployEnv, setAppEnvVar, stopEnv, updateAppSettings, type AppSettingsInput } from "../../actions/apps.ts";
1111 import { pullNow, PULL_NOW_REFUSALS } from "../../actions/app_pull.ts";
12 import { addAppDomain, checkAppDomain, keepOldRecords, listAppDomains, removeAppDomain } from "../../actions/domains.ts";
12 import { addAppDomain, claimAppDomain, claimProofRecord, checkAppDomain, keepOldRecords, listAppDomains, removeAppDomain } from "../../actions/domains.ts";
1313 import { previewBuild, previewBuildRefusalText } from "../../actions/build_preview.ts";
1414 import { SETTING_WORDS } from "../../actions/setting_words.ts";
1515 import { appsRuntime } from "../../apps/runtime.ts";
@@ -21,6 +21,8 @@ import { holds, weakEtag } from "../etag.ts";
2121 import { BUILD_ASKS_WINDOW_MS } from "../../actions/build_preview.ts";
2222 import { apiError, body, json, notFound, requireScope, respondAccess, str, tryAgain, unauthorized } from "../respond.ts";
2323 import { DEFAULT_ENV_SCOPE } from "../../core/app_env.ts";
24 import { getAppByRepo } from "../../core/apps.ts";
25 import { normalizeHost } from "../../core/domains.ts";
2426 import type { Router } from "../router.ts";
2527
2628 function appPatch(input: Record<string, unknown>): AppSettingsInput {
@@ -181,6 +183,23 @@ export function appRoutes(router: Router<Handler>): void {
181183 return json(added.value, 201);
182184 });
183185
186 // Claim a name another app holds, by proof: 422 names the TXT record to publish.
187 router.add("POST", "/api/v1/repos/:owner/:name/app/domains/:host/claim", async (ctx) => {
188 const denied = requireScope(ctx, "admin");
189 if (denied) return denied;
190 const repo = loadRepo(ctx);
191 if (!repo) return notFound();
192 const host = normalizeHost(ctx.params.host ?? "");
193 const claimed = await claimAppDomain(ctx.app, ctx.auth.user, repo, host);
194 if (claimed.ok) return json(claimed.value, 201);
195 if (claimed.error === "name_held") {
196 const app = getAppByRepo(ctx.app.db, repo.id);
197 const publish = app ? claimProofRecord(host, app.id) : null;
198 return json({ error: "name_held", publish }, 422);
199 }
200 return respondAccess(claimed.error, 422);
201 });
202
184203 router.add(
185204 "POST",
186205 "/api/v1/repos/:owner/:name/app/domains/:host/check",
src/tui/screens/deploys.ts +28
@@ -85,6 +85,8 @@ import { recordsFromLine, type OldRecord } from "../../core/domains.ts";
8585 import {
8686 addAppDomain,
8787 checkAppDomain,
88 claimAppDomain,
89 claimProofRecord,
8890 keepOldRecords,
8991 listAppDomains,
9092 removeAppDomain,
@@ -792,6 +794,27 @@ export function domainsScreen(f: Session, ctx: Ctx, repo: Repo, changed: () => v
792794 );
793795 };
794796
797 /** Claim a name another app holds, by proof: `claimAppDomain`, as the API's POST …/claim. */
798 const claim = (): void => {
799 ctx.push(
800 promptScreen(ctx, self, {
801 title: "Claim a domain another app holds",
802 label: "Domain",
803 placeholder: "app.example.com",
804 hint: "Publish the TXT record it names; the name moves once the holder's own record is gone.",
805 onSubmit: async (host) => {
806 const claimed = await claimAppDomain(f.app, f.user, repo, host);
807 if (claimed.ok) ctx.toast(domainAddedText(claimed.value));
808 else if (claimed.error === "name_held") {
809 const t = claimProofRecord(host, getAppByRepo(f.app.db, repo.id)?.id ?? "");
810 ctx.toast(`Held. Publish a TXT record at ${t.name} with the value ${t.value}, then claim again.`, "attn");
811 } else ctx.toast(refusal(claimed.error, "That domain could not be claimed."), "danger");
812 after();
813 },
814 }),
815 );
816 };
817
795818 const check = (d: AppDomainView): void =>
796819 runAsync(ctx, async () => {
797820 const checked = await checkAppDomain(f.app, f.user, repo, d.host);
@@ -897,6 +920,7 @@ export function domainsScreen(f: Session, ctx: Ctx, repo: Repo, changed: () => v
897920 : [
898921 binding(["j", "k"], "move"),
899922 binding(["+"], "add a domain"),
923 binding(["t"], "take a name another app holds"),
900924 ...(domains.length > 0
901925 ? [
902926 binding(["c"], "check its DNS now"),
@@ -946,6 +970,10 @@ export function domainsScreen(f: Session, ctx: Ctx, repo: Repo, changed: () => v
946970 add();
947971 return true;
948972 }
973 if (e.key === "t") {
974 claim();
975 return true;
976 }
949977 if (e.key === "c" && selected !== undefined) {
950978 check(selected);
951979 return true;
test/app-domains.test.ts +61 −1
@@ -15,10 +15,13 @@ import { newApp, updateAppSettings } from "../src/actions/apps.ts";
1515 import {
1616 addAppDomain,
1717 checkAppDomain,
18 claimAppDomain,
19 claimProofRecord,
1820 removeAppDomain,
1921 } from "../src/actions/domains.ts";
2022 import { createRepo } from "../src/actions/repos.ts";
2123 import {
24 checkClaim,
2225 checkDomain,
2326 recordsFor,
2427 type DomainResolver,
@@ -26,7 +29,7 @@ import {
2629 import { AppsRuntime } from "../src/apps/runtime.ts";
2730 import { certificateSaid, probeCertificate } from "../src/apps/certificate.ts";
2831 import { createApp as insertApp, type AppRecord } from "../src/core/apps.ts";
29 import { addDomain, getDomain, TOKEN_LENGTH } from "../src/core/domains.ts";
32 import { addDomain, claimToken, getDomain, TOKEN_LENGTH } from "../src/core/domains.ts";
3033 import { listEvents } from "../src/core/events.ts";
3134 import { eventVerb } from "../src/design/activity.ts";
3235 import type { Repo } from "../src/core/repos.ts";
@@ -895,6 +898,63 @@ describe("an app gets its own domain", () => {
895898 expect(getDomain(app.db, HOST)!.token).not.toBe(oldToken);
896899 expect(removeAppDomain(app, alice, repo, HOST).ok).toBe(false);
897900 });
901
902 test("a name moves to the app that proves it once the holder's record is gone", async () => {
903 const claimHost = "claim.example.com";
904 const proofName = `_forge-verify.${claimHost}`;
905 const CLAIM = `/api/v1/repos/alice/other/app/domains/${claimHost}/claim`;
906 const held = addAppDomain(app, alice, repo, claimHost);
907 if (!held.ok) throw new Error(held.error);
908 const holderToken = getDomain(app.db, claimHost)!.token;
909 const mine = claimProofRecord(claimHost, other.id);
910 expect(mine.name).toBe(proofName);
911 const published = [mine.value];
912 const holderValue = `forge-verify=${holderToken}`;
913
914 // The resolver failing proves nothing: the holder is taken to stand.
915 const failing: DomainResolver = {
916 resolveTxt: () => Promise.reject(Object.assign(new Error("x"), { code: "ESERVFAIL" })),
917 resolveCname: () => Promise.reject(Object.assign(new Error("x"), { code: "ESERVFAIL" })),
918 };
919 expect(await checkClaim(claimHost, holderToken, claimToken(claimHost, other.id), failing)).toEqual({
920 claimantPublished: false,
921 holderPublished: true,
922 });
923
924 // A non-admin is refused, and nothing moved.
925 expect((await claimAppDomain(app, bob, otherRepo, claimHost)).ok).toBe(false);
926 expect((await api(CLAIM, { method: "POST", token: bobToken })).status).toBe(404);
927 expect((await api(CLAIM, { method: "POST", token: rwToken })).status).toBeGreaterThanOrEqual(400);
928
929 // No proof yet: 422 `held`, with the TXT record to publish at the normalised name.
930 const none = await api(CLAIM.replace(claimHost, "CLAIM.Example.com"), { method: "POST" });
931 expect(none.status).toBe(422);
932 expect(await none.json()).toMatchObject({ error: "name_held", publish: mine });
933 expect(getDomain(app.db, claimHost)!.appId).toBe(record.id);
934
935 // The holder's record still stands beside the claim: held.
936 dns.txt.set(proofName, [[holderValue], published]);
937 expect((await api(CLAIM, { method: "POST" })).status).toBe(422);
938 expect(getDomain(app.db, claimHost)!.appId).toBe(record.id);
939
940 // The holder's record is gone and the claim is published: the name moves, verified.
941 dns.txt.set(proofName, [published]);
942 const moved = await api(CLAIM, { method: "POST" });
943 expect(moved.status).toBe(201);
944 const row = getDomain(app.db, claimHost)!;
945 expect(row.appId).toBe(other.id);
946 expect(row.verifiedAt).not.toBeNull();
947 expect(row.oldRecords).toEqual([]);
948 const claimed = listEvents(app.db, { repoId: otherRepo.id, limit: 20 }).find(
949 (e) => e.kind === "app.domain.claimed",
950 );
951 expect(claimed?.payload).toMatchObject({ host: claimHost, from: record.id });
952 // Claiming what is already yours says so; a name nobody holds is simply added.
953 expect(await claimAppDomain(app, alice, otherRepo, claimHost)).toEqual({ ok: false, error: "own_domain" });
954 const free = await claimAppDomain(app, alice, otherRepo, "free-claim.example.com");
955 expect(free.ok).toBe(true);
956 expect(getDomain(app.db, "free-claim.example.com")!.appId).toBe(other.id);
957 });
898958 });
899959
900960 describe("the certificate sentence", () => {
test/core-domains.test.ts +34
@@ -6,6 +6,7 @@
66 import { expect, test } from "bun:test";
77 import {
88 addDomain,
9 claimToken,
910 getVerifiedDomain,
1011 getDomain,
1112 listDomains,
@@ -14,6 +15,7 @@ import {
1415 primaryDomain,
1516 recordCheck,
1617 removeDomain,
18 takeOverDomain,
1719 type DomainRules,
1820 } from "../src/core/domains.ts";
1921 import { migrations } from "../src/core/schema.ts";
@@ -98,3 +100,35 @@ test("old records are checked at the boundary, kept with the row, and replaced w
98100 expect(getDomain(db, "shop.example.net")?.oldRecords).toEqual(parsed.value);
99101 expect(setOldRecords(db, "none.example.net", parsed.value)).toBe(false);
100102 });
103
104 test("a re-registered name moves to the app that proves it, once the holder's record is gone", () => {
105 const db = fresh();
106 add(db, "old.example.com", "a1");
107 recordCheck(db, "old.example.com", { verified: true, said: "ok" });
108 const taken = takeOverDomain(db, { host: "Old.Example.com", appId: "a2" }, RULES, { claimantPublished: true, holderPublished: false });
109 expect(taken.ok).toBe(true);
110 const row = getVerifiedDomain(db, "old.example.com");
111 expect(row?.appId).toBe("a2");
112 expect(row?.token).toBe(claimToken("old.example.com", "a2"));
113 expect(listDomains(db, "a1")).toEqual([]);
114 });
115
116 test("a live, proved name cannot be taken: the holder's record, or no claim record, keeps it", () => {
117 const db = fresh();
118 add(db, "live.example.com", "a1");
119 recordCheck(db, "live.example.com", { verified: true, said: "ok" });
120 for (const proof of [
121 { claimantPublished: true, holderPublished: true },
122 { claimantPublished: false, holderPublished: false },
123 ])
124 expect(takeOverDomain(db, { host: "live.example.com", appId: "a2" }, RULES, proof)).toEqual({ ok: false, error: "name_held" });
125 expect(getDomain(db, "live.example.com")?.appId).toBe("a1");
126 expect(takeOverDomain(db, { host: "live.example.com", appId: "a1" }, RULES, { claimantPublished: true, holderPublished: false })).toEqual({ ok: false, error: "own_domain" });
127 expect(takeOverDomain(db, { host: "free.example.com", appId: "a2" }, RULES, { claimantPublished: true, holderPublished: false })).toEqual({ ok: false, error: "not_held" });
128 });
129
130 test("claim tokens differ by app and host and are token-shaped", () => {
131 expect(claimToken("a.example.com", "a1")).toMatch(/^[0-9a-hjkmnp-tv-z]{24}$/);
132 expect(claimToken("a.example.com", "a1")).not.toBe(claimToken("a.example.com", "a2"));
133 expect(claimToken("a.example.com", "a1")).not.toBe(claimToken("b.example.com", "a1"));
134 });
test/tui-deploys.test.ts +6
@@ -262,6 +262,12 @@ test("the app's admin lists, adds, checks and removes a domain; a reader is offe
262262 "Another app already answers to that domain.",
263263 );
264264
265 // `t` takes a held name by proof: with no proof it says which TXT record to publish, and the
266 // holder keeps it.
267 const claimed = await drive(alice, ["n", "t", ..."parlour.example.com", "enter"], other.value);
268 expect(claimed).toContain("Held. Publish a TXT record at _forge-verify.parlour.example.com");
269 expect(hosts()).toEqual(["parlour.example.com"]);
270
265271 const checked = await drive(alice, ["n", "c"]);
266272 expect(checked).toContain("No TXT record at _forge-verify.parlour.example.com yet.");
267273 expect(checked).toContain("Still needed: a TXT record at _forge-verify.parlour.example.com");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs