Skip to content

Commit c9019b5

Browse files
luizotavio32claude
andcommitted
fix(permalink): return 404 instead of 500 for an invalid dashboard permalink key (#46332)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 8f6df58)
1 parent 10a80fb commit c9019b5

5 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎superset/commands/dashboard/permalink/get.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
from superset.commands.dashboard.permalink.base import BaseDashboardPermalinkCommand
2424
from superset.daos.dashboard import DashboardDAO
2525
from superset.daos.key_value import KeyValueDAO
26-
from superset.dashboards.permalink.exceptions import DashboardPermalinkGetFailedError
26+
from superset.dashboards.permalink.exceptions import (
27+
DashboardPermalinkGetFailedError,
28+
DashboardPermalinkInvalidKeyError,
29+
)
2730
from superset.dashboards.permalink.types import DashboardPermalinkValue
2831
from superset.key_value.exceptions import (
2932
KeyValueCodecDecodeException,
@@ -48,11 +51,12 @@ def run(self) -> Optional[DashboardPermalinkValue]:
4851
DashboardDAO.get_by_id_or_slug(value["dashboardId"])
4952
return value
5053
return None
54+
except KeyValueParseKeyError as ex:
55+
raise DashboardPermalinkInvalidKeyError() from ex
5156
except (
5257
DashboardNotFoundError,
5358
KeyValueCodecDecodeException,
5459
KeyValueGetFailedError,
55-
KeyValueParseKeyError,
5660
) as ex:
5761
raise DashboardPermalinkGetFailedError(message=ex.message) from ex
5862
except SQLAlchemyError as ex:

‎superset/dashboards/permalink/api.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
from superset.commands.dashboard.permalink.create import CreateDashboardPermalinkCommand
2828
from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand
2929
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP
30-
from superset.dashboards.permalink.exceptions import DashboardPermalinkInvalidStateError
30+
from superset.dashboards.permalink.exceptions import (
31+
DashboardPermalinkInvalidKeyError,
32+
DashboardPermalinkInvalidStateError,
33+
)
3134
from superset.dashboards.permalink.schemas import DashboardPermalinkStateSchema
3235
from superset.extensions import event_logger
3336
from superset.key_value.exceptions import KeyValueAccessDeniedError
@@ -225,5 +228,5 @@ def get(self, key: str) -> Response:
225228
return self.response(200, **value)
226229
except DashboardAccessDeniedError as ex:
227230
return self.response(403, message=str(ex))
228-
except DashboardNotFoundError as ex:
231+
except (DashboardNotFoundError, DashboardPermalinkInvalidKeyError) as ex:
229232
return self.response(404, message=str(ex))

‎superset/dashboards/permalink/exceptions.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ class DashboardPermalinkCreateFailedError(CreateFailedError):
2929

3030
class DashboardPermalinkGetFailedError(CommandException):
3131
message = _("An error occurred while accessing the value.")
32+
33+
34+
class DashboardPermalinkInvalidKeyError(DashboardPermalinkGetFailedError):
35+
status = 404
36+
message = _("Invalid permalink key")

‎tests/integration_tests/dashboards/permalink/api_tests.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,10 @@ def test_get(dashboard_id: int, permalink_salt: str, test_client, login_as_admin
112112
id_ = decode_permalink_id(key, permalink_salt)
113113
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
114114
db.session.commit()
115+
116+
117+
def test_get_invalid_key(test_client, login_as_admin):
118+
resp = test_client.get("api/v1/dashboard/permalink/undefined")
119+
assert resp.status_code == 404
120+
assert resp.json["message"] == "Invalid permalink key"
121+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from unittest.mock import patch, PropertyMock
18+
19+
import pytest
20+
from sqlalchemy.exc import SQLAlchemyError
21+
22+
from superset.commands.dashboard.permalink.get import GetDashboardPermalinkCommand
23+
from superset.dashboards.permalink.exceptions import (
24+
DashboardPermalinkGetFailedError,
25+
DashboardPermalinkInvalidKeyError,
26+
)
27+
28+
MODULE = "superset.commands.dashboard.permalink.get"
29+
30+
31+
@pytest.fixture(autouse=True)
32+
def salt():
33+
with patch(
34+
f"{MODULE}.GetDashboardPermalinkCommand.salt",
35+
new_callable=PropertyMock,
36+
return_value="test-salt",
37+
):
38+
yield
39+
40+
41+
def test_invalid_key_raises_invalid_key_error() -> None:
42+
with pytest.raises(DashboardPermalinkInvalidKeyError) as excinfo:
43+
GetDashboardPermalinkCommand("undefined").run()
44+
assert excinfo.value.status == 404
45+
46+
47+
def test_storage_failure_still_raises_generic_error() -> None:
48+
with (
49+
patch(f"{MODULE}.decode_permalink_id", return_value=1),
50+
patch(f"{MODULE}.KeyValueDAO.get_value", side_effect=SQLAlchemyError()),
51+
pytest.raises(DashboardPermalinkGetFailedError) as excinfo,
52+
):
53+
GetDashboardPermalinkCommand("abc").run()
54+
assert not isinstance(excinfo.value, DashboardPermalinkInvalidKeyError)
55+
assert excinfo.value.status == 500

0 commit comments

Comments
 (0)