mikebridge commented on code in PR #45060:
URL: https://github.com/apache/superset/pull/45060#discussion_r4224798000


##########
tests/integration_tests/charts/semantic_metadata_authz_tests.py:
##########
@@ -0,0 +1,283 @@
+# 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.
+"""Check whether denied chart-data requests call semantic provider metadata."""
+
+from collections.abc import Callable
+from unittest.mock import Mock, patch, PropertyMock
+
+import sqlalchemy as sa
+from flask import current_app, g, Response
+
+from superset.connectors.sqla.models import SqlaTable
+from superset.extensions import db, security_manager
+from superset.models.dashboard import Dashboard
+from superset.semantic_layers.models import SemanticLayer, SemanticView
+from superset.utils import json
+from tests.integration_tests.base_tests import SupersetTestCase
+from tests.integration_tests.conftest import with_feature_flags
+
+
+class TestSemanticMetadataAuthorization(SupersetTestCase):
+    """Exercise the chart-data route with a provider that records metadata 
calls."""
+
+    def test_denied_chart_data_skips_provider_metadata(self) -> None:
+        """A denied request should not load dimensions or metrics."""
+        self.login("gamma")
+        layer: SemanticLayer = SemanticLayer(name="authz-metadata-layer", 
type="test")
+        view: SemanticView = SemanticView(
+            name="authz-metadata-view", semantic_layer=layer
+        )
+        db.session.add(view)
+        db.session.commit()
+        provider: Mock = Mock()
+        provider.get_dimensions.return_value = set()
+        provider.get_metrics.return_value = set()
+        try:
+            with patch.object(
+                SemanticView,
+                "implementation",
+                new_callable=PropertyMock,
+                return_value=provider,
+            ):
+                response: Response = self.client.post(
+                    "/api/v1/chart/data",
+                    json={
+                        "datasource": {"id": view.id, "type": "semantic_view"},
+                        "queries": [{"columns": [], "metrics": []}],
+                    },
+                )
+            assert response.status_code == 403, response.json
+            provider.get_dimensions.assert_not_called()
+            provider.get_metrics.assert_not_called()
+        finally:
+            db.session.rollback()
+            db.session.delete(view)
+            db.session.delete(layer)
+            db.session.commit()
+
+    def test_allowed_chart_data_uses_provider_metadata(self) -> None:
+        """An entitled role still builds and validates a semantic query."""
+        self.login("gamma")
+        layer: SemanticLayer = SemanticLayer(name="allowed-metadata-layer", 
type="test")
+        view: SemanticView = SemanticView(
+            name="allowed-metadata-view", semantic_layer=layer
+        )
+        db.session.add(view)
+        db.session.commit()
+        provider: Mock = Mock()
+        provider.get_dimensions.return_value = set()
+        provider.get_metrics.return_value = set()
+        original_can_access: Callable[[str, str], bool] = 
security_manager.can_access
+
+        def can_access(permission_name: str, view_name: str) -> bool:
+            """Give Gamma this view's datasource grant for the request."""
+            if permission_name == "datasource_access" and view_name == 
view.perm:
+                return True
+            return original_can_access(permission_name, view_name)
+
+        try:
+            with (
+                patch.object(
+                    SemanticView,
+                    "implementation",
+                    new_callable=PropertyMock,
+                    return_value=provider,
+                ),
+                patch.object(security_manager, "can_access", 
side_effect=can_access),
+            ):
+                response: Response = self.client.post(
+                    "/api/v1/chart/data",
+                    json={
+                        "datasource": {"id": view.id, "type": "semantic_view"},
+                        "queries": [{"columns": [], "metrics": []}],
+                        "result_type": "query",
+                    },
+                )
+            assert response.status_code == 200, response.json
+            provider.get_dimensions.assert_called()
+        finally:
+            db.session.rollback()
+            db.session.delete(view)
+            db.session.delete(layer)
+            db.session.commit()
+
+    @with_feature_flags(ENABLE_VIEWERS=True)
+    def test_denied_viewer_filter_skips_provider_metadata(self) -> None:
+        """A denied ordinary dashboard filter must not load provider 
metadata."""
+        self.login("gamma")
+        layer: SemanticLayer = SemanticLayer(name="viewer-metadata-layer", 
type="test")
+        view: SemanticView = SemanticView(
+            name="viewer-metadata-view", semantic_layer=layer
+        )
+        db.session.add(view)
+        db.session.flush()
+        metadata: str = json.dumps(
+            {
+                "native_filter_configuration": [
+                    {
+                        "id": "filter-1",
+                        "targets": [
+                            {
+                                "datasetId": view.id + 1,
+                                "datasourceType": "semantic_view",
+                            }
+                        ],
+                    }
+                ]
+            }
+        )
+        dashboard_id: int = db.session.execute(
+            sa.insert(Dashboard.__table__).values(
+                dashboard_title="viewer-metadata-dashboard",
+                published=True,
+                json_metadata=metadata,
+            )
+        ).inserted_primary_key[0]
+        db.session.commit()
+        provider: Mock = Mock()
+        provider.get_dimensions.return_value = set()
+        provider.get_metrics.return_value = set()
+        provider.features = frozenset()
+        provider.selection_identity_version = None
+        provider.uid.return_value = "viewer-metadata-view"
+        try:
+            with (
+                patch.dict(current_app.config, {"VIEWER_PROMISCUOUS_MODE": 
True}),
+                patch.object(
+                    SemanticView,
+                    "implementation",
+                    new_callable=PropertyMock,
+                    return_value=provider,
+                ),
+                patch.object(security_manager, "is_viewer", return_value=True),
+            ):
+                response: Response = self.client.post(
+                    "/api/v1/chart/data",
+                    json={
+                        "datasource": {"id": view.id, "type": "semantic_view"},
+                        "queries": [{"columns": [], "metrics": []}],
+                        "form_data": {
+                            "dashboardId": dashboard_id,
+                            "type": "NATIVE_FILTER",
+                            "native_filter_id": "filter-1",
+                        },
+                    },
+                )
+            assert response.status_code == 403, response.json
+            provider.get_dimensions.assert_not_called()
+            provider.get_metrics.assert_not_called()
+        finally:
+            db.session.rollback()
+            db.session.execute(
+                sa.delete(Dashboard.__table__).where(Dashboard.id == 
dashboard_id)
+            )
+            db.session.delete(view)
+            db.session.delete(layer)
+            db.session.commit()
+
+    @with_feature_flags(EMBEDDED_SUPERSET=True)
+    def test_guest_dashboard_filter_access_is_unchanged(self) -> None:
+        """A guest can still use a dashboard-scoped semantic native filter."""
+        self.login("gamma")
+        layer: SemanticLayer = SemanticLayer(name="guest-metadata-layer", 
type="test")
+        view: SemanticView = SemanticView(
+            name="guest-metadata-view", semantic_layer=layer
+        )
+        db.session.add(view)
+        db.session.flush()
+        metadata: str = json.dumps(
+            {
+                "native_filter_configuration": [
+                    {
+                        "id": "filter-1",
+                        "targets": [
+                            {
+                                "datasetId": view.id,
+                                "datasourceType": "semantic_view",
+                                "column": {"name": "category"},
+                            }
+                        ],
+                    }
+                ]
+            }
+        )
+        dashboard_id: int = db.session.execute(
+            sa.insert(Dashboard.__table__).values(
+                dashboard_title="guest-metadata-dashboard",
+                published=True,
+                json_metadata=metadata,
+            )
+        ).inserted_primary_key[0]
+        db.session.commit()
+        provider: Mock = Mock()
+        provider.get_dimensions.return_value = set()
+        provider.get_metrics.return_value = set()
+        provider.features = frozenset()
+        provider.selection_identity_version = None
+        provider.uid.return_value = "guest-metadata-view"
+        try:
+            with (
+                patch.object(
+                    SemanticView,
+                    "implementation",
+                    new_callable=PropertyMock,
+                    return_value=provider,
+                ),
+                patch.object(security_manager, "is_guest_user", 
return_value=True),
+                patch.object(security_manager, "has_guest_access", 
return_value=True),
+            ):
+                g.user.rls = []
+                response: Response = self.client.post(
+                    "/api/v1/chart/data",
+                    json={
+                        "datasource": {"id": view.id, "type": "semantic_view"},
+                        "queries": [{"columns": [], "metrics": []}],
+                        "result_type": "query",
+                        "form_data": {
+                            "dashboardId": dashboard_id,
+                            "type": "NATIVE_FILTER",
+                            "native_filter_id": "filter-1",
+                        },
+                    },
+                )
+            assert response.status_code == 200, response.json
+            provider.get_dimensions.assert_called()
+        finally:
+            db.session.rollback()
+            db.session.execute(
+                sa.delete(Dashboard.__table__).where(Dashboard.id == 
dashboard_id)
+            )
+            db.session.delete(view)
+            db.session.delete(layer)
+            db.session.commit()
+
+    def test_sql_dataset_chart_data_is_unchanged(self) -> None:

