Repository navigation
fix: gate admin notifications to production only - #1305
Merged
Merged
Conversation
9 tasks done
This was referenced Sep 8, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The dev worker (api-test.nemar.org, ENVIRONMENT=development) shares production's
userstable 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 oneconsole.warnnaming the category, unless the opt-inDEV_ADMIN_NOTIFICATIONSenv var is exactly"1".It mirrors
isEmailDeliveryAllowed's notion of "production" (only the literal string"production"bypasses; unset/unknownENVIRONMENTfails toward suppressing) rather thanservices/environment.ts'sisNonProductionEnv(which treats an unknownENVIRONMENTas production) -- the dev worker's own admin account is onDEV_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.getAdminEmailsForCategorynow takes a requiredenv: AdminNotificationEnvparameter (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:398and:600(publish request, resend)backend/src/index.ts:376and: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 --isAdminNotificationAlloweddistinguishes 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_NOTIFICATIONSwas added to theBindingstype, documented next toDEV_EMAIL_ALLOWLIST. Nothing was added tobackend/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:isAdminNotificationAllowedexhaustively (production always allows; every other/unsetENVIRONMENTrequires the literal opt-in"1";undefinedenv refuses), andgetAdminEmailsForCategoryagainst 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 withENVIRONMENT: "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 thatDEV_ADMIN_NOTIFICATIONS=1opts 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):isAdminNotificationAllowedcheck ingetAdminEmailsForCategory(if (false && ...)) turned exactly the 4 tests expecting suppression red.isAdminNotificationAllowed's opt-in branch toreturn falseturned exactly the 17 tests that depend onDEV_ADMIN_NOTIFICATIONS=1red."production"comparison to"productionX"turned exactly the 3 tests expecting the production-allow path red.{ ENVIRONMENT: "production" }at theusers.tscall site (instead of threadingc.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, andbun test backend/test(2421 tests) are green.Deliberately left alone
backend/wrangler-sccn.toml: noDEV_ADMIN_NOTIFICATIONSadded anywhere (opt-in only, per design).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 behindisNonProductionEnv(env)(production or an unset/unrecognizedENVIRONMENT) 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 misconfiguredENVIRONMENT.