Skip to content

fix(security): run AI-client read-only SQL in a read-only transaction with limits - #121

Merged
lukyrys merged 1 commit into
mainfrom
fix/readonly-query-transaction
Oct 27, 2026
Merged

lukyrys merged 1 commit into
mainfrom
fix/readonly-query-transaction

Conversation

@blackenzee

@blackenzee blackenzee commented Oct 26, 2026 •

Copy link
Copy Markdown

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}/query and /api/databases/v2/{db}/query skip the intent when SqlReadOnlyGuard judges the statement read-only. The statement then ran:

  • as MySQL root;
  • through the mysql CLI (-e), or through a connection that allows multiple statements;
  • with no read-only transaction, no time limit and no row cap.

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:

  • one statement only (the multi-statement option is off);
  • inside 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;
  • with max_execution_time (MySQL) or max_statement_time (MariaDB) where the server supports it, plus a 15 s client timeout;
  • with a 1000-row cap, reported as truncated on 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_FILE passed: the LOAD keyword is matched with \b, and _ is a word character.

  • Reads with side effects were accepted:

    • SELECT … INTO variables
    • SLEEP / BENCHMARK
    • named locks (GET_LOCK, RELEASE_LOCK)
    • row locks (LOCK IN SHARE MODE)
    • sequence advances (NEXTVAL, SETVAL)
    • UDF exec helpers

    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 SqlReadOnlyGuardTests and databases.test.ts, kept as identical lists: 34 in each suite.

Verification

dotnet build WebDevConsole.sln -c Release   # 0 warnings
dotnet test tests/NKS.WebDevConsole.Daemon.Tests -c Release --no-build --filter SqlReadOnlyGuard   # 34 passed
cd services/mcp-server && npx vitest run src/tools/databases.test.ts   # 34 passed

Worth a manual check against a real MySQL 8 and MariaDB 11. With the MCP token, a plain SELECT returns rows, and a write smuggled past the guard (e.g. a stored function that inserts) fails with error 1792 (read-only transaction).


… 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.
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b3a59220-d2f1-4525-86b5-bb6044c78c93
Commits

Reviewing files that changed from the base of the PR and between 81df311 and a89cecc.

Files selected for processing: 7
  • services/mcp-server/src/tools/databases.test.ts
  • services/mcp-server/src/tools/databases.ts
  • src/daemon/NKS.WebDevConsole.Daemon/Data/DatabaseEndpoints.cs
  • src/daemon/NKS.WebDevConsole.Daemon/Data/IDbDriver.cs
  • src/daemon/NKS.WebDevConsole.Daemon/Data/MySqlDriver.cs
  • src/daemon/NKS.WebDevConsole.Daemon/Mcp/SqlReadOnlyGuard.cs
  • tests/NKS.WebDevConsole.Daemon.Tests/SqlReadOnlyGuardTests.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lukyrys
lukyrys changed the base branch from fix/mcp-destructive-db-cloudflare-gates to main October 27, 2026 15:00
@lukyrys
lukyrys merged commit 4488577 into main Oct 27, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants