Skip to content

Commit d784db7

Browse files
fix(explore): don't persist chart when overwrite dashboard check fails (#46399)
1 parent 4e37ad0 commit d784db7

2 files changed

Lines changed: 221 additions & 12 deletions

File tree

‎superset/views/core.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,19 +377,11 @@ def save_or_overwrite_slice( # noqa: C901
377377

378378
utils.remove_extra_adhoc_filters(form_data)
379379

380-
assert slc
381-
slc.params = json.dumps(form_data, indent=2, sort_keys=True)
382-
slc.datasource_name = datasource_name
383-
slc.viz_type = form_data["viz_type"]
384-
slc.datasource_type = datasource_type
385-
slc.datasource_id = datasource_id
386-
slc.last_saved_by = g.user
387-
slc.last_saved_at = datetime.now()
388-
slc.slice_name = slice_name
389-
slc.query_context = query_context
390-
391-
# Resolve and validate the dashboard target BEFORE saving the
380+
# Resolve and validate the dashboard target before touching the
392381
# chart: a failed dashboard check must not leave a chart behind.
382+
# On overwrite the loaded chart is already persistent, so fields
383+
# assigned before this check would be committed by the request's
384+
# event-log commit even when the request is refused.
393385
dash: Dashboard | None = None
394386

395387
save_to_dashboard_id = request.args.get("save_to_dashboard_id")
@@ -426,6 +418,17 @@ def save_or_overwrite_slice( # noqa: C901
426418
status=403,
427419
)
428420

421+
assert slc
422+
slc.params = json.dumps(form_data, indent=2, sort_keys=True)
423+
slc.datasource_name = datasource_name
424+
slc.viz_type = form_data["viz_type"]
425+
slc.datasource_type = datasource_type
426+
slc.datasource_id = datasource_id
427+
slc.last_saved_by = g.user
428+
slc.last_saved_at = datetime.now()
429+
slc.slice_name = slice_name
430+
slc.query_context = query_context
431+
429432
if action == "saveas" and slice_add_perm:
430433
ChartDAO.create(slc)
431434
db.session.commit() # pylint: disable=consider-using-transaction

‎tests/integration_tests/core_tests.py‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,161 @@ def test_save_slice_invalid_dashboard_id_returns_400(self):
320320
is None
321321
)
322322

