This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit c515cd331c7c6ed3577f46f4e69e55db4881be11 Author: Shaitan <[email protected]> AuthorDate: Tue Nov 10 15:12:31 2026 +0000 fix(charts): coerce chart and dashboard ids in the query log context (#45454) Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Evan Rusackas <[email protected]> (cherry picked from commit 360648e3a7d0ba029b50549419b7da1946b422fa) --- superset/charts/data/api.py | 15 +++++- .../unit_tests/charts/data/test_log_context_ids.py | 58 ++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/superset/charts/data/api.py b/superset/charts/data/api.py index c456e11e137..a7326e6158f 100644 --- a/superset/charts/data/api.py +++ b/superset/charts/data/api.py @@ -716,11 +716,22 @@ class ChartDataRestApi(ChartRestApi): return filename, expected_rows + @staticmethod + def _as_int(value: Any) -> int | None: + """Coerce a request-supplied id to an int, or None when it isn't one.""" + try: + return int(value) + except (TypeError, ValueError, OverflowError): + return None + def _map_form_data_datasource_to_dataset_id( self, form_data: dict[str, Any] ) -> dict[str, Any]: + inner = form_data.get("form_data", {}) return { - "dashboard_id": form_data.get("form_data", {}).get("dashboardId"), + # dashboardId / slice_id are request-supplied; keep them as ints (or + # None) rather than passing arbitrary strings into the log context. + "dashboard_id": self._as_int(inner.get("dashboardId")), "dataset_id": ( form_data.get("datasource", {}).get("id") if isinstance(form_data.get("datasource"), dict) @@ -728,7 +739,7 @@ class ChartDataRestApi(ChartRestApi): == DatasourceType.TABLE.value else None ), - "slice_id": form_data.get("form_data", {}).get("slice_id"), + "slice_id": self._as_int(inner.get("slice_id")), } @logs_context(context_func=_map_form_data_datasource_to_dataset_id) diff --git a/tests/unit_tests/charts/data/test_log_context_ids.py b/tests/unit_tests/charts/data/test_log_context_ids.py new file mode 100644 index 00000000000..87e33ae478e --- /dev/null +++ b/tests/unit_tests/charts/data/test_log_context_ids.py @@ -0,0 +1,58 @@ +# 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. +"""Chart/dashboard ids written to the log context are coerced to ints.""" + +from __future__ import annotations + +from typing import Any + +import pytest + + [email protected]( + "value,expected", + [ + (5, 5), + ("5", 5), + (None, None), + ("", None), + ("not-an-int", None), + ("5\nextra", None), + ({"nested": "dict"}, None), + (float("inf"), None), + (float("nan"), None), + ], +) +def test_as_int_coerces_or_drops(value: Any, expected: int | None) -> None: + from superset.charts.data.api import ChartDataRestApi + + assert ChartDataRestApi._as_int(value) == expected + + +def test_map_form_data_keeps_ids_as_ints(app_context: None) -> None: + from superset.charts.data.api import ChartDataRestApi + + api = ChartDataRestApi() + mapped = api._map_form_data_datasource_to_dataset_id( + { + "form_data": {"dashboardId": "7", "slice_id": "9\nextra"}, + "datasource": {"id": 3, "type": "table"}, + } + ) + assert mapped["dashboard_id"] == 7 + assert mapped["slice_id"] is None # non-int string is dropped, not carried through + assert mapped["dataset_id"] == 3
