codeant-ai-for-open-source[bot] commented on code in PR #40957: URL: https://github.com/apache/superset/pull/40957#discussion_r3417025687
########## superset/mcp_service/dashboard/tool/update_dashboard.py: ########## @@ -0,0 +1,352 @@ +# 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, certification, roles, tags, CSS, theme, 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, + serialize_chart_summary, + UpdateDashboardRequest, + UpdateDashboardResponse, +) +from superset.mcp_service.privacy import user_can_view_data_model_metadata +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", + "theme_id", + "certified_by", + "certification_details", + "roles", + "tags", +) + +# Convenience fields stored inside the dashboard's json_metadata blob. +_METADATA_FIELDS = ( + "color_scheme", + "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: **🟠Architect Review — HIGH** The update_dashboard MCP tool forwards the `css` field directly into UpdateDashboardCommand without running the CSS safety validation used on the REST update path, so MCP callers can persist dashboard CSS containing constructs (e.g. `@import`, script-scheme URLs) that the API explicitly rejects via `validate_css`. **Suggestion:** Before adding `css` to `properties` in `_build_update_properties`, validate it with the existing `validate_css` helper from `superset/dashboards/schemas.py` so MCP updates enforce the same CSS guardrails as DashboardPutSchema/REST updates, rather than introducing a separate rule set. [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0dc2739086214fe29eba7e08cb08b06c&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=0dc2739086214fe29eba7e08cb08b06c&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 an **Architect / Logical Review** comment left during a code review. These reviews are first-class, important findings — not optional suggestions. Do NOT dismiss this as a 'big architectural change' just because the title says architect review; most of these can be resolved with a small, localized fix once the intent is understood. **Path:** superset/mcp_service/dashboard/tool/update_dashboard.py **Line:** 87:91 **Comment:** *HIGH: The update_dashboard MCP tool forwards the `css` field directly into UpdateDashboardCommand without running the CSS safety validation used on the REST update path, so MCP callers can persist dashboard CSS containing constructs (e.g. `@import`, script-scheme URLs) that the API explicitly rejects via `validate_css`. 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. If a suggested approach is provided above, use it as the authoritative instruction. If no explicit code suggestion is given, you MUST still draft and apply your own minimal, localized fix — do not punt back with 'no suggestion provided, review manually'. Keep the change as small as possible: add a guard clause, gate on a loading state, reorder an await, wrap in a conditional, etc. Do not refactor surrounding code or expand scope beyond the finding. 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> -- 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]
