codeant-ai-for-open-source[bot] commented on code in PR #40346: URL: https://github.com/apache/superset/pull/40346#discussion_r3291051114
########## superset/mcp_service/saved_query/tool/get_saved_query_info.py: ########## @@ -0,0 +1,129 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Get saved query info FastMCP tool + +This module contains the FastMCP tool for getting detailed information +about a specific saved SQL query. +""" + +import logging +from datetime import datetime, timezone + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.mcp_core import ModelGetInfoCore +from superset.mcp_service.saved_query.schemas import ( + GetSavedQueryInfoRequest, + SavedQueryError, + SavedQueryInfo, + serialize_saved_query_object, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["discovery"], + class_permission_name="SavedQuery", + annotations=ToolAnnotations( + title="Get saved query info", + readOnlyHint=True, + destructiveHint=False, + ), +) +async def get_saved_query_info( + request: GetSavedQueryInfoRequest, ctx: Context +) -> SavedQueryInfo | SavedQueryError: + """Get saved query details by ID or UUID. + + Returns the full saved query including SQL text, label, database, + schema, and timestamps. + + IMPORTANT FOR LLM CLIENTS: + - Use numeric ID (e.g., 42) or UUID string (e.g., "a1b2c3d4-...") + - To find a saved query ID, use the list_saved_queries tool first + + Example usage: + ```json + { + "identifier": 42 + } + ``` + + Or with UUID: + ```json + { + "identifier": "a1b2c3d4-5678-90ab-cdef-1234567890ab" + } + ``` + """ + await ctx.info( + "Retrieving saved query information: identifier=%s" % (request.identifier,) + ) + + try: + from superset.daos.query import SavedQueryDAO + + with event_logger.log_context(action="mcp.get_saved_query_info.lookup"): + get_tool = ModelGetInfoCore( + dao_class=SavedQueryDAO, + output_schema=SavedQueryInfo, + error_schema=SavedQueryError, + serializer=serialize_saved_query_object, + supports_slug=False, + logger=logger, + ) + + result = get_tool.run_tool(request.identifier) + + if isinstance(result, SavedQueryInfo): + await ctx.info( + "Saved query information retrieved successfully: " + "saved_query_id=%s, label=%s, db_id=%s" + % ( + result.id, + result.label, + result.db_id, + ) + ) + else: + await ctx.warning( + "Saved query retrieval failed: error_type=%s, error=%s" + % (result.error_type, result.error) + ) + + return result + + except Exception as e: + await ctx.error( + "Saved query information retrieval failed: identifier=%s, error=%s, " + "error_type=%s" + % ( + request.identifier, + str(e), + type(e).__name__, + ) + ) + return SavedQueryError( + error=f"Failed to get saved query info: {str(e)}", + error_type="InternalError", + timestamp=datetime.now(timezone.utc), + ) Review Comment: **Suggestion:** The error response includes raw exception text (`str(e)`) in the payload returned to clients, which can disclose internal implementation details (database errors, query internals, stack context). Return a generic user-facing message instead and keep full exception details only in server logs. [security] <details> <summary><b>Severity Level:</b> Critical 🚨</summary> ```mdx - ❌ MCP `get_saved_query_info` leaks raw backend exception text. - ⚠️ Internal database or ORM errors exposed to MCP clients. - ⚠️ LLM consumers may see sensitive implementation details. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Start the MCP server, which imports and registers the saved query tools in `superset/mcp_service/app.py` (see imports of `get_saved_query_info` and `list_saved_queries` at `superset/mcp_service/app.py:628-651`, including the specific `from superset.mcp_service.saved_query.tool import get_saved_query_info` line reported by Grep at `app.py:648`). 2. An MCP client (e.g., an LLM tool consumer) invokes the `get_saved_query_info` tool by name, which causes FastMCP to execute the `get_saved_query_info` coroutine defined with the `@tool` decorator in `superset/mcp_service/saved_query/tool/get_saved_query_info.py:43-54`. 3. Inside `get_saved_query_info`, the function constructs a `ModelGetInfoCore` with `SavedQueryDAO` and calls `get_tool.run_tool(request.identifier)` within the `try` block at `get_saved_query_info.py:82-95`; if this DAO call or any code in the `try` (for example, a database connection failure or unexpected ORM error) raises an exception, control flows into the `except Exception as e` block at `get_saved_query_info.py:115-123`. 4. In the `except` block, the function logs the error via `ctx.error` and then returns a `SavedQueryError` instance at `get_saved_query_info.py:125-129` where the `error` field is set to `f"Failed to get saved query info: {str(e)}"`, so the full raw exception message `str(e)` is serialized according to the `SavedQueryError` schema in `superset/mcp_service/saved_query/schemas.py:1-3` and sent back to the MCP client, potentially exposing internal database error messages or other sensitive implementation details to the caller. ``` </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2b983294ad434310b19f803496629359&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=2b983294ad434310b19f803496629359&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/mcp_service/saved_query/tool/get_saved_query_info.py **Line:** 125:129 **Comment:** *Security: The error response includes raw exception text (`str(e)`) in the payload returned to clients, which can disclose internal implementation details (database errors, query internals, stack context). Return a generic user-facing message instead and keep full exception details only in server logs. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40346&comment_hash=93405970562716a6103220d37c07b1684c04f197e4a893c4811e97e4646417fb&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40346&comment_hash=93405970562716a6103220d37c07b1684c04f197e4a893c4811e97e4646417fb&reaction=dislike'>👎</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
