Fix: Restrict write_query to INSERT, UPDATE, DELETE #1343
Closed
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.
Fixes a vulnerability in the sqlite server's
write_querytool by enforcing stricter SQL command validation.Description
The
write_querytool was intended to only executeINSERT,UPDATE, orDELETEstatements as per the documentation. However, the existing validation only blockedSELECTstatements, leaving the endpoint open to other potentially harmful or unintended SQL commands (e.g.,PRAGMA,ATTACH,DROP). This change updates the validation logic to strictly allow only queries starting withINSERT,UPDATE, orDELETE(case-insensitive).Server Details
sqlitetools(specifically, thewrite_querytool implementation)Motivation and Context
This change is needed to address a security vulnerability where the
write_queryendpoint could execute arbitrary SQL commands beyond the intended scope (INSERT,UPDATE,DELETE). It aligns the actual behavior with the documented functionality and prevents potential misuse.How Has This Been Tested?
The code change directly addresses the identified vulnerability by implementing stricter input validation. Manual testing confirmed that only
INSERT,UPDATE, andDELETEqueries are now accepted. Further testing with an LLM client could be performed to verify behavior in integrated scenarios. Tested scenarios:INSERT,UPDATE,DELETEqueries (should succeed).SELECTqueries (should fail with ValueError).CREATE TABLE,DROP TABLE,PRAGMA(should fail with ValueError).Breaking Changes
This change is non-breaking for clients correctly using the
write_querytool according to documentation (i.e., only sendingINSERT,UPDATE, orDELETE). It might be considered a breaking change only for clients that were incorrectly relying on the previous permissive behavior to execute other SQL commands, but that usage was unintended and insecure.Types of changes
Checklist
Additional context
The fix involves checking if the uppercase, stripped query string
startswithone of the allowed prefixes ("INSERT","UPDATE","DELETE"). This provides a clear and secure validation mechanism aligned with the tool's documented purpose.SELECT query:

PRAGMA query:
