Skip to content

fix(viewer): enforce readonly queries in data_query MCP tool - #277

Closed
SougataXdev wants to merge 1 commit into
apple:mainfrom
SougataXdev:fix/mcp-readonly-query
Closed

SougataXdev wants to merge 1 commit into
apple:mainfrom
SougataXdev:fix/mcp-readonly-query

Conversation

@SougataXdev

Copy link
Copy Markdown

The data_query MCP tool claimed to be readonly but executed arbitrary SQL (TODO at packages/viewer/src/model_context/model_context.ts:78).

This change validates the statement before running it:

  • allow only SELECT/WITH/VALUES/DESCRIBE/SHOW/EXPLAIN (+ TABLE/DESC/SUMMARIZE/PIVOT)
  • reject stacked statements (semicolon) and DDL/DML keywords (INSERT/UPDATE/DELETE/DROP/CREATE/ALTER/COPY/ATTACH, etc.)
  • ignore keywords inside strings and comments

Tests:

  • new packages/viewer/test/model_context.test.ts (allow/deny/strings)
  • viewer suite: 12 files, 147 tests passed
  • prettier clean

@SougataXdev

Copy link
Copy Markdown
Author

@domoritz Could you please take a look at this PR when you get a chance? It addresses the read-only SQL issue in data_query. Thanks!

The data_query tool claimed to be readonly but executed arbitrary SQL. Validate the statement client-side: allow only SELECT/WITH/VALUES/DESCRIBE/SHOW/EXPLAIN, reject stacked statements and DDL/DML keywords, ignoring strings and comments.
@SougataXdev
SougataXdev force-pushed the fix/mcp-readonly-query branch from adb25a2 to 03d9dbe Compare October 7, 2026 07:52

@domoritz domoritz left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should rely on some brittle sql validation. Let's hold off on this for now.

@SougataXdev

Copy link
Copy Markdown
Author

Understood — regex allowlist is bypassable and would give false confidence. Closing this for now. If you want this hardened later, should it be a read-only DuckDB connection, removal/restriction of data_query, or something else?

@SougataXdev SougataXdev closed this Oct 8, 2026
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