Registry Mirror
Skip to content

Conversation

@lakhansamani

Copy link
Copy Markdown
Contributor

Root cause

AddUser in the ArangoDB, DynamoDB, Cassandra/ScyllaDB and Couchbase providers (and provider_template, which make generate-db-template copies for new backends) chained its two uniqueness checks:

if user.PhoneNumber != nil && ... { GetUserByPhoneNumber ... } else if user.Email != nil && ... { GetUserByEmail ... }

When the new user had both a phone number and an email, the email check was skipped. SQL, MongoDB and SurrealDB were fixed earlier to check the two independently. The regression test for that fix sat in the SQL-only testSQLCRUDCorrectnessFixes subtest, so CI (SQLite only) never covered the NoSQL backends.

Effect before this fix:

  • DynamoDB, Cassandra/ScyllaDB, Couchbase: a second user with an existing email and a different phone is inserted, which gives two accounts with the same email. None of these backends has a unique constraint on email to catch it.
  • ArangoDB: the sparse unique hash index on email rejects the insert, but the caller gets the raw driver error (unique constraint violated - in index idx_... of type hash over 'email'; conflicting key: ...) instead of user with given email already exists.

Fix

The checks are now two independent if blocks on all four backends and the template, the same code (and the same comment) as SQL/Mongo/Surreal. Error messages are unchanged. The lookups still use SQL's u, _ := "found a different user" shape, so a lookup error falls through to the insert on every backend, the same as SQL. I left that alone because this PR is about parity. It means that during a lookup outage only a DB-level unique constraint stops a duplicate (SQL, Mongo and Arango have one; Dynamo, Cassandra and Couchbase do not; see below).

UpdateUser on these backends does no email/phone uniqueness check at all, on any backend including SQL, so it has no else-if pattern to fix. SCIM PATCH pre-checks phone changes itself (scim.go around line 940).

AddUser caller audit

Caller Sets email + phone? Pre-check before AddUser
service/signup.go yes (both can be supplied) if isEmailSignup { GetUserByEmail } else { GetUserByPhoneNumber }, so with both supplied only the email is pre-checked and phone uniqueness depends on storage. On the affected backends the old storage code checked phone only, so service plus storage happened to cover both. The exposure was limited to races.
service/admin_access.go (invite) email only GetUserByEmail
service/magic_link_login.go email only GetUserByEmail (signup branch when absent)
service/scim/scim.go (create) email only (phone is never set on create) GetUserByExternalID + GetUserByEmail
http_handlers/oauth_callback.go (two sites) yes: oidcClaims.toUser() maps the phone_number claim GetUserByEmail decides signup vs login, but any error from it (including a transient one) goes down the signup path. With a provider that returns phone_number, the old code on Dynamo/Cassandra/Couchbase then skipped the email check and could create a duplicate-email account.
http_handlers/oauth_sso.go (org SSO JIT) email only GetUserByEmail

Unique indexes (not changed here)

  • ArangoDB: sparse unique hash indexes on email and phone_number already exist.
  • DynamoDB: no unique constraint is possible on a non-key attribute. A real guard needs a conditional transactional write to a separate uniqueness item, which is not cheap.
  • Cassandra/ScyllaDB: no unique secondary indexes. It would need LWT (INSERT ... IF NOT EXISTS) on a lookup table keyed by email.
  • Couchbase: the existing ponytail comment already records this as an accepted race. A fix would key a uniqueness document by email.

None of these is a trivial migration, so they are left out of this PR. The check-then-insert race remains on those three backends.

Tests

A new all-backend subtest, TestStorageProvider/*/AddUser_Uniqueness_With_Email_And_Phone (internal/storage/provider_test.go), checks two cases:

  • duplicate email with a different phone is rejected with user with given email already exists
  • duplicate phone with a different email is rejected with user with given phone number already exists

Run locally against containers started with the Makefile's own docker run lines (narrow go test ./internal/storage/..., not the full make test-<db> targets, which also re-run the SQLite integration suite):

Backend Pre-fix (main code) Post-fix targeted Post-fix TEST_DBS=<db> go test ./internal/storage/...
sqlite pass pass (covered by make test)
arangodb FAIL (raw unique-index error) pass pass
dynamodb FAIL (duplicate inserted) pass pass
cassandradb FAIL (duplicate inserted) pass pass
couchbase FAIL (duplicate inserted) pass pass

ScyllaDB shares the cassandradb provider and was not run separately. MongoDB/SurrealDB were not run; their code is unchanged.

Also run: go build ./..., go vet ./..., make lint (0 issues), make test.

ArangoDB, DynamoDB, Cassandra/ScyllaDB and Couchbase (and the provider
template) chained the phone and email uniqueness checks with else-if, so a
new user carrying a phone number skipped the email check. DynamoDB,
Cassandra and Couchbase inserted a duplicate-email account; ArangoDB was
saved only by its unique index and surfaced a raw driver error.

Check both independently, matching SQL, MongoDB and SurrealDB, and add an
all-backend storage test (the existing assertion was SQL-only).
@lakhansamani
lakhansamani force-pushed the fix/adduser-email-uniqueness-parity branch from a2633dc to dba016a Compare November 26, 2026 10:24
@lakhansamani
lakhansamani merged commit f259ea9 into main Nov 26, 2026
4 checks passed
@lakhansamani
lakhansamani deleted the fix/adduser-email-uniqueness-parity branch November 26, 2026 10:34
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