@@ -81,8 +81,8 @@ public function testCanAddArguments()
8181 $ command ->addArg ('-b= ' , array ('v4 ' ,'v5 ' ,'v6 ' ));
8282 $ command ->addArg ('-c ' , '' );
8383 $ command ->addArg ('some name ' , null , true );
84- $ this ->assertEquals ("--arg1=x --a --a '中文字äüp' --a 'v'\''1' 'v2' 'v3' -b=v -b ='v4' 'v5' 'v6' -c '' 'some name' " , $ command ->getArgs ());
85- $ this ->assertEquals ("test --arg1=x --a --a '中文字äüp' --a 'v'\''1' 'v2' 'v3' -b=v -b ='v4' 'v5' 'v6' -c '' 'some name' " , $ command ->getExecCommand ());
84+ $ this ->assertEquals ("--arg1=x ' --a' ' --a' '中文字äüp' ' --a' 'v'\''1' 'v2' 'v3' -b=v '-b' ='v4' 'v5' 'v6' '-c' '' 'some name' " , $ command ->getArgs ());
85+ $ this ->assertEquals ("test --arg1=x ' --a' ' --a' '中文字äüp' ' --a' 'v'\''1' 'v2' 'v3' -b=v '-b' ='v4' 'v5' 'v6' '-c' '' 'some name' " , $ command ->getExecCommand ());
8686 }
8787 public function testCanResetArguments ()
8888 {
@@ -102,14 +102,29 @@ public function testCanDisableEscaping()
102102 $ command ->addArg ('-b= ' ,'v ' , true );
103103 $ command ->addArg ('-b= ' , array ('v4 ' ,'v5 ' ,'v6 ' ));
104104 $ command ->addArg ('some name ' , null , true );
105- $ this ->assertEquals ("--a --a v --a v1 v2 v3 -b='v' -b=v4 v5 v6 'some name' " , $ command ->getArgs ());
105+ $ this ->assertEquals ("--a --a v --a v1 v2 v3 '-b'='v' -b=v4 v5 v6 'some name' " , $ command ->getArgs ());
106+ }
107+ public function testCanPreventCommandInjection ()
108+ {
109+ $ command = new Command (array (
110+ 'command ' => 'curl ' ,
111+ ));
112+ $ command ->addArg ('http://example.com --wrong-argument || echo "RCE 1" ' );
113+ $ this ->assertEquals ("'http://example.com --wrong-argument || echo \"RCE 1 \"' " , $ command ->getArgs ());
114+
115+ $ command = new Command (array (
116+ 'command ' => 'curl ' ,
117+ ));
118+ $ command ->addArg ('http://example.com ' );
119+ $ command ->addArg ('--header foo --wrong-argument || echo "RCE 2" || ' , 'bar ' );
120+ $ this ->assertEquals ("'http://example.com' '--header foo --wrong-argument || echo \"RCE 2 \" ||' 'bar' " , $ command ->getArgs ());
106121 }
107122 public function testCanRunCommandWithArguments ()
108123 {
109124 $ command = new Command ('ls ' );
110125 $ command ->addArg ('-l ' );
111126 $ command ->addArg ('-n ' );
112- $ this ->assertEquals ("ls -l -n " , $ command ->getExecCommand ());
127+ $ this ->assertEquals ("ls '-l' '-n' " , $ command ->getExecCommand ());
113128 $ this ->assertFalse ($ command ->getExecuted ());
114129 $ this ->assertTrue ($ command ->execute ());
115130 $ this ->assertTrue ($ command ->getExecuted ());
@@ -163,7 +178,7 @@ public function testCanCastToString()
163178 $ command = new Command ('ls ' );
164179 $ command ->addArg ('-l ' );
165180 $ command ->addArg ('-n ' );
166- $ this ->assertEquals ("ls -l -n " , (string )$ command );
181+ $ this ->assertEquals ("ls '-l' '-n' " , (string )$ command );
167182 }
168183
169184 // Exec
0 commit comments