Skip to content

fix: invite the existing account for aliased emails, keep Yahoo hyphens canonical - #14172

Merged
HarshMN2345 merged 13 commits into
mainfrom
fix/invite-aliased-email
Oct 8, 2026
Merged

HarshMN2345 merged 13 commits into
mainfrom
fix/invite-aliased-email

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

When a project denies aliased emails (canonicalEmails, on for the Cloud console), sign-up and OAuth store the unaliased address: [email protected] from Google is saved as [email protected], while the identity keeps the dotted providerEmail. Inviting the dotted form then missed the account:

  • with an identity holding that address: 409 user_email_already_exists ("A user with the same email already exists"), which is what an org owner hit on Cloud
  • without one: 400 user_email_not_canonical

POST /v1/teams/:teamId/memberships now does a second users.email lookup when the exact one misses and the policy is on, using the unaliased form built the same way OAuth builds it (canonical local part, typed domain kept). A match invites that account, and the mail goes to its stored address, which is the same inbox.

  • Typed input is never rewritten; an aliased address with no matching account still gets 400 user_email_not_canonical.
  • With the policy off nothing changes.

packages/emails: Yahoo keeps the hyphen

The Yahoo provider stripped everything after the last hyphen ([email protected] → [email protected]), copied from validator.js. On Yahoo a hyphen only appears in a disposable address, [email protected]: the nickname is chosen separately from the Yahoo ID (Yahoo help) and mail to the bare nickname is not delivered. The stripped form was someone else's address or nobody's, not the owner's mailbox. Yahoo canonicalization now only lowercases and folds the domain, so the invite lookup needs no provider-specific skip.

Trade-off: under deny-aliased-email, Yahoo disposable addresses are no longer rejected, so one Yahoo user can hold a few accounts (3 disposable addresses on a free account, up to 500 on Mail Plus, each created in Yahoo settings first).

Cloud's createInvite (org create / plan / payment validation invites) needs the same lookup; that follows in a Cloud PR with the server-ce bump.

Test Plan

  • composer lint and PHPStan on the changed file pass.
  • bin/monorepo check emails and bin/monorepo test emails pass (118 tests); the existing Yahoo cases now expect the hyphen to be kept.
  • Ran both branches side by side on a local stack (this branch vs main), same project, deny-aliased-email toggled per case:
Case main this branch
Policy on, account [email protected] exists, invite [email protected] 400 user_email_not_canonical 201, existing user
Same, invite [email protected] 400 user_email_not_canonical 201, existing user
Policy on, account [email protected] with a sign-in identity holding [email protected] (the incident), invite the dotted form 409 user_email_already_exists 201, existing user
Policy on, no account, invite [email protected] 400 user_email_not_canonical 400 user_email_not_canonical
Policy off, account [email protected] exists, invite [email protected] 201, new user 201, new user
Policy on, sign up as [email protected] 400 user_email_not_canonical 201

No PHP warnings or errors in either server's logs.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

When a project denies aliased emails, sign-up and OAuth store the
unaliased address (Gmail dots and +tags removed). Inviting the aliased
form missed that account: with an OAuth identity holding the aliased
address the invite failed with 409 user_email_already_exists, otherwise
with 400 user_email_not_canonical.

When the exact lookup misses and the policy is on, look up the unaliased
form built the same way OAuth builds it (canonical local part, typed
domain). Yahoo is skipped because its hyphen rule can point at another
person's mailbox. The identity conflict now explains what to do instead
of claiming a user with the same email exists.
@luxor-code-review

luxor-code-review Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Canonical fallback misses accounts stored under canonical domain — Create.php:157

This PR extends the membership invite flow in src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php to canonicalize the invitee email before the user lookup, behind a canonicalEmails feature flag. When the flag is enabled, the typed email is rewritten through the auth canonicalization helper, and if no direct account matches, a fallback lookup resolves alias forms (subaddressing/domain aliases) to the real account for the invite.

Files Change
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php Canonicalizes the invitee email and adds a fallback account lookup so invites to alias addresses match the underlying account, gated by the canonicalEmails flag.

Reviewed commit: 0f3b258

@ghost

ghost commented Oct 6, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

