aboutsummaryrefslogtreecommitdiff
path: root/test/test_utils.py
diff options
context:
space:
mode:
authorSimon Sawicki <contact@grub4k.xyz>2023-09-24 02:29:01 +0200
committerSimon Sawicki <contact@grub4k.xyz>2023-09-24 02:29:01 +0200
commitde015e930747165dbb8fcd360f8775fd973b7d6e (patch)
tree7588e5aefdba5eb635a8690b824b1a49672342d8 /test/test_utils.py
parent61bdf15fc7400601c3da1aa7a43917310a5bf391 (diff)
[core] Prevent RCE when using `--exec` with `%q` (CVE-2023-40581)
The shell escape function is now using `""` instead of `\"`. `utils.Popen` has been patched to properly quote commands. Prior to this fix using `--exec` together with `%q` when on Windows could cause remote code to execute. See https://github.com/yt-dlp/yt-dlp/security/advisories/GHSA-42h4-v29r-42qg for reference. Authored by: Grub4K
Diffstat (limited to 'test/test_utils.py')
-rw-r--r--test/test_utils.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/test/test_utils.py b/test/test_utils.py
index 47d1f71bf..dc2d8ce12 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -14,6 +14,7 @@ import contextlib
import io
import itertools
import json
+import subprocess
import xml.etree.ElementTree
from yt_dlp.compat import (
@@ -28,6 +29,7 @@ from yt_dlp.utils import (
InAdvancePagedList,
LazyList,
OnDemandPagedList,
+ Popen,
age_restricted,
args_to_str,
base_url,
@@ -2388,6 +2390,20 @@ Line 1
assert extract_basic_auth('http://user:@foo.bar') == ('http://foo.bar', 'Basic dXNlcjo=')
assert extract_basic_auth('http://user:pass@foo.bar') == ('http://foo.bar', 'Basic dXNlcjpwYXNz')
+ @unittest.skipUnless(compat_os_name == 'nt', 'Only relevant on Windows')
+ def test_Popen_windows_escaping(self):
+ def run_shell(args):
+ stdout, stderr, error = Popen.run(
+ args, text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ assert not stderr
+ assert not error
+ return stdout
+
+ # Test escaping
+ assert run_shell(['echo', 'test"&']) == '"test""&"\n'
+ # Test if delayed expansion is disabled
+ assert run_shell(['echo', '^!']) == '"^!"\n'
+ assert run_shell('echo "^!"') == '"^!"\n'
if __name__ == '__main__':
unittest.main()