diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-24 08:49:00 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-06-24 08:49:00 (GMT) |
| commit | 9dda2caca8edc7ff1285f6b0d1c5279b51854b7d (patch) | |
| tree | bfe785b8878b1bb8bb531a8a97a674ae4624b4e6 /Lib | |
| parent | 7709b4d57b433ef027a2e7e63b4cab3fc9ad910d (diff) | |
| download | cpython-9dda2caca8edc7ff1285f6b0d1c5279b51854b7d.zip cpython-9dda2caca8edc7ff1285f6b0d1c5279b51854b7d.tar.gz cpython-9dda2caca8edc7ff1285f6b0d1c5279b51854b7d.tar.bz2 | |
[2.7] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (#2372)
Prevent passing other invalid environment variables and command arguments..
(cherry picked from commit d174d24a5d37d1516b885dc7c82f71ecd5930700)
Diffstat (limited to 'Lib')
| -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 6272758..5220891 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -385,6 +385,46 @@ class ProcessTestCase(BaseTestCase): self.addCleanup(p.stdout.close) self.assertEqual(p.stdout.read(), "orange") + def test_invalid_cmd(self): + # null character in the command name + cmd = sys.executable + '\0' + with self.assertRaises(TypeError): + subprocess.Popen([cmd, "-c", "pass"]) + + # null character in the command argument + with self.assertRaises(TypeError): + 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(TypeError): + 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(TypeError): + 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" + p = subprocess.Popen([sys.executable, "-c", + 'import sys, os;' + 'sys.stdout.write(os.getenv("FRUIT"))'], + stdout=subprocess.PIPE, + env=newenv) + stdout, stderr = p.communicate() + self.assertEqual(stdout, "orange=lemon") + def test_communicate_stdin(self): p = subprocess.Popen([sys.executable, "-c", 'import sys;' |
