Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:auth): harden OIDC avatar synchronization
johaven5024afa7c0ca
diff --git a/backend/src/authentication/providers/oidc/auth-oidc.config.ts b/backend/src/authentication/providers/oidc/auth-oidc.config.tsindex 696f40c9..633b6df5 100644--- a/backend/src/authentication/providers/oidc/auth-oidc.config.ts+++ b/backend/src/authentication/providers/oidc/auth-oidc.config.ts@@ -44,6 +44,10 @@ export class AuthProviderOIDCSecurityConfig {@IsOptional()@IsBoolean()skipSubjectCheck? = false++ @IsOptional()+ @IsBoolean()+ allowPrivateIpAvatarDownload? = false}export class AuthProviderOIDCOptionsConfig {@@ -64,6 +68,10 @@ export class AuthProviderOIDCOptionsConfig {@IsBoolean()enablePasswordAuth? = true+ @IsOptional()+ @IsBoolean()+ autoSyncAvatar? = false+@IsOptional()@IsString()adminRoleOrGroup?: stringdiff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.tsindex f6bfc550..a1672afb 100644--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts@@ -40,11 +40,13 @@ vi.mock('../../../configuration/config.environment', () => ({userInfoSigningAlg: 'RS256',tokenEndpointAuthMethod: 'client_secret_basic',allowInsecureRequests: false,- skipSubjectCheck: false+ skipSubjectCheck: false,+ allowPrivateIpAvatarDownload: false},options: {enablePasswordAuth: false,autoCreateUser: true,+ autoSyncAvatar: false,adminRoleOrGroup: 'admins',autoCreatePermissions: ['read']}@@ -149,6 +151,8 @@ describe(AuthProviderOIDC.name, () => {;(service as any).config = null;(service as any).oidcConfig.security.supportPKCE = true;(service as any).oidcConfig.security.allowInsecureRequests = false+ ;(service as any).oidcConfig.security.allowPrivateIpAvatarDownload = false+ ;(service as any).oidcConfig.options.autoSyncAvatar = false})it('returns null when user is not found', async () => {@@ -327,6 +331,31 @@ describe(AuthProviderOIDC.name, () => {expect(result.role).toBe(USER_ROLE.ADMINISTRATOR)})+ it('does not sync the user avatar by default', async () => {+ const existingUser = { id: 20, login: 'alice', email: '[email protected]', role: USER_ROLE.USER, setFullName: vi.fn() } as any+ usersManager.findUser.mockResolvedValue(existingUser)+ const updatePictureUrlSpy = vi.spyOn(service as any, 'updatePictureUrl').mockResolvedValue(undefined)++ await (service as any).processUserInfo(+ { sub: 'x', email: '[email protected]', preferred_username: 'alice', picture: 'https://cdn.example.test/avatar.jpg' },+ '127.0.0.1'+ )++ expect(updatePictureUrlSpy).not.toHaveBeenCalled()+ })++ it('syncs the user avatar when enabled', async () => {+ ;(service as any).oidcConfig.options.autoSyncAvatar = true+ const existingUser = { id: 21, login: 'alice', email: '[email protected]', role: USER_ROLE.USER, setFullName: vi.fn() } as any+ const userInfo = { sub: 'x', email: '[email protected]', preferred_username: 'alice', picture: 'https://cdn.example.test/avatar.jpg' }+ usersManager.findUser.mockResolvedValue(existingUser)+ const updatePictureUrlSpy = vi.spyOn(service as any, 'updatePictureUrl').mockResolvedValue(undefined)++ await (service as any).processUserInfo(userInfo, '127.0.0.1')++ expect(updatePictureUrlSpy).toHaveBeenCalledWith(existingUser, userInfo)+ })+it('handles storage quota claim mapping cases', async () => {const scenarios = [{@@ -486,12 +515,43 @@ describe(AuthProviderOIDC.name, () => {2,expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }),'/tmp/sync-in/alice/tmp/avatar.png',- { allowPrivateIP: true, maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE }+ { allowPrivateIP: false, maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE })expect(convertSpy).toHaveBeenCalledWith('/tmp/sync-in/alice/tmp/avatar.png', '/tmp/sync-in/users/alice/avatar.png')expect(metadataSpy).toHaveBeenCalledWith('alice', 'https://cdn.example.test/avatar.jpg', 128, 'Mon, 01 Jan 2024 00:00:00 GMT')})+ it('allows private IP avatar downloads when explicitly enabled', async () => {+ ;(service as any).oidcConfig.security.allowPrivateIpAvatarDownload = true+ const downloadSpy = vi+ .spyOn(DownloadFile.prototype, 'download')+ .mockResolvedValueOnce({+ contentType: 'image/png',+ contentLength: 128,+ lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT'+ } as any)+ .mockResolvedValueOnce(undefined as any)+ vi.spyOn(avatarUtils, 'isAvatarMetadataUnchanged').mockResolvedValue(false)+ vi.spyOn(filesUtils, 'fileSize').mockResolvedValue(1024)+ vi.spyOn(UserModel, 'getHomePath').mockReturnValue('/tmp/sync-in/users/alice')+ vi.spyOn(imageUtils, 'convertTempImageToPng').mockResolvedValue(undefined)++ await (service as any).updatePictureUrl(oidcUser, userInfo())++ expect(downloadSpy).toHaveBeenNthCalledWith(+ 1,+ expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }),+ '/tmp/sync-in/alice/tmp/avatar.png',+ { allowPrivateIP: true, getContentInfo: true }+ )+ expect(downloadSpy).toHaveBeenNthCalledWith(+ 2,+ expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }),+ '/tmp/sync-in/alice/tmp/avatar.png',+ { allowPrivateIP: true, maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE }+ )+ })+it('downloads avatar when content length is missing and stores the actual downloaded size', async () => {const downloadSpy = vi.spyOn(DownloadFile.prototype, 'download')diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.tsindex 393cfd23..8713655c 100644--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts@@ -327,7 +327,9 @@ export class AuthProviderOIDC implements AuthProvider {// Create or update useruser = await this.updateOrCreateUser(identity, user)// Update picture url (if it exists)- await this.updatePictureUrl(user, userInfo)+ if (this.oidcConfig.options.autoSyncAvatar) {+ await this.updatePictureUrl(user, userInfo)+ }// Update user access logthis.usersManager.updateAccesses(user, ip, true).catch((e: Error) => this.logger.error({ tag: this.processUserInfo.name, msg: `${e}` }))@@ -446,11 +448,12 @@ export class AuthProviderOIDC implements AuthProvider {let pictureContentLength: number | undefinedlet pictureLastModified: string | undefinedconst downloader = new DownloadFile(this.http)+ const allowPrivateIP = this.oidcConfig.security.allowPrivateIpAvatarDownloadtry {const tmpPicturePath = path.join(user.tmpPath, USER_AVATAR_FILE_NAME)// retrieve headersconst { contentType, contentLength, lastModified } = await downloader.download(downloadDto, tmpPicturePath, {- allowPrivateIP: true, // trust the url source+ allowPrivateIP,getContentInfo: true})pictureContentLength = contentLength ?? undefined@@ -478,10 +481,10 @@ export class AuthProviderOIDC implements AuthProvider {this.logger.warn({ tag: this.updatePictureUrl.name, msg: `checks failed: ${e}` })}- // download avatar (trust the url source)+ // download avatarconst userAvatarTmpPath = path.join(user.tmpPath, USER_AVATAR_FILE_NAME)try {- await downloader.download(downloadDto, userAvatarTmpPath, { allowPrivateIP: true, maxSize: USER_AVATAR_MAX_UPLOAD_SIZE })+ await downloader.download(downloadDto, userAvatarTmpPath, { allowPrivateIP, maxSize: USER_AVATAR_MAX_UPLOAD_SIZE })} catch (e) {this.logger.warn({ tag: this.updatePictureUrl.name, msg: `download failed: ${e}` })returndiff --git a/environment/environment.dist.yaml b/environment/environment.dist.yamlindex e6c6315f..42a2ff9a 100755--- a/environment/environment.dist.yaml+++ b/environment/environment.dist.yaml@@ -267,6 +267,9 @@ auth:# Users must already exist locally and have a password set.# default: trueenablePasswordAuth: true+ # autoSyncAvatar: Automatically download and synchronize the user avatar from the OIDC `picture` claim.+ # default: false+ autoSyncAvatar: false# autoRedirect: Automatically redirect users to the OIDC login flow.# When enabled, the login page is skipped and users are sent directly to the OIDC provider.# default: false@@ -309,6 +312,10 @@ auth:# Set to true only for non-compliant or legacy OIDC providers.# default: falseskipSubjectCheck: false+ # allowPrivateIpAvatarDownload: Allow OIDC avatar downloads from private/internal IP ranges.+ # Set to true only when the OIDC provider returns trusted internal avatar URLs.+ # default: false+ allowPrivateIpAvatarDownload: falseapplications:users:# showUngroupedUsers: make users without a group globally visible to regular users/admins.