CVE-2023-35934 (Python, published 2023-07-06T20:15Z)
Description: value: yt-dlp is a command-line program to download videos from video sites. During file downloads, yt-dlp or the external downloaders that yt-dlp employs may leak cookies on HTTP redirects to a different host, or leak them when the host for download fragments differs from their parent manifest's host. This vulnerable behavior is present in yt-dlp prior to 2023.07.06 and nightly 2023.07.06.185519. All native and external downloaders are affected, except for `curl` and `httpie` (version 3.1.0 or later).

At the file download stage, all cookies are passed by yt-dlp to the file downloader as a `Cookie` header, thereby losing their scope. This also occurs in yt-dlp's info JSON output, which may be used by external tools. As a result, the downloader or external tool may indiscriminately send cookies with requests to domains or paths for which the cookies are not scoped.

yt-dlp version 2023.07.06 and nightly 2023.07.06.185519 fix this issue by removing the `Cookie` header upon HTTP redirects; having native downloaders calculate the `Cookie` header from the cookiejar, utilizing external downloaders' built-in support for cookies instead of passing them as header arguments, disabling HTTP redirectiong if the external downloader does not have proper cookie support, processing cookies passed as HTTP headers to limit their scope, and having a separate field for cookies in the info dict storing more information about scoping

Some workarounds are available for those who are unable to upgrade. Avoid using cookies and user authentication methods. While extractors may set custom cookies, these usually do not contain sensitive information. Alternatively, avoid using `--load-info-json`. Or, if authentication is a must: verify the integrity of download links from unknown sources in browser (including redirects) before passing them to yt-dlp; use `curl` as external downloader, since it is not impacted; and/or avoid fragmented formats such as HLS/m3u8, DASH/mpd and ISM.
Severity: nan (CVSS v3 base score: 8.2)
Weakness: CWE-200 \u2014 Exposure of Sensitive Information to an Unauthorized Actor
The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
Repository: https://github.com/yt-dlp/yt-dlp
Files changed:
test/test_YoutubeDL.py
yt_dlp/YoutubeDL.py
yt_dlp/downloader/common.py
Commit message: [core] Change how `Cookie` headers are handled

Cookies are now saved and loaded under `cookies` key in the info dict
instead of `http_headers.Cookie`. Cookies passed in headers are
auto-scoped to the input URLs with a warning.

Ref: https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-v8mc-9377-rwjj

Authored by: Grub4K


Vulnerable code:
from .cookies import load_cookies
        cookies = self.cookiejar.get_cookie_header(info_dict['url'])
            res['Cookie'] = cookies


Fixed code:
    def test_header_cookies(self):
        from http.cookiejar import Cookie

        ydl = FakeYDL()
        ydl.report_warning = lambda *_, **__: None

        def cookie(name, value, version=None, domain='', path='', secure=False, expires=None):
            return Cookie(
                version or 0, name, value, None, False,
                domain, bool(domain), bool(domain), path, bool(path),
                secure, expires, False, None, None, rest={})

        _test_url = 'https://yt.dlp/test'

        def test(encoded_cookies, cookies, headers=False, round_trip=None, error=None):
            def _test():
                ydl.cookiejar.clear()
                ydl._load_cookies(encoded_cookies, from_headers=headers)
                if headers:
                    ydl._apply_header_cookies(_test_url)
                data = {'url': _test_url}
                ydl._calc_headers(data)
                self.assertCountEqual(
                    map(vars, ydl.cookiejar), map(vars, cookies),
                    'Extracted cookiejar.Cookie is not the same')
                if not headers:
                    self.assertEqual(
                        data.get('cookies'), round_trip or encoded_cookies,
                        'Cookie is not the same as round trip')
                ydl.__dict__['_YoutubeDL__header_cookies'] = []

            with self.subTest(msg=encoded_cookies):
                if not error:
                    _test()
                    return
                with self.assertRaisesRegex(Exception, error):
                    _test()

        test('test=value; Domain=.yt.dlp', [cookie('test', 'value', domain='.yt.dlp')])
        test('test=value', [cookie('test', 'value')], error='Unscoped cookies are not allowed')
        test('cookie1=value1; Domain=.yt.dlp; Path=/test; cookie2=value2; Domain=.yt.dlp; Path=/', [
            cookie('cookie1', 'value1', domain='.yt.dlp', path='/test'),
            cookie('cookie2', 'value2', domain='.yt.dlp', path='/')])
        test('test=value; Domain=.yt.dlp; Path=/test; Secure; Expires=9999999999', [
            cookie('test', 'value', domain='.yt.dlp', path='/test', secure=True, expires=9999999999)])
        test('test="value; "; path=/test; domain=.yt.dlp', [
            cookie('test', 'value; ', domain='.yt.dlp', path='/test')],
            round_trip='test="value\\073 "; Domain=.yt.dlp; Path=/test')
        test('name=; Domain=.yt.dlp', [cookie('name', '', domain='.yt.dlp')],
             round_trip='name=""; Domain=.yt.dlp')

        test('test=value', [cookie('test', 'value', domain='.yt.dlp')], headers=True)
        test('cookie1=value; Domain=.yt.dlp; cookie2=value', [], headers=True, error='Invalid syntax')
        ydl.deprecated_feature = ydl.report_error
        test('test=value', [], headers=True, error='Passing cookies as a header is a potential security risk')

