diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-23 16:39:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-23 16:39:27 (GMT) |
commit | d174d24a5d37d1516b885dc7c82f71ecd5930700 (patch) | |
tree | d9fd67e5993b32d8b80c58099dd4a6aa0672722b /Lib/test/test_subprocess.py | |
parent | d352d689775699c289e011e8cec52c23c600b7fa (diff) | |
download | cpython-d174d24a5d37d1516b885dc7c82f71ecd5930700.zip cpython-d174d24a5d37d1516b885dc7c82f71ecd5930700.tar.gz cpython-d174d24a5d37d1516b885dc7c82f71ecd5930700.tar.bz2 |
bpo-30730: Prevent environment variables injection in subprocess on Windows. (#2325)
Prevent passing other invalid environment variables and command arguments.
Diffstat (limited to 'Lib/test/test_subprocess.py')
-rw-r--r-- | Lib/test/test_subprocess.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 52b05c1..4c6e159 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -655,6 +655,46 @@ class ProcessTestCase(BaseTestCase): if not is_env_var_to_ignore(k)] self.assertEqual(child_env_names, []) + def test_invalid_cmd(self): + # null character in the command name + cmd = sys.executable + '\0' + with self.assertRaises(ValueError): + subprocess.Popen([cmd, "-c", "pass"]) + + # null character in the command argument + with self.assertRaises(ValueError): + subprocess.Popen([sys.executable, "-c", "pass#\0"]) + + def test_invalid_env(self): + # null character in the enviroment variable name + newenv = os.environ.copy() + newenv["FRUIT\0VEGETABLE"] = "cabbage" + with self.assertRaises(ValueError): + subprocess.Popen([sys.executable, "-c", "pass"], env=newenv) + + # null character in the enviroment variable value + newenv = os.environ.copy() + newenv["FRUIT"] = "orange\0VEGETABLE=cabbage" + with self.assertRaises(ValueError): + subprocess.Popen([sys.executable, "-c", "pass"], env=newenv) + + # equal character in the enviroment variable name + newenv = os.environ.copy() + newenv["FRUIT=ORANGE"] = "lemon" + with self.assertRaises(ValueError): + subprocess.Popen([sys.executable, "-c", "pass"], env=newenv) + + # equal character in the enviroment variable value + newenv = os.environ.copy() + newenv["FRUIT"] = "orange=lemon" + with subprocess.Popen([sys.executable, "-c", + 'import sys, os;' + 'sys.stdout.write(os.getenv("FRUIT"))'], + stdout=subprocess.PIPE, + env=newenv) as p: + stdout, stderr = p.communicate() + self.assertEqual(stdout, b"orange=lemon") + def test_communicate_stdin(self): p = subprocess.Popen([sys.executable, "-c", 'import sys;' |