Skip to content

High performance self-hosted photo and video management solution.

fix(oauth): normalize email claim to lowercase and trim before account lookup and registration (#26841)

Timothy Dobrasd046f1686069
diff --git a/e2e/src/specs/server/api/oauth.e2e-spec.ts b/e2e/src/specs/server/api/oauth.e2e-spec.ts
index e4127438f7..98cb28c821 100644
--- a/e2e/src/specs/server/api/oauth.e2e-spec.ts
+++ b/e2e/src/specs/server/api/oauth.e2e-spec.ts
@@ -259,7 +259,7 @@ describe(`/oauth`, () => {
accessToken: expect.any(String),
isAdmin: false,
name: 'OAuth User',
- userEmail: '[email protected]',
+ userEmail: '[email protected]',
userId: expect.any(String),
});
});
diff --git a/server/src/services/auth.service.spec.ts b/server/src/services/auth.service.spec.ts
index 94b8acd25e..a21790f5fe 100644
--- a/server/src/services/auth.service.spec.ts
+++ b/server/src/services/auth.service.spec.ts
@@ -634,6 +634,26 @@ describe(AuthService.name, () => {
expect(mocks.user.update).toHaveBeenCalledWith(user.id, { oauthId: profile.sub });
});
+ it('should normalize the email from the OAuth profile before linking', async () => {
+ const user = UserFactory.create();
+ const profile = OAuthProfileFactory.create({ email: ' [email protected] ' });
+
+ mocks.systemMetadata.get.mockResolvedValue(systemConfigStub.oauthEnabled);
+ mocks.oauth.getProfile.mockResolvedValue(profile);
+ mocks.user.getByEmail.mockResolvedValue(user);
+ mocks.user.update.mockResolvedValue(user);
+ mocks.session.create.mockResolvedValue(SessionFactory.create());
+
+ await sut.callback(
+ { url: 'http://immich/auth/login?code=abc123', state: 'xyz789', codeVerifier: 'foobar' },
+ {},
+ loginDetails,
+ );
+
+ expect(mocks.user.getByEmail).toHaveBeenCalledWith('[email protected]');
+ expect(mocks.user.update).toHaveBeenCalledWith(user.id, { oauthId: profile.sub });
+ });
+
it('should not link to a user with a different oauth sub', async () => {
const user = UserFactory.create({ oauthId: 'existing-sub' });
diff --git a/server/src/services/auth.service.ts b/server/src/services/auth.service.ts
index 498c165888..ea3a896298 100644
--- a/server/src/services/auth.service.ts
+++ b/server/src/services/auth.service.ts
@@ -277,13 +277,14 @@ export class AuthService extends BaseService {
const url = this.resolveRedirectUri(oauth, dto.url);
const profile = await this.oauthRepository.getProfile(oauth, url, expectedState, codeVerifier);
+ const normalizedEmail = profile.email ? profile.email.trim().toLowerCase() : undefined;
const { autoRegister, defaultStorageQuota, storageLabelClaim, storageQuotaClaim, roleClaim } = oauth;
this.logger.debug(`Logging in with OAuth: ${JSON.stringify(profile)}`);
let user: UserAdmin | undefined = await this.userRepository.getByOAuthId(profile.sub);
// link by email
- if (!user && profile.email) {
- const emailUser = await this.userRepository.getByEmail(profile.email);
+ if (!user && normalizedEmail) {
+ const emailUser = await this.userRepository.getByEmail(normalizedEmail);
if (emailUser) {
if (emailUser.oauthId) {
throw new BadRequestException('User already exists, but is linked to another account.');
@@ -296,17 +297,16 @@ export class AuthService extends BaseService {
if (!user) {
if (!autoRegister) {
this.logger.warn(
- `Unable to register ${profile.sub}/${profile.email || '(no email)'}. To enable set OAuth Auto Register to true in admin settings.`,
+ `Unable to register ${profile.sub}/${normalizedEmail || '(no email)'}. To enable set OAuth Auto Register to true in admin settings.`,
);
throw new BadRequestException(`User does not exist and auto registering is disabled.`);
}
- const email = profile.email;
- if (!email) {
+ if (!normalizedEmail) {
throw new BadRequestException('OAuth profile does not have an email address');
}
- this.logger.log(`Registering new user: ${profile.sub}/${profile.email}`);
+ this.logger.log(`Registering new user: ${profile.sub}/${normalizedEmail}`);
const storageLabel = this.getClaim(profile, {
key: storageLabelClaim,
@@ -329,8 +329,8 @@ export class AuthService extends BaseService {
profile.name ||
`${profile.given_name || ''} ${profile.family_name || ''}`.trim() ||
profile.preferred_username ||
- email,
- email,
+ normalizedEmail,
+ email: normalizedEmail,
oauthId: profile.sub,
quotaSizeInBytes: storageQuota === null ? null : storageQuota * HumanReadableSize.GiB,
storageLabel: storageLabel || null,