No new defects found, but the previously reported missing API regression coverage remains unresolved.

Adds a policy-gated fallback lookup so team invitations to aliased emails resolve to existing accounts, preserving the typed domain and sending invitations to the stored address. Unmatched aliases retain canonical-email validation, and identity-conflict errors provide clearer guidance. Yahoo canonicalization now preserves hyphens, with updated library tests, so Yahoo addresses no longer need a special exclusion in the invitation lookup.

Latest changes: The newest commit preserves Yahoo hyphens in shared canonicalization, removes the obsolete hyphen-removal helper and invitation-specific Yahoo exclusion, and updates the Yahoo test expectations.

Verdict New comments Fixed Still open
✅ Approved 0 0 1
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php:157-168
**Add E2E coverage for alias-based invitation resolution**

The existing Teams E2E suites exercise membership creation and invitation delivery, but no test covers this new lookup; the isolated derivation check cannot catch selecting the wrong account or regressing the validation path. Please add API regression coverage for resolving an alias to the existing user and delivering to its stored address, plus the policy-off, unmatched-alias, and Yahoo exclusions, as required by AGENTS.md's HTTP success/failure coverage rule.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 5
File Change
packages/emails/src/Canonicals/Provider.php Removes the unused Yahoo hyphen-subaddress removal helper.
packages/emails/src/Canonicals/Providers/Yahoo.php Preserves hyphens in Yahoo canonical local parts.
packages/emails/tests/Canonicals/Providers/YahooTest.php Updates provider tests to expect preserved hyphens across Yahoo domains.
packages/emails/tests/EmailTest.php Updates Yahoo canonicalization expectations, including mixed-case addresses.
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php Adds policy-gated alias resolution and clearer identity-conflict errors, using shared canonicalization without a Yahoo-specific exclusion.
⏳ Still open from earlier reviews · 1

Merged main (f4c9901); no changes to this PR's code since 4f904ad · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

