Skip to content

fix(auth): allow-list the setup admin's email so emailed-code sign-in works (#4693) - #4694

Merged
vybe merged 3 commits into
devfrom
fix/4693-admin-email-code
Nov 20, 2026
Merged

vybe merged 3 commits into
devfrom
fix/4693-admin-email-code

Conversation

@trinity-ability

Copy link
Copy Markdown
Contributor

Fixes #4693

Journey Impact: extends: J01

Problem

First-run setup saved the admin's email on the admin account but did not add it to the email allow-list. POST /api/auth/email/request only creates a code for an address on that list, so the admin who used "Send Verification Code" got the generic 200, and no code was created or sent.

Where the rule lives: allow-list at the writers of the admin's email

The address goes on the allow-list wherever the admin's email is written. The request route is not changed. Reasons:

Covered writers:

  • Setup, both forms. The password form and the emailed-link claim form (trinity-enterprise#1312) both reach email_registered and then call allow_list_admin_email.
  • Existing installs. The first lifespan phase runs the same helper as a boot backfill. It runs only when setup is complete, does nothing if the row already exists, never raises, and logs no address. No manual step is needed.
  • Later email change. PUT /api/users/me/email by the setup admin account adds the new address too.

The new row uses default_role='user'. The admin's address resolves to the existing admin row, so that role is only used if the old address ever creates a new account after the admin moves to another address. That account must not be an admin.

Nothing changes when email_auth_enabled is off or a login policy turns email codes off.

Sign-in page

Before: "We sent a 6-digit code to X". After: "If X is registered, a 6-digit code is on its way."

Tests

tests/unit/test_4693_admin_email_code_login.py runs the real setup route and then the real request route over one in-memory DB, for both setup forms:

  • With the admin's address, a code is created and a send is dispatched.
  • With another address, nothing is created, and the body is the same.
  • Setup with no email adds nothing to the allow-list.
  • The backfill adds the row once and does nothing on a second run.
  • The backfill never raises.
  • AST pins check that the boot phase and the email-change route call the helper.

Red before the fix: I reverted the setup call and test_password_setup_with_email_then_code_request_mints_and_sends and test_email_link_setup_then_code_request_mints_and_sends failed. Restoring it made them pass.

  • Auth/setup/email unit neighbours (69 files): 2411 passed, 7 skipped
  • Frontend: loginNoEmailProvider.spec.js was updated first and failed (2 failed), then the wording change made it pass. Login specs + raw-color/source-text ratchets: 110 passed.

Out of scope (noted)

A non-admin user who changes their own email through /me/email is also not added to the allow-list. That is the same class of bug for other accounts. It is not changed here.

🤖 Generated with Claude Code

… works (#4693)

First-run setup stored the admin's email on the admin row but never added
it to the email allow-list, so POST /api/auth/email/request answered its
generic 200 and minted nothing for the instance's own admin.

The rule sits at the writers of the admin's email, not at the request
route: setup (password form and emailed-link form), the admin's own
email change, and a boot-time idempotent backfill for installs set up
before this fix. The request route and the #2381 redeemer re-check are
unchanged. The row's default_role is 'user', used only if the address
ever creates a new account after the admin moves to another address.

The sign-in page now says a code is on its way if the address is
registered, instead of claiming one was sent.

Red before the fix: test_password_setup_with_email_then_code_request_mints_and_sends
and test_email_link_setup_then_code_request_mints_and_sends (no code minted).

Fixes #4693

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@trinity-ability trinity-ability left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/validate-pr — #4694 at d43be2149 (Lane B, fresh detached checkout)

Recommendation: REQUEST CHANGES (one item; everything else checks out)

What was checked

  1. Sign-in as the admin by emailed code. The verify route resolves the account by the email column, so a code for the setup admin's address now lands an admin session. That is what the issue asks for. The #2381 re-check is still in place at verify and nothing was loosened. Only three things write a row: setup (run by whoever is creating the admin, and refused once an admin exists), PUT /me/email (require_interactive, and only when the caller is the setup admin), and the boot backfill (reads the admin row's own email). No other address can reach the list this way. default_role='user' is right for the stale row left behind when the admin moves to a new address.
  2. Boot backfill. It runs in every worker and isn't leader-gated. That is safe: there is a check before the insert and the email column is UNIQUE, so the worker that loses the race hits the except and logs only the exception type. It never raises and never logs the address.
  3. Request route. routers/auth.py is not in the diff, so the anti-enumeration contract is byte-for-byte unchanged. The Login.vue wording ("If X is registered…") is more honest than before.
  4. Production wiring is pinned. I deleted each call one at a time and re-ran the suite:
    • setup call removed → 2 failed
    • boot call removed → 1 failed
    • /me/email call removed → 1 failed

Critical (must fix)

  • The boot backfill re-adds the admin's address after an operator removes it. DELETE /api/settings/email-whitelist/{email} works on this row. The backfill runs on every boot where setup_completed is set, so the next restart puts the row back and email-code sign-in for the admin comes back with it. The PR's own argument against the alternative fix was that it "would let an account that was removed from the allow-list sign in again". This does that for the admin account. Suggested fix: make the backfill run once, using a system_settings marker that is set after the first pass whether or not a row was written. Then add a test that removes the row and runs the backfill a second time.

Warnings

  • Both conditions are unpinned. Replacing if current_user.username == admin_username(): in update_my_email with if True: stays green. So does inverting the setup_completed branch in main.py. The AST pins only check that the call exists. One test that runs update_my_email as a non-admin and asserts no row would cover the first.
  • An unverified address now grants something it didn't before. Setup binds the email without verifying it, and so does the console-provider /me/email path. Before this PR, a mistyped address gave nothing without the password. Now whoever owns that inbox can request a code and sign in as the admin. This follows from the issue, so it isn't blocking, but it is worth one line in the operator docs.
  • Nothing runs the helper against a real DB: every test uses FakeDB. A real-engine case would prove the UNIQUE-race path.

Suggestions

  • Test fixtures use acme.com. Repo convention is example.com.

Gates

  • Closing keyword Fixes #4693 ✅
  • Journey Impact extends: J01 ✅ (CI pass)
  • §4.1–4.3 clean (198.51.100.7 is a documentation range)
  • No env, mode or host-path changes
  • §5.5: users.py carries # mcp: none — …; setup.py is excluded
  • The /me/email write is already audited
  • Local: test_4693_* 8 passed
  • CI on tip: required contexts pass. pytest (head, seed 12345) was still pending when this was written, so Tier 1 is not yet complete.

@trinity-ability trinity-ability added the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Nov 20, 2026
Trinity Agent (trinity) and others added 2 commits November 20, 2026 18:00
The boot backfill now runs once, on a completed setup, behind a stored
marker that setup also sets, so removing the admin's address from the
allow-list survives a restart. The marker is set even when nothing was
written. Tests drive the /me/email admin condition and the setup_completed
condition through the real code; fixtures use example.com.

Mutations recorded red: marker check removed (3), marker write removed (2),
setup marker removed (1), setup_completed check removed (1), /me/email
condition replaced by True (1).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot removed the status-needs-fix PR has an unaddressed review/validation finding; cleared by the author's next push (#2815) label Nov 20, 2026

@trinity-ability trinity-ability left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/validate-pr re-check — #4694 at 5dd8dd277 (fresh detached checkout)

Decision: PASS. The Critical item and both warnings are fixed. CI on this tip was still pending when this was posted.

Fixed

  • The backfill now runs once per install. backfill_admin_email_once() runs only when setup has completed, and it is gated on a stored system_settings marker. The marker is set even when there was nothing to write. Setup sets the marker too, so the first boot after setup cannot re-add an address the operator removed in between. Tests: remove the row and run again, the row stays gone; marker set when nothing was written; nothing written and no marker on an unfinished setup; removal right after setup survives the next boot.
  • Both conditions now have tests that run the code. PUT /me/email runs through the real handler: the setup admin's new address is added, and another account's change adds nothing (that account is an admin in the test). The setup_completed check now lives in the service and is executed by a test. The AST pin in main.py covers only that the call is there.
  • Test fixtures now use example.com.
  • Docs: getting-started/setup.md now says the admin email can also sign in by emailed code, so use an address you control.

Mutations (each reverted afterwards, file restored byte-identical)

Mutation Result
marker check removed 3 failed
marker write removed 2 failed
setup's marker write removed 1 failed
setup_completed check removed 1 failed
/me/email condition replaced by True 1 failed

Tests at 5dd8dd277

  • test_4693_*: 13 passed
  • auth/setup neighbours (lifespan phases, 2381 ×2, 3004, 3237, 3455, 3676, 837, admin_email_login, ent1201, ent1253, ent127, 1310 ×2) plus the new tests: 341 passed
  • ent1312 setup link claim, ent720 email binding, setup operator profile: 85 passed

Still open, not blocking: nothing runs the helper against a real DB engine yet.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving on the validation re-check PASS against 5dd8dd2 and green checks on that tip.

@vybe
vybe merged commit f57bdf5 into dev Nov 20, 2026
27 checks passed
vybe pushed a commit that referenced this pull request Nov 21, 2026
…4679/#4677 (#4680)

Accepted growth, ruled at merge-train Gate 2 (2026-11-21):
- routers/setup.py 6 -> 9 (ent#1329 setup terms, #4683/#4694/#4698)
- client_portal/work/service.py 8 -> 10, agent_service/autonomy.py 12 -> 14
  (#4679's batch prefetch reads: fewer queries, more call sites)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
vybe pushed a commit that referenced this pull request Nov 23, 2026
)

* test(backend): ratchet on blocking calls inside async def (#4674)

A sync db, Docker, HTTP or sleep call written directly inside an
`async def` runs on the event loop and blocks the whole worker.
scripts/ci/check_sync_on_loop.py counts them per file by parsing (never
importing) and compares with scripts/ci/sync_on_loop_baseline.json:

- a file's count may not exceed its entry;
- a file with no entry starts at zero;
- an entry above reality fails too, so a paydown lowers the baseline.

Today: 1,911 call sites in 831 async functions across 194 files
(db 1,363; through a module's own sync function 386; Docker 161).

Counted: db / core_db / _db / *.db method calls, get_agent_container,
list_all_agents_fast, agent_container_states, time.sleep,
subprocess.run, requests.*, and calls to the same module's plain defs
that make one. Not counted: plain def handlers, nested defs, and the
arguments of to_thread / run_in_executor / run_in_threadpool.

The ratchet runs in the unit suite (test_4674_sync_on_loop_ratchet.py).
The helper list is one constant, for trinity-dev#82 to share.

Related to #4674

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* test(backend): the ratchet does not pin the offenders it exists to remove (#4674)

Review finding. One test asserted that heartbeat_service's watch loop
and dependencies.py are in the scan. Those are exactly what #4674 goes
on to fix, so the test would have failed on the fix. It now asserts
only that the live scan sees both families. The backend is parsed once
per module instead of once per test.

Related to #4674

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* test(backend): regenerate the sync-on-loop baseline on current dev (#4674)

dev moved under the branch and changed two files' counts, which is the
ratchet doing its job on its own PR.

Related to #4674

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* merge-train: regenerate the sync-on-loop baseline on dev after #4676/#4679/#4677 (#4680)

Accepted growth, ruled at merge-train Gate 2 (2026-11-21):
- routers/setup.py 6 -> 9 (ent#1329 setup terms, #4683/#4694/#4698)
- client_portal/work/service.py 8 -> 10, agent_service/autonomy.py 12 -> 14
  (#4679's batch prefetch reads: fewer queries, more call sites)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(backend): --write refuses to absorb growth into the baseline (#4674)

Regenerating the baseline wrote whatever the scan found, so a regeneration
could wave new blocking calls through and only the JSON diff showed it.
--write now prints each file whose count grew and writes nothing unless
--allow-growth is passed; a paydown is written as before.

Also: main() reads and writes the baseline path it names (the helpers'
default argument was bound at import), and the docstring no longer claims
a /code-health consumer that does not exist.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

---------

Co-authored-by: Claude Fable 5.2 <[email protected]>
Co-authored-by: trinity-ability <[email protected]>
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.

2 participants