-
Notifications
You must be signed in to change notification settings - Fork 182
Improve postgres query #426
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
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.
Pull Request Overview
This PR improves the PostgreSQL query tool by adding comprehensive security validation and query safety features to prevent SQL injection attacks and potential DoS vulnerabilities.
- Added extensive query validation logic with dangerous keyword detection, multiple statement prevention, and query length limits
- Implemented result size limits across database operations to prevent resource exhaustion
- Enhanced test coverage with dedicated test classes for query validation and parameterized query security
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
PostgresService.cs | Added security validation logic, dangerous keyword detection, result limits, and query safety checks |
PostgresServiceQueryValidationTests.cs | Comprehensive test suite covering dangerous queries, SQL injection attempts, and validation edge cases |
PostgresServiceParameterizedQueryTests.cs | Tests for parameterized query security and proper handling of malicious input |
CHANGELOG.md | Updated changelog to document the security improvements |
Comments suppressed due to low confidence (1)
tools/Azure.Mcp.Tools.Postgres/src/Services/PostgresService.cs:254
- This query uses string interpolation instead of parameterized queries, making it vulnerable to SQL injection. The table name should be parameterized using NpgsqlCommand parameters to prevent injection attacks.
var query = $"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table}';";
private const int MaxResultLimit = 10000; | ||
|
||
// Static arrays for security validation - initialized once per class | ||
private static readonly string[] DangerousKeywords = |
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 not use an ALLOW list instead of a DISALLOW list? I don't think we'll ever catch all dangerous keywords
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.
This implementation ported from https://github.com/microsoft/mcp/blob/main/tools/Azure.Mcp.Tools.MySql/src/Services/MySqlService.cs
My understanding is that the "allow by default" approach has more flexibility. While it may be less secure than the "disallow by default" approach, I this this trade-off is justifiable.
// Data manipulation that could be harmful | ||
"DROP", "DELETE", "TRUNCATE", "ALTER", "CREATE", "INSERT", "UPDATE", | ||
// Administrative operations | ||
"GRANT", "REVOKE", "SET", "RESET", "KILL", "SHUTDOWN", "RESTART", |
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.
KILL
and SHUTDOWN
don't exist in Postgres
// Administrative operations | ||
"GRANT", "REVOKE", "SET", "RESET", "KILL", "SHUTDOWN", "RESTART", | ||
// Information disclosure | ||
"SHOW", "EXPLAIN", "ANALYZE", |
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.
Postgres supports ANALYSE
as a synonym to ANALYZE
// Information disclosure | ||
"SHOW", "EXPLAIN", "ANALYZE", | ||
// System operations | ||
"COPY", "\\COPY", "VACUUM", "REINDEX", |
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.
\\COPY
doesn't exist
* Establish Area pattern and update all areas * Move resources and update codeowners
What does this PR do?
[Provide a clear, concise description of the changes]
Improve postgres query tool
[Any additional context, screenshots, or information that helps reviewers]
GitHub issue number?
[Link to the GitHub issue this PR addresses]
Pre-merge Checklist
servers/Azure.Mcp.Server/CHANGELOG.md
and/orservers/Fabric.Mcp.Server/CHANGELOG.md
for product changes (features, bug fixes, UI/UX, updated dependencies
)servers/Azure.Mcp.Server/README.md
and/orservers/Fabric.Mcp.Server/README.md
documentation/docs/azmcp-commands.md
and/or/docs/fabric-commands.md
ToolDescriptionEvaluator
and obtained a score of0.4
or more and a top 3 ranking for all related test prompts/docs/e2eTestPrompts.md
crypto mining, spam, data exfiltration, etc.
)/azp run mcp - pullrequest - live
to run Live Test Pipeline