-
-
Notifications
You must be signed in to change notification settings - Fork 23.7k
Create basic guardrails for GraphCypherQAChain #5748
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?
Conversation
Summary of ChangesHello @christopherholland-workday, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the security and robustness of the Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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 Review
This pull request introduces important security guardrails for the GraphCypherQAChain, including input sanitization, prompt injection detection, validation of generated Cypher queries to prevent write operations, and limiting query results to prevent data exfiltration. The changes are well-implemented and include a comprehensive suite of tests. My feedback focuses on improving the robustness of the prompt injection heuristics to avoid false positives and making hardcoded limits configurable for better flexibility.
| // Check for excessive special characters (potential obfuscation) | ||
| const specialCharCount = (input.match(/[{}()[\];|&$`\\]/g) || []).length | ||
| if (specialCharCount > 5) { | ||
| return true | ||
| } | ||
|
|
||
| // Check for suspicious Cypher keywords in close proximity | ||
| const cypherKeywords = ['MATCH', 'CREATE', 'MERGE', 'DELETE', 'DETACH', 'SET', 'REMOVE', 'RETURN', 'WHERE', 'WITH'] | ||
| const foundKeywords = cypherKeywords.filter((keyword) => lowerInput.includes(keyword.toLowerCase())) | ||
| if (foundKeywords.length >= 3) { | ||
| return true | ||
| } |
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.
The heuristic checks for special characters and keyword clustering are a good idea for defense-in-depth, but the current thresholds might be too sensitive and lead to false positives for legitimate user questions, especially if they are asking about code or Cypher queries.
- Special Character Count: The limit of 5 special characters (
/[{}()[\];|&$`]/g`) is quite low. A user asking a question about a JSON object or a code snippet could easily exceed this. - Keyword Clustering: The check for 3 or more Cypher keywords can be triggered by natural language questions about how to use Cypher.
I recommend increasing these thresholds (e.g., 10 for special characters, 4 for keywords) or making the logic more sophisticated to reduce false positives. For example, you could check for keywords in combination with query-like structures rather than just their presence.
| const cypherModel = nodeData.inputs?.cypherModel | ||
| const qaModel = nodeData.inputs?.qaModel | ||
| const graph = nodeData.inputs?.graph | ||
| const maxResults = 100 // Hardcoded limit to prevent data exfiltration |
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.
The maxResults limit is a great security measure to prevent data exfiltration. However, hardcoding it to 100 might be too restrictive for some use cases. I suggest making this a configurable parameter on the node (by adding it to the inputs array in the constructor), with 100 as the default value. This would allow administrators to adjust it based on their specific needs while still providing a safe default.
| const maxResults = 100 // Hardcoded limit to prevent data exfiltration | |
| const maxResults = (nodeData.inputs?.maxResults as number) ?? 100 // Hardcoded limit to prevent data exfiltration |
| const chain = nodeData.instance as GraphCypherQAChain | ||
| const moderations = nodeData.inputs?.inputModeration as Moderation[] | ||
| const returnDirect = nodeData.inputs?.returnDirect as boolean | ||
| const maxInputLength = 2000 // Hardcoded limit to prevent abuse |
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.
Similar to maxResults, the maxInputLength is hardcoded. While this is a good guardrail against abuse, making it configurable would provide more flexibility. I recommend exposing this as a node parameter (by adding it to the inputs array in the constructor) with a default of 2000.
| const maxInputLength = 2000 // Hardcoded limit to prevent abuse | |
| const maxInputLength = (nodeData.inputs?.maxInputLength as number) ?? 2000 // Hardcoded limit to prevent abuse |
Create basic guardrails for GraphCypherQAChain, this change does the following: