From 0cda530e15f76c8900a58120bdee23605555eb72 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 14 Sep 2026 18:34:08 -0400 Subject: [PATCH] fix: Scope authentication matching to the signing-in provider (#13737) --- server/commands/userProvisioner.test.ts | 35 ++++++++++++++++ server/commands/userProvisioner.ts | 54 +++++++++++++++++++------ 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/server/commands/userProvisioner.test.ts b/server/commands/userProvisioner.test.ts index eb70638626..fbd0a857ed 100644 --- a/server/commands/userProvisioner.test.ts +++ b/server/commands/userProvisioner.test.ts @@ -227,6 +227,41 @@ describe("userProvisioner", () => { expect(isNewUser).toEqual(false); }); + it("should not match an existing authentication from a different provider", async () => { + const existing = await buildUser(); + const authentications = await existing.$get("authentications"); + const existingAuth = authentications[0]; + const originalProviderId = existingAuth.authenticationProviderId; + + // A second provider of a different type, where the attacker is able to + // present the same external identifier as the existing user. + const otherAuthProvider = await AuthenticationProvider.create({ + name: "oidc", + providerId: randomString(32), + teamId: existing.teamId, + }); + + const { user, isNewUser } = await userProvisioner(ctx, { + name: "Attacker", + email: "attacker@example.com", + emailVerified: false, + teamId: existing.teamId, + authentication: { + authenticationProviderId: otherAuthProvider.id, + providerId: existingAuth.providerId, + accessToken: "123", + scopes: ["read"], + }, + }); + + expect(isNewUser).toEqual(true); + expect(user.id).not.toEqual(existing.id); + + await existingAuth.reload(); + expect(existingAuth.authenticationProviderId).toEqual(originalProviderId); + expect(existingAuth.accessToken).not.toEqual("123"); + }); + it("should create a new user", async () => { const team = await buildTeam(); const authenticationProviders = await team.$get("authenticationProviders"); diff --git a/server/commands/userProvisioner.ts b/server/commands/userProvisioner.ts index e4a7b963c6..0d3ad8f51f 100644 --- a/server/commands/userProvisioner.ts +++ b/server/commands/userProvisioner.ts @@ -8,7 +8,12 @@ import { InviteRequiredError, } from "@server/errors"; import Logger from "@server/logging/Logger"; -import { Team, User, UserAuthentication } from "@server/models"; +import { + AuthenticationProvider, + Team, + User, + UserAuthentication, +} from "@server/models"; import { sequelize } from "@server/storage/database"; import type { APIContext } from "@server/types"; import { UserFlag } from "@server/models/User"; @@ -73,22 +78,47 @@ export default async function userProvisioner( authentication, }: Props ): Promise { - const auth = authentication - ? await UserAuthentication.findOne({ + // The provider being signed in with, external user identifiers are only + // unique within the namespace of a single provider so it must be known + // before an existing authentication record can be matched. + const provider = authentication + ? await AuthenticationProvider.findOne({ where: { - providerId: String(authentication.providerId), + id: authentication.authenticationProviderId, + teamId, }, - include: [ - { - model: User, - as: "user", - where: { teamId }, - required: true, - }, - ], }) : undefined; + if (authentication && !provider) { + throw InvalidAuthenticationError(); + } + + const auth = + authentication && provider + ? await UserAuthentication.findOne({ + where: { + providerId: String(authentication.providerId), + }, + include: [ + { + model: User, + as: "user", + where: { teamId }, + required: true, + }, + { + // Restrict matching to the same type of provider, an identifier + // from one provider must never match a record from another. + model: AuthenticationProvider, + as: "authenticationProvider", + where: { name: provider.name }, + required: true, + }, + ], + }) + : undefined; + // Someone has signed in with this authentication before, we just // want to update the details instead of creating a new record if (auth && authentication) {