fix: Scope authentication matching to the signing-in provider (#13737)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-09-14 18:34:08 -04:00
1 parent f275632f0a
commit 0cda530e15
2 files changed
+77 -12

No files matched your search

+35
View File
@@ -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");
+42 -12
View File
@@ -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<UserProvisionerResult> {
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) {