File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -349,8 +349,11 @@ def _handle_key(self, event):
349349 handle_key_press = handle_key_release = _handle_key
350350
351351 def handle_toolbar_button (self , event ):
352- # TODO: Be more suspicious of the input
353- getattr (self .toolbar , event ['name' ])()
352+ name = event ['name' ]
353+ allowed = {item [3 ] for item in self .toolbar .toolitems }
354+ if name not in allowed :
355+ return
356+ getattr (self .toolbar , name )()
354357
355358 def handle_refresh (self , event ):
356359 if self .manager :
Original file line number Diff line number Diff line change 44import pytest
55
66import matplotlib .backends .backend_webagg_core
7+ from matplotlib .backends .backend_webagg_core import (
8+ FigureCanvasWebAggCore , NavigationToolbar2WebAgg ,
9+ )
710from matplotlib .testing import subprocess_run_for_testing
811
912
@@ -33,6 +36,25 @@ def test_webagg_core_no_toolbar():
3336 assert fm ._toolbar2_class is None
3437
3538
39+ def test_toolbar_button_dispatch_allowlist ():
40+ """Only declared toolbar items should be dispatched."""
41+ fig = MagicMock ()
42+ canvas = FigureCanvasWebAggCore (fig )
43+ canvas .toolbar = MagicMock (spec = NavigationToolbar2WebAgg )
44+ canvas .toolbar .toolitems = NavigationToolbar2WebAgg .toolitems
45+
46+ # Valid toolbar action should be dispatched.
47+ canvas .handle_toolbar_button ({'name' : 'home' })
48+ canvas .toolbar .home .assert_called_once ()
49+
50+ # Invalid names should be silently ignored.
51+ canvas .toolbar .reset_mock ()
52+ canvas .handle_toolbar_button ({'name' : '__init__' })
53+ canvas .handle_toolbar_button ({'name' : 'not_a_real_button' })
54+ # No methods should have been called.
55+ assert canvas .toolbar .method_calls == []
56+
57+
3658def test_websocket_check_origin ():
3759 """WebSocket should reject cross-origin connections."""
3860 pytest .importorskip ("tornado" )
You can’t perform that action at this time.
0 commit comments