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

Mail relay call: child asks parent to send, with from-domain allowlist and per-scope rate limit #17367

opened this

Merged 34h 11m ago, live in production

A child forge with no mail key of its own now asks its parent to send, and the parent sends on its own key. Tests for the new call, mail caps, mail key, verify-email, migrations, dead code and own-source pass, and tsc and the gate are clean.

Notes

opened this

A child forge with no mail key of its own now asks its parent to send, and the parent sends on its own key. Tests for the new call, mail caps, mail key, verify-email, migrations, dead code and own-source pass, and tsc and the gate are clean. I did not run the whole suite, because scripts/fast.ts named almost every test; the forge runs it on push.

What changed

  • The call (src/spawn/mail_relay.ts): the child sends a signed POST /api/v1/mail on the beat door with {to, subject, text, from}. The door is mounted in src/spawn/beat-door.ts. A bad signature or an unknown child gets a 404.
  • The answer: {ok:true, result:"sent"|"capped"|"failed"}, or {ok:false, error} where the error is from_domain, bad_message or no_key. The key is in neither the call nor the answer.
  • From-domain allowlist: the from domain must be on the child's list, which defaults to its own hostname. The parent sends with that from (sendWithMailKey takes an asFrom argument).
  • Rate limit: each relayed message counts under the child's name in mail_sent. A child is capped at SCOPE_DAILY (20) messages a day (sendCapped in src/actions/mail.ts), on top of the existing per-recipient and whole-forge caps. A sibling's allowance is separate.
  • Wiring: mailerOf in src/actions/verify.ts now returns the relay mailer when a forge has no key of its own and has a parent.json.
  • Migration: 20261202000000-mail-relay-scope adds mail_sent.scope, an index on (scope, at) and a mail_relay_domain table, and is pinned. I regenerated src/generated/ with bun scripts/own-source.ts.

Tests (test/mail-relay.test.ts): a real child and parent, with the call going through the real door.

  • A refused from-domain sends nothing.
  • A child over its limit gets capped, and the other scope's count is separate.
  • A bad signature gets a 404.
  • A planted SMTP URL and password appear in no body on the wire, either way.

What I did not do

  • No door: a commit line says why. This is a machine-to-machine call with no page. The terminal and page showing the key's scope, and any way to edit the allowlist, are part 3 of #15872. Until then the allowlist can only be set by writing rows to mail_relay_domain.
  • mailConfigured unchanged: it still ignores the relay, so a child with no key does not start holding people behind email verification.
  • Reuse: I reused signedCall, callParent, childNamed, sendCapped, holdSend, sendWithMailKey and resolveMailKey. The similar-function notes the gate gave were false matches.

Try it: on a preview, make a child forge with no mail key and have it send a mail. The parent's mail_sent rows then carry the child's name as scope.

Closes #17335

3 earlier comments
commented

A child forge with no mail key of its own now asks its parent to send, and the parent sends on its own key. Tests for the new call, mail caps, mail key, verify-email, migrations, dead code and own-source pass, and tsc and the gate are clean. I did not run the whole suite, because scripts/fast.ts named almost every test; the forge runs it on push.

What changed

  • The call (src/spawn/mail_relay.ts): the child sends a signed POST /api/v1/mail on the beat door with {to, subject, text, from}. The door is mounted in src/spawn/beat-door.ts. A bad signature or an unknown child gets a 404.
  • The answer: {ok:true, result:"sent"|"capped"|"failed"}, or {ok:false, error} where the error is from_domain, bad_message or no_key. The key is in neither the call nor the answer.
  • From-domain allowlist: the from domain must be on the child's list, which defaults to its own hostname. The parent sends with that from (sendWithMailKey takes an asFrom argument).
  • Rate limit: each relayed message counts under the child's name in mail_sent. A child is capped at SCOPE_DAILY (20) messages a day (sendCapped in src/actions/mail.ts), on top of the existing per-recipient and whole-forge caps. A sibling's allowance is separate.
  • Wiring: mailerOf in src/actions/verify.ts now returns the relay mailer when a forge has no key of its own and has a parent.json.
  • Migration: 20261202000000-mail-relay-scope adds mail_sent.scope, an index on (scope, at) and a mail_relay_domain table, and is pinned. I regenerated src/generated/ with bun scripts/own-source.ts.

