From ee624b64aa73aa597b16ece673c8d911a1349b2b Mon Sep 17 00:00:00 2001 From: luizotavio32 Date: Wed, 18 Nov 2026 17:10:51 -0300 Subject: [PATCH 1/2] fix(permalink): return 404 instead of 500 for an invalid dashboard permalink key Co-Authored-By: Claude Opus 5.5 --- superset/commands/dashboard/permalink/get.py | 8 ++- superset/dashboards/permalink/api.py | 7 ++- superset/dashboards/permalink/exceptions.py | 5 ++ .../dashboards/permalink/api_tests.py | 6 ++ .../commands/dashboard/permalink_get_test.py | 55 +++++++++++++++++++ 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/unit_tests/commands/dashboard/permalink_get_test.py diff --git a/superset/commands/dashboard/permalink/get.py b/superset/commands/dashboard/permalink/get.py index e87711a5bfeb..afa84409d855 100644 --- a/superset/commands/dashboard/permalink/get.py +++ b/superset/commands/dashboard/permalink/get.py @@ -23,7 +23,10 @@ from superset.commands.dashboard.permalink.base import BaseDashboardPermalinkCommand from superset.daos.dashboard import DashboardDAO from superset.daos.key_value import KeyValueDAO -from superset.dashboards.permalink.exceptions import DashboardPermalinkGetFailedError +from superset.dashboards.permalink.exceptions import ( + DashboardPermalinkGetFailedError, + DashboardPermalinkInvalidKeyError, +) from superset.dashboards.permalink.types import DashboardPermalinkValue from superset.key_value.exceptions import ( KeyValueCodecDecodeException, @@ -48,11 +51,12 @@ def run(self) -> Optional[DashboardPermalinkValue]: DashboardDAO.get_by_id_or_slug(value["dashboardId"]) return value return None + except KeyValueParseKeyError as ex: + raise DashboardPermalinkInvalidKeyError() from ex except ( DashboardNotFoundError, KeyValueCodecDecodeException, KeyValueGetFailedError, - KeyValueParseKeyError, ) as ex: raise DashboardPermalinkGetFailedError(message=ex.message) from ex except SQLAlchemyError as ex: diff --git a/superset/dashboards/permalink/api.py b/superset/dashboards/permalink/api.py index 14e6d3cb0486..61abb9a96700 100644 --- a/superset/dashboards/permalink/api.py +++ b/superset/dashboards/permalink/api.py @@ -27,7 +27,10 @@ from superset.commands.dashboard.permalink.create import CreateDashboardPermalinkCommand from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP -from superset.dashboards.permalink.exceptions import DashboardPermalinkInvalidStateError +from superset.dashboards.permalink.exceptions import ( + DashboardPermalinkInvalidKeyError, + DashboardPermalinkInvalidStateError, +) from superset.dashboards.permalink.schemas import DashboardPermalinkStateSchema from superset.extensions import event_logger from superset.key_value.exceptions import KeyValueAccessDeniedError @@ -225,5 +228,5 @@ def get(self, key: str) -> Response: return self.response(200, **value) except DashboardAccessDeniedError as ex: return self.response(403, message=str(ex)) - except DashboardNotFoundError as ex: + except (DashboardNotFoundError, DashboardPermalinkInvalidKeyError) as ex: return self.response(404, message=str(ex)) diff --git a/superset/dashboards/permalink/exceptions.py b/superset/dashboards/permalink/exceptions.py index c234d614f1a2..c29f543f7acd 100644 --- a/superset/dashboards/permalink/exceptions.py +++ b/superset/dashboards/permalink/exceptions.py @@ -29,3 +29,8 @@ class DashboardPermalinkCreateFailedError(CreateFailedError): class DashboardPermalinkGetFailedError(CommandException): message = _("An error occurred while accessing the value.") + + +class DashboardPermalinkInvalidKeyError(DashboardPermalinkGetFailedError): + status = 404 + message = _("Invalid permalink key.") diff --git a/tests/integration_tests/dashboards/permalink/api_tests.py b/tests/integration_tests/dashboards/permalink/api_tests.py index b2c3b3b1b6fa..288d7e3119a8 100644 --- a/tests/integration_tests/dashboards/permalink/api_tests.py +++ b/tests/integration_tests/dashboards/permalink/api_tests.py @@ -114,6 +114,12 @@ def test_get(dashboard_id: int, permalink_salt: str, test_client, login_as_admin db.session.commit() +def test_get_invalid_key(test_client, login_as_admin): + resp = test_client.get("api/v1/dashboard/permalink/undefined") + assert resp.status_code == 404 + assert resp.json["message"] == "Invalid permalink key." + + @pytest.fixture def standalone_dashboard_id(app_context) -> Iterator[int]: dashboard = Dashboard(dashboard_title="permalink_denial", published=True) diff --git a/tests/unit_tests/commands/dashboard/permalink_get_test.py b/tests/unit_tests/commands/dashboard/permalink_get_test.py new file mode 100644 index 000000000000..43dfac26e4ed --- /dev/null +++ b/tests/unit_tests/commands/dashboard/permalink_get_test.py @@ -0,0 +1,55 @@ +# 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. +from unittest.mock import patch, PropertyMock + +import pytest +from sqlalchemy.exc import SQLAlchemyError + +from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand +from superset.dashboards.permalink.exceptions import ( + DashboardPermalinkGetFailedError, + DashboardPermalinkInvalidKeyError, +) + +MODULE = "superset.commands.dashboard.permalink.get" + + +@pytest.fixture(autouse=True) +def salt(): + with patch( + f"{MODULE}.GetDashboardPermalinkCommand.salt", + new_callable=PropertyMock, + return_value="test-salt", + ): + yield + + +def test_invalid_key_raises_invalid_key_error() -> None: + with pytest.raises(DashboardPermalinkInvalidKeyError) as excinfo: + GetDashboardPermalinkCommand("undefined").run() + assert excinfo.value.status == 404 + + +def test_storage_failure_still_raises_generic_error() -> None: + with ( + patch(f"{MODULE}.decode_permalink_id", return_value=1), + patch(f"{MODULE}.KeyValueDAO.get_value", side_effect=SQLAlchemyError()), + pytest.raises(DashboardPermalinkGetFailedError) as excinfo, + ): + GetDashboardPermalinkCommand("abc").run() + assert not isinstance(excinfo.value, DashboardPermalinkInvalidKeyError) + assert excinfo.value.status == 500 From 3aa6615aa79c8380d36a38e99cb419d672512f65 Mon Sep 17 00:00:00 2001 From: luizotavio32 Date: Wed, 18 Nov 2026 17:37:00 -0300 Subject: [PATCH 2/2] fix(permalink): reuse the existing translated invalid-key message Co-Authored-By: Claude Opus 5.5 --- superset/dashboards/permalink/exceptions.py | 2 +- tests/integration_tests/dashboards/permalink/api_tests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/superset/dashboards/permalink/exceptions.py b/superset/dashboards/permalink/exceptions.py index c29f543f7acd..a3cfe56c2829 100644 --- a/superset/dashboards/permalink/exceptions.py +++ b/superset/dashboards/permalink/exceptions.py @@ -33,4 +33,4 @@ class DashboardPermalinkGetFailedError(CommandException): class DashboardPermalinkInvalidKeyError(DashboardPermalinkGetFailedError): status = 404 - message = _("Invalid permalink key.") + message = _("Invalid permalink key") diff --git a/tests/integration_tests/dashboards/permalink/api_tests.py b/tests/integration_tests/dashboards/permalink/api_tests.py index 288d7e3119a8..9eb74734100b 100644 --- a/tests/integration_tests/dashboards/permalink/api_tests.py +++ b/tests/integration_tests/dashboards/permalink/api_tests.py @@ -117,7 +117,7 @@ def test_get(dashboard_id: int, permalink_salt: str, test_client, login_as_admin def test_get_invalid_key(test_client, login_as_admin): resp = test_client.get("api/v1/dashboard/permalink/undefined") assert resp.status_code == 404 - assert resp.json["message"] == "Invalid permalink key." + assert resp.json["message"] == "Invalid permalink key" @pytest.fixture