From 6782a63a4bd98fc4d3b80e11c38abbec80ee1fb6 Mon Sep 17 00:00:00 2001 From: ANISAYAK MITRA <266799942+anisayakmitra-in@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:36:43 +0530 Subject: [PATCH] Fix case-insensitive authorization header lookup in websocket client WebSocket requests with custom authorization header casing (e.g. 'Authorization', 'AUTHORIZATION', or 'Sec-WebSocket-Protocol') were previously ignored due to strict lowercase dictionary key lookups in create_websocket and websocket_proxycare. This commit normalizes header keys to lowercase when extracting the authorization, protocol, and proxy-authorization headers, ensuring compatibility with standard HTTP clients and proxies. Signed-off-by: ANISAYAK MITRA <266799942+anisayakmitra-in@users.noreply.github.com> --- kubernetes/stream/ws_client.py | 13 ++++---- kubernetes/stream/ws_client_test.py | 50 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/kubernetes/stream/ws_client.py b/kubernetes/stream/ws_client.py index eeac7aba73..2bf9f4be8e 100644 --- a/kubernetes/stream/ws_client.py +++ b/kubernetes/stream/ws_client.py @@ -568,11 +568,12 @@ def create_websocket(configuration, url, headers=None, _request_timeout=None): # We just need to pass the Authorization, ignore all the other # http headers we get from the generated code header = [] - if headers and 'authorization' in headers: - header.append("authorization: %s" % headers['authorization']) - if headers and 'sec-websocket-protocol' in headers: + lower_headers = {k.lower(): v for k, v in headers.items()} if headers else {} + if 'authorization' in lower_headers: + header.append("authorization: %s" % lower_headers['authorization']) + if 'sec-websocket-protocol' in lower_headers: header.append("sec-websocket-protocol: %s" % - headers['sec-websocket-protocol']) + lower_headers['sec-websocket-protocol']) else: header.append("sec-websocket-protocol: %s,%s" % (V5_CHANNEL_PROTOCOL, V4_CHANNEL_PROTOCOL)) @@ -631,8 +632,8 @@ def websocket_proxycare(connect_opt, configuration, url, headers): proxy_url = urlparse(configuration.proxy) connect_opt.update({'http_proxy_host': proxy_url.hostname, 'http_proxy_port': proxy_url.port}) if configuration.proxy_headers: - for key,value in configuration.proxy_headers.items(): - if key == 'proxy-authorization' and value.startswith('Basic'): + for key, value in configuration.proxy_headers.items(): + if key.lower() == 'proxy-authorization' and value.startswith('Basic'): parts = value.split() if len(parts) < 2: raise ApiValueError( diff --git a/kubernetes/stream/ws_client_test.py b/kubernetes/stream/ws_client_test.py index afbe1b0685..23b45a217e 100644 --- a/kubernetes/stream/ws_client_test.py +++ b/kubernetes/stream/ws_client_test.py @@ -307,6 +307,56 @@ def test_create_websocket_header(self): expected_header = f"sec-websocket-protocol: {V5_CHANNEL_PROTOCOL},{V4_CHANNEL_PROTOCOL}" self.assertIn(expected_header, kwargs['header']) + def test_create_websocket_header_case_insensitive(self): + """Verify Authorization and Sec-WebSocket-Protocol headers with custom casing are preserved.""" + with patch.object(ws_client_module, 'WebSocket', autospec=True) as mock_ws_cls: + mock_ws = mock_ws_cls.return_value + + headers = { + 'Authorization': 'Bearer test-bearer-token', + 'Sec-WebSocket-Protocol': 'v5.channel.k8s.io' + } + ws_client_module.create_websocket( + self.config_mock, "ws://test", headers=headers + ) + + mock_ws.connect.assert_called_once() + call_args = mock_ws.connect.call_args + kwargs = call_args[1] + self.assertIn('header', kwargs) + self.assertIn('authorization: Bearer test-bearer-token', kwargs['header']) + self.assertIn('sec-websocket-protocol: v5.channel.k8s.io', kwargs['header']) + + def test_create_websocket_uppercase_authorization_header(self): + """Verify all-caps AUTHORIZATION header is recognized.""" + with patch.object(ws_client_module, 'WebSocket', autospec=True) as mock_ws_cls: + mock_ws = mock_ws_cls.return_value + + headers = { + 'AUTHORIZATION': 'Basic dXNlcjpwYXNz' + } + ws_client_module.create_websocket( + self.config_mock, "ws://test", headers=headers + ) + + mock_ws.connect.assert_called_once() + kwargs = mock_ws.connect.call_args[1] + self.assertIn('authorization: Basic dXNlcjpwYXNz', kwargs['header']) + expected_proto = f"sec-websocket-protocol: {V5_CHANNEL_PROTOCOL},{V4_CHANNEL_PROTOCOL}" + self.assertIn(expected_proto, kwargs['header']) + + def test_websocket_proxycare_case_insensitive(self): + """Verify proxy-authorization header casing in proxy_headers is handled.""" + config = Configuration() + config.proxy = "http://proxy.example.com:8080" + config.proxy_headers = { + 'Proxy-Authorization': 'Basic dXNlcjpwYXNzd29yZA==' + } + connect_opt = {} + result = websocket_proxycare(connect_opt, config, "ws://test", headers=None) + self.assertIn('http_proxy_auth', result) + self.assertEqual(result['http_proxy_auth'], ('user', 'password')) + def test_create_websocket_tuple_timeout(self): """Verify _request_timeout tuple sets connect timeout and resets socket timeout to read timeout."""