Repository navigation
fix(security): run AI-client read-only SQL in a read-only transaction with limits - #121
Merged
Merged
Conversation
… with limits
For the MCP token, /api/databases/{name}/query and /v2/{db}/query let a
statement through without an intent when SqlReadOnlyGuard says it is
read-only. That regex guard was the only barrier: the statement then ran
as MySQL root through the mysql CLI (or a multi-statement connection),
with no read-only transaction, time limit or row cap.
That path now runs through MySqlDriver.ExecuteReadOnlyQueryAsync:
single statement, START TRANSACTION READ ONLY (the server refuses any
table write, also from stored functions), always rolled back,
max_execution_time / max_statement_time where supported, a 15 s client
timeout and a 1000-row cap (reported as truncated). GUI callers and
intent-approved statements are unchanged.
The guard (C# and the MCP server twin) also rejects reads with side
effects: SELECT ... INTO, LOAD_FILE (the LOAD keyword missed it because
'_' is a word character), SLEEP, BENCHMARK, named and row locks and
sequence advances.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 58 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
lukyrys
changed the base branch from
fix/mcp-destructive-db-cloudflare-gates
to
main
October 27, 2026 15:00
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
Stacked on #112. Database part of security audit round 3, which an earlier audit pass did not cover.
High: the regex guard was the only barrier
For the MCP token,
/api/databases/{name}/queryand/api/databases/v2/{db}/queryskip the intent whenSqlReadOnlyGuardjudges the statement read-only. The statement then ran:mysqlCLI (-e), or through a connection that allows multiple statements;Any gap in the regex was therefore a write, a file read or a resource exhaustion run as root.
That path now runs through
MySqlDriver.ExecuteReadOnlyQueryAsync:START TRANSACTION READ ONLY, so the server itself refuses any table write, including one from a stored function, and the transaction is always rolled back;max_execution_time(MySQL) ormax_statement_time(MariaDB) where the server supports it, plus a 15 s client timeout;truncatedon the legacy route and as a warning on v2.GUI callers and intent-approved statements are unchanged.
Guard gaps (C# and the MCP server twin, kept in step)
LOAD_FILEpassed: theLOADkeyword is matched with\b, and_is a word character.Reads with side effects were accepted:
SELECT … INTOvariablesSLEEP/BENCHMARKGET_LOCK,RELEASE_LOCK)LOCK IN SHARE MODE)NEXTVAL,SETVAL)The guard now rejects all of these. Identifiers that only contain these words (e.g.
into_count,sleep_ms) are still allowed.Tests
New cases in
SqlReadOnlyGuardTestsanddatabases.test.ts, kept as identical lists: 34 in each suite.Verification
Worth a manual check against a real MySQL 8 and MariaDB 11. With the MCP token, a plain
SELECTreturns rows, and a write smuggled past the guard (e.g. a stored function that inserts) fails with error 1792 (read-only transaction).