[php-src] PHP-8.6: Merge branch 'PHP-8.5' into PHP-8.6

From: Date: Thu, 29 Oct 2026 15:09:17 +0000
Subject: [php-src] PHP-8.6: Merge branch 'PHP-8.5' into PHP-8.6
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-142946@lists.php.net to get a copy of this message
Author: Jorg Sowa (jorgsowa) Date: 2026-10-29T16:04:52+01:00 Commit: https://github.com/php/php-src/commit/1c92d577570e78ee3e3bd5b0ff443264862a3971 Raw diff: https://github.com/php/php-src/commit/1c92d577570e78ee3e3bd5b0ff443264862a3971.diff Merge branch 'PHP-8.5' into PHP-8.6 Changed paths: A ext/session/tests/session_id_nul_byte.phpt M NEWS M ext/session/mod_files.c M ext/session/php_session.h M ext/session/session.c Diff: diff --git a/NEWS b/NEWS index 0b39499544ca..e41fb4e5c697 100644 --- a/NEWS +++ b/NEWS @@ -119,6 +119,10 @@ PHP NEWS . Fixed infinite loop in sapi_header_op() for headers of 4 GiB or more. (Martin Mirchev) +- Session: + . Validate whole session ID instead of stopping at the first NUL byte. + (Sjoerd Langkemper) + - Sodium: . Pass sodium_crypto_xof_*() state via objects instead of by-ref strings. (timwolla) diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c index 03a2f4a2fcbf..9b5b8f331dc7 100644 --- a/ext/session/mod_files.c +++ b/ext/session/mod_files.c @@ -162,7 +162,7 @@ static void ps_files_open(ps_files *data, /* const */ zend_string *key) ps_files_close(data); - if (php_session_valid_key(ZSTR_VAL(key)) == FAILURE) { + if (php_session_valid_key_ex(key) == FAILURE) { php_error_docref(NULL, E_WARNING, "Session ID is too long or contains illegal characters. Only the A-Z, a-z, 0-9, \"-\", and \",\" characters are allowed"); return; } diff --git a/ext/session/php_session.h b/ext/session/php_session.h index e2b7a7e5a7d3..ee36eb291e52 100644 --- a/ext/session/php_session.h +++ b/ext/session/php_session.h @@ -252,6 +252,7 @@ PHPAPI const ps_module *_php_find_ps_module(const char *name); PHPAPI const ps_serializer *_php_find_ps_serializer(const char *name); PHPAPI zend_result php_session_valid_key(const char *key); +zend_result php_session_valid_key_ex(const zend_string *key); PHPAPI zend_result php_session_reset_id(void); /* Do not use a return statement in code because that may leak memory. diff --git a/ext/session/session.c b/ext/session/session.c index cca448b5a415..81c4455808bf 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -356,14 +356,18 @@ PHPAPI zend_string *php_session_create_id(PS_CREATE_SID_ARGS) /* Default session id char validation function allowed by ps_modules. * If you change the logic here, please also update the error message in - * ps_modules appropriately */ -PHPAPI zend_result php_session_valid_key(const char *key) + * ps_modules appropriately. */ +static zend_result php_session_valid_key_len(const char *key, size_t key_len) { - size_t key_len; - const char *p; - char c; + /* Somewhat arbitrary length limit here, but should be way more than + anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */ + if (key_len == 0 || key_len > PS_MAX_SID_LENGTH) { + return FAILURE; + } + + for (size_t i = 0; i < key_len; i++) { + unsigned char c = (unsigned char) key[i]; - for (p = key; (c = *p); p++) { /* valid characters are [a-z], [A-Z], [0-9], - (hyphen) and , (comma) */ if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') @@ -374,15 +378,20 @@ PHPAPI zend_result php_session_valid_key(const char *key) } } - key_len = p - key; + return SUCCESS; +} - /* Somewhat arbitrary length limit here, but should be way more than - anyone needs and avoids file-level warnings later on if we exceed MAX_PATH */ - if (key_len == 0 || key_len > PS_MAX_SID_LENGTH) { - return FAILURE; - } +/* Validate the full zend_string, including embedded NUL bytes. */ +zend_result php_session_valid_key_ex(const zend_string *key) +{ + return php_session_valid_key_len(ZSTR_VAL(key), ZSTR_LEN(key)); +} - return SUCCESS; +/* C-string compatibility wrapper. Callers that have a zend_string should use + * php_session_valid_key_ex() instead. */ +PHPAPI zend_result php_session_valid_key(const char *key) +{ + return php_session_valid_key_len(key, strlen(key)); } static zend_long php_session_gc(bool immediate) @@ -2515,7 +2524,7 @@ PHP_FUNCTION(session_create_id) zend_argument_value_error(1, "cannot be longer than %d characters", PS_MAX_SID_LENGTH); RETURN_THROWS(); } - if (php_session_valid_key(ZSTR_VAL(prefix)) == FAILURE) { + if (php_session_valid_key_ex(prefix) == FAILURE) { /* E_ERROR raised for security reason. */ php_error_docref(NULL, E_WARNING, "Prefix cannot contain special characters. Only the A-Z, a-z, 0-9, \"-\", and \",\" characters are allowed"); RETURN_FALSE; diff --git a/ext/session/tests/session_id_nul_byte.phpt b/ext/session/tests/session_id_nul_byte.phpt new file mode 100644 index 000000000000..287c28ae256c --- /dev/null +++ b/ext/session/tests/session_id_nul_byte.phpt @@ -0,0 +1,27 @@ +--TEST-- +Session IDs with NUL bytes are rejected as a whole, not truncated +--INI-- +session.save_handler=files +session.save_path= +session.use_cookies=0 +session.use_strict_mode=0 +--EXTENSIONS-- +session +--SKIPIF-- +<?php include 'skipif.inc'; ?> +--FILE-- +<?php +session_id("nulbug\0stillvalid"); +$started = session_start(); +var_dump($started); +var_dump(session_id()); +if ($started) { + session_destroy(); +} +?> +--EXPECTF-- +Warning: session_start(): Session ID is too long or contains illegal characters. Only the A-Z, a-z, 0-9, "-", and "," characters are allowed in %s on line %d + +Warning: session_start(): Failed to read session data: files (path: ) in %s on line %d +bool(false) +string(0) ""

« previous php.cvs (#142946) next »