feat(email): remove Gmail OAuth2, put SMTP behind a transport registry

Engagement Phase 1 (docs/website/ENGAGEMENT.md §1.2a, §3.1, §3.2). A
subtraction and a replacement in one commit, because leaving the OAuth2
flow half-wired across a release is worse than either end state.

Deleted, per the §1.2a inventory: GET /admin/email/connect/start and
/connect/callback, the connectStart/connectCallback controllers with the
email_oauth_tx signed cookie, the PKCE verifier and CSRF nonce plumbing,
the https://mail.google.com/ scope, the borrowed `google` auth-providers
client, the OAuth2 nodemailer transport with its smtp.gmail.com:465
literals, the refresh-token decrypt in the model, and the client's
Connect Gmail button, redirect banner and six Gmail error strings.
`provider` and `refresh_token_enc` stay as columns under the
additive-only discipline, unread.

Added: a mail transport registry (server/src/engagement/transports) with
`smtp` as the sole registration. `credentialFields` is the single
declaration the admin form renders, the sanitizer filters against, and
the "is it secret" answer comes from, so adding a transport is a
registration rather than four edits. email_config gains transport /
credential_enc (one encrypted JSON blob, since the field list is the
transport's to declare) / reply_to.

All six call sites keep their exact failure contracts: the contact
form's mailto fallback, the invite's copyable link, the reset's generic
200, and sendTeamNotification's never-throws. One deliberate behaviour
change: `enabled` now gates every sender rather than only isConfigured()
— the connect flow used to set it as a side effect, and with a credential
form the toggle has to mean what it says.

Send-test becomes the real verification. Under OAuth2 the sender came
back from Google and was guaranteed to belong to the credential;
operator-typed, it can be refused, so failures name the sender and the
SPF/DMARC reason (§1.2a consequence 2).

G22, the silent degradation: an upgraded deployment backfills to smtp
with no credentials and every sink politely does nothing. The admin
dashboard now warns when the deprecated Gmail token is present and no
replacement credential is, so the one deployment this happens to is told.
A fresh install has never had mail and is not nagged.

Guardrails: new `npm run check:hosts` (§3.2 rule 4) with its own
self-test, wired into pr-checks before the install; routes.manifest and
routes.guards regenerated (-2 routes).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-08-28 20:41:43 -05:00
parent e25e7ade80
commit 47c8b37d45
26 changed files with 1535 additions and 461 deletions

View File

@@ -0,0 +1,109 @@
// The mail transport registry (ENGAGEMENT.md §3.1) and the one transport core
// ships. The registry's job is that `credentialFields` is the single declaration
// the admin form, the sanitizer and the "is it a secret" answer all read — so
// most of what is asserted here is that nothing else knows a field name.
process.env.SECRET_ENC_KEY = process.env.SECRET_ENC_KEY || 'unit-test-enc-key'
process.env.DB_HOST = '127.0.0.1'
process.env.DB_PORT = '59999'
const { test, after } = require('node:test')
const assert = require('node:assert/strict')
const { transports } = require('../src/engagement')
const db = require('../src/utils/db')
after(() => db.close())
const SMTP_CRED = { host: 'relay.example.com', port: 587, secure: false, user: 'apikey', password: 'sec' }
test('requiring the subsystem is what registers core\'s transports', () => {
assert.equal(transports.has('smtp'), true)
const [smtp] = transports.describe()
assert.equal(smtp.id, 'smtp')
assert.ok(smtp.credentialFields.length > 0)
})
test('describe() carries no functions and no secret values', () => {
const [smtp] = transports.describe()
assert.equal(typeof smtp.build, 'undefined')
assert.equal(typeof smtp.isComplete, 'undefined')
const password = smtp.credentialFields.find((f) => f.key === 'password')
assert.equal(password.kind, 'secret')
assert.equal('value' in password, false)
})
test('sanitizeCredential drops undeclared keys', () => {
const out = transports.sanitizeCredential('smtp', { ...SMTP_CRED, evil: 'x' })
assert.deepEqual(Object.keys(out).sort(), ['host', 'password', 'port', 'secure', 'user'])
})
test('sanitizeCredential coerces to the declared kind', () => {
const out = transports.sanitizeCredential('smtp', { host: 'relay.example.com', port: '587', secure: 'yes' })
assert.equal(out.port, 587)
assert.equal(out.secure, true)
})
test('an empty secret is omitted, so a merge keeps the stored one', () => {
const patch = transports.sanitizeCredential('smtp', { ...SMTP_CRED, password: '' })
assert.equal('password' in patch, false)
const merged = transports.mergeCredential('smtp', { password: 'stored' }, patch)
assert.equal(merged.password, 'stored')
})
test('publicCredential and secretsPresent split the blob the way the API needs', () => {
assert.deepEqual(transports.publicCredential('smtp', SMTP_CRED), {
host: 'relay.example.com', port: 587, secure: false, user: 'apikey',
})
assert.deepEqual(transports.secretsPresent('smtp', SMTP_CRED), { password: true })
assert.deepEqual(transports.secretsPresent('smtp', { host: 'x' }), { password: false })
})
test('an unknown transport is a safe no-op everywhere, never a throw', () => {
assert.equal(transports.get('nope'), null)
assert.equal(transports.isComplete('nope', SMTP_CRED), false)
assert.deepEqual(transports.sanitizeCredential('nope', SMTP_CRED), {})
assert.deepEqual(transports.publicCredential('nope', SMTP_CRED), {})
assert.deepEqual(transports.secretsPresent('nope', SMTP_CRED), {})
})
// ── smtp's own completeness rule ────────────────────────────────────────────
test('smtp needs a destination, and auth is all-or-nothing', () => {
assert.equal(transports.isComplete('smtp', SMTP_CRED), true)
// A local MTA needs no credentials at all.
assert.equal(transports.isComplete('smtp', { host: 'mta.example.com', port: 25 }), true)
// A username with no password authenticates as nobody and fails at the server.
assert.equal(transports.isComplete('smtp', { host: 'relay.example.com', port: 587, user: 'apikey' }), false)
assert.equal(transports.isComplete('smtp', { port: 587 }), false)
assert.equal(transports.isComplete('smtp', {}), false)
})
test('smtp declares no default host — §3.2 rule 1, as a test', () => {
const [smtp] = transports.describe()
const host = smtp.credentialFields.find((f) => f.key === 'host')
assert.equal(host.default, null)
assert.equal(host.required, true)
})
// ── registration is validated at the call ───────────────────────────────────
test('registration rejects a bad shape and a collision', () => {
const ok = { id: 'fake', label: 'Fake', credentialFields: [{ key: 'k', kind: 'text' }], build: () => {}, isComplete: () => true }
assert.throws(() => transports.registerMailTransport({ ...ok, id: 'Not Valid' }), /invalid id/)
assert.throws(() => transports.registerMailTransport({ ...ok, label: '' }), /label required/)
assert.throws(() => transports.registerMailTransport({ ...ok, credentialFields: [] }), /credentialFields required/)
assert.throws(() => transports.registerMailTransport({ ...ok, credentialFields: [{ key: 'k', kind: 'wat' }] }), /unknown kind/)
assert.throws(() => transports.registerMailTransport({ ...ok, build: undefined }), /build\(\) required/)
assert.throws(() => transports.registerMailTransport({ ...ok, id: 'smtp' }), /already registered/)
})
test('a registered transport is a copy — a caller cannot mutate the catalog afterwards', () => {
const fields = [{ key: 'k', label: 'K', kind: 'text', required: true }]
transports.registerMailTransport({
id: 'tamper', label: 'Tamper', credentialFields: fields, build: () => {}, isComplete: () => true,
})
fields[0].kind = 'secret'
const def = transports.describe().find((t) => t.id === 'tamper')
assert.equal(def.credentialFields[0].kind, 'text')
})