Skip to content

Fix pre-account hijacks and harden account recovery - #1730

Open
BenjaminMichaelis wants to merge 2 commits into
mainfrom
security/identity-account-ownership
Open

BenjaminMichaelis wants to merge 2 commits into
mainfrom
security/identity-account-ownership

Conversation

@BenjaminMichaelis

Copy link
Copy Markdown
Member

Fixes the account-ownership findings from the recent security review. The common thread: a credential (password, external login, MCP token) or an email address could end up on an account before anyone proved they control that account's mailbox, and then survive the real owner confirming or resetting it.

Changes

External login registration (ExternalLogin)

  • The confirmation handler no longer calls AddLoginAsync for the typed (unverified) email. It records the external login as pending (in AspNetUserTokens), and ConfirmEmail links it only when the emailed link is opened in a browser that still holds that same external login. A link opened anywhere else confirms the email but links nothing; the page tells the user to log in with the provider again, which sends a fresh link.
  • Adds the missing hCaptcha check (widget on the page, validated before any lookup or account creation).
  • Account creation and recording the pending login succeed or fail together (the account is deleted if the second step fails).
  • Accounts that can already sign in are never linked from this page; they link from Manage → External logins as before.

Register / first email confirmation (Register, ConfirmEmail)

  • Register sets a short-lived, data-protected cookie that binds the new account to the registering browser. On the first confirmation, the registration password is kept only if the link is opened in that browser. Otherwise the password is removed and the page offers a Choose a password link, so whoever only controls the mailbox sets their own.
  • On the first confirmation, all external logins and MCP tokens are removed and the security stamp is rotated (this also cleans up accounts pre-linked before this fix).

Password reset (ResetPassword)

  • After a successful reset: remove external logins, revoke all MCP tokens, rotate the security stamp, and clear lockout. The confirmation page tells the user that linked logins and tokens were removed and can be re-added.

Email change (ConfirmEmailChange, Manage/Email)

  • ConfirmEmailChange now requires the signed-in user to be the account that requested the change (anonymous clicks go to the login page, and other users get an error).
  • The change email has its own subject and wording instead of reusing the registration message.

Email links

  • Confirmation, reset and email-change links are built from SiteSettings:BaseUrl (new Url.EmailPageLink helper) instead of the request Host.

Other

  • RegisterConfirmation no longer looks the user up; it always renders the same generic page.
  • Login: wrong passwords no longer count toward lockout for accounts that have no password (for example GitHub-only accounts).

Behavior changes to be aware of

  • A password reset now unlinks GitHub/Microsoft logins and revokes MCP tokens. Users re-link them from Manage.
  • Someone who registers on one device and confirms on another is asked to choose a password again.
  • External-login sign-up needs the confirmation link to be opened in the same browser (the Identity external cookie lasts about 5 minutes). Otherwise the user signs in with the provider again and gets a new link.

Not changed: AllowedHosts

I did not narrow AllowedHosts. The deploy workflow's smoke check calls https://$FQDN/health on the Container Apps default *.azurecontainerapps.io hostname, and container-app health probes may send a Host header that isn't one of the public names. Restricting hosts in appsettings.json could fail deployments or probes. With links now built from SiteSettings:BaseUrl, the request Host no longer affects emailed links. If you still want host filtering, it's safest to set AllowedHosts per environment in the Container App configuration, including the default FQDN.

Tests

New HTTP-level tests in EssentialCSharp.Web.Tests/Identity/ drive the real pages with separate cookie jars per "browser" and capture outgoing email:

  • ExternalLoginAccountLinkingTests: no link before confirmation; a pre-registered login doesn't survive confirm plus reset; linking in the same browser; no linking with a different external login; the other-browser recovery path; captcha required; legacy pre-linked logins removed.
  • RegisterAccountOwnershipTests: a squatter's password is removed when the email is confirmed in another browser; the choose-a-password flow; the same-browser registrant keeps the password; reset removes logins and tokens and rotates the stamp.
  • ConfirmEmailChangeTests: anonymous, other-user and requester clicks; change-specific email.
  • AccountRecoveryHardeningTests: RegisterConfirmation oracle; forged-Host reset and confirmation links; no lockout without a password; reset clears lockout.

Against unmodified main, 20 of the 23 new tests fail; the other 3 are positive controls. All 23 pass with this change. IdentityEmailFailureTests was updated for the new constructor parameters and now also asserts that the external-login handler doesn't call AddLoginAsync.

Full EssentialCSharp.Web.Tests run locally: 371/373 pass. The 2 failures (Layout_WithDefaultTestBackend_DoesNotRequestContextualSearch, McpCall_FindBookHelpForDiagnostic_ReturnsReadableTextAndStructuredContent) come from local AI user-secrets (an embedding call returns 401). They fail the same way on unmodified main.

Locally I built with SDK 10.0.204 because 10.0.403 isn't installed on this machine; global.json is unchanged in the PR.

🤖 Generated with Claude Code

Fixes the Identity findings from the 2026-11 security audit:

- ExternalLogin: never link an external login to an account for an
  unverified, typed email. The login is recorded as pending and linked by
  ConfirmEmail only when the emailed link is opened in a browser that still
  holds that same external login. Adds the missing hCaptcha check, and
  deletes the account if recording the pending login fails.
- Register: a password chosen at registration survives the first email
  confirmation only when the link is opened in the registering browser;
  otherwise it is removed and the mailbox owner chooses one.
- ConfirmEmail (first confirmation) and ResetPassword: remove external
  logins, revoke MCP tokens and rotate the security stamp. ResetPassword
  also clears lockout.
- ConfirmEmailChange: require the signed-in user to be the requester, and
  send a change-specific email.
- Build email links from SiteSettings:BaseUrl instead of the Host header.
- RegisterConfirmation: always render the generic page (no existence
  oracle).
- Login: wrong passwords no longer lock accounts that have no password.

Adds HTTP-level regression tests derived from the audit's reproductions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The HttpClient caller disposes it; fixes CodeQL cs/local-not-disposed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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