Skip to content

feat: adopt an existing user on internal provision by exact Google subject - #1105

Merged
jiashuoz merged 5 commits into
mainfrom
feat/provision-adopt-google-subject
Nov 21, 2026
Merged

jiashuoz merged 5 commits into
mainfrom
feat/provision-adopt-google-subject

Conversation

@jiashuoz

@jiashuoz jiashuoz commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

An existing direct signup can block control-plane provisioning with email_conflict. The HMAC-authenticated POST /api/internal/users/provision now accepts optional google_subject and links the existing account only on an exact Google subject match. Email alone never authorizes a link.

Final behavior

  • Matching evidence on a standard account returns 200 with user_id and adopted: true, preserving identity fields.
  • Subject-bearing retries resolve the existing issuer/ref mapping first and recheck the subject, including after an email change. Retries must resend the subject.
  • Missing or wrong evidence follows the existing evidence-less path. A free email creates a bootstrap account with 201 and no adopted field.
  • Subjects require 1–255 ASCII digits and the configured external issuer. Nonstandard accounts cannot be adopted.
  • A second ref for the same issuer is refused with external_principal_conflict; existing mappings cannot be repointed.
  • New links enforce read-only, deletion and tombstone protections. Existing-link replay follows attach semantics.
  • Existing ProvisionUser callers retain their signature. Deployment and observability docs are updated.

Verification

Application source verified at f198e4fb0132cf11dba6f6cc43eee54b9b958a64. Current head: 78dde6233a875c2d9432e285172679b3ba31f157, which merges the coverage fix from #1106 without application changes.

  • Fresh server build; 15 real HTTP checks against isolated local PostgreSQL passed: adoption, retry, identical wrong/missing-subject conflicts, duplicate-ref rejection, case normalization, creation, invalid subjects, missing/wrong issuer, paused/system-account refusal, bad signature, and retry after email change.
  • Expected mapping count and preserved identity fields verified; server error scan clean. Test server/database removed and scratch PostgreSQL stopped.
  • CI run 44839609355: unit and full integration suites, coverage generation, race checks, SDK contracts and all other test jobs passed.
  • The former coverage configuration blocker is fixed by merged fix(ci): restore coverage checks and pin checker version #1106 and included in the current head. Fresh CI has been triggered for the updated branch; its results are pending.
  • Plugin tests and Docker image build passed.
  • Prior independent/adversarial findings were addressed in ad9eb1384 and f198e4fb0 with regression tests; detailed dispositions remain in the follow-up comment.

Follow-up

The control plane must send verified Google subjects, retry eligible conflicts at sign-in, handle refusal codes and reconcile the design's retry wording. This PR alone does not fix the end-to-end login flow.

jiashuoz and others added 4 commits November 20, 2026 15:02
…ubject

ProvisionUserAdopting is ProvisionUser plus one evidence-gated exception
to "never attach on an email conflict": when the caller supplies a Google
subject and the requested email belongs to a live user whose
google_subject is exactly that value, the provision maps the caller's
(issuer, external_ref) to that user instead of failing. The user row is
never changed.

A missing, different or trashed match is still ErrEmailConflict,
indistinguishable from a request with no subject. A new mapping gets the
same read-only and tombstone checks AttachExternalPrincipal applies, a
pair already mapped elsewhere is still ErrExternalPrincipalConflict, and
a synthetic bootstrap: subject is never accepted as evidence. The check
runs before the insert because the insert's unique violation would abort
the transaction. ProvisionUser keeps its signature and behavior.

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

POST /api/internal/users/provision takes an optional google_subject,
honored only with external_issuer. On an email conflict the store adopts
the existing user when that user's google_subject equals it exactly, and
the response is 200 {"user_id", "adopted": true}. Every other response
is byte-identical to before, including the email_conflict a wrong
subject gets, so the field cannot be used to probe accounts.

A subject that is over 255 bytes, contains control characters, starts
with bootstrap:, or arrives without an issuer is 400
invalid_google_subject. Adopting an abuse-paused account is 403
account_read_only, the rule attach already applies. The provisioning
metric gains the bounded outcome "adopted"; deployment.md and
observability.md describe the contract and the label.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…adoption by mapping

