10 from datetime
import datetime
, timedelta
, timezone
11 from enum
import Enum
, auto
12 from hashlib
import pbkdf2_hmac
15 aes_cbc_decrypt_bytes
,
16 aes_gcm_decrypt_and_verify_bytes
,
19 from .compat
import compat_b64decode
, compat_cookiejar_Cookie
20 from .minicurses
import MultilinePrinter
, QuietMultilinePrinter
21 from .utils
import Popen
, YoutubeDLCookieJar
, error_to_str
, expand_path
25 SQLITE_AVAILABLE
= True
27 # although sqlite3 is part of the standard library, it is possible to compile python without
28 # sqlite support. See: https://github.com/yt-dlp/yt-dlp/issues/544
29 SQLITE_AVAILABLE
= False
34 SECRETSTORAGE_AVAILABLE
= True
36 SECRETSTORAGE_AVAILABLE
= False
37 SECRETSTORAGE_UNAVAILABLE_REASON
= (
38 'as the `secretstorage` module is not installed. '
39 'Please install by running `python3 -m pip install secretstorage`.')
40 except Exception as _err
:
41 SECRETSTORAGE_AVAILABLE
= False
42 SECRETSTORAGE_UNAVAILABLE_REASON
= f
'as the `secretstorage` module could not be initialized. {_err}'
45 CHROMIUM_BASED_BROWSERS
= {'brave', 'chrome', 'chromium', 'edge', 'opera', 'vivaldi'}
46 SUPPORTED_BROWSERS
= CHROMIUM_BASED_BROWSERS | {'firefox', 'safari'}
50 def __init__(self
, ydl
=None):
53 def debug(self
, message
):
55 self
._ydl
.write_debug(message
)
57 def info(self
, message
):
59 self
._ydl
.to_screen(f
'[Cookies] {message}')
61 def warning(self
, message
, only_once
=False):
63 self
._ydl
.report_warning(message
, only_once
)
65 def error(self
, message
):
67 self
._ydl
.report_error(message
)
69 def progress_bar(self
):
70 """Return a context manager with a print method. (Optional)"""
71 # Do not print to files/pipes, loggers, or when --no-progress is used
72 if not self
._ydl
or self
._ydl
.params
.get('noprogress') or self
._ydl
.params
.get('logger'):
74 file = self
._ydl
._out
_files
['error']
81 printer
= MultilinePrinter(file, preserve_output
=False)
82 printer
.print = lambda message
: printer
.print_at_line(f
'[Cookies] {message}', 0)
86 def _create_progress_bar(logger
):
87 if hasattr(logger
, 'progress_bar'):
88 printer
= logger
.progress_bar()
91 printer
= QuietMultilinePrinter()
92 printer
.print = lambda _
: None
96 def load_cookies(cookie_file
, browser_specification
, ydl
):
98 if browser_specification
is not None:
99 browser_name
, profile
, keyring
= _parse_browser_specification(*browser_specification
)
100 cookie_jars
.append(extract_cookies_from_browser(browser_name
, profile
, YDLLogger(ydl
), keyring
=keyring
))
102 if cookie_file
is not None:
103 cookie_file
= expand_path(cookie_file
)
104 jar
= YoutubeDLCookieJar(cookie_file
)
105 if os
.access(cookie_file
, os
.R_OK
):
106 jar
.load(ignore_discard
=True, ignore_expires
=True)
107 cookie_jars
.append(jar
)
109 return _merge_cookie_jars(cookie_jars
)
112 def extract_cookies_from_browser(browser_name
, profile
=None, logger
=YDLLogger(), *, keyring
=None):
113 if browser_name
== 'firefox':
114 return _extract_firefox_cookies(profile
, logger
)
115 elif browser_name
== 'safari':
116 return _extract_safari_cookies(profile
, logger
)
117 elif browser_name
in CHROMIUM_BASED_BROWSERS
:
118 return _extract_chrome_cookies(browser_name
, profile
, keyring
, logger
)
120 raise ValueError(f
'unknown browser: {browser_name}')
123 def _extract_firefox_cookies(profile
, logger
):
124 logger
.info('Extracting cookies from firefox')
125 if not SQLITE_AVAILABLE
:
126 logger
.warning('Cannot extract cookies from firefox without sqlite3 support. '
127 'Please use a python interpreter compiled with sqlite3 support')
128 return YoutubeDLCookieJar()
131 search_root
= _firefox_browser_dir()
132 elif _is_path(profile
):
133 search_root
= profile
135 search_root
= os
.path
.join(_firefox_browser_dir(), profile
)
137 cookie_database_path
= _find_most_recently_used_file(search_root
, 'cookies.sqlite', logger
)
138 if cookie_database_path
is None:
139 raise FileNotFoundError(f
'could not find firefox cookies database in {search_root}')
140 logger
.debug(f
'Extracting cookies from: "{cookie_database_path}"')
142 with tempfile
.TemporaryDirectory(prefix
='yt_dlp') as tmpdir
:
145 cursor
= _open_database_copy(cookie_database_path
, tmpdir
)
146 cursor
.execute('SELECT host, name, value, path, expiry, isSecure FROM moz_cookies')
147 jar
= YoutubeDLCookieJar()
148 with _create_progress_bar(logger
) as progress_bar
:
149 table
= cursor
.fetchall()
150 total_cookie_count
= len(table
)
151 for i
, (host
, name
, value
, path
, expiry
, is_secure
) in enumerate(table
):
152 progress_bar
.print(f
'Loading cookie {i: 6d}/{total_cookie_count: 6d}')
153 cookie
= compat_cookiejar_Cookie(
154 version
=0, name
=name
, value
=value
, port
=None, port_specified
=False,
155 domain
=host
, domain_specified
=bool(host
), domain_initial_dot
=host
.startswith('.'),
156 path
=path
, path_specified
=bool(path
), secure
=is_secure
, expires
=expiry
, discard
=False,
157 comment
=None, comment_url
=None, rest
={})
158 jar
.set_cookie(cookie
)
159 logger
.info(f
'Extracted {len(jar)} cookies from firefox')
162 if cursor
is not None:
163 cursor
.connection
.close()
166 def _firefox_browser_dir():
167 if sys
.platform
in ('linux', 'linux2'):
168 return os
.path
.expanduser('~/.mozilla/firefox')
169 elif sys
.platform
== 'win32':
170 return os
.path
.expandvars(R
'%APPDATA%\Mozilla\Firefox\Profiles')
171 elif sys
.platform
== 'darwin':
172 return os
.path
.expanduser('~/Library/Application Support/Firefox')
174 raise ValueError(f
'unsupported platform: {sys.platform}')
177 def _get_chromium_based_browser_settings(browser_name
):
178 # https://chromium.googlesource.com/chromium/src/+/HEAD/docs/user_data_dir.md
179 if sys
.platform
in ('linux', 'linux2'):
180 config
= _config_home()
182 'brave': os
.path
.join(config
, 'BraveSoftware/Brave-Browser'),
183 'chrome': os
.path
.join(config
, 'google-chrome'),
184 'chromium': os
.path
.join(config
, 'chromium'),
185 'edge': os
.path
.join(config
, 'microsoft-edge'),
186 'opera': os
.path
.join(config
, 'opera'),
187 'vivaldi': os
.path
.join(config
, 'vivaldi'),
190 elif sys
.platform
== 'win32':
191 appdata_local
= os
.path
.expandvars('%LOCALAPPDATA%')
192 appdata_roaming
= os
.path
.expandvars('%APPDATA%')
194 'brave': os
.path
.join(appdata_local
, R
'BraveSoftware\Brave-Browser\User Data'),
195 'chrome': os
.path
.join(appdata_local
, R
'Google\Chrome\User Data'),
196 'chromium': os
.path
.join(appdata_local
, R
'Chromium\User Data'),
197 'edge': os
.path
.join(appdata_local
, R
'Microsoft\Edge\User Data'),
198 'opera': os
.path
.join(appdata_roaming
, R
'Opera Software\Opera Stable'),
199 'vivaldi': os
.path
.join(appdata_local
, R
'Vivaldi\User Data'),
202 elif sys
.platform
== 'darwin':
203 appdata
= os
.path
.expanduser('~/Library/Application Support')
205 'brave': os
.path
.join(appdata
, 'BraveSoftware/Brave-Browser'),
206 'chrome': os
.path
.join(appdata
, 'Google/Chrome'),
207 'chromium': os
.path
.join(appdata
, 'Chromium'),
208 'edge': os
.path
.join(appdata
, 'Microsoft Edge'),
209 'opera': os
.path
.join(appdata
, 'com.operasoftware.Opera'),
210 'vivaldi': os
.path
.join(appdata
, 'Vivaldi'),
214 raise ValueError(f
'unsupported platform: {sys.platform}')
216 # Linux keyring names can be determined by snooping on dbus while opening the browser in KDE:
217 # dbus-monitor "interface='org.kde.KWallet'" "type=method_return"
221 'chromium': 'Chromium',
222 'edge': 'Microsoft Edge' if sys
.platform
== 'darwin' else 'Chromium',
223 'opera': 'Opera' if sys
.platform
== 'darwin' else 'Chromium',
224 'vivaldi': 'Vivaldi' if sys
.platform
== 'darwin' else 'Chrome',
227 browsers_without_profiles
= {'opera'}
230 'browser_dir': browser_dir
,
231 'keyring_name': keyring_name
,
232 'supports_profiles': browser_name
not in browsers_without_profiles
236 def _extract_chrome_cookies(browser_name
, profile
, keyring
, logger
):
237 logger
.info(f
'Extracting cookies from {browser_name}')
239 if not SQLITE_AVAILABLE
:
240 logger
.warning(f
'Cannot extract cookies from {browser_name} without sqlite3 support. '
241 'Please use a python interpreter compiled with sqlite3 support')
242 return YoutubeDLCookieJar()
244 config
= _get_chromium_based_browser_settings(browser_name
)
247 search_root
= config
['browser_dir']
248 elif _is_path(profile
):
249 search_root
= profile
250 config
['browser_dir'] = os
.path
.dirname(profile
) if config
['supports_profiles'] else profile
252 if config
['supports_profiles']:
253 search_root
= os
.path
.join(config
['browser_dir'], profile
)
255 logger
.error(f
'{browser_name} does not support profiles')
256 search_root
= config
['browser_dir']
258 cookie_database_path
= _find_most_recently_used_file(search_root
, 'Cookies', logger
)
259 if cookie_database_path
is None:
260 raise FileNotFoundError(f
'could not find {browser_name} cookies database in "{search_root}"')
261 logger
.debug(f
'Extracting cookies from: "{cookie_database_path}"')
263 decryptor
= get_cookie_decryptor(config
['browser_dir'], config
['keyring_name'], logger
, keyring
=keyring
)
265 with tempfile
.TemporaryDirectory(prefix
='yt_dlp') as tmpdir
:
268 cursor
= _open_database_copy(cookie_database_path
, tmpdir
)
269 cursor
.connection
.text_factory
= bytes
270 column_names
= _get_column_names(cursor
, 'cookies')
271 secure_column
= 'is_secure' if 'is_secure' in column_names
else 'secure'
272 cursor
.execute(f
'SELECT host_key, name, value, encrypted_value, path, expires_utc, {secure_column} FROM cookies')
273 jar
= YoutubeDLCookieJar()
275 unencrypted_cookies
= 0
276 with _create_progress_bar(logger
) as progress_bar
:
277 table
= cursor
.fetchall()
278 total_cookie_count
= len(table
)
279 for i
, line
in enumerate(table
):
280 progress_bar
.print(f
'Loading cookie {i: 6d}/{total_cookie_count: 6d}')
281 is_encrypted
, cookie
= _process_chrome_cookie(decryptor
, *line
)
285 elif not is_encrypted
:
286 unencrypted_cookies
+= 1
287 jar
.set_cookie(cookie
)
288 if failed_cookies
> 0:
289 failed_message
= f
' ({failed_cookies} could not be decrypted)'
292 logger
.info(f
'Extracted {len(jar)} cookies from {browser_name}{failed_message}')
293 counts
= decryptor
.cookie_counts
.copy()
294 counts
['unencrypted'] = unencrypted_cookies
295 logger
.debug(f
'cookie version breakdown: {counts}')
298 if cursor
is not None:
299 cursor
.connection
.close()
302 def _process_chrome_cookie(decryptor
, host_key
, name
, value
, encrypted_value
, path
, expires_utc
, is_secure
):
303 host_key
= host_key
.decode('utf-8')
304 name
= name
.decode('utf-8')
305 value
= value
.decode('utf-8')
306 path
= path
.decode('utf-8')
307 is_encrypted
= not value
and encrypted_value
310 value
= decryptor
.decrypt(encrypted_value
)
312 return is_encrypted
, None
314 return is_encrypted
, compat_cookiejar_Cookie(
315 version
=0, name
=name
, value
=value
, port
=None, port_specified
=False,
316 domain
=host_key
, domain_specified
=bool(host_key
), domain_initial_dot
=host_key
.startswith('.'),
317 path
=path
, path_specified
=bool(path
), secure
=is_secure
, expires
=expires_utc
, discard
=False,
318 comment
=None, comment_url
=None, rest
={})
321 class ChromeCookieDecryptor
:
326 - cookies are either v10 or v11
327 - v10: AES-CBC encrypted with a fixed key
328 - v11: AES-CBC encrypted with an OS protected key (keyring)
329 - v11 keys can be stored in various places depending on the activate desktop environment [2]
332 - cookies are either v10 or not v10
333 - v10: AES-CBC encrypted with an OS protected key (keyring) and more key derivation iterations than linux
334 - not v10: 'old data' stored as plaintext
337 - cookies are either v10 or not v10
338 - v10: AES-GCM encrypted with a key which is encrypted with DPAPI
339 - not v10: encrypted with DPAPI
342 - [1] https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/
343 - [2] https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/key_storage_linux.cc
344 - KeyStorageLinux::CreateService
347 def decrypt(self
, encrypted_value
):
348 raise NotImplementedError('Must be implemented by sub classes')
351 def cookie_counts(self
):
352 raise NotImplementedError('Must be implemented by sub classes')
355 def get_cookie_decryptor(browser_root
, browser_keyring_name
, logger
, *, keyring
=None):
356 if sys
.platform
in ('linux', 'linux2'):
357 return LinuxChromeCookieDecryptor(browser_keyring_name
, logger
, keyring
=keyring
)
358 elif sys
.platform
== 'darwin':
359 return MacChromeCookieDecryptor(browser_keyring_name
, logger
)
360 elif sys
.platform
== 'win32':
361 return WindowsChromeCookieDecryptor(browser_root
, logger
)
363 raise NotImplementedError(f
'Chrome cookie decryption is not supported on this platform: {sys.platform}')
366 class LinuxChromeCookieDecryptor(ChromeCookieDecryptor
):
367 def __init__(self
, browser_keyring_name
, logger
, *, keyring
=None):
368 self
._logger
= logger
369 self
._v
10_key
= self
.derive_key(b
'peanuts')
370 password
= _get_linux_keyring_password(browser_keyring_name
, keyring
, logger
)
371 self
._v
11_key
= None if password
is None else self
.derive_key(password
)
372 self
._cookie
_counts
= {'v10': 0, 'v11': 0, 'other': 0}
375 def derive_key(password
):
377 # https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/os_crypt_linux.cc
378 return pbkdf2_sha1(password
, salt
=b
'saltysalt', iterations
=1, key_length
=16)
381 def cookie_counts(self
):
382 return self
._cookie
_counts
384 def decrypt(self
, encrypted_value
):
385 version
= encrypted_value
[:3]
386 ciphertext
= encrypted_value
[3:]
388 if version
== b
'v10':
389 self
._cookie
_counts
['v10'] += 1
390 return _decrypt_aes_cbc(ciphertext
, self
._v
10_key
, self
._logger
)
392 elif version
== b
'v11':
393 self
._cookie
_counts
['v11'] += 1
394 if self
._v
11_key
is None:
395 self
._logger
.warning('cannot decrypt v11 cookies: no key found', only_once
=True)
397 return _decrypt_aes_cbc(ciphertext
, self
._v
11_key
, self
._logger
)
400 self
._cookie
_counts
['other'] += 1
404 class MacChromeCookieDecryptor(ChromeCookieDecryptor
):
405 def __init__(self
, browser_keyring_name
, logger
):
406 self
._logger
= logger
407 password
= _get_mac_keyring_password(browser_keyring_name
, logger
)
408 self
._v
10_key
= None if password
is None else self
.derive_key(password
)
409 self
._cookie
_counts
= {'v10': 0, 'other': 0}
412 def derive_key(password
):
414 # https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/os_crypt_mac.mm
415 return pbkdf2_sha1(password
, salt
=b
'saltysalt', iterations
=1003, key_length
=16)
418 def cookie_counts(self
):
419 return self
._cookie
_counts
421 def decrypt(self
, encrypted_value
):
422 version
= encrypted_value
[:3]
423 ciphertext
= encrypted_value
[3:]
425 if version
== b
'v10':
426 self
._cookie
_counts
['v10'] += 1
427 if self
._v
10_key
is None:
428 self
._logger
.warning('cannot decrypt v10 cookies: no key found', only_once
=True)
431 return _decrypt_aes_cbc(ciphertext
, self
._v
10_key
, self
._logger
)
434 self
._cookie
_counts
['other'] += 1
435 # other prefixes are considered 'old data' which were stored as plaintext
436 # https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/os_crypt_mac.mm
437 return encrypted_value
440 class WindowsChromeCookieDecryptor(ChromeCookieDecryptor
):
441 def __init__(self
, browser_root
, logger
):
442 self
._logger
= logger
443 self
._v
10_key
= _get_windows_v10_key(browser_root
, logger
)
444 self
._cookie
_counts
= {'v10': 0, 'other': 0}
447 def cookie_counts(self
):
448 return self
._cookie
_counts
450 def decrypt(self
, encrypted_value
):
451 version
= encrypted_value
[:3]
452 ciphertext
= encrypted_value
[3:]
454 if version
== b
'v10':
455 self
._cookie
_counts
['v10'] += 1
456 if self
._v
10_key
is None:
457 self
._logger
.warning('cannot decrypt v10 cookies: no key found', only_once
=True)
460 # https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/os_crypt_win.cc
462 nonce_length
= 96 // 8
464 # EVP_AEAD_AES_GCM_TAG_LEN
465 authentication_tag_length
= 16
467 raw_ciphertext
= ciphertext
468 nonce
= raw_ciphertext
[:nonce_length
]
469 ciphertext
= raw_ciphertext
[nonce_length
:-authentication_tag_length
]
470 authentication_tag
= raw_ciphertext
[-authentication_tag_length
:]
472 return _decrypt_aes_gcm(ciphertext
, self
._v
10_key
, nonce
, authentication_tag
, self
._logger
)
475 self
._cookie
_counts
['other'] += 1
476 # any other prefix means the data is DPAPI encrypted
477 # https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/os_crypt_win.cc
478 return _decrypt_windows_dpapi(encrypted_value
, self
._logger
).decode('utf-8')
481 def _extract_safari_cookies(profile
, logger
):
482 if profile
is not None:
483 logger
.error('safari does not support profiles')
484 if sys
.platform
!= 'darwin':
485 raise ValueError(f
'unsupported platform: {sys.platform}')
487 cookies_path
= os
.path
.expanduser('~/Library/Cookies/Cookies.binarycookies')
489 if not os
.path
.isfile(cookies_path
):
490 logger
.debug('Trying secondary cookie location')
491 cookies_path
= os
.path
.expanduser('~/Library/Containers/com.apple.Safari/Data/Library/Cookies/Cookies.binarycookies')
492 if not os
.path
.isfile(cookies_path
):
493 raise FileNotFoundError('could not find safari cookies database')
495 with open(cookies_path
, 'rb') as f
:
496 cookies_data
= f
.read()
498 jar
= parse_safari_cookies(cookies_data
, logger
=logger
)
499 logger
.info(f
'Extracted {len(jar)} cookies from safari')
503 class ParserError(Exception):
508 def __init__(self
, data
, logger
):
511 self
._logger
= logger
513 def read_bytes(self
, num_bytes
):
515 raise ParserError(f
'invalid read of {num_bytes} bytes')
516 end
= self
.cursor
+ num_bytes
517 if end
> len(self
._data
):
518 raise ParserError('reached end of input')
519 data
= self
._data
[self
.cursor
:end
]
523 def expect_bytes(self
, expected_value
, message
):
524 value
= self
.read_bytes(len(expected_value
))
525 if value
!= expected_value
:
526 raise ParserError(f
'unexpected value: {value} != {expected_value} ({message})')
528 def read_uint(self
, big_endian
=False):
529 data_format
= '>I' if big_endian
else '<I'
530 return struct
.unpack(data_format
, self
.read_bytes(4))[0]
532 def read_double(self
, big_endian
=False):
533 data_format
= '>d' if big_endian
else '<d'
534 return struct
.unpack(data_format
, self
.read_bytes(8))[0]
536 def read_cstring(self
):
539 c
= self
.read_bytes(1)
541 return b
''.join(buffer).decode('utf-8')
545 def skip(self
, num_bytes
, description
='unknown'):
547 self
._logger
.debug(f
'skipping {num_bytes} bytes ({description}): {self.read_bytes(num_bytes)!r}')
549 raise ParserError(f
'invalid skip of {num_bytes} bytes')
551 def skip_to(self
, offset
, description
='unknown'):
552 self
.skip(offset
- self
.cursor
, description
)
554 def skip_to_end(self
, description
='unknown'):
555 self
.skip_to(len(self
._data
), description
)
558 def _mac_absolute_time_to_posix(timestamp
):
559 return int((datetime(2001, 1, 1, 0, 0, tzinfo
=timezone
.utc
) + timedelta(seconds
=timestamp
)).timestamp())
562 def _parse_safari_cookies_header(data
, logger
):
563 p
= DataParser(data
, logger
)
564 p
.expect_bytes(b
'cook', 'database signature')
565 number_of_pages
= p
.read_uint(big_endian
=True)
566 page_sizes
= [p
.read_uint(big_endian
=True) for _
in range(number_of_pages
)]
567 return page_sizes
, p
.cursor
570 def _parse_safari_cookies_page(data
, jar
, logger
):
571 p
= DataParser(data
, logger
)
572 p
.expect_bytes(b
'\x00\x00\x01\x00', 'page signature')
573 number_of_cookies
= p
.read_uint()
574 record_offsets
= [p
.read_uint() for _
in range(number_of_cookies
)]
575 if number_of_cookies
== 0:
576 logger
.debug(f
'a cookies page of size {len(data)} has no cookies')
579 p
.skip_to(record_offsets
[0], 'unknown page header field')
581 with _create_progress_bar(logger
) as progress_bar
:
582 for i
, record_offset
in enumerate(record_offsets
):
583 progress_bar
.print(f
'Loading cookie {i: 6d}/{number_of_cookies: 6d}')
584 p
.skip_to(record_offset
, 'space between records')
585 record_length
= _parse_safari_cookies_record(data
[record_offset
:], jar
, logger
)
586 p
.read_bytes(record_length
)
587 p
.skip_to_end('space in between pages')
590 def _parse_safari_cookies_record(data
, jar
, logger
):
591 p
= DataParser(data
, logger
)
592 record_size
= p
.read_uint()
593 p
.skip(4, 'unknown record field 1')
594 flags
= p
.read_uint()
595 is_secure
= bool(flags
& 0x0001)
596 p
.skip(4, 'unknown record field 2')
597 domain_offset
= p
.read_uint()
598 name_offset
= p
.read_uint()
599 path_offset
= p
.read_uint()
600 value_offset
= p
.read_uint()
601 p
.skip(8, 'unknown record field 3')
602 expiration_date
= _mac_absolute_time_to_posix(p
.read_double())
603 _creation_date
= _mac_absolute_time_to_posix(p
.read_double()) # noqa: F841
606 p
.skip_to(domain_offset
)
607 domain
= p
.read_cstring()
609 p
.skip_to(name_offset
)
610 name
= p
.read_cstring()
612 p
.skip_to(path_offset
)
613 path
= p
.read_cstring()
615 p
.skip_to(value_offset
)
616 value
= p
.read_cstring()
617 except UnicodeDecodeError:
618 logger
.warning('failed to parse Safari cookie because UTF-8 decoding failed', only_once
=True)
621 p
.skip_to(record_size
, 'space at the end of the record')
623 cookie
= compat_cookiejar_Cookie(
624 version
=0, name
=name
, value
=value
, port
=None, port_specified
=False,
625 domain
=domain
, domain_specified
=bool(domain
), domain_initial_dot
=domain
.startswith('.'),
626 path
=path
, path_specified
=bool(path
), secure
=is_secure
, expires
=expiration_date
, discard
=False,
627 comment
=None, comment_url
=None, rest
={})
628 jar
.set_cookie(cookie
)
632 def parse_safari_cookies(data
, jar
=None, logger
=YDLLogger()):
635 - https://github.com/libyal/dtformats/blob/main/documentation/Safari%20Cookies.asciidoc
636 - this data appears to be out of date but the important parts of the database structure is the same
637 - there are a few bytes here and there which are skipped during parsing
640 jar
= YoutubeDLCookieJar()
641 page_sizes
, body_start
= _parse_safari_cookies_header(data
, logger
)
642 p
= DataParser(data
[body_start
:], logger
)
643 for page_size
in page_sizes
:
644 _parse_safari_cookies_page(p
.read_bytes(page_size
), jar
, logger
)
645 p
.skip_to_end('footer')
649 class _LinuxDesktopEnvironment(Enum
):
651 https://chromium.googlesource.com/chromium/src/+/refs/heads/main/base/nix/xdg_util.h
663 class _LinuxKeyring(Enum
):
665 https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/key_storage_util_linux.h
669 GNOMEKEYRING
= auto()
673 SUPPORTED_KEYRINGS
= _LinuxKeyring
.__members
__.keys()
676 def _get_linux_desktop_environment(env
):
678 https://chromium.googlesource.com/chromium/src/+/refs/heads/main/base/nix/xdg_util.cc
679 GetDesktopEnvironment
681 xdg_current_desktop
= env
.get('XDG_CURRENT_DESKTOP', None)
682 desktop_session
= env
.get('DESKTOP_SESSION', None)
683 if xdg_current_desktop
is not None:
684 xdg_current_desktop
= xdg_current_desktop
.split(':')[0].strip()
686 if xdg_current_desktop
== 'Unity':
687 if desktop_session
is not None and 'gnome-fallback' in desktop_session
:
688 return _LinuxDesktopEnvironment
.GNOME
690 return _LinuxDesktopEnvironment
.UNITY
691 elif xdg_current_desktop
== 'GNOME':
692 return _LinuxDesktopEnvironment
.GNOME
693 elif xdg_current_desktop
== 'X-Cinnamon':
694 return _LinuxDesktopEnvironment
.CINNAMON
695 elif xdg_current_desktop
== 'KDE':
696 return _LinuxDesktopEnvironment
.KDE
697 elif xdg_current_desktop
== 'Pantheon':
698 return _LinuxDesktopEnvironment
.PANTHEON
699 elif xdg_current_desktop
== 'XFCE':
700 return _LinuxDesktopEnvironment
.XFCE
701 elif desktop_session
is not None:
702 if desktop_session
in ('mate', 'gnome'):
703 return _LinuxDesktopEnvironment
.GNOME
704 elif 'kde' in desktop_session
:
705 return _LinuxDesktopEnvironment
.KDE
706 elif 'xfce' in desktop_session
:
707 return _LinuxDesktopEnvironment
.XFCE
709 if 'GNOME_DESKTOP_SESSION_ID' in env
:
710 return _LinuxDesktopEnvironment
.GNOME
711 elif 'KDE_FULL_SESSION' in env
:
712 return _LinuxDesktopEnvironment
.KDE
713 return _LinuxDesktopEnvironment
.OTHER
716 def _choose_linux_keyring(logger
):
718 https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/key_storage_util_linux.cc
721 desktop_environment
= _get_linux_desktop_environment(os
.environ
)
722 logger
.debug(f
'detected desktop environment: {desktop_environment.name}')
723 if desktop_environment
== _LinuxDesktopEnvironment
.KDE
:
724 linux_keyring
= _LinuxKeyring
.KWALLET
725 elif desktop_environment
== _LinuxDesktopEnvironment
.OTHER
:
726 linux_keyring
= _LinuxKeyring
.BASICTEXT
728 linux_keyring
= _LinuxKeyring
.GNOMEKEYRING
732 def _get_kwallet_network_wallet(logger
):
733 """ The name of the wallet used to store network passwords.
735 https://chromium.googlesource.com/chromium/src/+/refs/heads/main/components/os_crypt/kwallet_dbus.cc
736 KWalletDBus::NetworkWallet
737 which does a dbus call to the following function:
738 https://api.kde.org/frameworks/kwallet/html/classKWallet_1_1Wallet.html
739 Wallet::NetworkWallet
741 default_wallet
= 'kdewallet'
744 'dbus-send', '--session', '--print-reply=literal',
745 '--dest=org.kde.kwalletd5',
746 '/modules/kwalletd5',
747 'org.kde.KWallet.networkWallet'
748 ], stdout
=subprocess
.PIPE
, stderr
=subprocess
.DEVNULL
)
750 stdout
, stderr
= proc
.communicate_or_kill()
751 if proc
.returncode
!= 0:
752 logger
.warning('failed to read NetworkWallet')
753 return default_wallet
755 network_wallet
= stdout
.decode('utf-8').strip()
756 logger
.debug(f
'NetworkWallet = "{network_wallet}"')
757 return network_wallet
758 except Exception as e
:
759 logger
.warning(f
'exception while obtaining NetworkWallet: {e}')
760 return default_wallet
763 def _get_kwallet_password(browser_keyring_name
, logger
):
764 logger
.debug('using kwallet-query to obtain password from kwallet')
766 if shutil
.which('kwallet-query') is None:
767 logger
.error('kwallet-query command not found. KWallet and kwallet-query '
768 'must be installed to read from KWallet. kwallet-query should be'
769 'included in the kwallet package for your distribution')
772 network_wallet
= _get_kwallet_network_wallet(logger
)
777 '--read-password', f
'{browser_keyring_name} Safe Storage',
778 '--folder', f
'{browser_keyring_name} Keys',
780 ], stdout
=subprocess
.PIPE
, stderr
=subprocess
.DEVNULL
)
782 stdout
, stderr
= proc
.communicate_or_kill()
783 if proc
.returncode
!= 0:
784 logger
.error(f
'kwallet-query failed with return code {proc.returncode}. Please consult '
785 'the kwallet-query man page for details')
788 if stdout
.lower().startswith(b
'failed to read'):
789 logger
.debug('failed to read password from kwallet. Using empty string instead')
790 # this sometimes occurs in KDE because chrome does not check hasEntry and instead
791 # just tries to read the value (which kwallet returns "") whereas kwallet-query
792 # checks hasEntry. To verify this:
793 # dbus-monitor "interface='org.kde.KWallet'" "type=method_return"
794 # while starting chrome.
795 # this may be a bug as the intended behaviour is to generate a random password and store
796 # it, but that doesn't matter here.
799 logger
.debug('password found')
800 if stdout
[-1:] == b
'\n':
803 except Exception as e
:
804 logger
.warning(f
'exception running kwallet-query: {error_to_str(e)}')
808 def _get_gnome_keyring_password(browser_keyring_name
, logger
):
809 if not SECRETSTORAGE_AVAILABLE
:
810 logger
.error(f
'secretstorage not available {SECRETSTORAGE_UNAVAILABLE_REASON}')
812 # the Gnome keyring does not seem to organise keys in the same way as KWallet,
813 # using `dbus-monitor` during startup, it can be observed that chromium lists all keys
814 # and presumably searches for its key in the list. It appears that we must do the same.
815 # https://github.com/jaraco/keyring/issues/556
816 with contextlib
.closing(secretstorage
.dbus_init()) as con
:
817 col
= secretstorage
.get_default_collection(con
)
818 for item
in col
.get_all_items():
819 if item
.get_label() == f
'{browser_keyring_name} Safe Storage':
820 return item
.get_secret()
822 logger
.error('failed to read from keyring')
826 def _get_linux_keyring_password(browser_keyring_name
, keyring
, logger
):
827 # note: chrome/chromium can be run with the following flags to determine which keyring backend
828 # it has chosen to use
829 # chromium --enable-logging=stderr --v=1 2>&1 | grep key_storage_
830 # Chromium supports a flag: --password-store=<basic|gnome|kwallet> so the automatic detection
831 # will not be sufficient in all cases.
833 keyring
= _LinuxKeyring
[keyring
] if keyring
else _choose_linux_keyring(logger
)
834 logger
.debug(f
'Chosen keyring: {keyring.name}')
836 if keyring
== _LinuxKeyring
.KWALLET
:
837 return _get_kwallet_password(browser_keyring_name
, logger
)
838 elif keyring
== _LinuxKeyring
.GNOMEKEYRING
:
839 return _get_gnome_keyring_password(browser_keyring_name
, logger
)
840 elif keyring
== _LinuxKeyring
.BASICTEXT
:
841 # when basic text is chosen, all cookies are stored as v10 (so no keyring password is required)
843 assert False, f
'Unknown keyring {keyring}'
846 def _get_mac_keyring_password(browser_keyring_name
, logger
):
847 logger
.debug('using find-generic-password to obtain password from OSX keychain')
850 ['security', 'find-generic-password',
851 '-w', # write password to stdout
852 '-a', browser_keyring_name
, # match 'account'
853 '-s', f
'{browser_keyring_name} Safe Storage'], # match 'service'
854 stdout
=subprocess
.PIPE
, stderr
=subprocess
.DEVNULL
)
856 stdout
, stderr
= proc
.communicate_or_kill()
857 if stdout
[-1:] == b
'\n':
860 except Exception as e
:
861 logger
.warning(f
'exception running find-generic-password: {error_to_str(e)}')
865 def _get_windows_v10_key(browser_root
, logger
):
866 path
= _find_most_recently_used_file(browser_root
, 'Local State', logger
)
868 logger
.error('could not find local state file')
870 logger
.debug(f
'Found local state file at "{path}"')
871 with open(path
, encoding
='utf8') as f
:
874 base64_key
= data
['os_crypt']['encrypted_key']
876 logger
.error('no encrypted key in Local State')
878 encrypted_key
= compat_b64decode(base64_key
)
880 if not encrypted_key
.startswith(prefix
):
881 logger
.error('invalid key')
883 return _decrypt_windows_dpapi(encrypted_key
[len(prefix
):], logger
)
886 def pbkdf2_sha1(password
, salt
, iterations
, key_length
):
887 return pbkdf2_hmac('sha1', password
, salt
, iterations
, key_length
)
890 def _decrypt_aes_cbc(ciphertext
, key
, logger
, initialization_vector
=b
' ' * 16):
891 plaintext
= unpad_pkcs7(aes_cbc_decrypt_bytes(ciphertext
, key
, initialization_vector
))
893 return plaintext
.decode('utf-8')
894 except UnicodeDecodeError:
895 logger
.warning('failed to decrypt cookie (AES-CBC) because UTF-8 decoding failed. Possibly the key is wrong?', only_once
=True)
899 def _decrypt_aes_gcm(ciphertext
, key
, nonce
, authentication_tag
, logger
):
901 plaintext
= aes_gcm_decrypt_and_verify_bytes(ciphertext
, key
, authentication_tag
, nonce
)
903 logger
.warning('failed to decrypt cookie (AES-GCM) because the MAC check failed. Possibly the key is wrong?', only_once
=True)
907 return plaintext
.decode('utf-8')
908 except UnicodeDecodeError:
909 logger
.warning('failed to decrypt cookie (AES-GCM) because UTF-8 decoding failed. Possibly the key is wrong?', only_once
=True)
913 def _decrypt_windows_dpapi(ciphertext
, logger
):
916 - https://docs.microsoft.com/en-us/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata
918 from ctypes
.wintypes
import DWORD
920 class DATA_BLOB(ctypes
.Structure
):
921 _fields_
= [('cbData', DWORD
),
922 ('pbData', ctypes
.POINTER(ctypes
.c_char
))]
924 buffer = ctypes
.create_string_buffer(ciphertext
)
925 blob_in
= DATA_BLOB(ctypes
.sizeof(buffer), buffer)
926 blob_out
= DATA_BLOB()
927 ret
= ctypes
.windll
.crypt32
.CryptUnprotectData(
928 ctypes
.byref(blob_in
), # pDataIn
929 None, # ppszDataDescr: human readable description of pDataIn
930 None, # pOptionalEntropy: salt?
931 None, # pvReserved: must be NULL
932 None, # pPromptStruct: information about prompts to display
934 ctypes
.byref(blob_out
) # pDataOut
937 logger
.warning('failed to decrypt with DPAPI', only_once
=True)
940 result
= ctypes
.string_at(blob_out
.pbData
, blob_out
.cbData
)
941 ctypes
.windll
.kernel32
.LocalFree(blob_out
.pbData
)
946 return os
.environ
.get('XDG_CONFIG_HOME', os
.path
.expanduser('~/.config'))
949 def _open_database_copy(database_path
, tmpdir
):
950 # cannot open sqlite databases if they are already in use (e.g. by the browser)
951 database_copy_path
= os
.path
.join(tmpdir
, 'temporary.sqlite')
952 shutil
.copy(database_path
, database_copy_path
)
953 conn
= sqlite3
.connect(database_copy_path
)
957 def _get_column_names(cursor
, table_name
):
958 table_info
= cursor
.execute(f
'PRAGMA table_info({table_name})').fetchall()
959 return [row
[1].decode('utf-8') for row
in table_info
]
962 def _find_most_recently_used_file(root
, filename
, logger
):
963 # if there are multiple browser profiles, take the most recently used one
965 with _create_progress_bar(logger
) as progress_bar
:
966 for curr_root
, dirs
, files
in os
.walk(root
):
969 progress_bar
.print(f
'Searching for "{filename}": {i: 6d} files searched')
971 paths
.append(os
.path
.join(curr_root
, file))
972 return None if not paths
else max(paths
, key
=lambda path
: os
.lstat(path
).st_mtime
)
975 def _merge_cookie_jars(jars
):
976 output_jar
= YoutubeDLCookieJar()
979 output_jar
.set_cookie(cookie
)
980 if jar
.filename
is not None:
981 output_jar
.filename
= jar
.filename
986 return os
.path
.sep
in value
989 def _parse_browser_specification(browser_name
, profile
=None, keyring
=None):
990 if browser_name
not in SUPPORTED_BROWSERS
:
991 raise ValueError(f
'unsupported browser: "{browser_name}"')
992 if keyring
not in (None, *SUPPORTED_KEYRINGS
):
993 raise ValueError(f
'unsupported keyring: "{keyring}"')
994 if profile
is not None and _is_path(profile
):
995 profile
= os
.path
.expanduser(profile
)
996 return browser_name
, profile
, keyring