Commit b3dd3a9200f for php
commit b3dd3a9200f6354d769ee0505a257f2530953663
Author: Tim Düsterhus <tim@tideways-gmbh.com>
Date: Sat Nov 7 12:49:40 2026 +0100
Fix GHSA-mg3p-mxj9-g2mm: handle uriparser failures in php_uri_parser_rfc3986_to_string()
Co-authored-by: Alexandre Daubois <alex.daubois@gmail.com>
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 00000000000..57e9e9947d4
--- /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 00000000000..60755a914df
--- /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 00000000000..199515635b1
--- /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 7fd2102b28a..faf3d3acdbd 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;
}