@@ -192,31 +192,36 @@ export async function POST(request: NextRequest) {
192192
193193 const discovery = ( await discoveryResponse . json ( ) ) as Record < string , unknown >
194194
195- oidcConfig . authorizationEndpoint =
196- oidcConfig . authorizationEndpoint || discovery . authorization_endpoint
197- oidcConfig . tokenEndpoint = oidcConfig . tokenEndpoint || discovery . token_endpoint
198- oidcConfig . userInfoEndpoint = oidcConfig . userInfoEndpoint || discovery . userinfo_endpoint
199- oidcConfig . jwksEndpoint = oidcConfig . jwksEndpoint || discovery . jwks_uri
195+ const discoveredEndpoints : Record < string , unknown > = {
196+ authorization_endpoint : discovery . authorization_endpoint ,
197+ token_endpoint : discovery . token_endpoint ,
198+ userinfo_endpoint : discovery . userinfo_endpoint ,
199+ jwks_uri : discovery . jwks_uri ,
200+ }
200201
201- // Validate discovered endpoints against SSRF — these are fetched server-side
202- const endpointsToValidate = [
203- { name : 'tokenEndpoint' , url : oidcConfig . tokenEndpoint } ,
204- { name : 'userInfoEndpoint' , url : oidcConfig . userInfoEndpoint } ,
205- { name : 'jwksEndpoint' , url : oidcConfig . jwksEndpoint } ,
206- ]
207- for ( const { name, url } of endpointsToValidate ) {
208- if ( typeof url === 'string' ) {
209- const result = await validateUrlWithDNS ( url , `OIDC ${ name } ` )
210- if ( ! result . isValid ) {
211- logger . warn ( `Discovered OIDC ${ name } failed SSRF validation` , {
212- url,
213- error : result . error ,
202+ for ( const [ key , value ] of Object . entries ( discoveredEndpoints ) ) {
203+ if ( typeof value === 'string' ) {
204+ const endpointValidation = await validateUrlWithDNS ( value , `OIDC ${ key } ` )
205+ if ( ! endpointValidation . isValid ) {
206+ logger . warn ( 'OIDC discovered endpoint failed SSRF validation' , {
207+ endpoint : key ,
208+ url : value ,
209+ error : endpointValidation . error ,
214210 } )
215- return NextResponse . json ( { error : result . error } , { status : 400 } )
211+ return NextResponse . json (
212+ { error : `Discovered OIDC ${ key } failed security validation: ${ endpointValidation . error } ` } ,
213+ { status : 400 }
214+ )
216215 }
217216 }
218217 }
219218
219+ oidcConfig . authorizationEndpoint =
220+ oidcConfig . authorizationEndpoint || discovery . authorization_endpoint
221+ oidcConfig . tokenEndpoint = oidcConfig . tokenEndpoint || discovery . token_endpoint
222+ oidcConfig . userInfoEndpoint = oidcConfig . userInfoEndpoint || discovery . userinfo_endpoint
223+ oidcConfig . jwksEndpoint = oidcConfig . jwksEndpoint || discovery . jwks_uri
224+
220225 logger . info ( 'Merged OIDC endpoints (user-provided + discovery)' , {
221226 providerId,
222227 issuer,
0 commit comments