aminghadersohi commented on code in PR #41842: URL: https://github.com/apache/superset/pull/41842#discussion_r3574252502
########## superset/mcp_service/chart/tool/restore_chart.py: ########## @@ -0,0 +1,169 @@ +# 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: restore_chart +""" + +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.chart.exceptions import ( + ChartForbiddenError, + ChartNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.chart.schemas import ( + RestoreChartRequest, + RestoreChartResponse, +) +from superset.mcp_service.utils import ( + escape_llm_context_delimiters, + sanitize_for_llm_context, +) + +logger = logging.getLogger(__name__) + + +def _find_chart_for_restore(identifier: int | str) -> Any | None: + """Resolve a chart by numeric ID or UUID, including soft-deleted rows. + + ``skip_visibility_filter`` is the only bypass — the DAO ``base_filter`` + stays in effect, so rows the user cannot see in the live UI stay hidden. + Ownership is then enforced by ``RestoreChartCommand``. + """ + from superset.daos.chart import ChartDAO + + return ChartDAO.find_by_id_or_uuid(str(identifier), skip_visibility_filter=True) Review Comment: MEDIUM: this pre-lookup keeps `base_filter` enabled (only `skip_visibility_filter=True` is passed), but the canonical command it delegates to, `BaseRestoreCommand.validate()` (`superset/commands/restore.py:86-91`), passes `skip_base_filter=True` **and** `skip_visibility_filter=True` by explicit design — its own docstring says this keeps "an editor's own trash reachable even when the entity's RBAC base_filter has no editorship leg... a lost grant must not hide a row from the one audience that can restore it." codeant raised a version of this; the author declined citing "base_filter intact" in the command's own lookup, but that's not what `superset/commands/restore.py` does at this HEAD. Net effect: an editor whose current `base_filter` access lapsed (e.g. lost datasource grant, but still owns/can-edit the chart) gets a false `NotFound` from this pre-check, even though `RestoreChartCommand` would let them restore it. Not a security escalation (over-restrictive, not under-restrictive) but worth aligning — suggest passing `skip_base_filter=True` here to match the command's own semantics. ########## tests/unit_tests/mcp_service/chart/tool/test_restore_chart.py: ########## @@ -0,0 +1,208 @@ +# 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 restore_chart MCP tool. + +Run through the async MCP Client (not direct calls); auth is mocked via the +autouse mock_auth fixture, matching the other chart tool test files. +""" + +from collections.abc import Iterator +from datetime import datetime +from unittest.mock import Mock, patch +from uuid import UUID + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp + +_FIND = "superset.daos.chart.ChartDAO.find_by_id_or_uuid" Review Comment: MEDIUM: no test exercises the `LookupFailed` branch (`restore_chart.py:99-108`) — the try/except wrapping `_find_chart_for_restore` that was added specifically to fix codeant's "DB failure during find_by_id_or_uuid bubbles out uncaught" thread. The fix landed at this HEAD but shipped without a regression test (mock `_FIND` to raise `SQLAlchemyError`/`OperationalError` and assert `error_type == "LookupFailed"`). ########## tests/unit_tests/mcp_service/dashboard/tool/test_restore_dashboard.py: ########## @@ -0,0 +1,185 @@ +# 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 restore_dashboard MCP tool. + +Run through the async MCP Client (not direct calls); auth is mocked via the +autouse mock_auth fixture, matching the other dashboard tool test files. +""" + +from collections.abc import Iterator +from datetime import datetime +from unittest.mock import Mock, patch +from uuid import UUID + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp + +_FIND = "superset.daos.dashboard.DashboardDAO.find_by_id_or_uuid" Review Comment: MEDIUM: two coverage gaps here — (a) no `SQLAlchemyError`-masking test, unlike the chart sibling's `test_restore_chart_sqlalchemy_error_is_generic` (same hardening was applied at `restore_dashboard.py:164-177`); (b) no test for `DashboardSlugConflictError` (`superset/commands/dashboard/restore.py:47-64`) — the one dashboard-specific restore rule beyond the shared base class (slug reclaimed by another active dashboard while this one was trashed), which falls through to the generic `except (CommandException, ...)` branch at `restore_dashboard.py:164` untested. ########## superset/mcp_service/dashboard/tool/restore_dashboard.py: ########## @@ -0,0 +1,177 @@ +# 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: restore_dashboard +""" + +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.dashboard.exceptions import ( + DashboardForbiddenError, + DashboardNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + RestoreDashboardRequest, + RestoreDashboardResponse, +) +from superset.mcp_service.utils import ( + escape_llm_context_delimiters, + sanitize_for_llm_context, +) + +logger = logging.getLogger(__name__) + + +def _find_dashboard_for_restore(identifier: int | str) -> Any | None: + """Resolve a dashboard by numeric ID or UUID, including soft-deleted rows. + + ``skip_visibility_filter`` is the only bypass — the DAO ``base_filter`` + stays in effect, so rows the user cannot see in the live UI stay hidden. + Ownership is then enforced by ``RestoreDashboardCommand``. + """ + from superset.daos.dashboard import DashboardDAO + + return DashboardDAO.find_by_id_or_uuid(str(identifier), skip_visibility_filter=True) Review Comment: MEDIUM: same issue as the chart tool's pre-lookup — `base_filter` stays enabled here, but `BaseRestoreCommand.validate()` (`superset/commands/restore.py:86-91`) bypasses it (`skip_base_filter=True`) by design so an editor's own trash stays reachable even if their base_filter access lapsed. This pre-lookup being stricter than the command it wraps can produce a false `NotFound` for a legitimate editor. Suggest passing `skip_base_filter=True` to match. -- 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]
