diff --git a/kitty/remote_control.py b/kitty/remote_control.py index 4ae1ca689..25e50fa3d 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -233,10 +233,6 @@ def handle_cmd( return {'ok': False, 'error': 'Streaming send of data is not supported for this command'} if stream_id: payload['stream_id'] = stream_id - active_streams[stream_id] = cmd['cmd'] - if len(active_streams) > 32: - oldest = next(iter(active_streams)) - del active_streams[oldest] if async_id: payload['async_id'] = async_id if 'cancel_async' in cmd: @@ -247,6 +243,14 @@ def handle_cmd( if len(active_async_requests) > 32: oldest = next(iter(active_async_requests)) del active_async_requests[oldest] + if stream_id: + # Register the stream only after cancellations have returned, as they + # are allowed without authentication and requests on a registered + # stream are allowed without authentication as well + active_streams[stream_id] = cmd['cmd'] + if len(active_streams) > 32: + oldest = next(iter(active_streams)) + del active_streams[oldest] try: ans = c.response_from_kitty(boss, self_window or window, PayloadGetter(c, payload)) except Exception: diff --git a/kitty_tests/remote_control.py b/kitty_tests/remote_control.py index d11050d44..d9275454d 100644 --- a/kitty_tests/remote_control.py +++ b/kitty_tests/remote_control.py @@ -112,6 +112,22 @@ class TestRemoteControl(BaseTest): self.assertIsNone(handle_cmd(Mock(spec=KittyBoss), None, pcmd, 0, None)) rfk.assert_not_called() car.assert_called_once() + # a cancellation must not register its stream, otherwise later requests + # on that stream would be allowed without authentication + from kitty.remote_control import active_streams, close_active_stream + + sid = 'cancel-async-stream' + self.addCleanup(close_active_stream, sid) + pcmd = {'cmd': 'run', 'version': [0, 26, 0], 'stream_id': sid, 'async': 'y', 'cancel_async': True} + self.assertIs(is_cmd_allowed(pcmd, None, False, {}), True) + command = command_for_name('run') + with patch.object(command, 'response_from_kitty') as rfk, patch.object(command, 'cancel_async_request') as car: + self.assertIsNone(handle_cmd(Mock(spec=KittyBoss), None, pcmd, 0, None)) + rfk.assert_not_called() + car.assert_called_once() + self.assertNotIn(sid, active_streams) + pcmd = {'cmd': 'run', 'version': [0, 26, 0], 'stream_id': sid, 'payload': {'cmdline': ['true'], 'data': ''}} + self.assertIsNot(is_cmd_allowed(pcmd, None, False, {}), True) def test_combine_parse_error_reporting(self): from kitty.boss import Boss as KittyBoss