Skip to content

fix: gate admin notifications to production only - #1305

Merged
neuromechanist merged 1 commit into
devfrom
fix/admin-notifications-prod-only
Sep 8, 2026
Merged

neuromechanist merged 1 commit into
devfrom
fix/admin-notifications-prod-only

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Problem

The dev worker (api-test.nemar.org, ENVIRONMENT=development) shares production's users table and holds a live RESEND_API_KEY. Issue #957's delivery fence (isEmailDeliveryAllowed) allow-lists the repo owner's own address so staging sign-in codes reach them. Because that address is also an admin account, every ADMIN-facing notification the dev worker generates (new user awaiting approval, upload-access request, publication request, import recovery, cron digests) passed the per-recipient fence and landed in that inbox with a [DEV] prefix. A live-tier CI run and a staging walk-through produced a stream of such approval emails. Admin notifications must be production-only.

Design

getAdminEmailsForCategory (backend/src/services/email.ts) is the single chokepoint every admin-notifying call site uses. A new fence, isAdminNotificationAllowed, sits at the top of that function: on a non-production worker it returns [] before the D1 query runs and logs one console.warn naming the category, unless the opt-in DEV_ADMIN_NOTIFICATIONS env var is exactly "1".

It mirrors isEmailDeliveryAllowed's notion of "production" (only the literal string "production" bypasses; unset/unknown ENVIRONMENT fails toward suppressing) rather than services/environment.ts's isNonProductionEnv (which treats an unknown ENVIRONMENT as production) -- the dev worker's own admin account is on DEV_EMAIL_ALLOWLIST, so the recipient-level fence alone does not stop admin mail from reaching it; this fence has to fail toward NOT generating that mail at all.

getAdminEmailsForCategory now takes a required env: AdminNotificationEnv parameter (not optional), so TypeScript enforces every call site passes it -- no call site can silently forget the fence. All eight call sites were updated:

  • backend/src/routes/users.ts:247 (upload-access request)
  • backend/src/routes/auth.ts:713 (CLI verify -> admin notification)
  • backend/src/routes/datasets/publication.ts:398 and :600 (publish request, resend)
  • backend/src/index.ts:376 and :530 (exemplar-invariant alert, staleness admin review)
  • backend/src/services/import-recovery.ts:160 (import quarantine alert)
  • backend/src/services/email-verification.ts:265 (web verify -> admin notification)

backend/src/routes/users.ts's "no admin recipients" console.error (previously logged whenever the list came back empty) now only fires when the fence was open and the admin table itself came back empty -- isAdminNotificationAllowed distinguishes a real misconfiguration from the expected dev suppression. The other seven call sites already skip silently on an empty list, so no message needed adjusting there.

DEV_ADMIN_NOTIFICATIONS was added to the Bindings type, documented next to DEV_EMAIL_ALLOWLIST. Nothing was added to backend/wrangler-sccn.toml (neither prod nor the dev [env.dev] block) -- opt-in only, no default anywhere. AGENTS.md's "Dev D1 shares production users and the GitHub org" section gets one two-sentence addition documenting the variable next to the existing "can still email real people" / cron-allowlist language.

Tests

Real D1 (freshDb/realD1) and the existing Bun.serve Resend stand-in throughout, no mocks:

  • backend/test/email-delivery-fence.test.ts: isAdminNotificationAllowed exhaustively (production always allows; every other/unset ENVIRONMENT requires the literal opt-in "1"; undefined env refuses), and getAdminEmailsForCategory against a real seeded admin row (production returns the address, development/unset return [], development + opt-in returns the address again).
  • backend/test/tier-emails.test.ts: the existing "notifies the admins" test now runs against a production-configured app (previously ran with ENVIRONMENT: "test", which the new fence now suppresses); two new tests prove the same route (GET /auth/verify) suppresses the admin notification by default outside production while still mailing the user's key-ready email, and that DEV_ADMIN_NOTIFICATIONS=1 opts back in.
  • backend/test/upload-access-request-route.test.ts: its shared test env opts in (DEV_ADMIN_NOTIFICATIONS: "1") since that suite is about the review card's content/retry semantics, not the fence; a new test proves the same route suppresses the notification (response still 201, email_sent: false, admins_notified: 0, zero Resend calls) when the opt-in is absent, even though the admin's address is itself allow-listed for delivery.
  • backend/test/web-email-verification.test.ts: its shared env opts in for the same reason (its suite is about when the notification fires, across both the dedicated verify endpoint and code sign-in).

Every new assertion was proven able to fail (mutate-watch-red-revert, .rules/testing.md):

  • Neutralizing the isAdminNotificationAllowed check in getAdminEmailsForCategory (if (false && ...)) turned exactly the 4 tests expecting suppression red.
  • Forcing isAdminNotificationAllowed's opt-in branch to return false turned exactly the 17 tests that depend on DEV_ADMIN_NOTIFICATIONS=1 red.
  • Changing the literal "production" comparison to "productionX" turned exactly the 3 tests expecting the production-allow path red.
  • Hardcoding { ENVIRONMENT: "production" } at the users.ts call site (instead of threading c.env) turned the new route-level suppression test red, proving the test catches a wiring mistake at the call site, not just the unit-level fence.

All reverted; bun run typecheck, bun run lint, and bun test backend/test (2421 tests) are green.

Deliberately left alone

  • backend/wrangler-sccn.toml: no DEV_ADMIN_NOTIFICATIONS added anywhere (opt-in only, per design).
  • The six call sites that already skip silently on an empty admin list (all but users.ts) were left as-is -- no misleading log to adjust there.
  • index.ts:530's "no admins exist to notify; advance so we don't loop forever" branch was left as-is: it is already gated behind isNonProductionEnv(env) (production or an unset/unrecognized ENVIRONMENT) and carries no log message, so there was nothing misleading to fix, though the new fence does add a second layer of protection there for a misconfigured ENVIRONMENT.

@neuromechanist
neuromechanist merged commit 331576c into dev Sep 8, 2026
12 checks passed
@neuromechanist
neuromechanist deleted the fix/admin-notifications-prod-only branch September 8, 2026 15:27

This branch was previously deployed

1 inactive deployment
npm — 5bd2164b Deployed Sep 8, 2026 by neuromechanist via publish #2372
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant