Skip to content

Commit d9f4f48

Browse files
fix(explore): validate dashboard target before saving chart (#46324) (#46351)
1 parent 483542e commit d9f4f48

3 files changed

Lines changed: 247 additions & 15 deletions

File tree

‎superset/translations/messages.pot‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ msgid ""
2525
msgstr ""
2626
"Project-Id-Version: Superset VERSION\n"
2727
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
28-
"POT-Creation-Date: 2026-11-19 09:45+1100\n"
28+
"POT-Creation-Date: 2026-11-19 04:39+0000\n"
2929
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
3030
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
3131
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -4761,6 +4761,10 @@ msgstr ""
47614761
msgid "Dashboard %(dashboard_id)s not found"
47624762
msgstr ""
47634763

4764+
#, python-format
4765+
msgid "Dashboard %(id)s not found"
4766+
msgstr ""
4767+
47644768
msgid "Dashboard Filter"
47654769
msgstr ""
47664770

@@ -8223,6 +8227,10 @@ msgstr ""
82238227
msgid "Invalid currency code in saved metrics"
82248228
msgstr ""
82258229

8230+
#, python-format
8231+
msgid "Invalid dashboard id: %(id)s"
8232+
msgstr ""
8233+
82268234
msgid "Invalid date/timestamp format"
82278235
msgstr ""
82288236

‎superset/views/core.py‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -421,26 +421,27 @@ def save_or_overwrite_slice( # noqa: C901
421421
slc.slice_name = slice_name
422422
slc.query_context = query_context
423423

424-
if action == "saveas" and slice_add_perm:
425-
ChartDAO.create(slc)
426-
db.session.commit() # pylint: disable=consider-using-transaction
427-
elif action == "overwrite" and slice_overwrite_perm:
428-
ChartDAO.update(slc)
429-
db.session.commit() # pylint: disable=consider-using-transaction
430-
431-
# Adding slice to a dashboard if requested
424+
# Resolve and validate the dashboard target BEFORE saving the
425+
# chart: a failed dashboard check must not leave a chart behind.
432426
dash: Dashboard | None = None
433427

434428
save_to_dashboard_id = request.args.get("save_to_dashboard_id")
435429
new_dashboard_name = request.args.get("new_dashboard_name")
436430
if save_to_dashboard_id:
437431
# Adding the chart to an existing dashboard
438-
dash = cast(
439-
Dashboard,
440-
db.session.query(Dashboard)
441-
.filter_by(id=int(save_to_dashboard_id))
442-
.one(),
443-
)
432+
try:
433+
dashboard_id = int(save_to_dashboard_id)
434+
except ValueError:
435+
return json_error_response(
436+
__("Invalid dashboard id: %(id)s", id=save_to_dashboard_id),
437+
status=400,
438+
)
439+
dash = db.session.query(Dashboard).filter_by(id=dashboard_id).one_or_none()
440+
if dash is None:
441+
return json_error_response(
442+
__("Dashboard %(id)s not found", id=save_to_dashboard_id),
443+
status=404,
444+
)
444445
# check edit dashboard permissions
445446
dash_overwrite_perm = security_manager.is_editor(dash)
446447
if not dash_overwrite_perm:
@@ -458,6 +459,15 @@ def save_or_overwrite_slice( # noqa: C901
458459
status=403,
459460
)
460461

462+
if action == "saveas" and slice_add_perm:
463+
ChartDAO.create(slc)
464+
db.session.commit() # pylint: disable=consider-using-transaction
465+
elif action == "overwrite" and slice_overwrite_perm:
466+
ChartDAO.update(slc)
467+
db.session.commit() # pylint: disable=consider-using-transaction
468+
469+
if new_dashboard_name and dash is None:
470+
# Creating the new dashboard now that the chart is saved
461471
from superset.subjects.utils import (
462472
get_default_viewers_for_new_asset,
463473
get_user_subject,

‎tests/integration_tests/core_tests.py‎

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,220 @@ def test_save_slice(self):
225225
db.session.delete(slc)
226226
db.session.commit()
227227

228+
@pytest.mark.usefixtures("load_energy_table_with_slice")
229+
def test_save_slice_to_forbidden_dashboard_does_not_persist(self):
230+
"""Saving a chart to a dashboard the user cannot edit must fail
231+
with 403 and persist nothing (fix for #46324).
232+
233+
The legacy explore save committed the chart before the dashboard
234+
permission check, so the 403 response still left the chart saved.
235+
The refusal message is asserted (not just the status) so a 403
236+
from an unrelated check cannot satisfy the pin.
237+
"""
238+
admin = self.get_user(ADMIN_USERNAME)
239+
dashboard = self.insert_dashboard(
240+
"Forbidden Dashboard",
241+
"forbidden-dashboard-46324",
242+
[admin.id],
243+
created_by=admin,
244+
)
245+
try:
246+
tbl_id = self.table_ids.get("energy_usage")
247+
slice_name = f"Unsaved Slice {random.random()}" # noqa: S311
248+
form_data = {
249+
"adhoc_filters": [],
250+
"viz_type": "sankey",
251+
"groupby": ["target"],
252+
"metric": "sum__value",
253+
"row_limit": 5000,
254+
}
255+
table = self.get_table("energy_usage")
256+
gamma = self.get_user(GAMMA_USERNAME)
257+
# Drop the admin session before impersonating gamma: logging
258+
# in as the temporary user does not replace an already
259+
# authenticated session, so the admin would otherwise leak
260+
# into these checks.
261+
self.logout()
262+
with self.temporary_user(
263+
gamma,
264+
extra_pvms=[("datasource_access", table.perm)],
265+
login=True,
266+
):
267+
resp = self.client.post(
268+
"/explore/table/{}/?slice_name={}&action=saveas"
269+
"&save_to_dashboard_id={}".format(tbl_id, slice_name, dashboard.id),
270+
data={"form_data": json.dumps(form_data)},
271+
)
272+
assert resp.status_code == 403
273+
assert (
274+
"You don't have the rights to alter this dashboard"
275+
in resp.json["error"]
276+
)
277+
leaked = (
278+
db.session.query(Slice)
279+
.filter_by(slice_name=slice_name)
280+
.one_or_none()
281+
)
282+
# Clean up the leaked chart (the bug under test) so the
283+
# temporary user's teardown can still delete the user:
284+
# the chart references the user, and leaving it behind
285+
# breaks teardown with a foreign-key error.
286+
if leaked is not None:
287+
db.session.delete(leaked)
288+
db.session.commit()
289+
assert leaked is None
290+
finally:
291+
db.session.delete(dashboard)
292+
db.session.commit()
293+
294+
@pytest.mark.usefixtures("load_energy_table_with_slice")
295+
def test_save_slice_invalid_dashboard_id_returns_400(self):
296+
"""A non-numeric save_to_dashboard_id must fail with 400 and
297+
persist nothing (fix for #46324).
298+
299+
The id was parsed with an unguarded int() after the chart had
300+
already been committed, so the request 500ed and still saved.
301+
"""
302+
self.login(ADMIN_USERNAME)
303+
tbl_id = self.table_ids.get("energy_usage")
304+
slice_name = f"Unsaved Slice {random.random()}" # noqa: S311
305+
form_data = {
306+
"adhoc_filters": [],
307+
"viz_type": "sankey",
308+
"groupby": ["target"],
309+
"metric": "sum__value",
310+
"row_limit": 5000,
311+
}
312+
resp = self.client.post(
313+
"/explore/table/{}/?slice_name={}&action=saveas"
314+
"&save_to_dashboard_id=abc".format(tbl_id, slice_name),
315+
data={"form_data": json.dumps(form_data)},
316+
)
317+
assert resp.status_code == 400
318+
assert (
319+
db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
320+
is None
321+
)
322+
323+
@pytest.mark.usefixtures("load_energy_table_with_slice")
324+
def test_save_slice_missing_dashboard_id_returns_404(self):
325+
"""A nonexistent save_to_dashboard_id must fail with 404 and
326+
persist nothing (fix for #46324).
327+
328+
The dashboard was loaded with an unguarded .one() after the
329+
chart had already been committed, so the request 500ed and
330+
still saved.
331+
"""
332+
self.login(ADMIN_USERNAME)
333+
tbl_id = self.table_ids.get("energy_usage")
334+
slice_name = f"Unsaved Slice {random.random()}" # noqa: S311
335+
form_data = {
336+
"adhoc_filters": [],
337+
"viz_type": "sankey",
338+
"groupby": ["target"],
339+
"metric": "sum__value",
340+
"row_limit": 5000,
341+
}
342+
missing_id = self.get_nonexistent_numeric_id(Dashboard)
343+
resp = self.client.post(
344+
"/explore/table/{}/?slice_name={}&action=saveas"
345+
"&save_to_dashboard_id={}".format(tbl_id, slice_name, missing_id),
346+
data={"form_data": json.dumps(form_data)},
347+
)
348+
assert resp.status_code == 404
349+
assert (
350+
db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
351+
is None
352+
)
353+
354+
@pytest.mark.usefixtures("load_energy_table_with_slice")
355+
def test_save_slice_to_accessible_dashboard_persists(self):
356+
"""The happy path is unchanged: saving to a dashboard the user
357+
can edit still creates the chart and attaches it (fix for
358+
#46324 must not break the success path).
359+
"""
360+
admin = self.get_user(ADMIN_USERNAME)
361+
dashboard = self.insert_dashboard(
362+
"Accessible Dashboard",
363+
"accessible-dashboard-46324",
364+
[admin.id],
365+
created_by=admin,
366+
)
367+
try:
368+
self.login(ADMIN_USERNAME)
369+
tbl_id = self.table_ids.get("energy_usage")
370+
slice_name = f"Saved Slice {random.random()}" # noqa: S311
371+
form_data = {
372+
"adhoc_filters": [],
373+
"viz_type": "sankey",
374+
"groupby": ["target"],
375+
"metric": "sum__value",
376+
"row_limit": 5000,
377+
}
378+
resp = self.client.post(
379+
"/explore/table/{}/?slice_name={}&action=saveas"
380+
"&save_to_dashboard_id={}".format(tbl_id, slice_name, dashboard.id),
381+
data={"form_data": json.dumps(form_data)},
382+
)
383+
assert resp.status_code == 200
384+
slc = db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
385+
assert slc is not None
386+
assert slc in dashboard.slices
387+
assert resp.json["dashboard_id"] == dashboard.id
388+
finally:
389+
slc = db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
390+
if slc is not None:
391+
db.session.delete(slc)
392+
db.session.delete(dashboard)
393+
db.session.commit()
394+
395+
@pytest.mark.usefixtures("load_energy_table_with_slice")
396+
def test_save_slice_to_new_dashboard_persists(self):
397+
"""Saving to a brand new dashboard still creates the dashboard
398+
and attaches the chart (fix for #46324 moved the dashboard
399+
creation after the chart save; this pins the success path).
400+
"""
401+
self.login(ADMIN_USERNAME)
402+
tbl_id = self.table_ids.get("energy_usage")
403+
slice_name = f"Saved Slice {random.random()}" # noqa: S311
404+
dashboard_title = f"New Dashboard {random.random()}" # noqa: S311
405+
form_data = {
406+
"adhoc_filters": [],
407+
"viz_type": "sankey",
408+
"groupby": ["target"],
409+
"metric": "sum__value",
410+
"row_limit": 5000,
411+
}
412+
try:
413+
resp = self.client.post(
414+
"/explore/table/{}/?slice_name={}&action=saveas"
415+
"&new_dashboard_name={}".format(tbl_id, slice_name, dashboard_title),
416+
data={"form_data": json.dumps(form_data)},
417+
)
418+
assert resp.status_code == 200
419+
slc = db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
420+
assert slc is not None
421+
dash = (
422+
db.session.query(Dashboard)
423+
.filter_by(dashboard_title=dashboard_title)
424+
.one_or_none()
425+
)
426+
assert dash is not None
427+
assert slc in dash.slices
428+
assert resp.json["dashboard_id"] == dash.id
429+
finally:
430+
slc = db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
431+
if slc is not None:
432+
db.session.delete(slc)
433+
dash = (
434+
db.session.query(Dashboard)
435+
.filter_by(dashboard_title=dashboard_title)
436+
.one_or_none()
437+
)
438+
if dash is not None:
439+
db.session.delete(dash)
440+
db.session.commit()
441+
228442
@pytest.mark.usefixtures("load_energy_table_with_slice")
229443
def test_overwrite_refuses_externally_managed_slice(self):
230444
"""sc-120011: the legacy Explore overwrite is gated server-side.

0 commit comments

Comments
 (0)