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): disable insecure OIDC requests by default
johaven9e59a09e7aec
diff --git a/backend/src/authentication/providers/oidc/auth-oidc.config.ts b/backend/src/authentication/providers/oidc/auth-oidc.config.tsindex 977e4237..696f40c9 100644--- a/backend/src/authentication/providers/oidc/auth-oidc.config.ts+++ b/backend/src/authentication/providers/oidc/auth-oidc.config.ts@@ -25,6 +25,10 @@ export class AuthProviderOIDCSecurityConfig {@IsBoolean()supportPKCE? = true+ @IsOptional()+ @IsBoolean()+ allowInsecureRequests? = false+@Transform(({ value }) => value || OAuthTokenEndpoint.ClientSecretBasic)@IsEnum(OAuthTokenEndpoint)tokenEndpointAuthMethod: OAuthTokenEndpoint = OAuthTokenEndpoint.ClientSecretBasicdiff --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 807daf77..f6bfc550 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@@ -2,9 +2,11 @@ import { HttpStatus } from '@nestjs/common'import { HttpService } from '@nestjs/axios'import { Test, TestingModule } from '@nestjs/testing'import {+ allowInsecureRequests,authorizationCodeGrant,AuthorizationResponseError,calculatePKCECodeChallenge,+ discovery,fetchUserInfo,randomNonce,randomPKCECodeVerifier,@@ -37,6 +39,7 @@ vi.mock('../../../configuration/config.environment', () => ({tokenSigningAlg: 'RS256',userInfoSigningAlg: 'RS256',tokenEndpointAuthMethod: 'client_secret_basic',+ allowInsecureRequests: false,skipSubjectCheck: false},options: {@@ -110,6 +113,8 @@ describe(AuthProviderOIDC.name, () => {clearCookie: vi.fn()})+ const codedError = (message: string, code: string) => Object.assign(new Error(message), { code })+beforeAll(async () => {usersManager = {findUser: vi.fn(),@@ -141,6 +146,9 @@ describe(AuthProviderOIDC.name, () => {beforeEach(() => {vi.restoreAllMocks()vi.clearAllMocks()+ ;(service as any).config = null+ ;(service as any).oidcConfig.security.supportPKCE = true+ ;(service as any).oidcConfig.security.allowInsecureRequests = false})it('returns null when user is not found', async () => {@@ -164,6 +172,44 @@ describe(AuthProviderOIDC.name, () => {expect(result).toBe(guestUser)})+ it('does not allow insecure OIDC requests by default during discovery', async () => {+ vi.mocked(discovery).mockResolvedValue(makeConfig(true) as any)++ await service.getConfig()++ const discoveryOptions = vi.mocked(discovery).mock.calls[0][4] as Record<string, unknown>+ expect(discoveryOptions).toEqual(expect.objectContaining({ timeout: 6000 }))+ expect(discoveryOptions).not.toHaveProperty('execute')+ })++ it('allows insecure OIDC requests during discovery when explicitly enabled', async () => {+ ;(service as any).oidcConfig.security.allowInsecureRequests = true+ vi.mocked(discovery).mockResolvedValue(makeConfig(true) as any)++ await service.getConfig()++ const discoveryOptions = vi.mocked(discovery).mock.calls[0][4] as Record<string, unknown>+ expect(discoveryOptions).toEqual(expect.objectContaining({ execute: [allowInsecureRequests], timeout: 6000 }))+ })++ it('maps insecure OIDC discovery requests to BAD_REQUEST', async () => {+ vi.mocked(discovery).mockRejectedValue(codedError('only requests to HTTPS are allowed', 'OAUTH_HTTP_REQUEST_FORBIDDEN'))++ await expect(service.getConfig()).rejects.toMatchObject({+ status: HttpStatus.BAD_REQUEST,+ message: 'OIDC issuer URL must use HTTPS unless allowInsecureRequests is enabled'+ })+ })++ it('maps generic OAuth discovery errors to BAD_REQUEST', async () => {+ vi.mocked(discovery).mockRejectedValue(codedError('unexpected HTTP response status code', 'OAUTH_RESPONSE_IS_NOT_CONFORM'))++ await expect(service.getConfig()).rejects.toMatchObject({+ status: HttpStatus.BAD_REQUEST,+ message: 'OIDC provider configuration error'+ })+ })+it('builds the authorization url with PKCE data and cookies', async () => {vi.spyOn(service, 'getConfig').mockResolvedValue(makeConfig(true) as any)vi.mocked(randomState).mockReturnValue('state-1')diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.tsindex ea58f7d3..393cfd23 100644--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts@@ -248,7 +248,7 @@ export class AuthProviderOIDC implements AuthProvider {},this.getTokenAuthMethod(this.oidcConfig.security.tokenEndpointAuthMethod, this.oidcConfig.clientSecret),{- execute: [allowInsecureRequests],+ ...(this.oidcConfig.security.allowInsecureRequests ? { execute: [allowInsecureRequests] } : {}),timeout: 6000})@@ -256,17 +256,29 @@ export class AuthProviderOIDC implements AuthProvider {return config} catch (error) {this.logger.error({ tag: this.initializeOIDCClient.name, msg: `OIDC client initialization failed: ${error?.cause || error}` })- switch (error.cause?.code) {- case 'ECONNREFUSED':- case 'ENOTFOUND':- throw new HttpException('OIDC provider unavailable', HttpStatus.SERVICE_UNAVAILABLE)+ throw this.mapOIDCInitializationError(error)+ }+ }- case 'ETIMEDOUT':- throw new HttpException('OIDC provider timeout', HttpStatus.GATEWAY_TIMEOUT)+ private mapOIDCInitializationError(error: any): HttpException {+ const code = error?.code ?? error?.cause?.code+ switch (code) {+ case 'OAUTH_HTTP_REQUEST_FORBIDDEN':+ return new HttpException('OIDC issuer URL must use HTTPS unless allowInsecureRequests is enabled', HttpStatus.BAD_REQUEST)- default:- throw new HttpException('OIDC client initialization failed', HttpStatus.INTERNAL_SERVER_ERROR)- }+ case 'OAUTH_TIMEOUT':+ case 'ETIMEDOUT':+ return new HttpException('OIDC provider timeout', HttpStatus.GATEWAY_TIMEOUT)++ case 'ECONNREFUSED':+ case 'ENOTFOUND':+ return new HttpException('OIDC provider unavailable', HttpStatus.SERVICE_UNAVAILABLE)++ default:+ if (typeof code === 'string' && code.startsWith('OAUTH_')) {+ return new HttpException('OIDC provider configuration error', HttpStatus.BAD_REQUEST)+ }+ return new HttpException('OIDC client initialization failed', HttpStatus.INTERNAL_SERVER_ERROR)}}diff --git a/environment/environment.dist.yaml b/environment/environment.dist.yamlindex 6ade1160..e6c6315f 100755--- a/environment/environment.dist.yaml+++ b/environment/environment.dist.yaml@@ -283,6 +283,10 @@ auth:# When true, PKCE is used if supported by the OIDC provider.# default: truesupportPKCE: true+ # allowInsecureRequests: Allow OIDC discovery and token requests over insecure HTTP.+ # Set to true only for local development or trusted legacy providers.+ # default: false+ allowInsecureRequests: false# OAuth 2.0 / OIDC client authentication method used at the token endpoint.# Possible values:# - client_secret_basic (DEFAULT): HTTP Basic auth using client_id and client_secret.