Skip to content

Commit 701d8a6

Browse files
authored
ext/standard: Throw a ValueError when the parameter includes NUL bytes in putenv and getenv (php#21817)
1 parent fc9a4a3 commit 701d8a6

4 files changed

Lines changed: 41 additions & 2 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ PHP NEWS
170170
argument value is passed. (Girgias)
171171
. linkinfo() now raises a ValueError when the argument is an empty string.
172172
(Weilin Du)
173+
. getenv() and putenv() now raises a ValueError when the first argument
174+
contains null bytes. (Weilin Du)
173175

174176
- Streams:
175177
. Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ PHP 8.6 UPGRADE NOTES
8888
argument value is passed.
8989
. array_change_key_case() now raises a ValueError when an invalid $case
9090
argument value is passed.
91+
. getenv() and putenv() now raises a ValueError when the first argument
92+
contains null bytes.
9193
. linkinfo() now raises a ValueError when the $path argument is empty.
9294
. pathinfo() now raises a ValueError when an invalid $flag
9395
argument value is passed.

ext/standard/basic_functions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ PHP_FUNCTION(getenv)
696696

697697
ZEND_PARSE_PARAMETERS_START(0, 2)
698698
Z_PARAM_OPTIONAL
699-
Z_PARAM_STRING_OR_NULL(str, str_len)
699+
Z_PARAM_PATH_OR_NULL(str, str_len)
700700
Z_PARAM_BOOL(local_only)
701701
ZEND_PARSE_PARAMETERS_END();
702702

@@ -739,7 +739,7 @@ PHP_FUNCTION(putenv)
739739
#endif
740740

741741
ZEND_PARSE_PARAMETERS_START(1, 1)
742-
Z_PARAM_STRING(setting, setting_len)
742+
Z_PARAM_PATH(setting, setting_len)
743743
ZEND_PARSE_PARAMETERS_END();
744744

745745
if (setting_len == 0 || setting[0] == '=') {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
getenv() and putenv() reject null bytes
3+
--FILE--
4+
<?php
5+
6+
foreach ([false, true] as $local_only) {
7+
try {
8+
getenv("PHP_GETENV_NUL_TEST\0SUFFIX", $local_only);
9+
} catch (ValueError $exception) {
10+
echo $exception->getMessage() . "\n";
11+
}
12+
}
13+
14+
$var_name = 'PHP_PUTENV_NUL_TEST';
15+
16+
foreach ([
17+
$var_name . "\0SUFFIX=value",
18+
$var_name . "=va\0lue",
19+
] as $assignment) {
20+
try {
21+
putenv($assignment);
22+
} catch (ValueError $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
25+
}
26+
27+
var_dump(getenv($var_name));
28+
29+
?>
30+
--EXPECT--
31+
getenv(): Argument #1 ($name) must not contain any null bytes
32+
getenv(): Argument #1 ($name) must not contain any null bytes
33+
putenv(): Argument #1 ($assignment) must not contain any null bytes
34+
putenv(): Argument #1 ($assignment) must not contain any null bytes
35+
bool(false)

0 commit comments

Comments
 (0)