Review findings on the adoption path:

- A mismatched or trashed holder returned email_conflict before the
  evidence-less path's tombstone check, so with tombstones on it could
  answer 409 where a request without a subject answers 403/503. The
  adoption check now returns "no evidence" and the request continues on
  exactly that path, so the bytes are identical by construction.
- An adoption replay re-derived the user by email, so a replay after the
  adopted user's e2a email changed fell through to a fresh create and
  then external_principal_conflict. A replay now resolves by the
  existing (issuer, ref) mapping first, and only while the subject still
  matches.
- The subject bound moves into identity.ValidGoogleSubject so the store
  enforces the same rule as the handler.

Adds tests for the tombstoned mapping subject, the missing tombstone
key, a replay after pause, a replay with a different subject, the
handler's external_principal_conflict, and two concurrency cases (an
adoption waiting on a trash, and concurrent adoptions of one pair).
deployment.md now states the replay rule and every adoption error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ce per issuer

Adversarial review found two ways to adopt an account that is not the
caller's person:

- Seeded accounts carry public, non-"bootstrap:" subjects (the prober's
  and the conformance fixtures'), so a caller that knew one could adopt
  a system account. google_subject must now be 1..255 ASCII digits, the
  shape of a Google subject, and only account_class = 'standard' is
  adoptable; any other class answers exactly as a mismatch.
- A second external_ref presenting the same subject added a second live
  mapping to an account the issuer already maps, a delegated binding the
  control plane can never use. A new adoption now refuses with
  external_principal_conflict when the holder already has a mapping from
  that issuer.

deployment.md states both rules and names the answers that reveal a
matching subject (HMAC-only).

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

Copy link
Copy Markdown
Member Author

Independent + adversarial review: dispositions

Fixed in ad9eb1384 and f198e4fb0. Each fix has a regression test that fails on the prior commit.

  • Wrong subject vs no subject could differ (tombstones on: 409 vs 403/503). Fixed: a mismatched or trashed holder now falls through to the evidence-less path, so the bytes match by construction.
  • Adoption replay re-derived the user by email, so it broke after an e2a.dev sign-in changed the email. Fixed: a replay resolves by the existing (issuer, ref) mapping first, still gated on the same subject. This now implements the spec's "mapping-first" step, scoped to subject-bearing requests; an unconditional version would break TestProvisionUserExternalPrincipalConflictAborts.
  • Seeded system/fixture accounts were adoptable (public non-bootstrap: subjects such as the prober's). Fixed: google_subject must be 1–255 ASCII digits, and only account_class = 'standard' is adoptable.
  • A second ref could add a second same-issuer mapping to an already-mapped account. Both reviewers flagged it. Fixed: refused with 409 external_principal_conflict.
  • The store and handler now share one validator (identity.ValidGoogleSubject).
  • Added tests: tombstoned mapping subject, missing tombstone key, replay after pause, handler-level external_principal_conflict, an adoption racing a trash, and concurrent adoptions of one pair.
  • deployment.md now gives the replay rule, every adoption error, and the HMAC-only signals a matching subject reveals. The "empty-with-no-issuer" wording is fixed.

Deferred, with reasons:

  • Subject-less replay of an adopted pair returns email_conflict. Kept: a replay must repeat the evidence, and the control plane always sends it.
  • A holder whose stored email differs only in case is not found. Pre-existing behavior of the email = $1 lookup, affecting every provision path. Out of scope here.
  • 1024-byte body cap. A real request is far below it.
  • Replays count as adopted in the metric. Kept as is.

Verification after the fixes: make fmt-check and go test ./... pass. The integration run of identity/agent/telemetry is 1317 passed, 0 failed, 0 skipped. The full suite passes 62/63 packages; internal/e2e needs Mailpit and fails identically on main. The over-the-wire e2e ran before these fixes and was not re-run.

🤖 Generated with Claude Code

@jiashuoz
jiashuoz merged commit f303674 into main Nov 21, 2026
29 checks passed
@jiashuoz
jiashuoz deleted the feat/provision-adopt-google-subject branch November 21, 2026 08:05
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