Skip to content

feat(users): use an allowlisted request origin for account email links - #1137

Closed
antosubash wants to merge 7 commits into
mainfrom
feat/users-email-request-origin
Closed

antosubash wants to merge 7 commits into
mainfrom
feat/users-email-request-origin

Conversation

@antosubash

Copy link
Copy Markdown
Owner

Summary

  • Behaviour change (users: account emails fall back to the request origin when its host is allowlisted #1126): with neither users.base_url nor host.public_base_url / SM_PUBLIC_BASE_URL set, password-reset, verification and invitation emails now link to the current request's origin, but only if its host matches host.forwarded_host_allowlist. The match uses ForwardedHostMiddleware's matcher after proxy handling, so X-Forwarded-Host counts only from trusted_proxy. Plain http is accepted only from a trusted proxy or for a loopback host (incl. *.localhost). Otherwise the mail is skipped with the existing ERROR and the public response stays generic.
  • Precedence: users.base_url → host.public_base_url → allowlisted request origin → skip. Background/CLI sends without a request still require a configured origin.
  • New trusted_request_origin(request) helper in simple_module_hosting.pipeline.public_urls. Trusted-proxy CIDRs now match Uvicorn's validation (a CIDR with host bits set no longer widens trust).
  • make doctor: SM035 warns when no origin is configured and the allowlist is empty; modules can add read-only Doctor findings through the new ModuleBase.doctor_diagnostics(database_url) override; a failing or malformed module check reports SM036 and Doctor continues.
  • The public Mailer protocol keeps its original signatures; link_origin is passed only to mailers whose send methods accept it, and only when no origin is configured.

Fixes #1126

Follow-up filed from review: #1136 (the invite's companion verification email still has no request origin).

Verification (live app, port 8201, origins blank, allowlist ["localhost:8201"])

  • Allowlisted Host → reset link http://localhost:8201/users/reset-password?token=….
  • Forged Host: evil.example → no link, ERROR logged, 202 with a byte-identical body.
  • X-Forwarded-Host: evil.example from an untrusted peer → ignored, link host stays localhost:8201.
  • Unknown account → 202, byte-identical body.
  • Known caveat (documented): Uvicorn's own --proxy-headers trusts loopback by default, so X-Forwarded-Proto: https from 127.0.0.1 switched the link scheme to https. Only the scheme; the host allowlist still applied.
  • Local CI: make lint, make test-py (10,161 passed), make test-js (1,015 passed), make ci-doctor (0 errors, expected SM035), make ci-css-build, make ci-docs-build; full e2e suite 98 passed.
  • Verification report: https://claude.ai/artifact/HZbVDYaUwYaCjuL4XUSCQ5

Report links are Claude Artifacts and start private — the author can share them from claude.ai/code/artifacts if reviewers need access.

Test plan

  • With no origin configured and your host on host.forwarded_host_allowlist, request a password reset: the mail links to your host
  • With a forged Host, the same request returns the same response and sends nothing
  • CI is green

https://claude.ai/code/session_01KvSLu5cerHynWBZsScnK2D

When neither users.base_url nor host.public_base_url is set, password-reset,
verification and invitation emails now link to the current request's origin,
but only if its host matches host.forwarded_host_allowlist (the same matcher
ForwardedHostMiddleware uses, applied after proxy handling). Plain http is
accepted only from a trusted proxy or for a loopback host. Otherwise the mail
is still skipped with an ERROR and the public response stays generic.
Background sends with no request keep requiring a configured origin.

make doctor warns (SM035) when no origin is configured and the allowlist is
empty. Modules can add Doctor findings through the new read-only
ModuleBase.doctor_diagnostics(database_url) override.

Fixes #1126

Claude-Session: https://claude.ai/code/session_01KvSLu5cerHynWBZsScnK2D
…lenient Doctor settings

A configured origin always wins, so request_mail_options returns nothing when
the mailer has one, and a TemplatedMailer subclass that overrides a send
method with the original signature never receives link_origin. Doctor builds
HostSettings leniently, like boot, so a stale stored row cannot abort it.
Documents the Uvicorn --proxy-headers caveat for plain-HTTP links.

Claude-Session: https://claude.ai/code/session_01KvSLu5cerHynWBZsScnK2D
Origin resolution reads the effective Host instead of building the full URL; trusted-proxy CIDRs match Uvicorn's validation; bracketed IPv6 with embedded IPv4 is accepted; account-email loopback uses the shared helper (now covering *.localhost); Doctor resolves the database URL once and reports SM036 instead of aborting when a module hook raises; delivery honours TemplatedMailer subclasses' delivers_email; the public Mailer protocol keeps its original signatures.

Claude-Session: https://claude.ai/code/session_01KvSLu5cerHynWBZsScnK2D
Doctor validates module hook results inside the SM036 guard; bulk invites check delivery once per batch; trusted-proxy plain-HTTP endpoint coverage; SM036 docs clarify it is Doctor-only.

Claude-Session: https://claude.ai/code/session_01KvSLu5cerHynWBZsScnK2D
The mailer signature check requires a keyword-capable link_origin; Doctor rejects non-string diagnostic codes from module hooks under SM036; diagnostic-codes docs explain Doctor-only module checks.

Claude-Session: https://claude.ai/code/session_01KvSLu5cerHynWBZsScnK2D
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-24T21:30:56.002810Z f33bbcd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Deploying simple-module-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2535421
Status: ✅  Deploy successful!
Preview URL: https://59a725fa.simple-module-python.pages.dev
Branch Preview URL: https://feat-users-email-request-ori.simple-module-python.pages.dev

View logs

@antosubash antosubash added the shipped /ship passed on the head commit label Nov 24, 2026
@antosubash

Copy link
Copy Markdown
Owner Author

/ship passed on f33bbcd

  • Rounds: 1 (converged at the review cap: pass 3 left 0 unfixed findings; its two narrow fixes have regression tests)
  • Optimize: 1 safe cleanup applied, 2 skipped (needs-care; no-magic-strings)
  • Review: 3 passes (Codex correctness/security + maintainability). Fixed: trusted-proxy CIDR widening (high), origin read from effective Host, IPv6+IPv4 authority, shared loopback helper, Doctor SM036 isolation and validation, delivers_email override, Mailer protocol back-compat, bulk-invite single delivery check, keyword-only link_origin check. Rejected with reasons: Doctor hook read-only is a documented contract; Uvicorn trusting broader proxies can rewrite the scheme (allowlist still applies, documented).
  • QA: live-app reset probes (allowlisted host, forged Host, untrusted X-Forwarded-Host, unknown account) passed; full e2e 98 passed.
  • CI: all checks green (incl. Postgres tests)
  • Follow-up: users: invite companion verification email ignores the allowlisted request origin #1136

antosubash added a commit that referenced this pull request Nov 29, 2026
…#1150)

TrustedPeers parsed trusted_proxy entries with strict=False, so a CIDR
with host bits set (10.0.0.5/24) trusted the whole /24 in
ForwardedHostMiddleware while uvicorn's ProxyHeadersMiddleware treated
the same entry as a literal that matches nothing. ForwardedHost could
therefore trust peers ProxyHeaders did not.

Parse strictly, like uvicorn, and log a WARNING naming the intended
network so the now-inert entry does not fail silently.

Split out of #1137.


Claude-Session: https://claude.ai/code/session_01FXHA3ZEgbUuM5mzoeC4DTw

Co-authored-by: Claude <noreply@anthropic.com>
antosubash added a commit that referenced this pull request Nov 29, 2026
#1151)

* feat(users): use an allowlisted request origin for account email links

When neither users.base_url nor host.public_base_url is set, password-reset,
verification and invitation emails link to the current request's origin,
but only if its host matches host.forwarded_host_allowlist (the matcher
ForwardedHostMiddleware uses, applied after proxy handling). Plain http is
accepted only from a trusted proxy or for a loopback host. Otherwise the
mail is still skipped with an ERROR and the public response stays generic.
Sends with no request keep requiring a configured origin.

The shared host parser now uses fullmatch, rejects ports above 65535 and
accepts bracketed IPv6, which ForwardedHostMiddleware also picks up; that
is covered by new middleware tests.

Split out of #1137 (the CIDR fix and the Doctor hook are separate PRs).

Fixes #1126

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FXHA3ZEgbUuM5mzoeC4DTw

* feat(users): carry allowlisted request origin to invite verification mail (#1136)

---------

Co-authored-by: Claude <noreply@anthropic.com>
antosubash added a commit that referenced this pull request Nov 29, 2026
…eck (#1152)

* feat(users): use an allowlisted request origin for account email links

When neither users.base_url nor host.public_base_url is set, password-reset,
verification and invitation emails link to the current request's origin,
but only if its host matches host.forwarded_host_allowlist (the matcher
ForwardedHostMiddleware uses, applied after proxy handling). Plain http is
accepted only from a trusted proxy or for a loopback host. Otherwise the
mail is still skipped with an ERROR and the public response stays generic.
Sends with no request keep requiring a configured origin.

The shared host parser now uses fullmatch, rejects ports above 65535 and
accepts bracketed IPv6, which ForwardedHostMiddleware also picks up; that
is covered by new middleware tests.

Split out of #1137 (the CIDR fix and the Doctor hook are separate PRs).

Fixes #1126

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FXHA3ZEgbUuM5mzoeC4DTw

* feat(core): ModuleBase.doctor_diagnostics hook; users SM035 origin check

Modules can add read-only findings to make doctor / smpy doctor through
the new ModuleBase.doctor_diagnostics(database_url) override (never run at
boot). Doctor resolves the database URL once and passes it to every hook.
A hook that raises or returns something other than Diagnostic objects
becomes SM036 (ERROR), without its exception text, and Doctor continues.

The users module uses the hook for SM035: a WARNING when users.base_url,
host.public_base_url and host.forwarded_host_allowlist are all empty, so
account emails have no origin.

Framework-level tests cover the hook without the users module.

Split out of #1137; stacked on the account email origin PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FXHA3ZEgbUuM5mzoeC4DTw

* feat(users): carry allowlisted request origin to invite verification mail (#1136)

* fix(users): align SM035 origin and allowlist checks with runtime hydration

* Merge main into the Doctor hook branch after #1151

#1151 merged as a squash, so the old fallback commit on this branch is
resolved to main's versions. AGENTS.md moved its lifecycle section to
.agents/rules/architecture.md, so the doctor_diagnostics mention moves
there. The users menu URL and icon constants move to users.core.constants
to keep users/module.py under the 300-line cap with the new hook.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FXHA3ZEgbUuM5mzoeC4DTw

---------

Co-authored-by: Claude <noreply@anthropic.com>
@antosubash

Copy link
Copy Markdown
Owner Author

Superseded by the 3 split PRs, all shipped and merged: #1150 (strict trusted_proxy CIDR), #1151 (allowlisted request origin for account & invite verification emails, closing #1126 and #1136), and #1152 (ModuleBase.doctor_diagnostics hook & SM035).

@antosubash antosubash closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

shipped /ship passed on the head commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

users: account emails fall back to the request origin when its host is allowlisted

1 participant