-
Notifications
You must be signed in to change notification settings - Fork 865
Tool injector guardrail #1297
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
Tool injector guardrail #1297
Conversation
Co-authored-by: matter-code-review[bot] <150888575+matter-code-review[bot]@users.noreply.github.com>
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.
Code quality is good overall with proper TypeScript types and error handling. Found several potential runtime bugs and areas for improvement.
Skipped files
plugins/default/manifest.json: Skipped file pattern
| ): Record<string, any> => { | ||
| const json = context.request.json; | ||
| const updatedJson = { ...json }; | ||
| const messages = Array.isArray(json.messages) ? [...json.messages] : []; |
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.
🐛 Bug Fix
Issue: Potential null pointer exception when json.messages is null/undefined
Fix: Add null check before array operations
Impact: Prevents runtime crashes when messages property is missing
| const messages = Array.isArray(json.messages) ? [...json.messages] : []; | |
| const messages = Array.isArray(json?.messages) ? [...json.messages] : []; |
| const isEmptyContent = | ||
| (typeof content === 'string' && content.trim().length === 0) || | ||
| (Array.isArray(content) && content.length === 0); |
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.
🐛 Bug Fix
Issue: Logical OR operator creates incorrect boolean condition - will always be true
Fix: Use logical AND operator for proper empty content detection
Impact: Fixes empty content detection logic to work correctly
| const isEmptyContent = | |
| (typeof content === 'string' && content.trim().length === 0) || | |
| (Array.isArray(content) && content.length === 0); | |
| const isEmptyContent = | |
| (typeof content === 'string' && content.trim().length === 0) || | |
| (Array.isArray(content) && content.length === 0) || | |
| content == null; |
| const existingTools: AnyTool[] = Array.isArray(originalJson.tools) | ||
| ? [...originalJson.tools] | ||
| : []; |
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.
🐛 Bug Fix
Issue: Potential null pointer exception when accessing originalJson.tools
Fix: Add null check before array operations
Impact: Prevents runtime crashes when tools property is missing
| const existingTools: AnyTool[] = Array.isArray(originalJson.tools) | |
| ? [...originalJson.tools] | |
| : []; | |
| const existingTools: AnyTool[] = Array.isArray(originalJson?.tools) | |
| ? [...originalJson.tools] | |
| : []; |
| const hasExistingToolChoice = | ||
| (originalJson as Record<string, unknown>)?.tool_choice !== undefined && | ||
| (originalJson as Record<string, unknown>)?.tool_choice !== null; |
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.
🛠️ Code Refactor
Issue: Complex type assertions make code hard to read and maintain
Fix: Extract type definitions for better readability
Impact: Improves code maintainability and type safety
| const hasExistingToolChoice = | |
| (originalJson as Record<string, unknown>)?.tool_choice !== undefined && | |
| (originalJson as Record<string, unknown>)?.tool_choice !== null; | |
| type JsonWithToolChoice = Record<string, unknown> & { | |
| tool_choice?: unknown; | |
| }; | |
| const hasExistingToolChoice = | |
| (originalJson as JsonWithToolChoice)?.tool_choice !== undefined && | |
| (originalJson as JsonWithToolChoice)?.tool_choice !== null; |
| const exists = nextTools.some( | ||
| (t) => | ||
| t && | ||
| (t as { type?: unknown; function?: { name?: unknown } }).type === | ||
| 'function' && | ||
| (t as { function?: { name?: unknown } }).function?.name === | ||
| requiredFunctionName | ||
| ); |
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.
🛠️ Code Refactor
Issue: Deeply nested type assertions are hard to read and error-prone
Fix: Extract type guard function for tool validation
Impact: Improves code readability and reduces type assertion complexity
| const exists = nextTools.some( | |
| (t) => | |
| t && | |
| (t as { type?: unknown; function?: { name?: unknown } }).type === | |
| 'function' && | |
| (t as { function?: { name?: unknown } }).function?.name === | |
| requiredFunctionName | |
| ); | |
| const isValidFunctionTool = (t: AnyTool): boolean => { | |
| return t?.type === 'function' && | |
| typeof t.function === 'object' && | |
| t.function?.name === requiredFunctionName; | |
| }; | |
| const exists = nextTools.some(isValidFunctionTool); |
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.
Why was this changed?
|
Please reopen if needed |
Description
Motivation
Type of Change
How Has This Been Tested?
Screenshots (if applicable)
Checklist
Related Issues