@@ -53,3 +53,27 @@ def test_toolbar_button_dispatch_allowlist():
5353 canvas .handle_toolbar_button ({'name' : 'not_a_real_button' })
5454 # No methods should have been called.
5555 assert canvas .toolbar .method_calls == []
56+
57+
58+ @pytest .mark .parametrize ("host, origin, allowed" , [
59+ ("localhost:8988" , "http://localhost:8988" , True ),
60+ ("localhost:8988" , "http://evil.com" , False ),
61+ ("localhost:8988" , "http://127.0.0.1:8988" , False ),
62+ ("localhost:8988" , "http://[::1]:8988" , False ),
63+ ("127.0.0.1:8988" , "http://127.0.0.1:8988" , True ),
64+ ("127.0.0.1:8988" , "http://localhost:8988" , False ),
65+ ("127.0.0.1:8988" , "http://[::1]:8988" , False ),
66+ ("[::1]:8988" , "http://[::1]:8988" , True ),
67+ ("[::1]:8988" , "http://[::2]:8988" , False ),
68+ ("[::1]:8988" , "http://localhost:8988" , False ),
69+ ("[::1]:8988" , "http://evil.com" , False ),
70+ ])
71+ def test_websocket_rejects_cross_origin (host , origin , allowed ):
72+ """Verify Tornado's default check_origin rejects cross-origin requests."""
73+ pytest .importorskip ("tornado" )
74+ from matplotlib .backends .backend_webagg import WebAggApplication
75+
76+ ws = WebAggApplication .WebSocket .__new__ (WebAggApplication .WebSocket )
77+ ws .request = MagicMock ()
78+ ws .request .headers = {"Host" : host }
79+ assert ws .check_origin (origin ) is allowed
0 commit comments