Review Comment:
   Good catch, thanks. That test relied on another one having loaded birth 
names. It now declares `load_birth_names_dashboard_with_slices`, and passes 
when run alone. Addressed in `7c29cf978d`.



##########
superset/common/query_context_factory.py:
##########
@@ -72,6 +75,24 @@ def create(  # pylint: disable=too-many-arguments
         result_type = result_type or ChartDataResultType.FULL
         result_format = result_format or ChartDataResultFormat.JSON
 
+        if (
+            self._authorize_semantic_before_metadata
+            and datasource_model_instance is not None
+            and DatasourceType(datasource["type"]) == 
DatasourceType.SEMANTIC_VIEW
+        ):
+            # Guest dashboard and payload checks need the completed query 
context;
+            # keep their authorization path and timing unchanged.
+            if not security_manager.is_guest_user():
+                QueryContext(
+                    datasource=datasource_model_instance,
+                    queries=[],

Review Comment:
   Agreed. I reproduced it with a hook that grants based on 
`query_context.queries`: 403 on this branch, 200 without the early check. The 
early check is now skipped whenever `EXTRA_RAISE_FOR_ACCESS_BYPASS` is 
configured, so the hook always sees the full request 
(`test_payload_based_access_bypass_still_sees_queries`). The `config.py` 
comment for the setting now says so.
   
   I also landed the invariant test here rather than as a follow-up: for a 
non-guest caller, `raise_for_access` never reads `query_context.queries`. The 
11 cases cover the direct path; the native-filter, chart, multi-layer, 
drill-to-detail and drill-by branches, each refused at its own last check; and 
successful drills through to the final dashboard check, both allowed and 
denied. Two changes make it fail: removing the `is_guest_user()` guard, or 
adding a `queries` read anywhere along those branches, including just before 
`can_access_dashboard`. Addressed in `efe852c7c5`, `f018b74a9a`, `70b56c94b7` 
and `b5eeb463f1`.



##########
superset/common/query_context_factory.py:
##########
@@ -41,9 +42,11 @@ def create_query_object_factory() -> QueryObjectFactory:
 
 class QueryContextFactory:  # pylint: disable=too-few-public-methods
     _query_object_factory: QueryObjectFactory
+    _authorize_semantic_before_metadata: bool
 
-    def __init__(self) -> None:
+    def __init__(self, authorize_semantic_before_metadata: bool = False) -> 
None:

Review Comment:
   Opted it in. `POST /api/v1/query/` now uses the same early check. A denied 
request on a semantic view returns 403 with no `get_dimensions()` call, where 
before there was one (`test_denied_legacy_query_api_skips_provider_metadata`). 
The remaining default-factory callers (MCP chart data and chart SQL, 
`Slice.get_query_context`, the native-filter cache task and Excel export) are 
listed as a follow-up in the description. Addressed in `efe852c7c5`.



##########
superset/common/query_context_factory.py:
##########
@@ -72,6 +75,24 @@ def create(  # pylint: disable=too-many-arguments
         result_type = result_type or ChartDataResultType.FULL
         result_format = result_format or ChartDataResultFormat.JSON
 
+        if (
+            self._authorize_semantic_before_metadata
+            and datasource_model_instance is not None
+            and DatasourceType(datasource["type"]) == 
DatasourceType.SEMANTIC_VIEW
+        ):
+            # Guest dashboard and payload checks need the completed query 
context;
+            # keep their authorization path and timing unchanged.
+            if not security_manager.is_guest_user():
+                QueryContext(
+                    datasource=datasource_model_instance,
+                    queries=[],
+                    slice_=slice_,
+                    form_data=form_data,
+                    result_type=result_type,
+                    result_format=result_format,
+                    cache_values={},
+                ).raise_for_access()

Review Comment:
   You're right. In that mode the drill-by decision itself needs the view's 
dimension names (`has_drill_by_columns`), so a denied drill still makes one 
provider call, down from two. That call is part of the authorization decision, 
not metadata loaded ahead of it, so I've kept the code as is and corrected the 
description: denied requests skip provider metadata *except* where the access 
decision depends on it, i.e. viewer drill-by. Addressed in the PR description, 
updated with `b5eeb463f1`.



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