Comment on lines +157 to +168
if ($invitee->isEmpty() && $canonicalEmails) {
$unaliased = $email;
try {
$parsedEmail = new Email($email);
if (! (new Yahoo())->supports($parsedEmail->getDomain())) {
$unaliased = \explode('@', $parsedEmail->getCanonical(), 2)[0] . '@' . $parsedEmail->getDomain();
}
} catch (\Throwable) {
}
if ($unaliased !== $email) {
$invitee = $dbForProject->findOne('users', [Query::equal('email', [$unaliased])]);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add E2E coverage for alias-based invitation resolution

The existing Teams E2E suites exercise membership creation and invitation delivery, but no test covers this new lookup; the isolated derivation check cannot catch selecting the wrong account or regressing the validation path. Please add API regression coverage for resolving an alias to the existing user and delivering to its stored address, plus the policy-off, unmatched-alias, and Yahoo exclusions, as required by AGENTS.md's HTTP success/failure coverage rule.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php
Line: 157-168

Comment:
**Add E2E coverage for alias-based invitation resolution**

The existing Teams E2E suites exercise membership creation and invitation delivery, but no test covers this new lookup; the isolated derivation check cannot catch selecting the wrong account or regressing the validation path. Please add API regression coverage for resolving an alias to the existing user and delivering to its stored address, plus the policy-off, unmatched-alias, and Yahoo exclusions, as required by AGENTS.md's HTTP success/failure coverage rule.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@luxor-code-review luxor-code-review Bot 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.

Adds email canonicalization to the team-membership invite lookup so invites sent to alias forms (plus-addressing or domain aliases) resolve to the canonical account.

Key findings

  • 🟡 Canonical fallback misses accounts stored under canonical domain — Create.php:157

Comment on lines +157 to +169
if ($invitee->isEmpty() && $canonicalEmails) {
$unaliased = $email;
try {
$parsedEmail = new Email($email);
if (! (new Yahoo())->supports($parsedEmail->getDomain())) {
$unaliased = \explode('@', $parsedEmail->getCanonical(), 2)[0] . '@' . $parsedEmail->getDomain();
}
} catch (\Throwable) {
}
if ($unaliased !== $email) {
$invitee = $dbForProject->findOne('users', [Query::equal('email', [$unaliased])]);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 bug · medium

Canonical fallback misses accounts stored under canonical domain

The new alias fallback never matches accounts whose stored email uses the provider's canonical domain (src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php:162). The rebuild keeps the typed domain (matching the OAuth convention), but native sign-up stores and enforces the full canonical address including domain (app/controllers/api/account.php:394), so inviting [email protected] does not find an existing [email protected] account.

Additionally, Yahoo is skipped entirely (line 161) rather than only its unsafe hyphen rule, so [email protected] or [email protected] cannot find [email protected] either. In all these cases the flow falls through to the creation path, emailIsCanonical is false, and the invite fails with USER_EMAIL_NOT_CANONICAL even though the recipient's account exists. The failure is safe but defeats the fallback's purpose for a large class of legitimate alias addresses.

📋 Prompt for AI Agents

In src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php lines 157-169, extend the fallback: after computing $unaliased (canonical local part + typed domain), also compute the full canonical address via $parsedEmail->getCanonical() and, when it differs from $unaliased, run an additional $dbForProject->findOne('users', [Query::equal('email', [$canonical])]) so variant-domain invites (hotmail.com, me.com, fastmail.fm, proton.me) match native accounts holding the canonical-domain address. Also reconsider the blanket Yahoo skip at line 161: keep the hyphen rule disabled but still map variant Yahoo domains (yahoo.co.uk, ymail.com, rocketmail.com) to yahoo.com for the lookup.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/invite-aliased-email (after).

Metric Before After Change
🚀 Requests/sec 207.07 206.98 ⚪ 0%
⏱️ Latency P50 82.53 ms 83.8 ms ⚪ +1.5%
⏱️ Latency P95 201.92 ms 198.63 ms ⚪ -1.6%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 83.8 198.63 12,825 206.98 -3.29
Account 169.27 315.31 675 11.3 +11.21
TablesDB 81.1 152.93 6,975 114.47 +3.31
Storage 75.88 163.85 3,375 57.5 -0.18
Functions 119.56 228.05 1,800 31.36 -37.81

Top API waits (after)

API request Max wait (ms)
account.name.update 542.21
account.prefs.update 509.15
functions.variables.update 505.86
tablesdb.rows.create 386.69
functions.delete 340.44

OAuth already stores and matches Yahoo hyphen aliases by the same
canonical rule, so invites follow it too instead of naming a provider.
If that rule is wrong it belongs in packages/emails, where it would
cover OAuth, sign-up and invites together.

@ghost ghost 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.

🔴 Tier D · 1 blocking finding to address. Summary

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

A hyphen in a Yahoo local part only appears in a disposable address,
[email protected]. The nickname is chosen separately from the
Yahoo ID and mail to the bare nickname is not delivered, so stripping
"-keyword" produced someone else's address, or nobody's, rather than
the owner's mailbox.

Yahoo canonicalization now only lowercases and folds the domain.
removeHyphenSubaddress had no other caller and is removed.

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

HarshMN2345 and others added 2 commits October 7, 2026 11:59
Only the invitee knows which email their account uses, so ask them instead of telling the inviter to use it.
@HarshMN2345 HarshMN2345 changed the title fix(teams): invite the existing account when the email is an alias fix: invite the existing account for aliased emails, keep Yahoo hyphens canonical Oct 7, 2026
Restores the packages/emails Yahoo canonicalization to main's behaviour
and skips Yahoo-family domains in the invite alias lookup again, so this
change only touches the invite flow.
@HarshMN2345 HarshMN2345 changed the title fix: invite the existing account for aliased emails, keep Yahoo hyphens canonical fix(teams): invite the existing account when the email is an alias Oct 7, 2026
Restores the packages/emails Yahoo change, so the invite alias lookup
needs no provider-specific skip.
@HarshMN2345 HarshMN2345 changed the title fix(teams): invite the existing account when the email is an alias fix: invite the existing account for aliased emails, keep Yahoo hyphens canonical Oct 7, 2026
@HarshMN2345
HarshMN2345 merged commit 19bb7eb into main Oct 8, 2026
5 checks passed
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.

🐛 Bug Report: Inviting a dotted Gmail address to an organization fails with 409 user_email_already_exists

1 participant