codeant-ai-for-open-source[bot] commented on code in PR #41497:
URL: https://github.com/apache/superset/pull/41497#discussion_r3532834318


##########
tests/unit_tests/mcp_service/theme/tool/test_create_theme.py:
##########
@@ -0,0 +1,171 @@
+# 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.
+
+"""Unit tests for the create_theme MCP tool."""
+
+import importlib
+from unittest.mock import MagicMock, Mock, patch
+
+import pytest
+from fastmcp import Client
+from marshmallow import ValidationError
+
+from superset.mcp_service.app import mcp
+from superset.utils import json
+
+# Resolve the module object directly so patch.object targets the module, not
+# the function re-exported through __init__.py.
+create_theme_module = importlib.import_module(
+    "superset.mcp_service.theme.tool.create_theme"
+)
+
+
+def _make_mock_theme(
+    theme_id: int = 7,
+    theme_name: str = "Corporate Blue",
+    uuid: str = "22222222-2222-2222-2222-222222222222",
+) -> MagicMock:
+    theme = MagicMock()
+    theme.id = theme_id
+    theme.theme_name = theme_name
+    theme.uuid = uuid
+    return theme
+
+
[email protected]
+def mcp_server():
+    return mcp
+
+
[email protected](autouse=True)
+def mock_auth():
+    with patch("superset.mcp_service.auth.get_user_from_request") as 
mock_get_user:
+        mock_user = Mock()
+        mock_user.id = 1
+        mock_user.username = "admin"
+        mock_get_user.return_value = mock_user
+        yield mock_get_user
+
+
[email protected](create_theme_module.db.session, "commit")
+@patch("superset.daos.theme.ThemeDAO.create")
[email protected](create_theme_module, "_sanitize_and_validate_theme_config")
[email protected]
+async def test_create_theme_success_with_dict(
+    mock_sanitize, mock_create, mock_commit, mcp_server
+):
+    """Happy path: dict json_data is sanitized, persisted, and id/uuid 
returned."""
+    config = {"token": {"colorPrimary": "#1d4ed8"}}
+    mock_sanitize.return_value = config
+    mock_create.return_value = _make_mock_theme()
+
+    async with Client(mcp_server) as client:
+        result = await client.call_tool(
+            "create_theme",
+            {
+                "request": {
+                    "theme_name": "Corporate Blue",
+                    "json_data": config,
+                }
+            },
+        )
+        data = json.loads(result.content[0].text)
+
+    assert data["success"] is True
+    assert data["id"] == 7
+    assert data["uuid"] == "22222222-2222-2222-2222-222222222222"
+    assert data["theme_name"] == "Corporate Blue"

Review Comment:
   **Suggestion:** This assertion hard-codes an unsanitized `theme_name`, which 
conflicts with the MCP pattern of wrapping user-controlled text for LLM safety 
and can let prompt-injection boundary regressions slip through. Update the 
expectation to validate sanitized/wrapped output (or at least avoid requiring 
raw equality) so the test enforces the security contract. [security]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Theme MCP tool returns unsanitized names to LLM agents.
   - ⚠️ Tests don’t enforce sanitize_for_llm_context on theme_name.
   - ⚠️ Prompt-injection boundaries for theme_name not regression-tested.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Inspect the create_theme tool implementation at
   `superset/mcp_service/theme/tool/create_theme.py:114-136`, where the 
successful response
   is built as `CreateThemeResponse(success=True, id=theme.id, uuid=...,
   theme_name=theme.theme_name, message=...)` without applying 
`sanitize_for_llm_context` to
   `theme_name`.
   
   2. Inspect the theme serialization helpers at
   `superset/mcp_service/theme/schemas.py:252-263`, where
   `_sanitize_theme_info_for_llm_context()` wraps `theme_name` via
   `sanitize_for_llm_context(payload.get("theme_name"), 
field_path=("theme_name",))`,
   establishing the MCP pattern that user-controlled theme text is sanitized 
before LLM
   exposure.
   
   3. Inspect the LLM sanitization utility at
   `superset/mcp_service/utils/sanitization.py:116-82`, which shows
   `sanitize_for_llm_context()` wrapping strings in explicit untrusted-content 
delimiters;
   this confirms that a sanitized `theme_name` will differ structurally from 
the raw input
   for non-empty strings.
   
   4. Run the unit test `test_create_theme_success_with_dict` in
   `tests/unit_tests/mcp_service/theme/tool/test_create_theme.py:68-92` (e.g., 
`python -m
   pytest tests/unit_tests/mcp_service/theme/tool/test_create_theme.py -q`); it 
calls the MCP
   server via `Client(mcp)` and asserts `data["theme_name"] == "Corporate 
Blue"`, thereby
   requiring raw equality and not verifying that the MCP response applies the 
established
   `sanitize_for_llm_context` wrapper to user-supplied `theme_name`, so 
prompt-injection
   boundary regressions on this field can pass tests undetected.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e168767742304edf8d602fa1d8396534&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e168767742304edf8d602fa1d8396534&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:** tests/unit_tests/mcp_service/theme/tool/test_create_theme.py
   **Line:** 91:91
   **Comment:**
        *Security: This assertion hard-codes an unsanitized `theme_name`, which 
conflicts with the MCP pattern of wrapping user-controlled text for LLM safety 
and can let prompt-injection boundary regressions slip through. Update the 
expectation to validate sanitized/wrapped output (or at least avoid requiring 
raw equality) so the test enforces the security contract.
   
   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%2F41497&comment_hash=928dfddb8f4c53084e6eaf1611d8be2f3b5104b941b47b5149441ec01e9641ec&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41497&comment_hash=928dfddb8f4c53084e6eaf1611d8be2f3b5104b941b47b5149441ec01e9641ec&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]

Reply via email to