]> jfr.im git - yt-dlp.git/blame - test/test_cookies.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / test / test_cookies.py
CommitLineData
c305a25c 1import datetime as dt
982ee69a 2import unittest
982ee69a
MB
3
4from yt_dlp import cookies
5from yt_dlp.cookies import (
8817a80d 6 LenientSimpleCookie,
982ee69a
MB
7 LinuxChromeCookieDecryptor,
8 MacChromeCookieDecryptor,
9 WindowsChromeCookieDecryptor,
f59f5ef8
MB
10 _get_linux_desktop_environment,
11 _LinuxDesktopEnvironment,
f8271158 12 parse_safari_cookies,
13 pbkdf2_sha1,
982ee69a
MB
14)
15
16
1b629e1b 17class Logger:
f0500bd1 18 def debug(self, message, *args, **kwargs):
1b629e1b 19 print(f'[verbose] {message}')
20
f0500bd1 21 def info(self, message, *args, **kwargs):
1b629e1b 22 print(message)
23
f0500bd1 24 def warning(self, message, *args, **kwargs):
1b629e1b 25 self.error(message)
26
f0500bd1 27 def error(self, message, *args, **kwargs):
1b629e1b 28 raise Exception(message)
29
30
982ee69a
MB
31class MonkeyPatch:
32 def __init__(self, module, temporary_values):
33 self._module = module
34 self._temporary_values = temporary_values
35 self._backup_values = {}
36
37 def __enter__(self):
38 for name, temp_value in self._temporary_values.items():
39 self._backup_values[name] = getattr(self._module, name)
40 setattr(self._module, name, temp_value)
41
42 def __exit__(self, exc_type, exc_val, exc_tb):
43 for name, backup_value in self._backup_values.items():
44 setattr(self._module, name, backup_value)
45
46
47class TestCookies(unittest.TestCase):
f59f5ef8
MB
48 def test_get_desktop_environment(self):
49 """ based on https://chromium.googlesource.com/chromium/src/+/refs/heads/main/base/nix/xdg_util_unittest.cc """
50 test_cases = [
51 ({}, _LinuxDesktopEnvironment.OTHER),
b38d4c94
MB
52 ({'DESKTOP_SESSION': 'my_custom_de'}, _LinuxDesktopEnvironment.OTHER),
53 ({'XDG_CURRENT_DESKTOP': 'my_custom_de'}, _LinuxDesktopEnvironment.OTHER),
f59f5ef8
MB
54
55 ({'DESKTOP_SESSION': 'gnome'}, _LinuxDesktopEnvironment.GNOME),
56 ({'DESKTOP_SESSION': 'mate'}, _LinuxDesktopEnvironment.GNOME),
b38d4c94
MB
57 ({'DESKTOP_SESSION': 'kde4'}, _LinuxDesktopEnvironment.KDE4),
58 ({'DESKTOP_SESSION': 'kde'}, _LinuxDesktopEnvironment.KDE3),
f59f5ef8
MB
59 ({'DESKTOP_SESSION': 'xfce'}, _LinuxDesktopEnvironment.XFCE),
60
61 ({'GNOME_DESKTOP_SESSION_ID': 1}, _LinuxDesktopEnvironment.GNOME),
b38d4c94
MB
62 ({'KDE_FULL_SESSION': 1}, _LinuxDesktopEnvironment.KDE3),
63 ({'KDE_FULL_SESSION': 1, 'DESKTOP_SESSION': 'kde4'}, _LinuxDesktopEnvironment.KDE4),
f59f5ef8
MB
64
65 ({'XDG_CURRENT_DESKTOP': 'X-Cinnamon'}, _LinuxDesktopEnvironment.CINNAMON),
b38d4c94 66 ({'XDG_CURRENT_DESKTOP': 'Deepin'}, _LinuxDesktopEnvironment.DEEPIN),
f59f5ef8
MB
67 ({'XDG_CURRENT_DESKTOP': 'GNOME'}, _LinuxDesktopEnvironment.GNOME),
68 ({'XDG_CURRENT_DESKTOP': 'GNOME:GNOME-Classic'}, _LinuxDesktopEnvironment.GNOME),
69 ({'XDG_CURRENT_DESKTOP': 'GNOME : GNOME-Classic'}, _LinuxDesktopEnvironment.GNOME),
70
71 ({'XDG_CURRENT_DESKTOP': 'Unity', 'DESKTOP_SESSION': 'gnome-fallback'}, _LinuxDesktopEnvironment.GNOME),
b38d4c94
MB
72 ({'XDG_CURRENT_DESKTOP': 'KDE', 'KDE_SESSION_VERSION': '5'}, _LinuxDesktopEnvironment.KDE5),
73 ({'XDG_CURRENT_DESKTOP': 'KDE', 'KDE_SESSION_VERSION': '6'}, _LinuxDesktopEnvironment.KDE6),
74 ({'XDG_CURRENT_DESKTOP': 'KDE'}, _LinuxDesktopEnvironment.KDE4),
f59f5ef8 75 ({'XDG_CURRENT_DESKTOP': 'Pantheon'}, _LinuxDesktopEnvironment.PANTHEON),
b38d4c94 76 ({'XDG_CURRENT_DESKTOP': 'UKUI'}, _LinuxDesktopEnvironment.UKUI),
f59f5ef8
MB
77 ({'XDG_CURRENT_DESKTOP': 'Unity'}, _LinuxDesktopEnvironment.UNITY),
78 ({'XDG_CURRENT_DESKTOP': 'Unity:Unity7'}, _LinuxDesktopEnvironment.UNITY),
79 ({'XDG_CURRENT_DESKTOP': 'Unity:Unity8'}, _LinuxDesktopEnvironment.UNITY),
80 ]
81
82 for env, expected_desktop_environment in test_cases:
b38d4c94 83 self.assertEqual(_get_linux_desktop_environment(env, Logger()), expected_desktop_environment)
f59f5ef8 84
982ee69a
MB
85 def test_chrome_cookie_decryptor_linux_derive_key(self):
86 key = LinuxChromeCookieDecryptor.derive_key(b'abc')
87 self.assertEqual(key, b'7\xa1\xec\xd4m\xfcA\xc7\xb19Z\xd0\x19\xdcM\x17')
88
89 def test_chrome_cookie_decryptor_mac_derive_key(self):
90 key = MacChromeCookieDecryptor.derive_key(b'abc')
91 self.assertEqual(key, b'Y\xe2\xc0\xd0P\xf6\xf4\xe1l\xc1\x8cQ\xcb|\xcdY')
92
93 def test_chrome_cookie_decryptor_linux_v10(self):
94 with MonkeyPatch(cookies, {'_get_linux_keyring_password': lambda *args, **kwargs: b''}):
95 encrypted_value = b'v10\xccW%\xcd\xe6\xe6\x9fM" \xa7\xb0\xca\xe4\x07\xd6'
96 value = 'USD'
1b629e1b 97 decryptor = LinuxChromeCookieDecryptor('Chrome', Logger())
982ee69a
MB
98 self.assertEqual(decryptor.decrypt(encrypted_value), value)
99
100 def test_chrome_cookie_decryptor_linux_v11(self):
f59f5ef8 101 with MonkeyPatch(cookies, {'_get_linux_keyring_password': lambda *args, **kwargs: b''}):
982ee69a
MB
102 encrypted_value = b'v11#\x81\x10>`w\x8f)\xc0\xb2\xc1\r\xf4\x1al\xdd\x93\xfd\xf8\xf8N\xf2\xa9\x83\xf1\xe9o\x0elVQd'
103 value = 'tz=Europe.London'
1b629e1b 104 decryptor = LinuxChromeCookieDecryptor('Chrome', Logger())
982ee69a
MB
105 self.assertEqual(decryptor.decrypt(encrypted_value), value)
106
982ee69a
MB
107 def test_chrome_cookie_decryptor_windows_v10(self):
108 with MonkeyPatch(cookies, {
add96eb9 109 '_get_windows_v10_key': lambda *args, **kwargs: b'Y\xef\xad\xad\xeerp\xf0Y\xe6\x9b\x12\xc2<z\x16]\n\xbb\xb8\xcb\xd7\x9bA\xc3\x14e\x99{\xd6\xf4&',
982ee69a
MB
110 }):
111 encrypted_value = b'v10T\xb8\xf3\xb8\x01\xa7TtcV\xfc\x88\xb8\xb8\xef\x05\xb5\xfd\x18\xc90\x009\xab\xb1\x893\x85)\x87\xe1\xa9-\xa3\xad='
112 value = '32101439'
1b629e1b 113 decryptor = WindowsChromeCookieDecryptor('', Logger())
982ee69a
MB
114 self.assertEqual(decryptor.decrypt(encrypted_value), value)
115
116 def test_chrome_cookie_decryptor_mac_v10(self):
117 with MonkeyPatch(cookies, {'_get_mac_keyring_password': lambda *args, **kwargs: b'6eIDUdtKAacvlHwBVwvg/Q=='}):
118 encrypted_value = b'v10\xb3\xbe\xad\xa1[\x9fC\xa1\x98\xe0\x9a\x01\xd9\xcf\xbfc'
119 value = '2021-06-01-22'
1b629e1b 120 decryptor = MacChromeCookieDecryptor('', Logger())
982ee69a
MB
121 self.assertEqual(decryptor.decrypt(encrypted_value), value)
122
123 def test_safari_cookie_parsing(self):
add96eb9 124 cookies = (
125 b'cook\x00\x00\x00\x01\x00\x00\x00i\x00\x00\x01\x00\x01\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00Y'
126 b'\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x008\x00\x00\x00B\x00\x00\x00F\x00\x00\x00H'
127 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x03\xa5>\xc3A\x00\x00\x80\xc3\x07:\xc3A'
128 b'localhost\x00foo\x00/\x00test%20%3Bcookie\x00\x00\x00\x054\x07\x17 \x05\x00\x00\x00Kbplist00\xd1\x01'
129 b'\x02_\x10\x18NSHTTPCookieAcceptPolicy\x10\x02\x08\x0b&\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00'
130 b'\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00(')
982ee69a
MB
131
132 jar = parse_safari_cookies(cookies)
133 self.assertEqual(len(jar), 1)
add96eb9 134 cookie = next(iter(jar))
982ee69a
MB
135 self.assertEqual(cookie.domain, 'localhost')
136 self.assertEqual(cookie.port, None)
137 self.assertEqual(cookie.path, '/')
138 self.assertEqual(cookie.name, 'foo')
139 self.assertEqual(cookie.value, 'test%20%3Bcookie')
140 self.assertFalse(cookie.secure)
c305a25c 141 expected_expiration = dt.datetime(2021, 6, 18, 21, 39, 19, tzinfo=dt.timezone.utc)
982ee69a
MB
142 self.assertEqual(cookie.expires, int(expected_expiration.timestamp()))
143
144 def test_pbkdf2_sha1(self):
145 key = pbkdf2_sha1(b'peanuts', b' ' * 16, 1, 16)
146 self.assertEqual(key, b'g\xe1\x8e\x0fQ\x1c\x9b\xf3\xc9`!\xaa\x90\xd9\xd34')
8817a80d
SS
147
148
149class TestLenientSimpleCookie(unittest.TestCase):
150 def _run_tests(self, *cases):
151 for message, raw_cookie, expected in cases:
152 cookie = LenientSimpleCookie(raw_cookie)
153
154 with self.subTest(message, expected=expected):
155 self.assertEqual(cookie.keys(), expected.keys(), message)
156
157 for key, expected_value in expected.items():
158 morsel = cookie[key]
159 if isinstance(expected_value, tuple):
160 expected_value, expected_attributes = expected_value
161 else:
162 expected_attributes = {}
163
164 attributes = {
165 key: value
166 for key, value in dict(morsel).items()
add96eb9 167 if value != ''
8817a80d
SS
168 }
169 self.assertEqual(attributes, expected_attributes, message)
170
171 self.assertEqual(morsel.value, expected_value, message)
172
173 def test_parsing(self):
174 self._run_tests(
175 # Copied from https://github.com/python/cpython/blob/v3.10.7/Lib/test/test_http_cookies.py
176 (
add96eb9 177 'Test basic cookie',
178 'chips=ahoy; vienna=finger',
179 {'chips': 'ahoy', 'vienna': 'finger'},
8817a80d
SS
180 ),
181 (
add96eb9 182 'Test quoted cookie',
8817a80d 183 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
add96eb9 184 {'keebler': 'E=mc2; L="Loves"; fudge=\012;'},
8817a80d
SS
185 ),
186 (
187 "Allow '=' in an unquoted value",
add96eb9 188 'keebler=E=mc2',
189 {'keebler': 'E=mc2'},
8817a80d
SS
190 ),
191 (
192 "Allow cookies with ':' in their name",
add96eb9 193 'key:term=value:term',
194 {'key:term': 'value:term'},
8817a80d
SS
195 ),
196 (
197 "Allow '[' and ']' in cookie values",
add96eb9 198 'a=b; c=[; d=r; f=h',
199 {'a': 'b', 'c': '[', 'd': 'r', 'f': 'h'},
8817a80d
SS
200 ),
201 (
add96eb9 202 'Test basic cookie attributes',
8817a80d 203 'Customer="WILE_E_COYOTE"; Version=1; Path=/acme',
add96eb9 204 {'Customer': ('WILE_E_COYOTE', {'version': '1', 'path': '/acme'})},
8817a80d
SS
205 ),
206 (
add96eb9 207 'Test flag only cookie attributes',
8817a80d 208 'Customer="WILE_E_COYOTE"; HttpOnly; Secure',
add96eb9 209 {'Customer': ('WILE_E_COYOTE', {'httponly': True, 'secure': True})},
8817a80d
SS
210 ),
211 (
add96eb9 212 'Test flag only attribute with values',
213 'eggs=scrambled; httponly=foo; secure=bar; Path=/bacon',
214 {'eggs': ('scrambled', {'httponly': 'foo', 'secure': 'bar', 'path': '/bacon'})},
8817a80d
SS
215 ),
216 (
217 "Test special case for 'expires' attribute, 4 digit year",
218 'Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT',
add96eb9 219 {'Customer': ('W', {'expires': 'Wed, 01 Jan 2010 00:00:00 GMT'})},
8817a80d
SS
220 ),
221 (
222 "Test special case for 'expires' attribute, 2 digit year",
223 'Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT',
add96eb9 224 {'Customer': ('W', {'expires': 'Wed, 01 Jan 98 00:00:00 GMT'})},
8817a80d
SS
225 ),
226 (
add96eb9 227 'Test extra spaces in keys and values',
228 'eggs = scrambled ; secure ; path = bar ; foo=foo ',
229 {'eggs': ('scrambled', {'secure': True, 'path': 'bar'}), 'foo': 'foo'},
8817a80d
SS
230 ),
231 (
add96eb9 232 'Test quoted attributes',
8817a80d 233 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"',
add96eb9 234 {'Customer': ('WILE_E_COYOTE', {'version': '1', 'path': '/acme'})},
8817a80d
SS
235 ),
236 # Our own tests that CPython passes
237 (
238 "Allow ';' in quoted value",
239 'chips="a;hoy"; vienna=finger',
add96eb9 240 {'chips': 'a;hoy', 'vienna': 'finger'},
8817a80d
SS
241 ),
242 (
add96eb9 243 'Keep only the last set value',
244 'a=c; a=b',
245 {'a': 'b'},
8817a80d
SS
246 ),
247 )
248
249 def test_lenient_parsing(self):
250 self._run_tests(
251 (
add96eb9 252 'Ignore and try to skip invalid cookies',
8817a80d 253 'chips={"ahoy;": 1}; vienna="finger;"',
add96eb9 254 {'vienna': 'finger;'},
8817a80d
SS
255 ),
256 (
add96eb9 257 'Ignore cookies without a name',
258 'a=b; unnamed; c=d',
259 {'a': 'b', 'c': 'd'},
8817a80d
SS
260 ),
261 (
262 "Ignore '\"' cookie without name",
263 'a=b; "; c=d',
add96eb9 264 {'a': 'b', 'c': 'd'},
8817a80d
SS
265 ),
266 (
add96eb9 267 'Skip all space separated values',
268 'x a=b c=d x; e=f',
269 {'a': 'b', 'c': 'd', 'e': 'f'},
8817a80d
SS
270 ),
271 (
add96eb9 272 'Skip all space separated values',
8817a80d 273 'x a=b; data={"complex": "json", "with": "key=value"}; x c=d x',
add96eb9 274 {'a': 'b', 'c': 'd'},
8817a80d
SS
275 ),
276 (
add96eb9 277 'Expect quote mending',
8817a80d 278 'a=b; invalid="; c=d',
add96eb9 279 {'a': 'b', 'c': 'd'},
8817a80d
SS
280 ),
281 (
add96eb9 282 'Reset morsel after invalid to not capture attributes',
283 'a=b; invalid; Version=1; c=d',
284 {'a': 'b', 'c': 'd'},
8817a80d 285 ),
36069409 286 (
add96eb9 287 'Reset morsel after invalid to not capture attributes',
288 'a=b; $invalid; $Version=1; c=d',
289 {'a': 'b', 'c': 'd'},
36069409 290 ),
8817a80d 291 (
add96eb9 292 'Continue after non-flag attribute without value',
293 'a=b; path; Version=1; c=d',
294 {'a': 'b', 'c': 'd'},
8817a80d 295 ),
36069409 296 (
add96eb9 297 'Allow cookie attributes with `$` prefix',
36069409 298 'Customer="WILE_E_COYOTE"; $Version=1; $Secure; $Path=/acme',
add96eb9 299 {'Customer': ('WILE_E_COYOTE', {'version': '1', 'secure': True, 'path': '/acme'})},
36069409
SS
300 ),
301 (
add96eb9 302 'Invalid Morsel keys should not result in an error',
303 'Key=Value; [Invalid]=Value; Another=Value',
304 {'Key': 'Value', 'Another': 'Value'},
36069409 305 ),
8817a80d 306 )