Skip to content

dev/core#6931 - Fix orphan contact on login for a second account with the same email - #38237

Open
ayush-compuco wants to merge 2 commits into
civicrm:masterfrom
compucorp:dev-core-6931-ufmatch-no-orphan
Open

ayush-compuco wants to merge 2 commits into
civicrm:masterfrom
compucorp:dev-core-6931-ufmatch-no-orphan

Conversation

@ayush-compuco

@ayush-compuco ayush-compuco commented Nov 23, 2026 •

Copy link
Copy Markdown

Overview

Fixes dev/core#6931 (related: dev/core#4500). A CMS user can log in with an email that another account's civicrm_uf_match row already holds as uf_name. CiviCRM then creates a contact that it can never link to this user, and a new one appears on every fresh login. With this change, CiviCRM links the user to its own contact and saves the match with uf_name = NULL. The approach comes from jfilter's review below. A second commit stops updateContactEmail() from writing a uf_name that another user holds.

Before

A CMS user with no civicrm_uf_match row logs in. Another user's row already has the same email as uf_name. synchronizeUFMatch() creates a contact first, then finds the uf_name conflict and saves no match. The new contact stays, and the next login adds another one. Core never logs the message it builds for this case.

In a headless test, three syncs of the second user leave three new contacts and no match. One Drupal 7 production site got 14 contacts with this pattern between 1 September and 9 November 2026.

On Drupal 7, an admin who adds a user with such an email gets "DB Error: already exists". Drupal rolls back the account, but the new contact stays.

After

The user gets one contact of its own, linked to it. Later syncs reuse it, and core never gives this user another user's contact. Core logs the conflict.

An admin can now add a user with such an email. The new user gets its own linked contact.

Technical Details

  • uf_name is unique per domain. Before core saves the match, it now checks whether another user holds the email as uf_name. If so, core saves the match with uf_name = NULL. The unique index allows several NULLs.
  • The check covers both paths: a new contact, and an unlinked contact that core finds by email.
  • No contact is shared, so ACLs stay with each user's own contact. The CRM-9091 check still refuses a contact that another user holds.
  • An empty email skips the check.
  • Core now also logs the existing conflict message.
  • updateContactEmail() copies the CMS email to uf_name. On Drupal, civicrm_register_data() calls it right after the sync when a user is added through a form, such as the admin's Add user page. civicrm_user_update() calls it when a user changes email. It now skips the uf_name write when another user holds the email, as updateUFName() already does (CRM-6928).
  • updateContactEmail() now returns early without a contact ID. Without one, its lookup has no contact filter. In a local test it changed the first user's uf_name and an unrelated contact's primary email.
  • CRM_Core_BAO_UFMatchTest covers the login and form paths, a stale uf_name, both updateContactEmail() cases, and two guards for normal matching.

Comments

If both accounts belong to one person, that person now has two contacts, one for each account. Staff can merge them after they remove the extra CMS account.

@civibot

civibot Bot commented Nov 23, 2026

Copy link
Copy Markdown

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
PR commands & links...
  • /rebase <?branch-name> will rebase your branch and change the base of the PR (if <branch-name> is specified).
  • /squash will combine all commits (keeping only the first commit messsage).
  • /port <branch-name> will create a copy of this PR against a different branch.
  • /lint will automatically fix linting errors, amending commits as needed.
  • retest this please will rerun the tests and rebuild the demo site.
  • 📖 Review standards
  • 🗒️ Review template (brief or verbose)

➡️ Online demo of this PR 🔗

@civibot civibot Bot added the master label Nov 23, 2026
@civibot

civibot Bot commented Nov 23, 2026

Copy link
Copy Markdown

The issue associated with the Pull Request can be viewed at https://lab.civicrm.org/dev/core/-/issues/6931

@jfilter

jfilter commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@civireview

@civireview civireview left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CiviReview: 🟢 Ready · Confidence: 4/5 ★★★★☆

Summary: Stops synchronizeUFMatch() from creating an orphan contact when another CMS account already holds the same email as uf_name. The session uses the other account's contact, and the conflict is now logged. A/B testing proved 2 test(s) failed on base, passed on PR.

Merge Confidence (4/5): CI / Jenkins failed (likely unrelated flake).

  • PR: civicrm/civicrm-core#38237 · Kind: core-bugfix · Commits: 96f3b25 → 28c3991

Findings

No verified high-signal issues found above the confidence gate.

Empirical Verification Matrix

