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


##########
superset/mcp_service/dataset/tool/update_dataset_metric.py:
##########
@@ -0,0 +1,258 @@
+# 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.
+
+"""Update a saved metric on a dataset (FastMCP tool)."""
+
+import difflib
+import logging
+from typing import Any
+
+from fastmcp import Context
+from superset_core.mcp.decorators import tool, ToolAnnotations
+
+from superset.extensions import event_logger
+from superset.mcp_service.dataset.schemas import (
+    DatasetMetricDetail,
+    MetricCurrency,
+    UpdateDatasetMetricRequest,
+    UpdateDatasetMetricResponse,
+)
+from superset.mcp_service.utils import (
+    escape_llm_context_delimiters,
+    sanitize_for_llm_context,
+)
+
+logger = logging.getLogger(__name__)
+
+
+def _find_metric(metrics: list[Any], identifier: int | str) -> Any | None:
+    """Find a saved metric by ID, UUID, or metric_name."""
+    if isinstance(identifier, str):
+        try:
+            identifier = int(identifier)
+        except ValueError:
+            pass
+
+    if isinstance(identifier, int):
+        return next((m for m in metrics if m.id == identifier), None)
+
+    lowered = identifier.lower()
+    by_uuid = next(
+        (m for m in metrics if m.uuid and str(m.uuid).lower() == lowered), None
+    )
+    if by_uuid is not None:
+        return by_uuid
+    return next((m for m in metrics if m.metric_name == identifier), None)
+
+
+def _metric_not_found_message(metrics: list[Any], identifier: int | str) -> 
str:
+    names = [m.metric_name for m in metrics]
+    msg = f"Metric '{identifier}' not found on this dataset."
+    if not names:
+        return f"{msg} This dataset has no saved metrics."
+    suggestions = difflib.get_close_matches(str(identifier), names, n=3, 
cutoff=0.6)
+    if suggestions:
+        return f"{msg} Did you mean: {', '.join(suggestions)}?"
+    return f"{msg} Available metrics: {', '.join(sorted(names))}."

Review Comment:
   Yes — this is a **valid security issue**.
   
   ### Why it matters
   `_metric_not_found_message()` currently builds the error string from:
   
   - the caller-controlled `identifier`
   - stored `metric_name` values
   
   and returns it directly in `UpdateDatasetMetricResponse.error`. Since this 
is MCP output, raw delimiter/control content can leak into the LLM context and 
create prompt-injection risk.
   
   ### Minimal fix
   Sanitize/escape the values before interpolating them into the error message, 
consistent with `_serialize_metric()`.
   
   For example:
   
   ```python
   from superset.mcp_service.utils import (
       escape_llm_context_delimiters,
       sanitize_for_llm_context,
   )
   
   def _metric_not_found_message(metrics: list[Any], identifier: int | str) -> 
str:
       safe_identifier = sanitize_for_llm_context(str(identifier), 
field_path=("metric", "identifier"))
       names = [
           escape_llm_context_delimiters(m.metric_name) or ""
           for m in metrics
       ]
       msg = f"Metric '{safe_identifier}' not found on this dataset."
       if not names:
           return f"{msg} This dataset has no saved metrics."
       suggestions = difflib.get_close_matches(str(identifier), [m.metric_name 
for m in metrics], n=3, cutoff=0.6)
       if suggestions:
           safe_suggestions = [escape_llm_context_delimiters(name) or "" for 
name in suggestions]
           return f"{msg} Did you mean: {', '.join(safe_suggestions)}?"
       return f"{msg} Available metrics: {', '.join(sorted(names))}."
   ```
   
   ### Recommendation
   I’d keep the fix focused on this helper rather than changing the response 
schema, since the issue is specifically in how the message is composed.
   
   If you want, I can also check the rest of the PR comments and help 
validate/fix those too.



-- 
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