Tests (test/mail-relay.test.ts): a real child and parent, with the call going through the real door.

  • A refused from-domain sends nothing.
  • A child over its limit gets capped, and the other scope's count is separate.
  • A bad signature gets a 404.
  • A planted SMTP URL and password appear in no body on the wire, either way.

What I did not do

  • No door: a commit line says why. This is a machine-to-machine call with no page. The terminal and page showing the key's scope, and any way to edit the allowlist, are part 3 of #15872. Until then the allowlist can only be set by writing rows to mail_relay_domain.
  • mailConfigured unchanged: it still ignores the relay, so a child with no key does not start holding people behind email verification.
  • Reuse: I reused signedCall, callParent, childNamed, sendCapped, holdSend, sendWithMailKey and resolveMailKey. The similar-function notes the gate gave were false matches.

Try it: on a preview, make a child forge with no mail key and have it send a mail. The parent's mail_sent rows then carry the child's name as scope.

commented

I merged main into the branch and pushed it. The only conflict was src/generated/migrations.ts.

Both sides had added a migration at the same position in the list. I regenerated the file with bun scripts/own-source.ts instead of editing the conflict markers, so it now lists both dep-accept (from main) and mail-relay-scope (from this branch). The mail-relay migration is now number 459 in the generated list.

bun x tsc --noEmit is clean. I ran only the own-source and migration/mail tests, 105 in all, and they pass. I did not run the full suite; the forge runs it on the push.

gate.ts --fix changed nothing. It printed two notes that mail_log.ts looks similar to other functions, and one about a long comment line in src/spawn/beat-door.ts. I left all three alone.

commented

The duplicate-import check was failing because test/mail-relay.test.ts imported from src/actions/spawn.ts twice. I merged beatKeyOf into the existing import on line 11 and deleted the second one.

test/dup-imports.test.ts and test/mail-relay.test.ts pass (12 tests), and bun x tsc --noEmit is clean. I didn't run the full suite; the forge runs it on the push.

commented

This round takes juniper's second option for the allowlist: I dropped the mail_relay_domain table and its reader, so a child sends only as its own hostname. A kept per-child from-domain list now belongs to #17336 (part 3 of #15872), which owns the doors where it can be set and shown. The relay is otherwise unchanged: signed door, body capped by bytes read, scope rate limit on the signed child name, and the key never on the wire.

What changed

  • Allowlist (1): relayDomains is gone. answerMailCall refuses from_domain unless the from domain is the child's own hostname. The migration now adds only mail_sent.scope and its index. It was new in this pull, so I edited it and re-pinned it in place. The header comments in src/spawn/mail_relay.ts and the migration say a kept list is #17336.
  • Door line (2): The commit line now says the terminal's mail already goes through mailerOf. The fallback to the relay only applies when the forge has a parent.json and no key of its own, so nothing the terminal or page can do changes.
  • Tidy (4): Long lines are wrapped in mail_relay.ts and mail_key.ts. from as string and d["to"] as string are gone, because to and from are narrowed with typeof checks. The no-key test builds a real App by dropping the mailer and mail keys, not by casting.

Tests (3), in test/mail-relay.test.ts

  • (a) A second real child, carlos, is spawned and sends successfully through a signed call after maria is capped. Its own relay mailer also sends.
  • (b) Not added, because the kept-domain row went with the table. A refusal of a foreign domain is still tested.
  • (c) bad_message is tested for a bad to, a non-string to, a newline in subject, a subject over 200 characters, a text over 8 KB, and a malformed from. A body over the byte cap is also tested and never reaches the sender.
  • (d) no_key is tested by calling answerMailCall on a parent with no mailer and no key.
  • (e) A forge with no parent.json and no key gets mailerOf undefined.

Checks

  • bun scripts/gate.ts: 0 fail. bun x tsc --noEmit is clean.
  • Ran mail-relay, mail-key, verify-email, unused-exports, llms-budget, schema and own-source, and all pass.
  • Not run here: tests that bind a unix socket (skipped in this sandbox) and the rest of the suite. The forge's check runs those.

Not done: a writer for per-child allowed domains, which is the #17336 work.

Try it: On a preview, spawn a child forge with no mail key and request a verification mail. The parent's mail_sent rows carry the child's scope.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs