-
Notifications
You must be signed in to change notification settings - Fork 106
Security: Sanitize Lightspeed Core error responses #3296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@red-hat-developer-hub/backstage-plugin-lightspeed-backend': patch | ||
| --- | ||
|
|
||
| Security: Sanitize LCS error responses to prevent information disclosure |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { sanitizeLCSError } from './utils'; | ||
|
|
||
| describe('sanitizeLCSError', () => { | ||
| const mockLogger = { | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| info: jest.fn(), | ||
| debug: jest.fn(), | ||
| child: jest.fn(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should return generic error message to client', () => { | ||
| const errorBody = { | ||
| error: { | ||
| message: | ||
| 'Model gpt-4-0613 failed with OpenAI API error: rate limit exceeded', | ||
| }, | ||
| detail: { | ||
| cause: 'OpenAIError: Rate limit reached', | ||
| }, | ||
| }; | ||
|
|
||
| const result = sanitizeLCSError(errorBody, mockLogger, 'processing query'); | ||
|
|
||
| expect(result).toBe( | ||
| 'Error from lightspeed-core server while processing query', | ||
| ); | ||
| }); | ||
|
|
||
| it('should log full error details server-side', () => { | ||
| const errorBody = { | ||
| error: { | ||
| message: 'Database connection failed', | ||
| }, | ||
| detail: { | ||
| cause: 'PostgreSQL connection timeout', | ||
| }, | ||
| }; | ||
|
|
||
| sanitizeLCSError(errorBody, mockLogger, 'sending feedback'); | ||
|
|
||
| expect(mockLogger.error).toHaveBeenCalledWith( | ||
| `Error from lightspeed-core server while sending feedback: ${JSON.stringify(errorBody)}`, | ||
| ); | ||
| }); | ||
|
|
||
| it('should not expose internal details in return value', () => { | ||
| const errorBody = { | ||
| error: { | ||
| message: | ||
| 'Model gpt-4-0613 failed with organization org-abc123 rate limit', | ||
| }, | ||
| detail: { | ||
| cause: 'OpenAIError', | ||
| provider: 'openai', | ||
| model_id: 'gpt-4-0613', | ||
| }, | ||
| }; | ||
|
|
||
| const result = sanitizeLCSError( | ||
| errorBody, | ||
| mockLogger, | ||
| 'interrupting query', | ||
| ); | ||
|
|
||
| expect(result).not.toContain('gpt-4'); | ||
| expect(result).not.toContain('org-abc123'); | ||
| expect(result).not.toContain('openai'); | ||
| expect(result).not.toContain('OpenAIError'); | ||
| }); | ||
|
|
||
| it('should handle empty error body', () => { | ||
| const errorBody = {}; | ||
|
|
||
| const result = sanitizeLCSError( | ||
| errorBody, | ||
| mockLogger, | ||
| 'updating conversation', | ||
| ); | ||
|
|
||
| expect(result).toBe( | ||
| 'Error from lightspeed-core server while updating conversation', | ||
| ); | ||
| expect(mockLogger.error).toHaveBeenCalled(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright Red Hat, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { LoggerService } from '@backstage/backend-plugin-api'; | ||
|
|
||
| /** | ||
| * Interface for Lightspeed Core Service error response structure | ||
| */ | ||
| export interface LCSErrorResponse { | ||
| error?: { | ||
| message?: string; | ||
| }; | ||
| detail?: { | ||
| cause?: string; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Sanitizes Lightspeed Core Service (LCS) error responses to prevent | ||
| * information disclosure. Logs full error details server-side for debugging | ||
| * while returning only generic messages to clients. | ||
| * | ||
| * @param errorBody - The error response body from LCS | ||
| * @param logger - Logger instance for server-side logging | ||
| * @param context - Context string describing the operation (e.g., "sending feedback") | ||
| * @returns Generic error message safe to return to clients | ||
| */ | ||
| export function sanitizeLCSError( | ||
| errorBody: LCSErrorResponse, | ||
| logger: LoggerService, | ||
| context: string, | ||
| ): string { | ||
| // Log full error details server-side for debugging | ||
| logger.error( | ||
| `Error from lightspeed-core server while ${context}: ${JSON.stringify(errorBody)}`, | ||
| ); | ||
|
|
||
| // Return only generic message to client (no internal LCS details) | ||
| return `Error from lightspeed-core server while ${context}`; | ||
| } | ||
|
|
||
| /** | ||
| * Handles LCS fetch errors by sanitizing the error response and sending it to the client. | ||
| * This helper eliminates code duplication across multiple endpoints. | ||
| * | ||
| * @param fetchResponse - The failed fetch response from LCS | ||
| * @param logger - Logger instance for server-side logging | ||
| * @param context - Context string describing the operation | ||
| * @param response - Express response object | ||
| */ | ||
| export async function handleLCSFetchError( | ||
| fetchResponse: Response, | ||
| logger: LoggerService, | ||
| context: string, | ||
| response: any, | ||
| ): Promise<void> { | ||
| const errorBody = await fetchResponse.json(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's wrap this in a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then let's add a test for this case as well |
||
| const sanitizedError = sanitizeLCSError(errorBody, logger, context); | ||
| response.status(fetchResponse.status).json({ error: sanitizedError }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to type this to
express.Responseor is ts being weird about it and that is why it'sany?