Skip to content

Commit 955d872

Browse files
barlowa124Avasam
andcommitted
fix(xauth): match FamilyWild Xauthority entries
libXau matches an entry of family `FamilyWild` (65535) against any family and address. Mutter's XWayland writes one next to its `FamilyLocal` entry, so libXau clients still connect when `socket.gethostname()` no longer matches the hostname written there, while `get_best_auth` raised `XNoAuthError`. Based on python-xlib#296. Unlike that PR, a wild entry only applies when no exact entry of the same auth name matched first, like entries without a display number already did, so an exact match still wins regardless of file order. Fixes python-xlib#294. That report's own diagnosis, entries with an empty display number, is already handled since 0.32. Co-authored-by: Avasam <[email protected]>
1 parent bf38e50 commit 955d872

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎Xlib/xauth.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
FamilyServerInterpreted = X.FamilyServerInterpreted
3131
FamilyInternetV6 = X.FamilyInternetV6
3232
FamilyLocal = 256
33+
# Matches any family and address, as written by Mutter's XWayland for example
34+
FamilyWild = 65535
3335

3436
class Xauthority(object):
3537
def __init__(self, filename = None):
@@ -107,6 +109,9 @@ def get_best_auth(self, family, address, dispno,
107109
"""Find an authentication entry matching FAMILY, ADDRESS and
108110
DISPNO.
109111
112+
Entries of family FamilyWild match any family and address,
113+
like in libXau.
114+
110115
The name of the auth scheme must match one of the names in
111116
TYPES. If several entries match, the first scheme in TYPES
112117
will be choosen.
@@ -122,6 +127,8 @@ def get_best_auth(self, family, address, dispno,
122127
for efam, eaddr, enum, ename, edata in self.entries:
123128
if enum == b'' and ename not in matches:
124129
enum = num
130+
if efam == FamilyWild and ename not in matches:
131+
efam, eaddr = family, address
125132
if efam == family and eaddr == address and num == enum:
126133
matches[ename] = edata
127134

‎test/test_xauth.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import tempfile
3+
import unittest
4+
5+
from Xlib import error, xauth
6+
from . import xauth_entry
7+
8+
COOKIE = b'MIT-MAGIC-COOKIE-1'
9+
10+
11+
class TestGetBestAuth(unittest.TestCase):
12+
13+
def get_best_auth(self, entries, family, address, dispno):
14+
fd, filename = tempfile.mkstemp(prefix='xlib-test-', suffix='.Xauthority')
15+
self.addCleanup(os.unlink, filename)
16+
with os.fdopen(fd, 'wb') as f:
17+
f.write(b''.join(xauth_entry(*entry) for entry in entries))
18+
return xauth.Xauthority(filename).get_best_auth(family, address, dispno)
19+
20+
def test_exact_match(self):
21+
self.assertEqual(self.get_best_auth([
22+
(xauth.FamilyLocal, b'other', b'0', COOKIE, b'wrong host'),
23+
(xauth.FamilyLocal, b'host', b'1', COOKIE, b'wrong display'),
24+
(xauth.FamilyLocal, b'host', b'0', COOKIE, b'right'),
25+
], xauth.FamilyLocal, b'host', 0), (COOKIE, b'right'))
26+
27+
def test_any_display_number(self):
28+
self.assertEqual(self.get_best_auth([
29+
(xauth.FamilyLocal, b'host', b'', COOKIE, b'any display'),
30+
], xauth.FamilyLocal, b'host', 3), (COOKIE, b'any display'))
31+
32+
def test_wild_family(self):
33+
# Mutter's XWayland writes a FamilyWild entry, which libXau matches
34+
# whatever the family and address, for example after a hostname change
35+
self.assertEqual(self.get_best_auth([
36+
(xauth.FamilyLocal, b'old-host', b'', COOKIE, b'wrong host'),
37+
(xauth.FamilyWild, b'old-host', b'', COOKIE, b'wild'),
38+
], xauth.FamilyLocal, b'new-host', 0), (COOKIE, b'wild'))
39+
40+
def test_exact_match_preferred_over_wild_family(self):
41+
for entries in (
42+
[(xauth.FamilyWild, b'', b'', COOKIE, b'wild'), (xauth.FamilyLocal, b'host', b'0', COOKIE, b'exact')],
43+
[(xauth.FamilyLocal, b'host', b'0', COOKIE, b'exact'), (xauth.FamilyWild, b'', b'', COOKIE, b'wild')],
44+
):
45+
self.assertEqual(self.get_best_auth(entries, xauth.FamilyLocal, b'host', 0), (COOKIE, b'exact'))
46+
47+
def test_no_match(self):
48+
with self.assertRaises(error.XNoAuthError):
49+
self.get_best_auth([
50+
(xauth.FamilyLocal, b'other', b'0', COOKIE, b'wrong host'),
51+
], xauth.FamilyLocal, b'host', 0)
52+
53+
54+
if __name__ == '__main__':
55+
unittest.main()

0 commit comments

Comments
 (0)