Skip to content

Conversation

xiangyan99
Copy link
Member

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

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Updated servers/Azure.Mcp.Server/CHANGELOG.md and/or servers/Fabric.Mcp.Server/CHANGELOG.md for product changes (features, bug fixes, UI/UX, updated dependencies)
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Updated command list in /docs/azmcp-commands.md and/or /docs/fabric-commands.md
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
  • Extra steps for Azure MCP Server tool changes:
    • Updated test prompts in /docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

@xiangyan99 xiangyan99 marked this pull request as ready for review September 12, 2025 20:07
@xiangyan99 xiangyan99 requested a review from a team as a code owner September 12, 2025 20:07
Copy link
Contributor

@Copilot Copilot AI left a 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 =
Copy link
Contributor

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

Copy link
Member Author

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",
Copy link
Contributor

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",
Copy link
Contributor

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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\\COPY doesn't exist

charris-msft pushed a commit to charris-msft/mcp that referenced this pull request Sep 16, 2025
* Establish Area pattern and update all areas
* Move resources and update codeowners
@joshfree joshfree moved this from Untriaged to In Progress in Azure MCP Server Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

4 participants