codeant-ai-for-open-source[bot] commented on code in PR #40957: URL: https://github.com/apache/superset/pull/40957#discussion_r3418076092
########## superset/mcp_service/dashboard/tool/update_dashboard.py: ########## @@ -0,0 +1,340 @@ +# 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. + +""" +MCP tool: update_dashboard + +This tool performs a partial update of dashboard metadata (title, slug, +published state, tags, CSS, and selected json_metadata settings). +""" + +import logging +from typing import Any + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + DashboardInfo, + UpdateDashboardRequest, + UpdateDashboardResponse, +) +from superset.mcp_service.utils.url_utils import get_superset_base_url +from superset.utils import json + +logger = logging.getLogger(__name__) + +# Direct dashboard columns accepted by UpdateDashboardCommand +# (subset of DashboardPutSchema). +_DIRECT_FIELDS = ( + "dashboard_title", + "slug", + "published", + "css", + "tags", +) + +# Convenience fields stored inside the dashboard's json_metadata blob. +_METADATA_FIELDS = ( + "cross_filters_enabled", + "refresh_frequency", + "filter_bar_orientation", +) + + +def _build_update_properties( + request: UpdateDashboardRequest, dashboard: Any +) -> tuple[dict[str, Any], list[str]]: + """Build the UpdateDashboardCommand properties dict from the request. + + Returns ``(properties, updated_fields)`` where *updated_fields* lists + the request fields that will be changed. + + json_metadata is a stringified JSON blob and + ``DashboardDAO.set_dash_metadata`` resets absent keys to defaults + (e.g. ``expanded_slices`` -> {}). To avoid silently destroying state, + the dashboard's FULL current json_metadata is read, the requested + changes are merged in, and the complete blob is written back. + """ + properties: dict[str, Any] = {} + updated_fields: list[str] = [] + + for field in _DIRECT_FIELDS: + value = getattr(request, field) + if value is not None: + properties[field] = value + updated_fields.append(field) Review Comment: **Suggestion:** The update path copies `slug` directly from the request without applying the REST schema's slug normalization/validation (`strip`, space-to-hyphen, invalid-character removal). This breaks contract parity with the existing dashboard update API and can persist malformed slugs that produce inconsistent URLs or routing behavior. Reuse the same slug cleaning/validation logic used by `DashboardPutSchema` before building command properties. [incomplete implementation] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ MCP update_dashboard can persist unnormalized slugs from requests. - ⚠️ Dashboard.url may produce malformed or encoded slug URLs. - ⚠️ Behavior diverges from REST dashboard PUT normalization. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Start the MCP service; `superset/mcp_service/app.py:660-739` imports `update_dashboard` from `superset.mcp_service.dashboard.tool`, so the FastMCP `@tool`-decorated function in `superset/mcp_service/dashboard/tool/update_dashboard.py:217-227` is registered as an MCP tool. 2. From an MCP client, call the `update_dashboard` tool with a payload like `{"dashboard_id": 1, "slug": " My Slug!? "}`; this is parsed into `UpdateDashboardRequest` in `superset/mcp_service/dashboard/schemas.py:12-32`, which defines `slug: str | None` without any field validator to normalize or clean it. 3. Inside `update_dashboard` (`superset/mcp_service/dashboard/tool/update_dashboard.py:261-267`), the code calls `_build_update_properties(request, dashboard)`, which iterates `_DIRECT_FIELDS` at lines 79-83; for the `"slug"` field it does `value = getattr(request, field)` and, because it is not `None`, sets `properties["slug"] = value` with no transformation. 4. `UpdateDashboardCommand` in `superset/commands/dashboard/update.py:52-79` receives this `properties` dict; its `validate` method at lines 81-101 only checks slug uniqueness via `DashboardDAO.validate_update_slug_uniqueness` and does not normalize it. The slug is then persisted to `Dashboard.slug` (`superset/models/dashboard.py:131-145`), and URLs are generated using `Dashboard.url` (`superset/models/dashboard.py:198-205`) as `/superset/dashboard/{self.slug or self.id}/`, meaning the stored value `" My Slug!? "` will produce an odd, escaped URL. In contrast, the REST API update path uses `DashboardPutSchema` (`superset/dashboards/schemas.py:64-74`), which inherits `BaseDashboardSchema.post_load` (`superset/dashboards/schemas.py:34-42`) to strip, replace spaces with `-`, and remove invalid characters, so the same input would be normalized before persistence. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=3df16994b64c486282f8863364ee1634&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=3df16994b64c486282f8863364ee1634&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/dashboard/tool/update_dashboard.py **Line:** 79:83 **Comment:** *Incomplete Implementation: The update path copies `slug` directly from the request without applying the REST schema's slug normalization/validation (`strip`, space-to-hyphen, invalid-character removal). This breaks contract parity with the existing dashboard update API and can persist malformed slugs that produce inconsistent URLs or routing behavior. Reuse the same slug cleaning/validation logic used by `DashboardPutSchema` before building command properties. 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%2F40957&comment_hash=2d79bfe4f6ae2b6f9f01e5c08f81a0b10c6f11d21ac923ef0d997e4260149fb1&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40957&comment_hash=2d79bfe4f6ae2b6f9f01e5c08f81a0b10c6f11d21ac923ef0d997e4260149fb1&reaction=dislike'>👎</a> ########## superset/mcp_service/dashboard/tool/update_dashboard.py: ########## @@ -0,0 +1,340 @@ +# 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. + +""" +MCP tool: update_dashboard + +This tool performs a partial update of dashboard metadata (title, slug, +published state, tags, CSS, and selected json_metadata settings). +""" + +import logging +from typing import Any + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + DashboardInfo, + UpdateDashboardRequest, + UpdateDashboardResponse, +) +from superset.mcp_service.utils.url_utils import get_superset_base_url +from superset.utils import json + +logger = logging.getLogger(__name__) + +# Direct dashboard columns accepted by UpdateDashboardCommand +# (subset of DashboardPutSchema). +_DIRECT_FIELDS = ( + "dashboard_title", + "slug", + "published", + "css", + "tags", +) + +# Convenience fields stored inside the dashboard's json_metadata blob. +_METADATA_FIELDS = ( + "cross_filters_enabled", + "refresh_frequency", + "filter_bar_orientation", +) + + +def _build_update_properties( + request: UpdateDashboardRequest, dashboard: Any +) -> tuple[dict[str, Any], list[str]]: + """Build the UpdateDashboardCommand properties dict from the request. + + Returns ``(properties, updated_fields)`` where *updated_fields* lists + the request fields that will be changed. + + json_metadata is a stringified JSON blob and + ``DashboardDAO.set_dash_metadata`` resets absent keys to defaults + (e.g. ``expanded_slices`` -> {}). To avoid silently destroying state, + the dashboard's FULL current json_metadata is read, the requested + changes are merged in, and the complete blob is written back. + """ + properties: dict[str, Any] = {} + updated_fields: list[str] = [] + + for field in _DIRECT_FIELDS: + value = getattr(request, field) + if value is not None: + properties[field] = value + updated_fields.append(field) + + metadata_changes = { + field: value + for field in _METADATA_FIELDS + if (value := getattr(request, field)) is not None + } + if metadata_changes: + try: + current_metadata = json.loads(dashboard.json_metadata or "{}") + except (ValueError, TypeError): + logger.warning( + "Failed to parse existing json_metadata for dashboard %s; " + "starting from an empty metadata object", + dashboard.id, + ) + current_metadata = {} + if not isinstance(current_metadata, dict): + current_metadata = {} + properties["json_metadata"] = json.dumps( + {**current_metadata, **metadata_changes} Review Comment: **Suggestion:** The metadata update flow is a read-modify-write on `json_metadata` without any concurrency guard, so concurrent dashboard updates can overwrite each other and silently lose unrelated metadata keys. Perform the merge under a row lock or add optimistic concurrency/version checking before writing back the full blob. [race condition] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Concurrent MCP updates can drop dashboard json_metadata changes. - ⚠️ Cross-filters or refresh settings may be silently overwritten. - ⚠️ Multi-user dashboard edits via MCP become unreliable under contention. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Run the MCP service so that `update_dashboard` is registered via the imports in `superset/mcp_service/app.py:660-739`; this exposes the tool defined in `superset/mcp_service/dashboard/tool/update_dashboard.py:227-327` to MCP clients. 2. Ensure an existing dashboard has non-empty `json_metadata` (stored on the `Dashboard` model in `superset/models/dashboard.py:143-145`) and is editable by the current user so `_find_and_authorize_dashboard` (`update_dashboard.py:110-138`) succeeds. 3. From two separate MCP clients or threads, concurrently call the `update_dashboard` tool with the same `dashboard_id` but different metadata fields, e.g. request A sets `{"cross_filters_enabled": true}` and request B sets `{"refresh_frequency": 60}`; both calls go through `_build_update_properties` (`update_dashboard.py:62-107`). 4. In each call, `_build_update_properties` executes `current_metadata = json.loads(dashboard.json_metadata or "{}")` at line 92, then merges its own `metadata_changes` and sets `properties["json_metadata"] = json.dumps({**current_metadata, **metadata_changes})` at lines 102-104. `UpdateDashboardCommand.run` (`superset/commands/dashboard/update.py:58-79`) then calls `DashboardDAO.update` and `DashboardDAO.set_dash_metadata` with this full JSON blob. Because there is no row lock or version check in `UpdateDashboardCommand.validate` (`update.py:81-127`) or `DashboardDAO.set_dash_metadata` (`superset/daos/dashboard.py:44-52`), whichever transaction commits last will overwrite the other's changes with the stale snapshot it read at line 92, silently dropping fields updated by the concurrent call. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b10c266e046f447690b7e6a2746d21a5&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b10c266e046f447690b7e6a2746d21a5&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/dashboard/tool/update_dashboard.py **Line:** 92:103 **Comment:** *Race Condition: The metadata update flow is a read-modify-write on `json_metadata` without any concurrency guard, so concurrent dashboard updates can overwrite each other and silently lose unrelated metadata keys. Perform the merge under a row lock or add optimistic concurrency/version checking before writing back the full blob. 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%2F40957&comment_hash=bf5919b73fe4104b0618b4f640ababc05f12edaaaa119e2500164968fdcd8ed6&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40957&comment_hash=bf5919b73fe4104b0618b4f640ababc05f12edaaaa119e2500164968fdcd8ed6&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]
