Skip to content

Commit 26eeeae

Browse files
committed
fix(unix_connect): decode xauth list cookie to bytes in old_get_auth
The hex cookie was rebuilt by adding `chr()` results to `b''`, which raises `TypeError` on Python 3 whenever `xauth list` returns an entry. Use `bytes.fromhex`. Also reported in python-xlib#294.
1 parent 65b5a35 commit 26eeeae

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎Xlib/support/unix_connect.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,9 @@ def old_get_auth(sock, dname, host, dno):
211211
if len(parts) == 3:
212212
auth_name = parts[1]
213213
hexauth = parts[2]
214-
auth = b''
215214

216215
# Translate hexcode into binary
217-
for i in range(0, len(hexauth), 2):
218-
auth = auth + chr(int(hexauth[i:i+2], 16))
219-
220-
auth_data = auth
216+
auth_data = bytes.fromhex(hexauth)
221217
except os.error:
222218
pass
223219

‎test/test_unix_connect.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from functools import partial
44
import gc
5+
import io
56
import socket
67
import sys
78
import unittest
@@ -152,6 +153,15 @@ def ensure_not_inheritable(*args):
152153
expected_calls.append(('ensure_not_inheritable', s))
153154
self.assertEqual(calls, expected_calls)
154155

156+
def test_old_get_auth(self):
157+
for xauth_output, expected in (
158+
('host/unix:0 MIT-MAGIC-COOKIE-1 0123abcd\n', ('MIT-MAGIC-COOKIE-1', b'\x01\x23\xab\xcd')),
159+
# No cookie for this display.
160+
('', ('', b'')),
161+
):
162+
with patch('os.popen', return_value=io.StringIO(xauth_output)):
163+
self.assertEqual(unix_connect.old_get_auth(None, 'host/unix:0', 'host', 0), expected)
164+
155165
def test_leaking_sockets(self):
156166
with warnings.catch_warnings():
157167
warnings.simplefilter("error", ResourceWarning)

0 commit comments

Comments
 (0)