Conversation
query_asset_tool is documented as read-only ("CRITICAL: Use
READ-ONLY queries"), but tools/query.py only validated that sql
and connection_qualified_name were non-empty before building and
executing a QueryRequest. Any SQL, including INSERT/UPDATE/DELETE/
DDL or multi-statement payloads, was passed straight through to
client.queries.stream().
Add utils/sql_validator.py: a small, dependency-free validator that
strips string literals/comments, rejects multi-statement SQL, and
only allows statements starting with SELECT/WITH/SHOW/DESCRIBE/
EXPLAIN/VALUES, also rejecting write/DDL keywords anywhere in the
statement (so a write hidden inside a CTE, e.g. "WITH t AS (INSERT
... RETURNING *) SELECT * FROM t", is caught too). query_asset()
now calls this before constructing a QueryRequest and returns the
existing success/error response shape on rejection, so it fails
closed without ever reaching the Atlan client.
Also:
- Document the enforcement in the query_asset_tool docstring.
- Add query_asset_tool to the README's tool-restriction list, which
was already possible via RESTRICTED_TOOLS but undocumented, and
note that the tool now rejects non-read-only SQL on its own.
- Add a tests/ package (pytest, wired via pyproject's
[tool.pytest.ini_options]) with unit tests for the validator and
for query_asset()'s rejection path.
Fixes atlanhq#226
Author
|
The only red check I can see is with no public details. Endor Labs is green. I don't have access to the Snyk org output, so this likely needs a maintainer-side rerun or details from the Snyk dashboard. |
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
Fixes #226.
query_asset_toolis documented as read-only ("CRITICAL: Use READ-ONLY queries to retrieve data. Write and modify queries are not supported by this tool."), buttools/query.py::query_asset()only checked thatsqlandconnection_qualified_namewere non-empty before building aQueryRequestand callingclient.queries.stream(...). Any SQL —INSERT/UPDATE/DELETE/DDL, or multiple statements — was passed straight through to the Atlan query service. An operator restricting write tools viaRESTRICTED_TOOLS(per the README's "Read-Only Access" example) would still have unrestricted SQL execution available, sincequery_asset_toolwasn't even in that list.Changes
modelcontextprotocol/utils/sql_validator.py(new): a small, dependency-freevalidate_read_only_sql(sql)that:;-separated),SELECT/WITH/SHOW/DESCRIBE/DESC/EXPLAIN/VALUES,INSERT,UPDATE,DELETE,DROP,ALTER,CREATE,GRANT,INTO, ...) anywhere in the statement, so a write hidden inside a CTE (e.g.WITH t AS (INSERT INTO ... RETURNING *) SELECT * FROM t) is also caught.modelcontextprotocol/tools/query.py: call the validator before constructingQueryRequest; on rejection, return the existing{success, data, error, query_info}shape (fails closed, never reaches the Atlan client) rather than raising.modelcontextprotocol/server.py: note the enforcement in thequery_asset_tooldocstring.modelcontextprotocol/README.md: addquery_asset_toolto the documented tool-restriction list (it was already restrictable viaRESTRICTED_TOOLS, just undocumented), and note that the tool now rejects non-read-only SQL on its own.modelcontextprotocol/tests/(new): this repo has no test suite yet (tracked separately in [FEATURE] Add tests #36), so I added a minimalpytestsetup (wired via[tool.pytest.ini_options]inpyproject.toml,pytestadded as adevextra) with:test_sql_validator.py: parametrized allow/reject cases for the validator.test_query.py: confirmsquery_asset()rejects write/multi-statement SQL without callingget_atlan_client(), and that a validSELECTstill reachesQueryRequest/client.queries.stream()(mocked, no network/credentials needed).This is intentionally a conservative keyword-based check rather than a full SQL parser/AST — it's meant to fail closed on anything ambiguous rather than to validate SQL correctness, per the issue's suggested approach.
Test plan
pytest tests/— 27 passedruff check/ruff formatclean on all new/touched filesINSERT/UPDATE/DELETE/DDL/multi-statement SQL rejected beforeQueryRequestis built; existingSELECTexamples from the tool's own docstring still work)🤖 Generated with Claude Code