mirror of
https://github.com/apache/superset.git
synced 2026-06-11 10:39:15 +00:00
Compare commits
5 Commits
fix/chart-
...
fix/securi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e0b430a03 | ||
|
|
b725e11199 | ||
|
|
95b46bcf02 | ||
|
|
7f804262ed | ||
|
|
2063484ef4 |
@@ -103,19 +103,6 @@ class DatasourceTypeUpdateRequiredValidationError(ValidationError):
|
||||
)
|
||||
|
||||
|
||||
class ChartQueryContextDatasourceMismatchValidationError(ValidationError):
|
||||
"""
|
||||
Raised when a query-context-only update carries a datasource that does not
|
||||
match the chart's own datasource.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(
|
||||
_("The query context datasource does not match the chart datasource"),
|
||||
field_name="query_context",
|
||||
)
|
||||
|
||||
|
||||
class ChartNotFoundError(CommandException):
|
||||
message = "Chart not found."
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ from superset.commands.chart.exceptions import (
|
||||
ChartForbiddenError,
|
||||
ChartInvalidError,
|
||||
ChartNotFoundError,
|
||||
ChartQueryContextDatasourceMismatchValidationError,
|
||||
ChartUpdateFailedError,
|
||||
DashboardsForbiddenError,
|
||||
DashboardsNotFoundValidationError,
|
||||
@@ -42,7 +41,6 @@ from superset.exceptions import SupersetSecurityException
|
||||
from superset.models.dashboard import Dashboard
|
||||
from superset.models.slice import Slice
|
||||
from superset.tags.models import ObjectType
|
||||
from superset.utils import json
|
||||
from superset.utils.decorators import on_error, transaction
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -103,51 +101,6 @@ class UpdateChartCommand(UpdateMixin, BaseCommand):
|
||||
if not security_manager.is_owner(dash):
|
||||
raise DashboardsForbiddenError()
|
||||
|
||||
def _validate_query_context_datasource(
|
||||
self, exceptions: list[ValidationError]
|
||||
) -> None:
|
||||
"""
|
||||
Ensure a query-context-only update keeps the chart's own datasource.
|
||||
|
||||
The submitted query context is only verified when it carries a parseable
|
||||
``datasource`` object; a payload that references a different datasource than
|
||||
the chart's persisted one is rejected. Payloads without a datasource fall
|
||||
back to the chart's datasource at execution time and need no check.
|
||||
"""
|
||||
if not self._model:
|
||||
return
|
||||
|
||||
raw_query_context = self._properties.get("query_context")
|
||||
if not raw_query_context:
|
||||
return
|
||||
|
||||
try:
|
||||
query_context = json.loads(raw_query_context)
|
||||
except (TypeError, ValueError):
|
||||
# An unparseable payload cannot be verified or replayed; leave it for
|
||||
# downstream handling rather than guessing at its intent.
|
||||
return
|
||||
|
||||
datasource = (
|
||||
query_context.get("datasource") if isinstance(query_context, dict) else None
|
||||
)
|
||||
if not isinstance(datasource, dict):
|
||||
return
|
||||
|
||||
try:
|
||||
ids_match = int(datasource["id"]) == self._model.datasource_id
|
||||
except (KeyError, TypeError, ValueError):
|
||||
ids_match = False
|
||||
|
||||
datasource_type = datasource.get("type")
|
||||
types_match = (
|
||||
datasource_type is None
|
||||
or str(datasource_type) == self._model.datasource_type
|
||||
)
|
||||
|
||||
if not ids_match or not types_match:
|
||||
exceptions.append(ChartQueryContextDatasourceMismatchValidationError())
|
||||
|
||||
def validate(self) -> None: # noqa: C901
|
||||
exceptions: list[ValidationError] = []
|
||||
dashboard_ids = self._properties.get("dashboards")
|
||||
@@ -181,12 +134,6 @@ class UpdateChartCommand(UpdateMixin, BaseCommand):
|
||||
raise ChartForbiddenError() from ex
|
||||
except ValidationError as ex:
|
||||
exceptions.append(ex)
|
||||
else:
|
||||
# The query-context-only path skips the ownership check so report and
|
||||
# alert workers can refresh a chart's cached payload. Keep that payload
|
||||
# bound to the chart's own datasource so it cannot be repointed at an
|
||||
# unrelated one.
|
||||
self._validate_query_context_datasource(exceptions)
|
||||
|
||||
# validate tags
|
||||
try:
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
# 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.
|
||||
"""fix Security view menu case
|
||||
|
||||
Revision ID: b4a3f2e1d0c9
|
||||
Revises: 31dae2559c05
|
||||
Create Date: 2026-05-29 00:00:00.000000
|
||||
|
||||
On MySQL with the default case-insensitive collation, five Superset views that
|
||||
declare ``class_permission_name = "security"`` (lowercase) cause FAB to look up
|
||||
or insert a view-menu entry named ``"security"``. MySQL's case-insensitive
|
||||
``UNIQUE`` constraint means the lookup finds an existing ``"Security"`` row and
|
||||
uses it, OR a fresh install stores ``"security"`` because that view is
|
||||
registered before the FAB built-in views register ``"Security"``.
|
||||
|
||||
Python string comparisons in ``sync_role_definitions`` and the menu-hiding
|
||||
logic are case-sensitive, so an all-lowercase ``"security"`` row breaks the
|
||||
Security menu for MySQL (and MariaDB) deployments.
|
||||
|
||||
This migration normalises the row:
|
||||
* If only the lowercase ``"security"`` row exists: rename it to ``"Security"``.
|
||||
* If both rows exist (SQLite / PostgreSQL fresh-install with the old code):
|
||||
reassign every ``ab_permission_view`` from the lowercase row to the
|
||||
correctly-cased row, then delete the duplicate.
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "b4a3f2e1d0c9"
|
||||
down_revision = "31dae2559c05"
|
||||
|
||||
import logging # noqa: E402
|
||||
|
||||
from alembic import op # noqa: E402
|
||||
from sqlalchemy.orm import Session # noqa: E402
|
||||
|
||||
from superset.migrations.shared.security_converge import ( # noqa: E402
|
||||
PermissionView,
|
||||
ViewMenu,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("alembic.env")
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
session = Session(bind=bind)
|
||||
|
||||
# Fetch all view menus and compare in Python (SQL filter_by is
|
||||
# case-insensitive on MySQL, so we cannot rely on it here).
|
||||
all_vms: list[ViewMenu] = session.query(ViewMenu).all()
|
||||
security_upper = next((vm for vm in all_vms if vm.name == "Security"), None)
|
||||
security_lower = next((vm for vm in all_vms if vm.name == "security"), None)
|
||||
|
||||
if security_lower is None:
|
||||
logger.info("No lowercase 'security' view-menu found; nothing to do.")
|
||||
return
|
||||
|
||||
if security_upper is None:
|
||||
# Simple rename — only the incorrect row exists.
|
||||
logger.info(
|
||||
"Renaming ab_view_menu 'security' -> 'Security' (id=%d)",
|
||||
security_lower.id,
|
||||
)
|
||||
security_lower.name = "Security"
|
||||
session.flush()
|
||||
else:
|
||||
# Both rows exist. Re-home every permission_view from the lowercase
|
||||
# entry to the correctly-cased entry, then drop the duplicate.
|
||||
logger.info(
|
||||
"Both 'security' (id=%d) and 'Security' (id=%d) found; merging.",
|
||||
security_lower.id,
|
||||
security_upper.id,
|
||||
)
|
||||
pvms_lower: list[PermissionView] = (
|
||||
session.query(PermissionView)
|
||||
.filter(PermissionView.view_menu_id == security_lower.id)
|
||||
.all()
|
||||
)
|
||||
for pvm in pvms_lower:
|
||||
upper_pvm = (
|
||||
session.query(PermissionView)
|
||||
.filter(
|
||||
PermissionView.view_menu_id == security_upper.id,
|
||||
PermissionView.permission_id == pvm.permission_id,
|
||||
)
|
||||
.one_or_none()
|
||||
)
|
||||
if upper_pvm:
|
||||
# Transfer role bindings from the duplicate row to the
|
||||
# surviving row before discarding the duplicate, so no
|
||||
# role silently loses a permission.
|
||||
for role in list(pvm.role):
|
||||
if upper_pvm not in role.permissions:
|
||||
role.permissions.append(upper_pvm)
|
||||
session.flush()
|
||||
session.delete(pvm)
|
||||
else:
|
||||
pvm.view_menu_id = security_upper.id
|
||||
session.flush()
|
||||
session.delete(security_lower)
|
||||
|
||||
session.commit()
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# There is no safe way to determine the original state (whether the row was
|
||||
# lowercase or whether there were two rows), so downgrade is a no-op.
|
||||
pass
|
||||
@@ -25,7 +25,7 @@ from .base import BaseSupersetView
|
||||
|
||||
class GroupsListView(BaseSupersetView):
|
||||
route_base = "/"
|
||||
class_permission_name = "security"
|
||||
class_permission_name = "Security"
|
||||
|
||||
@expose("/list_groups/")
|
||||
@has_access
|
||||
|
||||
@@ -25,7 +25,7 @@ from .base import BaseSupersetView
|
||||
|
||||
class ActionLogView(BaseSupersetView):
|
||||
route_base = "/"
|
||||
class_permission_name = "security"
|
||||
class_permission_name = "Security"
|
||||
|
||||
@expose("/actionlog/list")
|
||||
@has_access
|
||||
|
||||
@@ -25,7 +25,7 @@ from .base import BaseSupersetView
|
||||
|
||||
class RolesListView(BaseSupersetView):
|
||||
route_base = "/"
|
||||
class_permission_name = "security"
|
||||
class_permission_name = "Security"
|
||||
|
||||
@expose("/roles/")
|
||||
@has_access
|
||||
|
||||
@@ -25,7 +25,7 @@ from .base import BaseSupersetView
|
||||
|
||||
class UserRegistrationsView(BaseSupersetView):
|
||||
route_base = "/"
|
||||
class_permission_name = "security"
|
||||
class_permission_name = "Security"
|
||||
|
||||
@expose("/registrations/")
|
||||
@has_access
|
||||
|
||||
@@ -25,7 +25,7 @@ from .base import BaseSupersetView
|
||||
|
||||
class UsersListView(BaseSupersetView):
|
||||
route_base = "/"
|
||||
class_permission_name = "security"
|
||||
class_permission_name = "Security"
|
||||
|
||||
@expose("/users/")
|
||||
@has_access
|
||||
|
||||
@@ -17,11 +17,10 @@
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from superset.commands.chart.exceptions import ChartForbiddenError, ChartInvalidError
|
||||
from superset.commands.chart.exceptions import ChartForbiddenError
|
||||
from superset.commands.chart.update import UpdateChartCommand
|
||||
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
|
||||
from superset.exceptions import SupersetSecurityException
|
||||
from superset.utils import json
|
||||
|
||||
|
||||
def _ownership_exc() -> SupersetSecurityException:
|
||||
@@ -92,73 +91,3 @@ def test_update_chart_owner_can_perform_regular_update(
|
||||
|
||||
find_by_id.assert_called_once_with(1)
|
||||
raise_for_ownership.assert_called_once()
|
||||
|
||||
|
||||
def _query_context_payload(datasource: object) -> dict[str, object]:
|
||||
return {
|
||||
"query_context": json.dumps({"datasource": datasource, "queries": []}),
|
||||
"query_context_generation": True,
|
||||
}
|
||||
|
||||
|
||||
def test_update_chart_query_context_matching_datasource_is_allowed(
|
||||
mocker: MockerFixture,
|
||||
) -> None:
|
||||
"""A query context that targets the chart's own datasource is accepted."""
|
||||
find_by_id = mocker.patch("superset.commands.chart.update.ChartDAO.find_by_id")
|
||||
find_by_id.return_value = mocker.MagicMock(
|
||||
id=1, tags=[], dashboards=[], datasource_id=42, datasource_type="table"
|
||||
)
|
||||
mocker.patch("superset.commands.chart.update.security_manager.raise_for_ownership")
|
||||
|
||||
UpdateChartCommand(
|
||||
1, _query_context_payload({"id": 42, "type": "table"})
|
||||
).validate()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"datasource",
|
||||
[
|
||||
{"id": 99, "type": "table"}, # different id
|
||||
{"id": 42, "type": "query"}, # different type
|
||||
{"id": "99", "type": "table"}, # different id as string
|
||||
],
|
||||
)
|
||||
def test_update_chart_query_context_mismatched_datasource_is_rejected(
|
||||
mocker: MockerFixture,
|
||||
datasource: dict[str, object],
|
||||
) -> None:
|
||||
"""A query context pointing at a different datasource is rejected with a 4xx."""
|
||||
find_by_id = mocker.patch("superset.commands.chart.update.ChartDAO.find_by_id")
|
||||
find_by_id.return_value = mocker.MagicMock(
|
||||
id=1, tags=[], dashboards=[], datasource_id=42, datasource_type="table"
|
||||
)
|
||||
mocker.patch("superset.commands.chart.update.security_manager.raise_for_ownership")
|
||||
|
||||
with pytest.raises(ChartInvalidError):
|
||||
UpdateChartCommand(1, _query_context_payload(datasource)).validate()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"query_context",
|
||||
[
|
||||
"{}", # no datasource key
|
||||
'{"datasource": null}', # null datasource
|
||||
"not-json", # unparseable payload
|
||||
],
|
||||
)
|
||||
def test_update_chart_query_context_without_datasource_is_allowed(
|
||||
mocker: MockerFixture,
|
||||
query_context: str,
|
||||
) -> None:
|
||||
"""Payloads with no verifiable datasource fall back to the chart's own."""
|
||||
find_by_id = mocker.patch("superset.commands.chart.update.ChartDAO.find_by_id")
|
||||
find_by_id.return_value = mocker.MagicMock(
|
||||
id=1, tags=[], dashboards=[], datasource_id=42, datasource_type="table"
|
||||
)
|
||||
mocker.patch("superset.commands.chart.update.security_manager.raise_for_ownership")
|
||||
|
||||
UpdateChartCommand(
|
||||
1,
|
||||
{"query_context": query_context, "query_context_generation": True},
|
||||
).validate()
|
||||
|
||||
Reference in New Issue
Block a user