fix(oidc): a sign-in links to an account by email only when the address was not set by the account itself

An SSO login that matched no account by its subject took the account with the
same email address and linked the provider identity to it. That address proves
nothing when the account typed it in itself: anyone could sign up with, or
switch their profile to, a colleague's address and receive the colleague's
first SSO login. The provider switch branch (#2110) relinked a row on any email
match without even asking for email_verified, and also for another subject of
the same provider.

users.email_self_asserted remembers an address the account set itself: signup
(not the first account) and an email change in the profile settings set it, an
admin entering a new address clears it, and accounts the provider creates start
without it. Existing accounts keep linking, except those whose address moved
away from the registered one without an admin editing it. A login by email now
needs a verified address and an unmarked account, and is refused for another
subject of the same issuer, with email_link_blocked and a translated message on
the login screen. An email change in the profile settings writes a
user.email_change audit row.

Refs #2110
This commit is contained in:
Maurice
2026-10-12 17:06:52 +02:00
parent 1b58acc78f
commit 3d7e8be9aa
49 changed files with 688 additions and 29 deletions
+25 -11
View File
@@ -665,21 +665,35 @@ export class OidcService implements OnModuleDestroy {
userInfo: OidcUserInfo,
picture: string | null,
): Promise<{ user: User; roleChange?: OidcRoleChange } | { error: string }> {
// Reaching here without an oidc_sub means we matched an existing local
// account by email. Only auto-link the OIDC identity when the IdP asserts
// the email is verified; an unverified email must not auto-link.
if (!user.oidc_sub) {
// A row that does not carry this sub and issuer was found by its email
// address alone. Linking it takes two promises: the IdP says the address is
// verified, and the address on the row was not typed in by the account itself
// (signup, profile settings), or whoever did that would receive this person's
// sign-in. The unverified answer comes first, so it never tells an unverified
// login whether such an account exists.
if (user.oidc_sub !== sub || user.oidc_issuer !== config.issuer) {
const emailVerified = userInfo.email_verified === true || userInfo.email_verified === 'true';
if (!emailVerified) {
return { error: 'email_not_verified' };
}
await this.usersRepo.linkOidcIdentity(user.id, sub, config.issuer);
user = { ...user, oidc_sub: sub, oidc_issuer: config.issuer };
} else if (user.oidc_issuer !== config.issuer || user.oidc_sub !== sub) {
// The admin pointed the instance at a different IdP. We got here through the
// verified-email lookup, so this is the same person arriving from the new
// provider; leaving the old sub and issuer on the row would keep the account
// pinned to a provider that no longer exists (#2110).
if (user.email_self_asserted) {
console.warn(
`[OIDC] Refused to link a sign-in to user ${user.id} by email: the account set that address itself. An admin can confirm it by changing the email in the admin panel.`,
);
return { error: 'email_link_blocked' };
}
// The same provider with another subject is another person at that
// provider, never this account under a new name.
if (user.oidc_sub && user.oidc_issuer === config.issuer) {
console.warn(
`[OIDC] Refused to link a sign-in to user ${user.id} by email: it is linked to another subject of this provider.`,
);
return { error: 'email_link_blocked' };
}
// Without an oidc_sub this is the account's first SSO sign-in. With one from
// another issuer, the admin pointed the instance at a different IdP: leaving
// the old sub and issuer on the row would keep the account pinned to a
// provider that no longer exists (#2110).
await this.usersRepo.linkOidcIdentity(user.id, sub, config.issuer);
user = { ...user, oidc_sub: sub, oidc_issuer: config.issuer };
}