Skip to content

Commit fe5a985

Browse files
committed
Reject null bytes in quote arguments
1 parent bb77894 commit fe5a985

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

src/functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,9 @@ function quote(string|int $arg): string
998998
if ($arg === '') {
999999
return "\$''";
10001000
}
1001+
if (str_contains($arg, "\0")) {
1002+
throw new \InvalidArgumentException('quote(): null byte is not allowed in shell arguments');
1003+
}
10011004
if (preg_match('/^[\w\/.\-+@:=,%]+$/', $arg)) {
10021005
return $arg;
10031006
}
@@ -1009,7 +1012,6 @@ function quote(string|int $arg): string
10091012
"\r" => '\\r',
10101013
"\t" => '\\t',
10111014
"\v" => '\\v',
1012-
"\0" => '\\0',
10131015
]) . "'";
10141016
}
10151017

tests/src/QuoteTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public static function unsafeStringsProvider(): array
6060
'carriage return' => ["line1\rline2", "\$'line1\\rline2'"],
6161
'form feed' => ["page\fbreak", "\$'page\\fbreak'"],
6262
'vertical tab' => ["vert\vtab", "\$'vert\\vtab'"],
63-
'null byte' => ["null\0byte", "\$'null\\0byte'"],
6463
'semicolon' => ['cmd; rm -rf /', "\$'cmd; rm -rf /'"],
6564
'pipe' => ['a | b', "\$'a | b'"],
6665
'ampersand' => ['a & b', "\$'a & b'"],
@@ -85,11 +84,17 @@ public function testMultipleEscapes()
8584

8685
public function testAllSpecialCharsAtOnce()
8786
{
88-
$input = "'\\\f\n\r\t\v\0";
89-
$expected = "\$'\\'\\\\\\f\\n\\r\\t\\v\\0'";
87+
$input = "'\\\f\n\r\t\v";
88+
$expected = "\$'\\'\\\\\\f\\n\\r\\t\\v'";
9089
self::assertEquals($expected, quote($input));
9190
}
9291

92+
public function testNullByteRejected()
93+
{
94+
$this->expectException(\InvalidArgumentException::class);
95+
quote("null\0byte");
96+
}
97+
9398
public function testUnicodeContent()
9499
{
95100
self::assertEquals("\$'héllo wörld'", quote('héllo wörld'));

0 commit comments

Comments
 (0)