Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ PHP 8.7 UPGRADE NOTES
not present in the result set. It previously reported the condition
through PDO::ATTR_ERRMODE and returned false.

- PGSQL:
. pg_query_params(), pg_execute(), pg_send_query_params() and
pg_send_execute() now throw a ValueError when a parameter string contains
a null byte, as do pg_escape_string(), pg_escape_literal() and
pg_escape_identifier() for their $string argument. The value was
previously truncated silently at the first null byte.

- Standard:
. The number of filters that can be chained in a php://filter URL is limited
to 16 by default. Set the stream context option max_filter_count to change
Expand Down
24 changes: 15 additions & 9 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ static void _php_pgsql_free_params(char **params, uint32_t num_params)
efree(params);
}

static char **php_pgsql_make_arguments(const HashTable *param_arr, int *num_params)
static char **php_pgsql_make_arguments(const HashTable *param_arr, int *num_params, uint32_t arg_num)
{
/* This conversion is safe because of the limit of number of elements in a table. */
*num_params = (int) zend_hash_num_elements(param_arr);
Expand All @@ -1230,6 +1230,12 @@ static char **php_pgsql_make_arguments(const HashTable *param_arr, int *num_para
_php_pgsql_free_params(params, i);
return NULL;
}
if (UNEXPECTED(zend_str_has_nul_byte(param_str))) {
zend_argument_value_error(arg_num, "must not contain strings with any null bytes");
zend_string_release(param_str);
_php_pgsql_free_params(params, i);
return NULL;
}
params[i] = ZSTR_VAL(param_str);
}
i++;
Expand Down Expand Up @@ -1290,7 +1296,7 @@ PHP_FUNCTION(pg_query_params)
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
}

params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params);
params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params, ZEND_NUM_ARGS());
if (UNEXPECTED(!params)) {
RETURN_THROWS();
}
Expand Down Expand Up @@ -1473,7 +1479,7 @@ PHP_FUNCTION(pg_execute)
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
}

params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params);
params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params, ZEND_NUM_ARGS());
if (UNEXPECTED(!params)) {
RETURN_THROWS();
}
Expand Down Expand Up @@ -3544,15 +3550,15 @@ PHP_FUNCTION(pg_escape_string)
switch (ZEND_NUM_ARGS()) {
case 1:
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(from)
Z_PARAM_PATH_STR(from)
ZEND_PARSE_PARAMETERS_END();

link = FETCH_DEFAULT_LINK();
break;
default:
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(from)
Z_PARAM_PATH_STR(from)
ZEND_PARSE_PARAMETERS_END();

link = Z_PGSQL_LINK_P(pgsql_link);
Expand Down Expand Up @@ -3656,7 +3662,7 @@ static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_l
switch (ZEND_NUM_ARGS()) {
case 1:
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(from)
Z_PARAM_PATH_STR(from)
ZEND_PARSE_PARAMETERS_END();

link = FETCH_DEFAULT_LINK();
Expand All @@ -3666,7 +3672,7 @@ static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_l
default:
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJECT_OF_CLASS(pgsql_link, pgsql_link_ce)
Z_PARAM_STR(from)
Z_PARAM_PATH_STR(from)
ZEND_PARSE_PARAMETERS_END();

link = Z_PGSQL_LINK_P(pgsql_link);
Expand Down Expand Up @@ -4041,7 +4047,7 @@ PHP_FUNCTION(pg_send_query_params)
"There are results on this connection. Call pg_get_result() until it returns FALSE");
}

params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params);
params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params, ZEND_NUM_ARGS());
if (UNEXPECTED(!params)) {
RETURN_THROWS();
}
Expand Down Expand Up @@ -4196,7 +4202,7 @@ PHP_FUNCTION(pg_send_execute)
"There are results on this connection. Call pg_get_result() until it returns FALSE");
}

params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params);
params = php_pgsql_make_arguments(Z_ARRVAL_P(pv_param_arr), &num_params, ZEND_NUM_ARGS());
if (UNEXPECTED(!params)) {
RETURN_THROWS();
}
Expand Down
55 changes: 55 additions & 0 deletions ext/pgsql/tests/pg_null_bytes_params_escape.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
pg_query_params(), pg_execute() and the escape functions must reject null bytes
--EXTENSIONS--
pgsql
--SKIPIF--
<?php include("inc/skipif.inc"); ?>
--FILE--
<?php
include('inc/config.inc');

$db = pg_connect($conn_str);
$value = "hello\0world";
$sql = 'SELECT CAST($1 AS text), CAST($2 AS text), CAST($3 AS text)';
$params = ['ok', null, $value];

pg_prepare($db, 'null_bytes_stmt', $sql);

$calls = [
'pg_query_params' => fn() => pg_query_params($db, $sql, $params),
'pg_execute' => fn() => pg_execute($db, 'null_bytes_stmt', $params),
'pg_send_query_params' => fn() => pg_send_query_params($db, $sql, $params),
'pg_send_execute' => fn() => pg_send_execute($db, 'null_bytes_stmt', $params),
'pg_escape_string' => fn() => pg_escape_string($db, $value),
'pg_escape_literal' => fn() => pg_escape_literal($db, $value),
'pg_escape_identifier' => fn() => pg_escape_identifier($db, $value),
];

foreach ($calls as $call) {
try {
var_dump($call());
} catch (ValueError $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}

var_dump(pg_escape_bytea($db, $value));
var_dump(pg_fetch_row(pg_query_params($db, $sql, ['a', null, 'c'])));
?>
--EXPECT--
ValueError: pg_query_params(): Argument #3 ($params) must not contain strings with any null bytes
ValueError: pg_execute(): Argument #3 ($params) must not contain strings with any null bytes
ValueError: pg_send_query_params(): Argument #3 ($params) must not contain strings with any null bytes
ValueError: pg_send_execute(): Argument #3 ($params) must not contain strings with any null bytes
ValueError: pg_escape_string(): Argument #2 ($string) must not contain any null bytes
ValueError: pg_escape_literal(): Argument #2 ($string) must not contain any null bytes
ValueError: pg_escape_identifier(): Argument #2 ($string) must not contain any null bytes
string(24) "\x68656c6c6f00776f726c64"
array(3) {
[0]=>
string(1) "a"
[1]=>
NULL
[2]=>
string(1) "c"
}
Loading