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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14888-domain-claims-proved-names-should-lapse → main 4 commits fork
10 files
changed +337
−35
docs/features/app-own-domain.md +24
| @@ -83,3 +83,27 @@ All in `test/app-domains.test.ts`: | ||
| 83 | 83 | with the domain"; |
| 84 | 84 | - removal makes the host unknown again and the ask 404: "removal makes the |
| 85 | 85 | 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 @@ | ||
| 7 | 7 | |
| 8 | 8 | import type { App } from "../app.ts"; |
| 9 | 9 | 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"; | |
| 11 | 11 | import { appHostSuffix } from "../apps/host.ts"; |
| 12 | 12 | import { appsRuntime, type AppsRuntime } from "../apps/runtime.ts"; |
| 13 | 13 | import { getAppByRepo, type AppRecord } from "../core/apps.ts"; |
| 14 | 14 | import { |
| 15 | 15 | addDomain, |
| 16 | claimToken, | |
| 16 | 17 | getDomain, |
| 18 | normalizeHost, | |
| 19 | takeOverDomain, | |
| 20 | type TakeOverError, | |
| 17 | 21 | listDomains, |
| 18 | 22 | parseRecordsInput, |
| 19 | 23 | setOldRecords, |
| @@ -64,6 +68,19 @@ function serving(app: App): Result<AppsRuntime, "no_runtime"> { | ||
| 64 | 68 | return runtime === undefined ? err("no_runtime") : ok(runtime); |
| 65 | 69 | } |
| 66 | 70 | |
| 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 | ||
| 67 | 84 | /** The forge's own hostname — the one name no app may take. */ |
| 68 | 85 | function forgeHost(app: App): string { |
| 69 | 86 | return app.config.url !== undefined |
| @@ -107,15 +124,18 @@ export function listAppDomains( | ||
| 107 | 124 | actor: User | undefined, |
| 108 | 125 | repo: Repo, |
| 109 | 126 | ): Result<AppDomainView[], DomainError> { |
| 110 | const gate = admitted(app, actor, repo); | |
| 127 | const gate = admittedServing(app, actor, repo); | |
| 111 | 128 | 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 | }; | |
| 119 | 139 | } |
| 120 | 140 | |
| 121 | 141 | /** Add a domain; the answer carries the two records to publish. */ |
| @@ -126,22 +146,15 @@ export function addAppDomain( | ||
| 126 | 146 | host: string, |
| 127 | 147 | oldRecords?: unknown, |
| 128 | 148 | ): Result<AppDomainView, DomainError | AddDomainError | "invalid_records"> { |
| 129 | const gate = admitted(app, actor, repo); | |
| 149 | const gate = admittedServing(app, actor, repo); | |
| 130 | 150 | 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; | |
| 134 | 152 | const kept = oldRecords === undefined ? ok<OldRecord[]>([]) : parseRecordsInput(oldRecords); |
| 135 | 153 | if (!kept.ok) return kept; |
| 136 | 154 | const added = addDomain( |
| 137 | 155 | app.db, |
| 138 | 156 | { 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), | |
| 145 | 158 | ); |
| 146 | 159 | if (!added.ok) return added; |
| 147 | 160 | if (kept.value.length > 0) setOldRecords(app.db, added.value.host, kept.value); |
| @@ -151,7 +164,56 @@ export function addAppDomain( | ||
| 151 | 164 | actorId: admin.id, |
| 152 | 165 | payload: { host: added.value.host, slug: record.slug }, |
| 153 | 166 | }); |
| 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)) }; | |
| 155 | 217 | } |
| 156 | 218 | |
| 157 | 219 | /** |
| @@ -165,11 +227,9 @@ export function keepOldRecords( | ||
| 165 | 227 | host: string, |
| 166 | 228 | records: unknown, |
| 167 | 229 | ): Result<AppDomainView, DomainError | "no_such_domain" | "invalid_records"> { |
| 168 | const gate = admitted(app, actor, repo); | |
| 230 | const gate = admittedServing(app, actor, repo); | |
| 169 | 231 | 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; | |
| 173 | 233 | const domain = ownedDomain(app, record, host); |
| 174 | 234 | if (domain === undefined) return err("no_such_domain"); |
| 175 | 235 | const parsed = parseRecordsInput(records); |
| @@ -181,7 +241,7 @@ export function keepOldRecords( | ||
| 181 | 241 | actorId: admin.id, |
| 182 | 242 | payload: { host: domain.host, slug: record.slug, count: parsed.value.length }, |
| 183 | 243 | }); |
| 184 | return ok(view(runtime.value, record, { ...domain, oldRecords: parsed.value })); | |
| 244 | return ok(view(runtime, record, { ...domain, oldRecords: parsed.value })); | |
| 185 | 245 | } |
| 186 | 246 | |
| 187 | 247 | /** |
| @@ -194,24 +254,22 @@ export async function checkAppDomain( | ||
| 194 | 254 | repo: Repo, |
| 195 | 255 | host: string, |
| 196 | 256 | ): Promise<Result<AppDomainView, DomainError | "no_such_domain">> { |
| 197 | const gate = admitted(app, actor, repo); | |
| 257 | const gate = admittedServing(app, actor, repo); | |
| 198 | 258 | 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; | |
| 202 | 260 | const domain = ownedDomain(app, record, host); |
| 203 | 261 | if (domain === undefined) return err("no_such_domain"); |
| 204 | 262 | const found = await checkDomain( |
| 205 | 263 | domain.host, |
| 206 | 264 | 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, | |
| 209 | 267 | ); |
| 210 | 268 | // A verified name is served, so its certificate is worth reading: a failed |
| 211 | 269 | // renewal shows here instead of on the day it expires. |
| 212 | 270 | const said = found.verified |
| 213 | 271 | ? `${found.said} ${certificateSaid( |
| 214 | await (runtime.value.options.certificate ?? ((h) => probeCertificate(h)))( | |
| 272 | await (runtime.options.certificate ?? ((h) => probeCertificate(h)))( | |
| 215 | 273 | domain.host, |
| 216 | 274 | ), |
| 217 | 275 | new Date(), |
| @@ -228,7 +286,7 @@ export async function checkAppDomain( | ||
| 228 | 286 | payload: { host: domain.host, slug: record.slug }, |
| 229 | 287 | }); |
| 230 | 288 | } |
| 231 | return ok(view(runtime.value, record, updated)); | |
| 289 | return ok(view(runtime, record, updated)); | |
| 232 | 290 | } |
| 233 | 291 | |
| 234 | 292 | /** Take a domain away. The forge stops answering there at once. */ |
src/apps/domains.ts +19
| @@ -195,6 +195,25 @@ export async function checkDomain( | ||
| 195 | 195 | }; |
| 196 | 196 | } |
| 197 | 197 | |
| 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 | ||
| 198 | 217 | /** Where the name sends traffic, as one clause of the verified sentence. */ |
| 199 | 218 | async function pointing( |
| 200 | 219 | host: string, |
src/core/domains.ts +53
| @@ -186,6 +186,59 @@ function newToken(): string { | ||
| 186 | 186 | return out; |
| 187 | 187 | } |
| 188 | 188 | |
| 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 | ||
| 189 | 242 | export function addDomain( |
| 190 | 243 | db: Database, |
| 191 | 244 | input: { host: string; appId: string; createdBy?: string | undefined }, |
src/design/activity.ts +1
| @@ -99,6 +99,7 @@ export function eventVerb(event: ForgeEvent): string { | ||
| 99 | 99 | if (kind.startsWith("app.domain.")) { |
| 100 | 100 | const host = payload["host"]; |
| 101 | 101 | const which = typeof host === "string" ? host : "a domain"; |
| 102 | if (kind === "app.domain.claimed") return `claimed the domain ${which} by proof`; | |
| 102 | 103 | if (kind === "app.domain.added") return `gave the app the domain ${which}`; |
| 103 | 104 | if (kind === "app.domain.verified") return `verified the domain ${which}`; |
| 104 | 105 | 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 @@ | ||
| 9 | 9 | import { readAppMembers, setAppMembersThrough, type AppMembersError } from "../../actions/app_members.ts"; |
| 10 | 10 | import { appView, appVisitors, deleteAppEnvVar, disableApp, enableApp, listAppEnv, openEnvUrl, redeployEnv, setAppEnvVar, stopEnv, updateAppSettings, type AppSettingsInput } from "../../actions/apps.ts"; |
| 11 | 11 | 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"; | |
| 13 | 13 | import { previewBuild, previewBuildRefusalText } from "../../actions/build_preview.ts"; |
| 14 | 14 | import { SETTING_WORDS } from "../../actions/setting_words.ts"; |
| 15 | 15 | import { appsRuntime } from "../../apps/runtime.ts"; |
| @@ -21,6 +21,8 @@ import { holds, weakEtag } from "../etag.ts"; | ||
| 21 | 21 | import { BUILD_ASKS_WINDOW_MS } from "../../actions/build_preview.ts"; |
| 22 | 22 | import { apiError, body, json, notFound, requireScope, respondAccess, str, tryAgain, unauthorized } from "../respond.ts"; |
| 23 | 23 | import { DEFAULT_ENV_SCOPE } from "../../core/app_env.ts"; |
| 24 | import { getAppByRepo } from "../../core/apps.ts"; | |
| 25 | import { normalizeHost } from "../../core/domains.ts"; | |
| 24 | 26 | import type { Router } from "../router.ts"; |
| 25 | 27 | |
| 26 | 28 | function appPatch(input: Record<string, unknown>): AppSettingsInput { |
| @@ -181,6 +183,23 @@ export function appRoutes(router: Router<Handler>): void { | ||
| 181 | 183 | return json(added.value, 201); |
| 182 | 184 | }); |
| 183 | 185 | |
| 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 | ||
| 184 | 203 | router.add( |
| 185 | 204 | "POST", |
| 186 | 205 | "/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"; | ||
| 85 | 85 | import { |
| 86 | 86 | addAppDomain, |
| 87 | 87 | checkAppDomain, |
| 88 | claimAppDomain, | |
| 89 | claimProofRecord, | |
| 88 | 90 | keepOldRecords, |
| 89 | 91 | listAppDomains, |
| 90 | 92 | removeAppDomain, |
| @@ -792,6 +794,27 @@ export function domainsScreen(f: Session, ctx: Ctx, repo: Repo, changed: () => v | ||
| 792 | 794 | ); |
| 793 | 795 | }; |
| 794 | 796 | |
| 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 | ||
| 795 | 818 | const check = (d: AppDomainView): void => |
| 796 | 819 | runAsync(ctx, async () => { |
| 797 | 820 | 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 | ||
| 897 | 920 | : [ |
| 898 | 921 | binding(["j", "k"], "move"), |
| 899 | 922 | binding(["+"], "add a domain"), |
| 923 | binding(["t"], "take a name another app holds"), | |
| 900 | 924 | ...(domains.length > 0 |
| 901 | 925 | ? [ |
| 902 | 926 | binding(["c"], "check its DNS now"), |
| @@ -946,6 +970,10 @@ export function domainsScreen(f: Session, ctx: Ctx, repo: Repo, changed: () => v | ||
| 946 | 970 | add(); |
| 947 | 971 | return true; |
| 948 | 972 | } |
| 973 | if (e.key === "t") { | |
| 974 | claim(); | |
| 975 | return true; | |
| 976 | } | |
| 949 | 977 | if (e.key === "c" && selected !== undefined) { |
| 950 | 978 | check(selected); |
| 951 | 979 | return true; |
test/app-domains.test.ts +61 −1
| @@ -15,10 +15,13 @@ import { newApp, updateAppSettings } from "../src/actions/apps.ts"; | ||
| 15 | 15 | import { |
| 16 | 16 | addAppDomain, |
| 17 | 17 | checkAppDomain, |
| 18 | claimAppDomain, | |
| 19 | claimProofRecord, | |
| 18 | 20 | removeAppDomain, |
| 19 | 21 | } from "../src/actions/domains.ts"; |
| 20 | 22 | import { createRepo } from "../src/actions/repos.ts"; |
| 21 | 23 | import { |
| 24 | checkClaim, | |
| 22 | 25 | checkDomain, |
| 23 | 26 | recordsFor, |
| 24 | 27 | type DomainResolver, |
| @@ -26,7 +29,7 @@ import { | ||
| 26 | 29 | import { AppsRuntime } from "../src/apps/runtime.ts"; |
| 27 | 30 | import { certificateSaid, probeCertificate } from "../src/apps/certificate.ts"; |
| 28 | 31 | 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"; | |
| 30 | 33 | import { listEvents } from "../src/core/events.ts"; |
| 31 | 34 | import { eventVerb } from "../src/design/activity.ts"; |
| 32 | 35 | import type { Repo } from "../src/core/repos.ts"; |
| @@ -895,6 +898,63 @@ describe("an app gets its own domain", () => { | ||
| 895 | 898 | expect(getDomain(app.db, HOST)!.token).not.toBe(oldToken); |
| 896 | 899 | expect(removeAppDomain(app, alice, repo, HOST).ok).toBe(false); |
| 897 | 900 | }); |
| 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 | }); | |
| 898 | 958 | }); |
| 899 | 959 | |
| 900 | 960 | describe("the certificate sentence", () => { |
test/core-domains.test.ts +34
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | import { expect, test } from "bun:test"; |
| 7 | 7 | import { |
| 8 | 8 | addDomain, |
| 9 | claimToken, | |
| 9 | 10 | getVerifiedDomain, |
| 10 | 11 | getDomain, |
| 11 | 12 | listDomains, |
| @@ -14,6 +15,7 @@ import { | ||
| 14 | 15 | primaryDomain, |
| 15 | 16 | recordCheck, |
| 16 | 17 | removeDomain, |
| 18 | takeOverDomain, | |
| 17 | 19 | type DomainRules, |
| 18 | 20 | } from "../src/core/domains.ts"; |
| 19 | 21 | 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 | ||
| 98 | 100 | expect(getDomain(db, "shop.example.net")?.oldRecords).toEqual(parsed.value); |
| 99 | 101 | expect(setOldRecords(db, "none.example.net", parsed.value)).toBe(false); |
| 100 | 102 | }); |
| 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 | ||
| 262 | 262 | "Another app already answers to that domain.", |
| 263 | 263 | ); |
| 264 | 264 | |
| 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 | ||
| 265 | 271 | const checked = await drive(alice, ["n", "c"]); |
| 266 | 272 | expect(checked).toContain("No TXT record at _forge-verify.parlour.example.com yet."); |
| 267 | 273 | expect(checked).toContain("Still needed: a TXT record at _forge-verify.parlour.example.com"); |
new above
offline