Conversation
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
force-pushed
the
fix/adduser-email-uniqueness-parity
branch
from
November 26, 2026 10:24
a2633dc to
dba016a
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
AddUserin the ArangoDB, DynamoDB, Cassandra/ScyllaDB and Couchbase providers (andprovider_template, whichmake generate-db-templatecopies for new backends) chained its two uniqueness checks: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
testSQLCRUDCorrectnessFixessubtest, so CI (SQLite only) never covered the NoSQL backends.Effect before this fix:
emailto catch it.emailrejects the insert, but the caller gets the raw driver error (unique constraint violated - in index idx_... of type hash over 'email'; conflicting key: ...) instead ofuser with given email already exists.Fix
The checks are now two independent
ifblocks 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'su, _ :="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).UpdateUseron these backends does no email/phone uniqueness check at all, on any backend including SQL, so it has noelse-ifpattern to fix. SCIM PATCH pre-checks phone changes itself (scim.goaround line 940).AddUser caller audit
service/signup.goif 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)GetUserByEmailservice/magic_link_login.goGetUserByEmail(signup branch when absent)service/scim/scim.go(create)GetUserByExternalID+GetUserByEmailhttp_handlers/oauth_callback.go(two sites)oidcClaims.toUser()maps thephone_numberclaimGetUserByEmaildecides signup vs login, but any error from it (including a transient one) goes down the signup path. With a provider that returnsphone_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)GetUserByEmailUnique indexes (not changed here)
emailandphone_numberalready exist.INSERT ... IF NOT EXISTS) on a lookup table keyed by email.ponytailcomment 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:user with given email already existsuser with given phone number already existsRun locally against containers started with the Makefile's own
docker runlines (narrowgo test ./internal/storage/..., not the fullmake test-<db>targets, which also re-run the SQLite integration suite):TEST_DBS=<db> go test ./internal/storage/...make test)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.