fix(security): add SQL injection prevention to ExplainPlanTool#161
Open
RajatRaiD11 wants to merge 2 commits intocrystaldba:mainfrom
Open
fix(security): add SQL injection prevention to ExplainPlanTool#161RajatRaiD11 wants to merge 2 commits intocrystaldba:mainfrom
RajatRaiD11 wants to merge 2 commits intocrystaldba:mainfrom
Conversation
Add _validate_explain_input() using pglast to parse and validate SQL before f-string concatenation. Prevents multi-statement injection and restricts EXPLAIN ANALYZE to SELECT-only.
17 unit tests + 5 integration tests covering valid queries, multi-statement injection rejection, empty/invalid input, EXPLAIN ANALYZE DML restrictions, and COPY exfiltration prevention.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
explain_querytool is annotated withreadOnlyHint=True, but in UNRESTRICTED mode (default), user-supplied SQL is concatenated via f-string into EXPLAIN queries without validation. This allows:SELECT 1; DROP TABLE userspasses throughf"EXPLAIN (FORMAT JSON) {sql_input}"and reachescursor.execute()which supports multi-statement execution viacursor.nextset()EXPLAIN ANALYZE DELETE FROM usersactually executes the DELETE, but the tool doesn't restrict thisWhat this PR does
Adds
_validate_explain_input()static method usingpglast(already a project dependency) to:Integrates validation into
explain(),explain_with_hypothetical_indexes(), andgenerate_explain_plan_with_hypothetical_indexes().What the codebase already gets right
SafeSqlDriverprovides excellent pglast-based validation in RESTRICTED modepsycopg.sql.Literal()is used correctly forIndexDefinition(not exploitable)This fix extends the same validation approach to UNRESTRICTED mode for the EXPLAIN tool specifically.
Files changed
src/postgres_mcp/explain/explain_plan.py— adds_validate_explain_input()and integrates it (57 additions, 0 deletions)tests/unit/explain/test_explain_input_validation.py— 22 test cases (17 unit + 5 integration)Test coverage
explain(),explain_analyze(), andexplain_with_hypothetical_indexes()block injection and never send malicious SQL to the database