Check Status Detail
CI / Jenkins ⚠️ WARN 1 CI checks: 0 passed, 1 failed, 0 pending (3 failed test(s) extracted). — failed tests: CiviDrupal\PhpVersionTest::testConstantMatch, CiviDrupal\PhpVersionTest::testInfoMatch, CiviWP\PhpVersionTest::testConstantMatch — likely unrelated flake: Failures are CiviDrupal/CiviWP PhpVersionTest constant/info version mismatches ('8.1.2'). They are unrelated to UFMatch, and the PR's own tests pass.
A/B Proof-of-Fix ✅ PASS 2 test(s) failed on base, passed on PR (+3 regression-guard test(s) passed on both base & PR: testSynchronizeUFMatchDoesNotHandOverContactOfStaleUFName, testSynchronizeUFMatchLinksUnlinkedContactWithSameEmail, testSynchronizeUFMatchCreatesLinkedContactForNewEmail)
Base-Test Regression ✅ PASS ck ab (junit) exited with 0
Live Surface Diff ✅ PASS Live APIv4/APIv3/routes/Afform surface diff captured with 0 risk flags.
Detailed Walkthrough (7 notes)
  • PROVEN (A/B): the login and form-submission cases of testSynchronizeUFMatchDoesNotCreateContactWhenEmailIsTaken are red on base and green on the PR. No regressions were reported.
  • CONFIRMED (diff): a new pre-check, getMatchOfAccountWithSameEmail(), runs before contact creation. It looks up uf_name in the domain and calls getUserObject() to check that the other account's current CMS email still matches. If it does, the session gets that account's contact_id and no uf_match row is saved. The existing $msg conflict is now logged.
  • NEEDS_QUESTION (security): a session gets another account's contact when the emails match. getLoggedInContactID() then points at someone else's record, so ACLs and permissions apply to that contact. Is this safe where the CMS does not verify emails, and does it allow impersonation or data exposure?
  • NEEDS_QUESTION: the early return skips the rest of synchronizeUFMatch() (session setup, the $status return path). Is it correct that the return value is $newContact when $status is set? No row is saved, so every page view may repeat the lookup.
  • Three of the added tests passed on the unfixed base (stale uf_name, link existing contact, create new). They guard existing behaviour and do not prove the fix.
  • NOT VERIFIED: real Drupal, Backdrop and WordPress behaviour. getUserObject() is only mocked in tests, and WordPress, Joomla and Standalone are unchanged by design. The stale-uf_name case still creates a new contact, as the author notes.
  • CI: the failures are PhpVersionTest constant/info mismatches, which are unrelated to UFMatch.
CiviCRM Core Standards (4 checks — all PASS/N/A)
Code Status Note
r-explain PASS The PR body and code comments explain the change.
r-tech PASS Backed by sec-1: in the conflict case synchronizeUFMatch returns another account's contact_id, and synchronize() puts it into the session as the logged-in contact. That changes access to own-contact data on CMS setups that allow shared emails. (Reconciled: no surviving verified findings.)
r-maint PASS Two data sets fail on the base and pass with the fix. Three added tests pass on the base too, so they are regression guards for normal matching and the stale uf_name case.
r-test PASS The CI failures (PhpVersionTest) are unrelated. The A/B run found no regressions.

Reviewed by civireview · sonnet-opus · 197,683 tokens · $0.43

@ayush-compuco
ayush-compuco force-pushed the dev-core-6931-ufmatch-no-orphan branch from 28c3991 to bb0d87b Compare November 24, 2026 07:52
ayush-compuco added a commit to compucorp/civicrm-core that referenced this pull request Nov 24, 2026
ayush-compuco added a commit to compucorp/civicrm-core that referenced this pull request Nov 24, 2026
@ayush-compuco

Copy link
Copy Markdown
Author

Updated the PR to address the CI / Jenkins check failing.

@jfilter

jfilter commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

I checked the logs of the civireview run above (the tool is still very alpha) and found an issue that got lost in the final verification stage. The review should not have said "Ready".

This PR introduces a security problem. When a second CMS account logs in with an email that another account already holds, synchronizeUFMatch() now returns the first account's contact_id, and synchronize() stores it as userID in the session. CiviCRM ACLs are contact-based, so the second user inherits the other contact's ACL roles and can see and edit that person's data (profiles, contact dashboard, Afform autofill).

Default Drupal enforces unique emails, so this only triggers where duplicates are possible (e.g. sharedemail, SSO/social login, imports). That is exactly where the same email does not mean the same person. The existing conflict check deliberately refuses to link a contact that is already linked to another account (CRM-9091).

On master this is not a security issue, it only leaves orphan contacts. A safer fix: create the contact once and save the uf_match row with uf_name = NULL (the unique index on uf_name, domain_id allows multiple NULLs). Then no contact is shared.

@ayush-compuco
ayush-compuco force-pushed the dev-core-6931-ufmatch-no-orphan branch from bb0d87b to b4e54c8 Compare November 25, 2026 04:58
ayush-compuco added a commit to compucorp/civicrm-core that referenced this pull request Nov 25, 2026
ayush-compuco added a commit to compucorp/civicrm-core that referenced this pull request Nov 25, 2026
@ayush-compuco

Copy link
Copy Markdown
Author

Thanks @jfilter, you're right: the session would carry the other contact's ACLs. I've switched to your suggestion. When another user already holds the email as uf_name, the user is now linked to its own contact with uf_name = NULL. The contact is created once, later syncs reuse it, and no contact is shared. This also covers the stale uf_name case, which the previous version left alone. I've updated the description and the test. The test now checks one linked contact per user, a NULL uf_name, and that the other user's contact is never returned.

ayush-compuco added a commit to compucorp/civicrm-core that referenced this pull request Nov 26, 2026
ayush-compuco added a commit to compucorp/civicrm-core that referenced this pull request Nov 26, 2026
@ayush-compuco

Copy link
Copy Markdown
Author

@jfilter I've added a second commit. Our QA found that adding a user in Drupal failed with "DB Error: already exists" when another user held the email as uf_name. civicrm_register_data() calls updateContactEmail() right after the sync, and that function then wrote the email to uf_name. It now skips that write when another user holds the email, as updateUFName() does. It also returns early without a contact ID, because its lookup then matched the first civicrm_uf_match row. Two new tests cover both cases.

marcelocompucorp pushed a commit to compucorp/civicrm-core that referenced this pull request Nov 26, 2026
marcelocompucorp pushed a commit to compucorp/civicrm-core that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants