[php-src] PHP-8.5: Fix GHSA-mg3p-mxj9-g2mm: handle uriparser failures in php_uri_parser_rfc3986_to_string()

From: Date: Thu, 19 Nov 2026 15:27:35 +0000
Subject: [php-src] PHP-8.5: Fix GHSA-mg3p-mxj9-g2mm: handle uriparser failures in php_uri_parser_rfc3986_to_string()
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143922@lists.php.net to get a copy of this message
Author: Tim Düsterhus (TimWolla) Committer: Jakub Zelenka (bukka) Date: 2026-11-19T16:10:52+01:00 Commit: https://github.com/php/php-src/commit/b3dd3a9200f6354d769ee0505a257f2530953663 Raw diff: https://github.com/php/php-src/commit/b3dd3a9200f6354d769ee0505a257f2530953663.diff Fix GHSA-mg3p-mxj9-g2mm: handle uriparser failures in php_uri_parser_rfc3986_to_string() Co-authored-by: Alexandre Daubois <alex.daubois@gmail.com> Changed paths: A ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt A ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt A ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt M ext/uri/uri_parser_rfc3986.c Diff: diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt new file mode 100644 index 000000000000..57e9e9947d40 --- /dev/null +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-1.phpt @@ -0,0 +1,35 @@ +--TEST-- +GHSA-mg3p-mxj9-g2mm (Uri\Rfc3986\Uri recomposed to exactly INT_MAX characters) +--SKIPIF-- +<?php +if (!getenv('RUN_RESOURCE_HEAVY_TESTS')) die('skip resource-heavy test'); +if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); +if (PHP_INT_SIZE !== 8) die("skip this test is for 64bit platforms only"); +$memInfo = @file_get_contents('/proc/meminfo'); +if ($memInfo && preg_match('/MemAvailable:\s+(\d+) kB/', $memInfo, $m) && $m[1] < 5 * 1024 * 1024) { + die('skip Insufficient available memory (less than 5 GiB)'); +} +?> +--CONFLICTS-- +all +--INI-- +memory_limit=-1 +--FILE-- +<?php +$uri = new Uri\Rfc3986\Uri("a://b/" . str_repeat("x", 2147483647 - 6)); + +try { + $uri->toRawString(); +} catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; +} + +try { + $uri->toString(); +} catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; +} +?> +--EXPECT-- +Uri\UriError: Cannot recompose Uri\Rfc3986\Uri to a string +Uri\UriError: Cannot recompose Uri\Rfc3986\Uri to a string diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt new file mode 100644 index 000000000000..60755a914dfb --- /dev/null +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-2.phpt @@ -0,0 +1,35 @@ +--TEST-- +GHSA-mg3p-mxj9-g2mm (Uri\Rfc3986\Uri recomposed to more than INT_MAX characters) +--SKIPIF-- +<?php +if (!getenv('RUN_RESOURCE_HEAVY_TESTS')) die('skip resource-heavy test'); +if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); +if (PHP_INT_SIZE !== 8) die("skip this test is for 64bit platforms only"); +$memInfo = @file_get_contents('/proc/meminfo'); +if ($memInfo && preg_match('/MemAvailable:\s+(\d+) kB/', $memInfo, $m) && $m[1] < 5 * 1024 * 1024) { + die('skip Insufficient available memory (less than 5 GiB)'); +} +?> +--CONFLICTS-- +all +--INI-- +memory_limit=-1 +--FILE-- +<?php +$uri = new Uri\Rfc3986\Uri("a://b/" . str_repeat("x", 2147483648 - 6)); + +try { + $uri->toRawString(); +} catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; +} + +try { + $uri->toString(); +} catch (Throwable $e) { + echo $e::class, ": ", $e->getMessage(), "\n"; +} +?> +--EXPECT-- +Uri\UriError: Cannot recompose Uri\Rfc3986\Uri to a string +Uri\UriError: Cannot recompose Uri\Rfc3986\Uri to a string diff --git a/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt new file mode 100644 index 000000000000..199515635b11 --- /dev/null +++ b/ext/uri/tests/rfc3986/general/GHSA-mg3p-mxj9-g2mm-3.phpt @@ -0,0 +1,29 @@ +--TEST-- +GHSA-mg3p-mxj9-g2mm (Uri\Rfc3986\Uri recomposed to INT_MAX - 1 characters) +--SKIPIF-- +<?php +if (!getenv('RUN_RESOURCE_HEAVY_TESTS')) die('skip resource-heavy test'); +if (getenv("SKIP_SLOW_TESTS")) die("skip slow test"); +if (PHP_INT_SIZE !== 8) die("skip this test is for 64bit platforms only"); +$memInfo = @file_get_contents('/proc/meminfo'); +if ($memInfo && preg_match('/MemAvailable:\s+(\d+) kB/', $memInfo, $m) && $m[1] < 5 * 1024 * 1024) { + die('skip Insufficient available memory (less than 5 GiB)'); +} +?> +--CONFLICTS-- +all +--INI-- +memory_limit=-1 +--FILE-- +<?php +$uri = new Uri\Rfc3986\Uri("a://b/" . str_repeat("x", 2147483646 - 6)); + +$string = $uri->toRawString(); +var_dump(strlen($string)); +var_dump(substr($string, 0, 8)); +var_dump(strspn($string, "x", 6) === 2147483646 - 6); +?> +--EXPECT-- +int(2147483646) +string(8) "a://b/xx" +bool(true) diff --git a/ext/uri/uri_parser_rfc3986.c b/ext/uri/uri_parser_rfc3986.c index 7fd2102b28a3..faf3d3acdbda 100644 --- a/ext/uri/uri_parser_rfc3986.c +++ b/ext/uri/uri_parser_rfc3986.c @@ -664,6 +664,8 @@ ZEND_ATTRIBUTE_NONNULL static void *php_uri_parser_rfc3986_clone(void *uri) ZEND_ATTRIBUTE_NONNULL static zend_string *php_uri_parser_rfc3986_to_string(void *uri, php_uri_recomposition_mode recomposition_mode, bool exclude_fragment) { + zend_string *uri_string = NULL; + php_uri_parser_rfc3986_uris *uriparser_uris = uri; const UriUriA *uriparser_uri; @@ -672,19 +674,30 @@ ZEND_ATTRIBUTE_NONNULL static zend_string *php_uri_parser_rfc3986_to_string(void } else { uriparser_uri = get_normalized_uri(uriparser_uris); if (UNEXPECTED(uriparser_uri == NULL)) { - return NULL; + goto fail; } } - int charsRequired = 0; - int result = uriToStringCharsRequiredA(uriparser_uri, &charsRequired); - ZEND_ASSERT(result == URI_SUCCESS); + int output_length = 0; + + /* uriToStringCharsRequiredA() returns the number of characters WITHOUT the trailing NUL. */ + int result = uriToStringCharsRequiredA(uriparser_uri, &output_length); + if (UNEXPECTED(result != URI_SUCCESS)) { + goto fail; + } + if (UNEXPECTED(output_length == INT_MAX)) { + goto fail; + } - charsRequired++; + /* uriToStringA() expects the size of the allocation in maxChars (i.e. WITH the trailing NUL). */ + int allocation_size = output_length + 1; - zend_string *uri_string = zend_string_alloc(charsRequired - 1, false); - result = uriToStringA(ZSTR_VAL(uri_string), uriparser_uri, charsRequired, NULL); - ZEND_ASSERT(result == URI_SUCCESS); + uri_string = zend_string_alloc(output_length, false); + result = uriToStringA(ZSTR_VAL(uri_string), uriparser_uri, /* maxChars */ allocation_size, /* charsWritten */ NULL); + if (UNEXPECTED(result != URI_SUCCESS)) { + /* This should be unreachable in practice. */ + goto fail; + } if (exclude_fragment) { const char *pos = zend_memrchr(ZSTR_VAL(uri_string), '#', ZSTR_LEN(uri_string)); @@ -694,6 +707,15 @@ ZEND_ATTRIBUTE_NONNULL static zend_string *php_uri_parser_rfc3986_to_string(void } } + return uri_string; + + fail: + + if (uri_string != NULL) { + zend_string_free(uri_string); + uri_string = NULL; + } + return uri_string; }

« previous php.cvs (#143922) next »