import copy
import http.cookiejar
from .cookies import LenientSimpleCookie, load_cookies
        self.__header_cookies = []
        self._load_cookies(traverse_obj(self.params.get('http_headers'), 'cookie', casesense=False))  # compat

    def _load_cookies(self, data, *, from_headers=True):
        """Loads cookies from a `Cookie` header

        This tries to work around the security vulnerability of passing cookies to every domain.
        See: https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-v8mc-9377-rwjj
        The unscoped cookies are saved for later to be stored in the jar with a limited scope.

        @param data         The Cookie header as string to load the cookies from
        @param from_headers If `False`, allows Set-Cookie syntax in the cookie string (at least a domain will be required)
        """
        for cookie in LenientSimpleCookie(data).values():
            if from_headers and any(cookie.values()):
                raise ValueError('Invalid syntax in Cookie Header')

            domain = cookie.get('domain') or ''
            expiry = cookie.get('expires')
            if expiry == '':  # 0 is valid
                expiry = None
            prepared_cookie = http.cookiejar.Cookie(
                cookie.get('version') or 0, cookie.key, cookie.value, None, False,
                domain, True, True, cookie.get('path') or '', bool(cookie.get('path')),
                cookie.get('secure') or False, expiry, False, None, None, {})

            if domain:
                self.cookiejar.set_cookie(prepared_cookie)
            elif from_headers:
                self.deprecated_feature(
                    'Passing cookies as a header is a potential security risk; '
                    'they will be scoped to the domain of the downloaded urls. '
                    'Please consider loading cookies from a file or browser instead.')
                self.__header_cookies.append(prepared_cookie)
            else:
                self.report_error('Unscoped cookies are not allowed; please specify some sort of scoping',
                                  tb=False, is_error=False)

    def _apply_header_cookies(self, url):
        """Applies stray header cookies to the provided url

        This loads header cookies and scopes them to the domain provided in `url`.
        While this is not ideal, it helps reduce the risk of them being sent
        to an unintended destination while mostly maintaining compatibility.
        """
        parsed = urllib.parse.urlparse(url)
        if not parsed.hostname:
            return

        for cookie in map(copy.copy, self.__header_cookies):
            cookie.domain = f'.{parsed.hostname}'
            self.cookiejar.set_cookie(cookie)

        self._apply_header_cookies(url)

        cookies = self.cookiejar.get_cookies_for_url(info_dict['url'])
            encoder = LenientSimpleCookie()
            values = []
            for cookie in cookies:
                _, value = encoder.value_encode(cookie.value)
                values.append(f'{cookie.name}={value}')
                if cookie.domain:
                    values.append(f'Domain={cookie.domain}')
                if cookie.path:
                    values.append(f'Path={cookie.path}')
                if cookie.secure:
                    values.append('Secure')
                if cookie.expires:
                    values.append(f'Expires={cookie.expires}')
                if cookie.version:
                    values.append(f'Version={cookie.version}')
            info_dict['cookies'] = '; '.join(values)
            self._load_cookies(info.get('cookies'), from_headers=False)
            self._load_cookies(traverse_obj(info.get('http_headers'), 'Cookie', casesense=False))  # compat
from ..utils.traversal import traverse_obj
        # Filter the `Cookie` header from the info_dict to prevent leaks.
        # See: https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-v8mc-9377-rwjj
        info_dict['http_headers'] = dict(traverse_obj(info_dict, (
            'http_headers', {dict.items}, lambda _, pair: pair[0].lower() != 'cookie'))) or None