diff --git a/src/auth/oauth-provider.test.ts b/src/auth/oauth-provider.test.ts index 32126a9f..4fb12e76 100644 --- a/src/auth/oauth-provider.test.ts +++ b/src/auth/oauth-provider.test.ts @@ -1985,7 +1985,7 @@ describe('ExternalOAuthProvider', () => { await provider.handleCallback(mockReq, mockRes); expect(mockRes.status).toHaveBeenCalledWith(500); - expect(mockRes.send).toHaveBeenCalledWith('Internal Server Error during token exchange'); + expect(mockRes.send).toHaveBeenCalledWith(expect.stringMatching(/^Internal Server Error during token exchange \(correlation ID: [a-f0-9-]+\)$/)); }); }); diff --git a/src/auth/oauth-provider.ts b/src/auth/oauth-provider.ts index 981e4aad..a66da933 100644 --- a/src/auth/oauth-provider.ts +++ b/src/auth/oauth-provider.ts @@ -36,6 +36,7 @@ import { PROXY_CREDENTIALS, } from '../core/constants.js'; import { escapeHtmlSafe } from '../validation/validation-utils.js'; +import { generateCorrelationId } from '../core/errors.js'; import { SSRFValidator } from '../security/ssrf-validator.js'; import { parseOAuthMetadataEndpoints } from './oauth-metadata.js'; import { InMemoryClientsStore } from './client-store/in-memory-clients-store.js'; @@ -867,8 +868,9 @@ export class ExternalOAuthProvider implements OAuthServerProvider { res.redirect(clientUrl.toString()); } catch (err) { - this.logger.error('Callback handling failed', err as Error); - res.status(500).send('Internal Server Error during token exchange'); + const correlationId = generateCorrelationId(); + this.logger.error('Callback handling failed', err as Error, { correlationId }); + res.status(500).send(`Internal Server Error during token exchange (correlation ID: ${correlationId})`); } } diff --git a/src/transport/http-transport.test.ts b/src/transport/http-transport.test.ts index f14646e1..d68f6575 100644 --- a/src/transport/http-transport.test.ts +++ b/src/transport/http-transport.test.ts @@ -2340,7 +2340,7 @@ describeIfListen('HttpTransport', () => { .query({ code: 'test-code' }); expect(response.status).toBe(500); - expect(response.text).toBe('OAuth callback failed'); + expect(response.text).toMatch(/^OAuth callback failed \(correlation ID: [a-f0-9-]+\)$/); }); }); diff --git a/src/transport/http-transport.ts b/src/transport/http-transport.ts index 263fea71..096f8b1f 100644 --- a/src/transport/http-transport.ts +++ b/src/transport/http-transport.ts @@ -1814,8 +1814,9 @@ export class HttpTransport { await profileState.oauthProvider.authorize(client, params, res); } catch (error) { - this.logger.error('OAuth authorize error', error instanceof Error ? error : new Error(String(error))); - res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send('OAuth authorization failed'); + const correlationId = generateCorrelationId(); + this.logger.error('OAuth authorize error', error instanceof Error ? error : new Error(String(error)), { correlationId }); + res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send(`OAuth authorization failed (correlation ID: ${correlationId})`); } } @@ -2077,9 +2078,10 @@ export class HttpTransport { await profileState.oauthProvider.handleCallback(req, res); } catch (error) { - this.logger.error('OAuth callback error', error instanceof Error ? error : new Error(String(error))); + const correlationId = generateCorrelationId(); + this.logger.error('OAuth callback error', error instanceof Error ? error : new Error(String(error)), { correlationId }); if (!res.headersSent) { - res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send('OAuth callback failed'); + res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send(`OAuth callback failed (correlation ID: ${correlationId})`); } } } @@ -2113,8 +2115,9 @@ export class HttpTransport { }; res.json(buildAuthorizationServerMetadata(metadata, profileState.context.enterpriseAuthorization)); } catch (error) { - this.logger.error('OAuth authorization server metadata error', error instanceof Error ? error : new Error(String(error))); - res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send('OAuth metadata failed'); + const correlationId = generateCorrelationId(); + this.logger.error('OAuth authorization server metadata error', error instanceof Error ? error : new Error(String(error)), { correlationId }); + res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).send(`OAuth metadata failed (correlation ID: ${correlationId})`); } }