Public: anyone can read this; the crew can change it.
Mail key resolution: app > domain > forge > platform, scope visible, key never #17340
Merged 30h 15m ago, live in production
torque/forge:torque/17334-mail-key-resolution-app-domain-forge-pla → main 0 commits fork 3 threads · 3 unresolved
8 files
changed +423
−37
docs/OPERATING.md +6 −1
| @@ -75,7 +75,12 @@ Mail out | ||
| 75 | 75 | (the link that proves a newcomer's email) is SMTP submission over TLS, |
| 76 | 76 | configured by the forge-level secrets `MAIL_SMTP_URL` |
| 77 | 77 | (`smtps://user:pass@host:465`) and `MAIL_FROM`, set live under Settings → |
| 78 | Secrets, or at boot by `FORGE_SMTP_URL` / `FORGE_MAIL_FROM`. With neither, no | |
| 78 | Secrets, or at boot by `FORGE_SMTP_URL` / `FORGE_MAIL_FROM`. The pair is | |
| 79 | resolved narrowest first: an app's own (a repository's secrets), its | |
| 80 | organization's, the forge's, then the boot configuration (the platform's); | |
| 81 | removing one falls back to the next wider (`src/core/mail_key.ts`, #17334). | |
| 82 | The Secrets card at each place and the terminal's Secrets screen say which scope's key is | |
| 83 | in effect for it; a mail that passes no app or domain uses the forge's or platform's; no page, answer or log ever carries the key. With none, no | |
| 79 | 84 | mail is sent and a newcomer waits for the owner to verify them. With |
| 80 | 85 | `FORGE_TURNSTILE_SECRET` and `FORGE_TURNSTILE_SITEKEY` set (the forge refuses to |
| 81 | 86 | boot with only the secret), `/signup` renders the Turnstile widget, its CSP |
docs/features/app-secrets.md +11
| @@ -37,3 +37,14 @@ again with the form under the list. | ||
| 37 | 37 | From a repository that is an app, the palette offers **Variables** (`src/tui/screens/appenv.ts`: `a` sets one, choosing its scope and whether it is a secret or a variable, and setting an existing name again replaces its value; the list shows each kind and a variable's value, never a secret's) and **Secrets this app uses** (`src/tui/screens/appsecrets.ts`: needs, has, missing; `a` grants, `x` revokes, a forge admin only). |
| 38 | 38 | |
| 39 | 39 | Code: `src/core/app_secrets.ts`, `src/actions/secrets.ts` (`grantToRepo`, `revokeFromRepo`), `src/web/pages/app_secrets.ts`. Tests: `test/app-secrets.test.ts`, `test/app-secrets-value.test.ts`, `test/app-secrets-web.test.ts`, `test/guest-pull.test.ts`. |
| 40 | ||
| 41 | ## Which mail key is in effect | |
| 42 | ||
| 43 | The forge's mail key (`MAIL_SMTP_URL` + `MAIL_FROM`, both at one place) is | |
| 44 | taken from the narrowest place that holds both: the app, its organization, | |
| 45 | the forge, then the host's boot configuration. Removing one falls back to the | |
| 46 | next. The Secrets card (at the forge, an organization and a repository) and the | |
| 47 | terminal's Secrets screen (`src/tui/screens/secrets.ts`, on its own row) say which | |
| 48 | place's key sends mail for the place in view, never the key (#17334). `mailerOf` | |
| 49 | takes `{app, domain}`; today's mails (sign-up, orders, recovery) are the forge's | |
| 50 | own, so they pass none and use the forge's or the platform's key. |
src/actions/secrets.ts +29
| @@ -16,6 +16,7 @@ import { getAppById, getAppByRepo } from "../core/apps.ts"; | ||
| 16 | 16 | import { readerFor } from "../git/read.ts"; |
| 17 | 17 | import { repoDir } from "../git/repos.ts"; |
| 18 | 18 | import type { Repo } from "../core/repos.ts"; |
| 19 | import { resolveMailKey } from "../core/mail_key.ts"; | |
| 19 | 20 | import { requireRepoAdmin } from "./access.ts"; |
| 20 | 21 | import { neededNames, needRows, type NeedRow, appsUsing, grantable, grantSecret, revokeGrant, usesOf, type SecretUse } from "../core/app_secrets.ts"; |
| 21 | 22 | import { |
| @@ -61,6 +62,34 @@ function admin(actor: User | undefined): Result<User, "not_found"> { | ||
| 61 | 62 | |
| 62 | 63 | const log = createLog("secrets"); |
| 63 | 64 | |
| 65 | /** | |
| 66 | * Which scope's mail key sends mail for a place, in words — never the key | |
| 67 | * (`core/mail_key.ts`). A repository is the app and its owning organization | |
| 68 | * the domain; an organization is a domain; the forge is neither. Admins only; | |
| 69 | * the page and the terminal say the same. | |
| 70 | */ | |
| 71 | export function mailKeyLine( | |
| 72 | app: App, | |
| 73 | actor: User | undefined, | |
| 74 | scope: string = "forge", | |
| 75 | ref = "", | |
| 76 | ): string | undefined { | |
| 77 | if (!admin(actor).ok) return undefined; | |
| 78 | const target = resolveTarget(app, scope, ref); | |
| 79 | if (!target.ok) return undefined; | |
| 80 | const asker: { app?: string; domain?: string } = {}; | |
| 81 | if (target.value.scope === "repo") { | |
| 82 | asker.app = target.value.scopeId; | |
| 83 | const [owner = ""] = ref.split("/"); | |
| 84 | const org = getOrg(app.db, owner); | |
| 85 | if (org) asker.domain = org.id; | |
| 86 | } else if (target.value.scope === "org") asker.domain = target.value.scopeId; | |
| 87 | const won = resolveMailKey(app.db, asker, app.config.mail); | |
| 88 | return won === undefined | |
| 89 | ? "No mail key is set: no mail goes out." | |
| 90 | : `Mail goes out with the ${won}'s key (the key itself is never shown).`; | |
| 91 | } | |
| 92 | ||
| 64 | 93 | export function listSecrets( |
| 65 | 94 | app: App, |
| 66 | 95 | actor: User | undefined, |
src/actions/verify.ts +27 −31
| @@ -18,9 +18,9 @@ import { | ||
| 18 | 18 | resetVerification, |
| 19 | 19 | } from "../core/verify.ts"; |
| 20 | 20 | import { handoverOwner, spendHandover } from "../core/edge_handover.ts"; |
| 21 | import { listSecretNames, resolveSecrets } from "../core/secret_store.ts"; | |
| 22 | 21 | import { getUserById, isActive, type User } from "../core/users.ts"; |
| 23 | import { sendMail, type Mailer } from "../kernel/mail.ts"; | |
| 22 | import type { Mailer } from "../kernel/mail.ts"; | |
| 23 | import { resolveMailKey, sendWithMailKey } from "../core/mail_key.ts"; | |
| 24 | 24 | import { mayMail, sendCapped } from "./mail.ts"; |
| 25 | 25 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 26 | 26 | import { requireUser } from "./access.ts"; |
| @@ -55,41 +55,37 @@ export function requireContributor( | ||
| 55 | 55 | return mayContribute(app, user) ? ok(user) : err("unverified"); |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | /** The forge-level secrets that configure mail, set live at /settings/secrets. */ | |
| 59 | export const MAIL_SECRETS = ["MAIL_SMTP_URL", "MAIL_FROM"] as const; | |
| 60 | ||
| 61 | 58 | /** |
| 62 | * Where mail goes: the forge-level secrets first (the owner sets them at | |
| 63 | * /settings/secrets, no restart), else FORGE_SMTP_URL / FORGE_MAIL_FROM read | |
| 64 | * at boot. Both of a pair, or neither. Read per message, so a changed secret | |
| 65 | * takes effect on the next mail; each read is audited by the store. | |
| 66 | */ | |
| 67 | /** | |
| 68 | * Whether the forge can send mail at all — both secrets set, or the boot | |
| 69 | * configuration. Asked by name only: nothing is opened to answer it. | |
| 59 | * Whether the forge can send mail at all: a whole key at the forge scope, or | |
| 60 | * the boot configuration (`core/mail_key.ts`). Asked by name only: nothing is | |
| 61 | * opened to answer it. | |
| 70 | 62 | */ |
| 71 | 63 | export function mailConfigured(app: App): boolean { |
| 72 | if (app.config.mailer !== undefined || app.config.mail !== undefined) | |
| 73 | return true; | |
| 74 | const set = new Set(listSecretNames(app.db, "forge").map((s) => s.name)); | |
| 75 | return MAIL_SECRETS.every((name) => set.has(name)); | |
| 64 | return ( | |
| 65 | app.config.mailer !== undefined || | |
| 66 | resolveMailKey(app.db, {}, app.config.mail) !== undefined | |
| 67 | ); | |
| 76 | 68 | } |
| 77 | 69 | |
| 78 | export function mailerOf(app: App, forUser: { readonly id: string }): Mailer | undefined { | |
| 70 | /** | |
| 71 | * Where mail goes: the narrowest scope with a whole key — app, domain, the | |
| 72 | * forge's secrets (set at /settings/secrets, no restart), else the boot | |
| 73 | * configuration. Read per message, so a changed secret takes effect on the | |
| 74 | * next mail; each open is audited by the store. | |
| 75 | */ | |
| 76 | export function mailerOf( | |
| 77 | app: App, | |
| 78 | forUser: { readonly id: string }, | |
| 79 | whose: { readonly app?: string; readonly domain?: string } = {}, | |
| 80 | ): Mailer | undefined { | |
| 79 | 81 | if (app.config.mailer !== undefined) return app.config.mailer; |
| 80 | const stored = resolveSecrets( | |
| 81 | app.db, | |
| 82 | app.config.dataDir, | |
| 83 | app.bus, | |
| 84 | MAIL_SECRETS, | |
| 85 | // The store's audit names who a read was for: the person mailed. | |
| 86 | { actor: forUser.id }, | |
| 87 | ); | |
| 88 | const url = stored["MAIL_SMTP_URL"]; | |
| 89 | const from = stored["MAIL_FROM"]; | |
| 90 | const config = | |
| 91 | url !== undefined && from !== undefined ? { url, from } : app.config.mail; | |
| 92 | return config === undefined ? undefined : (mail) => sendMail(config, mail); | |
| 82 | // The store's audit names who a read was for: the person mailed. | |
| 83 | const who = { ...whose, actor: forUser.id }; | |
| 84 | if (resolveMailKey(app.db, who, app.config.mail) === undefined) return undefined; | |
| 85 | return async (mail) => | |
| 86 | ( | |
| 87 | await sendWithMailKey(app.db, app.config.dataDir, app.bus, who, mail, app.config.mail) | |
| 88 | )?.sent === true; | |
| 93 | 89 | } |
| 94 | 90 | |
| 95 | 91 | function verifyUrl(app: App, token: string): string { |
src/core/mail_key.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * Whose mail key is in effect (#17334, part 1 of #15872). A key is the pair | |
| 3 | * of secrets `MAIL_SMTP_URL` + `MAIL_FROM`, sealed at one scope. Narrowest | |
| 4 | * scope that holds BOTH wins: the app (a repository's secret), then its | |
| 5 | * domain (its organization's), then the forge's, then the platform's — the | |
| 6 | * key the host booted this forge with (`FORGE_SMTP_URL`). Removing a scope's | |
| 7 | * key falls back to the next one wider. | |
| 8 | * | |
| 9 | * `resolveMailKey` answers with the scope only and opens nothing, so a page, | |
| 10 | * an API answer or a log line can say "the forge's key is in effect" and has | |
| 11 | * no value to leak. `sendWithMailKey` is the one function that opens a key, | |
| 12 | * and it hands it straight to the SMTP sender; the value is never returned. | |
| 13 | */ | |
| 14 | ||
| 15 | import type { Database } from "bun:sqlite"; | |
| 16 | import type { EventBus } from "./events.ts"; | |
| 17 | import { sendMail, type Mail, type MailConfig } from "../kernel/mail.ts"; | |
| 18 | import { listSecretNames, resolveSecrets, type SecretScope } from "./secret_store.ts"; | |
| 19 | ||
| 20 | export type MailScope = "app" | "domain" | "forge" | "platform"; | |
| 21 | ||
| 22 | /** Which mail key is wanted for: an app (repository id) and its domain (organization id). */ | |
| 23 | export interface MailAsker { | |
| 24 | readonly app?: string | undefined; | |
| 25 | readonly domain?: string | undefined; | |
| 26 | /** Who asks, as the store's audit will say. */ | |
| 27 | readonly actor?: string | undefined; | |
| 28 | } | |
| 29 | ||
| 30 | export const MAIL_KEY_NAMES = ["MAIL_SMTP_URL", "MAIL_FROM"] as const; | |
| 31 | ||
| 32 | /** The secret-store scope each mail scope is kept at (the platform's is not stored). */ | |
| 33 | const STORED: Record<Exclude<MailScope, "platform">, SecretScope> = { | |
| 34 | app: "repo", | |
| 35 | domain: "org", | |
| 36 | forge: "forge", | |
| 37 | }; | |
| 38 | ||
| 39 | const heldAt = (db: Database, scope: SecretScope, id: string): boolean => { | |
| 40 | const set = new Set(listSecretNames(db, scope, id).map((s) => s.name)); | |
| 41 | return MAIL_KEY_NAMES.every((n) => set.has(n)); | |
| 42 | }; | |
| 43 | ||
| 44 | /** | |
| 45 | * The scope whose key is in effect, by name only — nothing is opened. Absent | |
| 46 | * when no scope has a whole key. `platform` is the boot configuration, if any. | |
| 47 | */ | |
| 48 | export function resolveMailKey( | |
| 49 | db: Database, | |
| 50 | who: MailAsker, | |
| 51 | platform?: MailConfig | undefined, | |
| 52 | ): MailScope | undefined { | |
| 53 | if (who.app && heldAt(db, STORED.app, who.app)) return "app"; | |
| 54 | if (who.domain && heldAt(db, STORED.domain, who.domain)) return "domain"; | |
| 55 | if (heldAt(db, STORED.forge, "")) return "forge"; | |
| 56 | return platform === undefined ? undefined : "platform"; | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Send one message with the key in effect, and say which scope's it was | |
| 61 | * (`undefined`, nothing sent, when no scope has a key). The only place a | |
| 62 | * mail key is opened; the open is audited by the store (`secret.read`). | |
| 63 | */ | |
| 64 | export async function sendWithMailKey( | |
| 65 | db: Database, | |
| 66 | dataDir: string, | |
| 67 | bus: EventBus, | |
| 68 | who: MailAsker, | |
| 69 | mail: Mail, | |
| 70 | platform?: MailConfig | undefined, | |
| 71 | ): Promise<{ scope: MailScope; sent: boolean } | undefined> { | |
| 72 | const scope = resolveMailKey(db, who, platform); | |
| 73 | if (scope === undefined) return undefined; | |
| 74 | if (scope === "platform") { | |
| 75 | return platform === undefined ? undefined : { scope, sent: await sendMail(platform, mail) }; | |
| 76 | } | |
| 77 | const opened = resolveSecrets(db, dataDir, bus, MAIL_KEY_NAMES, { | |
| 78 | actor: who.actor, | |
| 79 | repo: who.app, | |
| 80 | org: who.domain, | |
| 81 | // Only the winning scope: a wider one is never mixed into the pair. | |
| 82 | only: [STORED[scope]], | |
| 83 | }); | |
| 84 | const url = opened["MAIL_SMTP_URL"]; | |
| 85 | const from = opened["MAIL_FROM"]; | |
| 86 | if (url === undefined || from === undefined) return { scope, sent: false }; | |
| 87 | return { scope, sent: await sendMail({ url, from }, mail) }; | |
| 88 | } |
src/tui/screens/secrets.ts +5 −2
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | * the screen directly, sees and changes nothing. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { listSecrets, putSecret, removeSecret } from "../../actions/secrets.ts"; | |
| 11 | import { listSecrets, mailKeyLine, putSecret, removeSecret } from "../../actions/secrets.ts"; | |
| 12 | 12 | import { isSecretName, type SecretName } from "../../core/secret_store.ts"; |
| 13 | 13 | import type { Ctx, Screen } from "../app/app.ts"; |
| 14 | 14 | import { binding } from "../app/keys.ts"; |
| @@ -68,7 +68,7 @@ export function secretsScreen(f: Session, ctx: Ctx): Screen { | ||
| 68 | 68 | ], |
| 69 | 69 | render(area, s) { |
| 70 | 70 | const c = ctx.theme.c; |
| 71 | const [head, list] = split(area, "v", [len(1), fill(1)]); | |
| 71 | const [head, list] = split(area, "v", [len(2), fill(1)]); | |
| 72 | 72 | const rows = names(); |
| 73 | 73 | const text = !allowed |
| 74 | 74 | ? "Secrets are kept by a forge admin." |
| @@ -78,6 +78,9 @@ export function secretsScreen(f: Session, ctx: Ctx): Screen { | ||
| 78 | 78 | ? `No secret at ${showPlace(place)}. a sets one.` |
| 79 | 79 | : `Sealed secrets at ${showPlace(place)}; values are never shown.`; |
| 80 | 80 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 81 | const mail = mailKeyLine(f.app, f.user, place.scope, place.ref); | |
| 82 | if (mail !== undefined) | |
| 83 | s.text(head!.x, head!.y + 1, truncate(mail, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 81 | 84 | drawList( |
| 82 | 85 | s, |
| 83 | 86 | list!, |
src/web/pages/secrets.ts +5 −3
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { sshOn } from "../../settings/ssh.ts"; |
| 11 | 11 | import type { SecretUse } from "../../core/app_secrets.ts"; |
| 12 | import { listSecrets, putSecret, removeSecret, secretUses } from "../../actions/secrets.ts"; | |
| 12 | import { listSecrets, mailKeyLine, putSecret, removeSecret, secretUses } from "../../actions/secrets.ts"; | |
| 13 | 13 | import type { Ctx } from "../../http/context.ts"; |
| 14 | 14 | import { |
| 15 | 15 | notePhase, |
| @@ -68,6 +68,7 @@ export function secretsCard( | ||
| 68 | 68 | ref: string, |
| 69 | 69 | names: SecretName[], |
| 70 | 70 | uses: ReadonlyMap<string, readonly SecretUse[]> = new Map(), |
| 71 | mailLine?: string, | |
| 71 | 72 | ): Html { |
| 72 | 73 | const rows = names.map((s) => |
| 73 | 74 | row({ |
| @@ -96,7 +97,7 @@ export function secretsCard( | ||
| 96 | 97 | ); |
| 97 | 98 | return html`<section class="settings-block"> |
| 98 | 99 | <h2>Secrets</h2> |
| 99 | <p class="muted small">Sealed values the forge hands to a run by name; the narrowest place wins.</p> | |
| 100 | <p class="muted small">Sealed values the forge hands to a run by name; the narrowest place wins.${mailLine === undefined ? "" : html` ${mailLine}`}</p> | |
| 100 | 101 | ${when( |
| 101 | 102 | rows.length > 0, |
| 102 | 103 | () => |
| @@ -144,7 +145,8 @@ export function secretsSection( | ||
| 144 | 145 | const uses = new Map( |
| 145 | 146 | names.value.map((n) => [n.name, secretUses(ctx.app, n.scope, n.scopeId, n.name)] as const), |
| 146 | 147 | ); |
| 147 | return secretsCard(scope, ref, names.value, uses); | |
| 148 | const line = mailKeyLine(ctx.app, ctx.auth.user, scope, ref); | |
| 149 | return secretsCard(scope, ref, names.value, uses, line); | |
| 148 | 150 | } |
| 149 | 151 | |
| 150 | 152 | function requireAdmin(ctx: Ctx): Response | undefined { |
test/mail-key.test.ts added +252
| @@ -0,0 +1,252 @@ | ||
| 1 | /** | |
| 2 | * The mail key in effect (#17334): the narrowest scope with a whole key wins | |
| 3 | * (app > domain > forge > platform), removing it falls back, and only the | |
| 4 | * send opens a value; the scope is all anyone else is told. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { resolveMailKey, sendWithMailKey } from "../src/core/mail_key.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createOrg } from "../src/core/orgs.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { mailKeyLine } from "../src/actions/secrets.ts"; | |
| 17 | import { deleteSecret, setSecret } from "../src/core/secret_store.ts"; | |
| 18 | import { mailerOf, mailConfigured } from "../src/actions/verify.ts"; | |
| 19 | import { createSession } from "../src/core/sessions.ts"; | |
| 20 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 21 | import { secretsScreen } from "../src/tui/screens/secrets.ts"; | |
| 22 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 23 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 24 | import { settleApp } from "./helpers/tui.ts"; | |
| 25 | import { quietLogs } from "./helpers/globals.ts"; | |
| 26 | ||
| 27 | quietLogs(); | |
| 28 | ||
| 29 | /** A stand-in SMTP server on loopback that records who authenticated. */ | |
| 30 | function smtpStub(): { port: number; users: string[]; stop: () => void } { | |
| 31 | const users: string[] = []; | |
| 32 | const server = Bun.listen<{ buf: string; data: boolean }>({ | |
| 33 | hostname: "127.0.0.1", | |
| 34 | port: 0, | |
| 35 | socket: { | |
| 36 | open(s) { | |
| 37 | s.data = { buf: "", data: false }; | |
| 38 | s.write("220 stub\r\n"); | |
| 39 | }, | |
| 40 | data(s, chunk) { | |
| 41 | s.data.buf += new TextDecoder().decode(chunk); | |
| 42 | for (;;) { | |
| 43 | if (s.data.data) { | |
| 44 | const end = s.data.buf.indexOf("\r\n.\r\n"); | |
| 45 | if (end === -1) return; | |
| 46 | s.data.buf = s.data.buf.slice(end + 5); | |
| 47 | s.data.data = false; | |
| 48 | s.write("250 ok\r\n"); | |
| 49 | continue; | |
| 50 | } | |
| 51 | const nl = s.data.buf.indexOf("\r\n"); | |
| 52 | if (nl === -1) return; | |
| 53 | const line = s.data.buf.slice(0, nl); | |
| 54 | s.data.buf = s.data.buf.slice(nl + 2); | |
| 55 | if (line.startsWith("EHLO")) s.write("250 hi\r\n"); | |
| 56 | else if (line.startsWith("AUTH PLAIN")) { | |
| 57 | users.push( | |
| 58 | Buffer.from(line.slice(11), "base64").toString().split("\0")[1]!, | |
| 59 | ); | |
| 60 | s.write("235 ok\r\n"); | |
| 61 | } else if (line === "DATA") { | |
| 62 | s.data.data = true; | |
| 63 | s.write("354 go\r\n"); | |
| 64 | } else if (line === "QUIT") s.write("221 bye\r\n"); | |
| 65 | else s.write("250 ok\r\n"); | |
| 66 | } | |
| 67 | }, | |
| 68 | }, | |
| 69 | }); | |
| 70 | return { port: server.port, users, stop: () => server.stop(true) }; | |
| 71 | } | |
| 72 | ||
| 73 | describe("mail key resolution", () => { | |
| 74 | let dataDir = ""; | |
| 75 | let app: App; | |
| 76 | let stub: ReturnType<typeof smtpStub>; | |
| 77 | const url = (user: string): string => `smtp://${user}:[email protected]:${stub.port}`; | |
| 78 | const key = (scope: string, id: string, user: string): void => { | |
| 79 | const pair = { MAIL_SMTP_URL: url(user), MAIL_FROM: `${user}@forge.test` }; | |
| 80 | for (const [name, value] of Object.entries(pair)) { | |
| 81 | const r = setSecret(app.db, dataDir, scope, id, name, value); | |
| 82 | if (!r.ok) throw new Error(r.error); | |
| 83 | } | |
| 84 | }; | |
| 85 | const unkey = (scope: string, id: string): void => { | |
| 86 | deleteSecret(app.db, scope, id, "MAIL_SMTP_URL"); | |
| 87 | deleteSecret(app.db, scope, id, "MAIL_FROM"); | |
| 88 | }; | |
| 89 | let admin: User | undefined; | |
| 90 | let who = { app: "repo1", domain: "org1" }; | |
| 91 | const mail = { to: "[email protected]", subject: "hi", text: "hello" }; | |
| 92 | const platform = { url: "", from: "[email protected]" }; | |
| 93 | ||
| 94 | beforeAll(() => { | |
| 95 | stub = smtpStub(); | |
| 96 | platform.url = url("platform"); | |
| 97 | }); | |
| 98 | beforeAll(async () => { | |
| 99 | dataDir = await mkdtemp(join(tmpdir(), "forge-mail-key-")); | |
| 100 | app = createApp({ dataDir, port: 0 }); | |
| 101 | const u = await createUser(app.db, { | |
| 102 | origin: "seed", | |
| 103 | trusted: true, | |
| 104 | username: "alice", | |
| 105 | email: "[email protected]", | |
| 106 | password: "a-good-passphrase", | |
| 107 | adminIfFirst: true, | |
| 108 | }); | |
| 109 | if (!u.ok) throw new Error(u.error); | |
| 110 | admin = u.value; | |
| 111 | const org = createOrg(app.db, "acme", u.value.id); | |
| 112 | if (!org.ok) throw new Error(org.error); | |
| 113 | const repo = await createRepo(app, u.value, { name: "site", visibility: "private" }); | |
| 114 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 115 | who = { app: repo.value.id, domain: org.value.id }; | |
| 116 | }); | |
| 117 | afterAll(async () => { | |
| 118 | stub.stop(); | |
| 119 | await rm(dataDir, { recursive: true, force: true }); | |
| 120 | }); | |
| 121 | ||
| 122 | test("no key anywhere: nothing resolves, nothing is sent", async () => { | |
| 123 | expect(resolveMailKey(app.db, who)).toBeUndefined(); | |
| 124 | expect(await sendWithMailKey(app.db, dataDir, app.bus, who, mail)).toBeUndefined(); | |
| 125 | }); | |
| 126 | ||
| 127 | test("the platform's key is inherited when nothing narrower is set", async () => { | |
| 128 | expect(resolveMailKey(app.db, who, platform)).toBe("platform"); | |
| 129 | const r = await sendWithMailKey(app.db, dataDir, app.bus, who, mail, platform); | |
| 130 | expect(r).toEqual({ scope: "platform", sent: true }); | |
| 131 | expect(stub.users.at(-1)).toBe("platform"); | |
| 132 | }); | |
| 133 | ||
| 134 | test("each narrower scope overrides the wider, and removing it falls back", async () => { | |
| 135 | key("forge", "", "forge"); | |
| 136 | expect(resolveMailKey(app.db, who, platform)).toBe("forge"); | |
| 137 | key("org", who.domain, "domain"); | |
| 138 | expect(resolveMailKey(app.db, who, platform)).toBe("domain"); | |
| 139 | key("repo", who.app, "app"); | |
| 140 | expect(resolveMailKey(app.db, who, platform)).toBe("app"); | |
| 141 | expect(await sendWithMailKey(app.db, dataDir, app.bus, who, mail, platform)).toEqual({ | |
| 142 | scope: "app", | |
| 143 | sent: true, | |
| 144 | }); | |
| 145 | expect(stub.users.at(-1)).toBe("app"); | |
| 146 | ||
| 147 | // Another app of the same domain does not see the first app's key. | |
| 148 | expect(resolveMailKey(app.db, { app: "repo2", domain: who.domain }, platform)).toBe("domain"); | |
| 149 | // Another domain falls to the forge's. | |
| 150 | expect(resolveMailKey(app.db, { app: "repo3", domain: "org2" }, platform)).toBe("forge"); | |
| 151 | ||
| 152 | unkey("repo", who.app); | |
| 153 | expect(resolveMailKey(app.db, who, platform)).toBe("domain"); | |
| 154 | await sendWithMailKey(app.db, dataDir, app.bus, who, mail, platform); | |
| 155 | expect(stub.users.at(-1)).toBe("domain"); | |
| 156 | unkey("org", who.domain); | |
| 157 | expect(resolveMailKey(app.db, who, platform)).toBe("forge"); | |
| 158 | unkey("forge", ""); | |
| 159 | expect(resolveMailKey(app.db, who, platform)).toBe("platform"); | |
| 160 | }); | |
| 161 | ||
| 162 | test("half a key is no key: the pair comes from one scope", async () => { | |
| 163 | setSecret(app.db, dataDir, "repo", who.app, "MAIL_FROM", "[email protected]"); | |
| 164 | expect(resolveMailKey(app.db, who, platform)).toBe("platform"); | |
| 165 | unkey("repo", who.app); | |
| 166 | }); | |
| 167 | ||
| 168 | test("the answer carries a scope, never a value; the open is audited without it", async () => { | |
| 169 | key("forge", "", "forge"); | |
| 170 | const r = await sendWithMailKey(app.db, dataDir, app.bus, who, mail); | |
| 171 | expect(JSON.stringify(r)).not.toContain("pw@"); | |
| 172 | const events = app.db | |
| 173 | .query<{ payload: string }, []>("SELECT payload FROM event WHERE kind = 'secret.read'") | |
| 174 | .all(); | |
| 175 | expect(events.length).toBeGreaterThan(0); | |
| 176 | expect(events.map((e) => e.payload).join("")).not.toContain("pw@"); | |
| 177 | unkey("forge", ""); | |
| 178 | }); | |
| 179 | ||
| 180 | test("admins are told the scope in words, others nothing", () => { | |
| 181 | key("forge", "", "forge"); | |
| 182 | expect(mailKeyLine(app, admin)).toContain("forge's key"); | |
| 183 | expect(mailKeyLine(app, admin)).not.toContain("pw@"); | |
| 184 | expect(mailKeyLine(app, undefined)).toBeUndefined(); | |
| 185 | unkey("forge", ""); | |
| 186 | expect(mailKeyLine(app, admin)).toContain("No mail key"); | |
| 187 | }); | |
| 188 | ||
| 189 | test("mailerOf mails with the winning scope, and mailConfigured sees the forge's key", async () => { | |
| 190 | expect(mailConfigured(app)).toBe(false); | |
| 191 | expect(mailerOf(app, admin!, who)).toBeUndefined(); | |
| 192 | key("forge", "", "forge"); | |
| 193 | expect(mailConfigured(app)).toBe(true); | |
| 194 | key("repo", who.app, "app"); | |
| 195 | expect(await mailerOf(app, admin!, who)!(mail)).toBe(true); | |
| 196 | expect(stub.users.at(-1)).toBe("app"); | |
| 197 | // Without the app's identity the forge's key sends. | |
| 198 | expect(await mailerOf(app, admin!)!(mail)).toBe(true); | |
| 199 | expect(stub.users.at(-1)).toBe("forge"); | |
| 200 | unkey("repo", who.app); | |
| 201 | unkey("forge", ""); | |
| 202 | }); | |
| 203 | ||
| 204 | test("the line names the scope of the place viewed", () => { | |
| 205 | key("org", who.domain, "domain"); | |
| 206 | // A user's repository has no domain; the organization's own place does. | |
| 207 | expect(mailKeyLine(app, admin, "repo", "alice/site")).toContain("No mail key"); | |
| 208 | key("repo", who.app, "app"); | |
| 209 | expect(mailKeyLine(app, admin, "repo", "alice/site")).toContain("app's key"); | |
| 210 | unkey("repo", who.app); | |
| 211 | expect(mailKeyLine(app, admin, "org", "acme")).toContain("domain's key"); | |
| 212 | expect(mailKeyLine(app, admin, "forge", "")).toContain("No mail key"); | |
| 213 | unkey("org", who.domain); | |
| 214 | }); | |
| 215 | ||
| 216 | test("the Secrets page and the terminal screen show the line to an admin only", async () => { | |
| 217 | key("forge", "", "forge"); | |
| 218 | const servers: Servers = await serve(app); | |
| 219 | const bob = await createUser(app.db, { | |
| 220 | origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase", | |
| 221 | }); | |
| 222 | if (!bob.ok) throw new Error(bob.error); | |
| 223 | const get = async (u: User): Promise<{ status: number; text: string }> => { | |
| 224 | const r = await fetch(`${servers.url}/settings/secrets`, { | |
| 225 | headers: { cookie: `forge_session=${createSession(app.db, u.id).token}` }, | |
| 226 | redirect: "manual", | |
| 227 | }); | |
| 228 | return { status: r.status, text: await r.text() }; | |
| 229 | }; | |
| 230 | const seenAdmin = await get(admin!); | |
| 231 | expect(seenAdmin.status).toBe(200); | |
| 232 | expect(seenAdmin.text).toContain("Mail goes out with the forge"); | |
| 233 | expect(seenAdmin.text).not.toContain("pw@"); | |
| 234 | expect((await get(bob.value)).text).not.toContain("Mail goes out"); | |
| 235 | ||
| 236 | // Terminal: the scope sits on its own row, whole, on a narrow screen. | |
| 237 | const terminal = headlessTerminal(60, 20); | |
| 238 | const { app: tui, session } = createForgeApp( | |
| 239 | app, | |
| 240 | { sshUsername: "t", remoteAddress: "127.0.0.1", user: admin! }, | |
| 241 | { terminal, exitHooks: false }, | |
| 242 | ); | |
| 243 | tui.start(); | |
| 244 | await settleApp(tui); | |
| 245 | tui.push(secretsScreen(session, tui.ctx)); | |
| 246 | await settleApp(tui); | |
| 247 | expect(tui.screen.toText()).toContain("Mail goes out with the forge"); | |
| 248 | tui.stop(); | |
| 249 | servers.stop(); | |
| 250 | unkey("forge", ""); | |
| 251 | }); | |
| 252 | }); |
new above
offline
Resolves with
{}, so it can only ever answer forge or platform. Pass the app/domain of the page or screen being viewed, or the docs' claim that the page says which scope is in effect is untrue for app and domain.