323+
@pytest.mark.usefixtures("load_energy_table_with_slice")
324+
def test_overwrite_slice_invalid_dashboard_id_does_not_persist(self):
325+
"""A rejected dashboard target must not overwrite the chart either
326+
(#46324): the request's event-log commit must not persist the
327+
chart fields assigned before the dashboard check.
328+
"""
329+
self.login(ADMIN_USERNAME)
330+
tbl_id = self.table_ids.get("energy_usage")
331+
original_name = f"Overwrite Target {random.random()}" # noqa: S311
332+
form_data = {
333+
"adhoc_filters": [],
334+
"viz_type": "sankey",
335+
"groupby": ["target"],
336+
"metric": "sum__value",
337+
"row_limit": 5000,
338+
}
339+
url = "/explore/table/{}/?slice_name={}&action={}"
340+
resp = self.client.post(
341+
url.format(tbl_id, original_name, "saveas"),
342+
data={"form_data": json.dumps(form_data)},
343+
)
344+
slice_id = resp.json["form_data"]["slice_id"]
345+
try:
346+
resp = self.client.post(
347+
url.format(tbl_id, "Overwritten Name", "overwrite")
348+
+ "&save_to_dashboard_id=abc",
349+
data={
350+
"form_data": json.dumps(
351+
{**form_data, "slice_id": slice_id, "row_limit": 7}
352+
)
353+
},
354+
)
355+
assert resp.status_code == 400
356+
db.session.expire_all()
357+
slc = db.session.query(Slice).filter_by(id=slice_id).one()
358+
assert slc.slice_name == original_name
359+
assert slc.form_data["row_limit"] == 5000
360+
finally:
361+
db.session.rollback()
362+
db.session.delete(db.session.query(Slice).filter_by(id=slice_id).one())
363+
db.session.commit()
364+
365+
@pytest.mark.usefixtures("load_energy_table_with_slice")
366+
def test_overwrite_slice_missing_dashboard_does_not_persist(self):
367+
"""Same overwrite gap as the invalid-id case, for a nonexistent
368+
dashboard id (#46324): expect 404 and the chart left untouched.
369+
"""
370+
self.login(ADMIN_USERNAME)
371+
tbl_id = self.table_ids.get("energy_usage")
372+
original_name = f"Overwrite Target {random.random()}" # noqa: S311
373+
form_data = {
374+
"adhoc_filters": [],
375+
"viz_type": "sankey",
376+
"groupby": ["target"],
377+
"metric": "sum__value",
378+
"row_limit": 5000,
379+
}
380+
url = "/explore/table/{}/?slice_name={}&action={}"
381+
resp = self.client.post(
382+
url.format(tbl_id, original_name, "saveas"),
383+
data={"form_data": json.dumps(form_data)},
384+
)
385+
slice_id = resp.json["form_data"]["slice_id"]
386+
missing_id = self.get_nonexistent_numeric_id(Dashboard)
387+
try:
388+
resp = self.client.post(
389+
url.format(tbl_id, "Overwritten Name", "overwrite")
390+
+ f"&save_to_dashboard_id={missing_id}",
391+
data={
392+
"form_data": json.dumps(
393+
{**form_data, "slice_id": slice_id, "row_limit": 7}
394+
)
395+
},
396+
)
397+
assert resp.status_code == 404
398+
db.session.expire_all()
399+
slc = db.session.query(Slice).filter_by(id=slice_id).one()
400+
assert slc.slice_name == original_name
401+
assert slc.form_data["row_limit"] == 5000
402+
finally:
403+
db.session.rollback()
404+
db.session.delete(db.session.query(Slice).filter_by(id=slice_id).one())
405+
db.session.commit()
406+
407+
@pytest.mark.usefixtures("load_energy_table_with_slice")
408+
def test_overwrite_slice_forbidden_dashboard_does_not_persist(self):
409+
"""Overwriting onto a dashboard the chart editor cannot edit must
410+
fail with 403 and leave the chart untouched (#46324). Uses a
411+
non-admin chart editor, since admins can edit every dashboard.
412+
"""
413+
admin = self.get_user(ADMIN_USERNAME)
414+
dashboard = self.insert_dashboard(
415+
f"Forbidden Dashboard {random.random()}", # noqa: S311
416+
None,
417+
[admin.id],
418+
created_by=admin,
419+
)
420+
tbl_id = self.table_ids.get("energy_usage")
421+
original_name = f"Overwrite Target {random.random()}" # noqa: S311
422+
form_data = {
423+
"adhoc_filters": [],
424+
"viz_type": "sankey",
425+
"groupby": ["target"],
426+
"metric": "sum__value",
427+
"row_limit": 5000,
428+
}
429+
table = self.get_table("energy_usage")
430+
gamma = self.get_user(GAMMA_USERNAME)
431+
url = "/explore/table/{}/?slice_name={}&action={}"
432+
self.logout()
433+
try:
434+
with self.temporary_user(
435+
gamma,
436+
extra_pvms=[("datasource_access", table.perm)],
437+
login=True,
438+
):
439+
resp = self.client.post(
440+
url.format(tbl_id, original_name, "saveas"),
441+
data={"form_data": json.dumps(form_data)},
442+
)
443+
assert resp.status_code == 200
444+
slice_id = resp.json["form_data"]["slice_id"]
445+
try:
446+
resp = self.client.post(
447+
url.format(tbl_id, "Overwritten Name", "overwrite")
448+
+ f"&save_to_dashboard_id={dashboard.id}",
449+
data={
450+
"form_data": json.dumps(
451+
{**form_data, "slice_id": slice_id, "row_limit": 7}
452+
)
453+
},
454+
)
455+
status, error = resp.status_code, resp.json.get("error")
456+
db.session.expire_all()
457+
slc = db.session.query(Slice).filter_by(id=slice_id).one()
458+
name, row_limit = slc.slice_name, slc.form_data["row_limit"]
459+
in_dashboard = slc in dashboard.slices
460+
finally:
461+
# Delete the chart inside the block so the temporary
462+
# user's teardown can still delete the user.
463+
db.session.rollback()
464+
db.session.delete(
465+
db.session.query(Slice).filter_by(id=slice_id).one()
466+
)
467+
db.session.commit()
468+
assert status == 403
469+
assert "rights to alter this dashboard" in error
470+
assert not in_dashboard
471+
assert name == original_name
472+
assert row_limit == 5000
473+
finally:
474+
db.session.rollback()
475+
db.session.delete(db.session.merge(dashboard))
476+
db.session.commit()
477+
323478
@pytest.mark.usefixtures("load_energy_table_with_slice")
324479
def test_save_slice_missing_dashboard_id_returns_404(self):
325480
"""A nonexistent save_to_dashboard_id must fail with 404 and
@@ -439,6 +594,57 @@ def test_save_slice_to_new_dashboard_persists(self):
439594
db.session.delete(dash)
440595
db.session.commit()
441596

597+
@pytest.mark.usefixtures("load_energy_table_with_slice")
598+
def test_save_slice_to_new_dashboard_without_create_perm_returns_403(self):
599+
"""Creating a dashboard from Explore without the create permission
600+
must fail with 403 and persist nothing (#46324): the
601+
new_dashboard_name permission check runs before the save, and
602+
this pins it so a later reorder cannot quietly undo it.
603+
"""
604+
self.login(ADMIN_USERNAME)
605+
tbl_id = self.table_ids.get("energy_usage")
606+
slice_name = f"Unsaved Slice {random.random()}" # noqa: S311
607+
dashboard_title = f"New Dashboard {random.random()}" # noqa: S311
608+
form_data = {
609+
"adhoc_filters": [],
610+
"viz_type": "sankey",
611+
"groupby": ["target"],
612+
"metric": "sum__value",
613+
"row_limit": 5000,
614+
}
615+
table = self.get_table("energy_usage")
616+
gamma = self.get_user(GAMMA_USERNAME)
617+
# Drop the admin session before impersonating gamma: logging in
618+
# as the temporary user does not replace an already authenticated
619+
# session, so the admin would otherwise leak into these checks.
620+
# Stock Gamma can create dashboards, so clone it without the
621+
# Dashboard write permission to exercise the refusal path.
622+
self.logout()
623+
with self.temporary_user(
624+
gamma,
625+
extra_pvms=[("datasource_access", table.perm)],
626+
pvms_to_remove=[("can_write", "Dashboard")],
627+
login=True,
628+
):
629+
resp = self.client.post(
630+
"/explore/table/{}/?slice_name={}&action=saveas"
631+
"&new_dashboard_name={}".format(tbl_id, slice_name, dashboard_title),
632+
data={"form_data": json.dumps(form_data)},
633+
)
634+
assert resp.status_code == 403
635+
assert (
636+
"You don't have the rights to create a dashboard" in resp.json["error"]
637+
)
638+
leaked = (
639+
db.session.query(Slice).filter_by(slice_name=slice_name).one_or_none()
640+
)
641+
# Clean up a leaked chart (the bug under test) so the
642+
# temporary user's teardown can still delete the user.
643+
if leaked is not None:
644+
db.session.delete(leaked)
645+
db.session.commit()
646+
assert leaked is None
647+
442648
@pytest.mark.usefixtures("load_energy_table_with_slice")
443649
def test_overwrite_refuses_externally_managed_slice(self):
444650
"""sc-120011: the legacy Explore overwrite is gated server-side.

0 commit comments

Comments
 (0)