From 662668f7559840bb10362c0b089473bb9aa910d0 Mon Sep 17 00:00:00 2001 From: lazerg Date: Sat, 28 Nov 2026 16:39:14 +0500 Subject: [PATCH 1/2] Fix GH-25869: UrlBuilder::setHost() accepts forbidden host code points --- .../builder/host_error_forbidden_code_point.phpt | 16 ++++++++++++++++ .../whatwg/builder/host_error_null_byte.phpt | 16 ++++++++++++++++ ext/uri/uri_parser_whatwg.c | 4 +++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 ext/uri/tests/whatwg/builder/host_error_forbidden_code_point.phpt create mode 100644 ext/uri/tests/whatwg/builder/host_error_null_byte.phpt diff --git a/ext/uri/tests/whatwg/builder/host_error_forbidden_code_point.phpt b/ext/uri/tests/whatwg/builder/host_error_forbidden_code_point.phpt new file mode 100644 index 000000000000..14b1ce598d08 --- /dev/null +++ b/ext/uri/tests/whatwg/builder/host_error_forbidden_code_point.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test Uri\WhatWg\UrlBuilder::setHost() - error - contains a forbidden host code point +--FILE-- +setHost("exa mple.com"); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +Uri\WhatWg\InvalidUrlException: The specified host is malformed (HostInvalidCodePoint) diff --git a/ext/uri/tests/whatwg/builder/host_error_null_byte.phpt b/ext/uri/tests/whatwg/builder/host_error_null_byte.phpt new file mode 100644 index 000000000000..6db12b321959 --- /dev/null +++ b/ext/uri/tests/whatwg/builder/host_error_null_byte.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test Uri\WhatWg\UrlBuilder::setHost() - error - contains a null byte +--FILE-- +setHost("\0bébé.be"); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +Uri\WhatWg\InvalidUrlException: The specified host is malformed (HostInvalidCodePoint) diff --git a/ext/uri/uri_parser_whatwg.c b/ext/uri/uri_parser_whatwg.c index 2a4457e3dc9c..ef2486bb9cf5 100644 --- a/ext/uri/uri_parser_whatwg.c +++ b/ext/uri/uri_parser_whatwg.c @@ -894,10 +894,12 @@ ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_whatwg_host_validate(const zen /* Validate the entire host before the hostname setter can stop at a URL delimiter. * Backslash is a delimiter only for special URLs, but is also forbidden in opaque hosts. + * The other forbidden host code points are rejected by every host parser. * https://url.spec.whatwg.org/#hostname-state * https://url.spec.whatwg.org/#opaque-host-parser */ for (const char *p = first; p < last; p++) { - if (*p == '/' || *p == '?' || *p == '#' || *p == '\\') { + if (*p == '/' || *p == '?' || *p == '#' || *p == '\\' + || *p == '\0' || *p == ' ' || *p == '<' || *p == '>' || *p == '@' || *p == '^' || *p == '|') { return php_uri_parser_whatwg_component_error("host", LXB_URL_ERROR_TYPE_HOST_INVALID_CODE_POINT); } } From d8ddf162d6676347671ec3622008fa042e4dfa7d Mon Sep 17 00:00:00 2001 From: lazerg Date: Sat, 28 Nov 2026 16:43:34 +0500 Subject: [PATCH 2/2] Reject port delimiter and stray brackets in UrlBuilder::setHost() --- .../builder/host_error_port_delimiter.phpt | 16 ++++++++++++++++ ext/uri/uri_parser_whatwg.c | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 ext/uri/tests/whatwg/builder/host_error_port_delimiter.phpt diff --git a/ext/uri/tests/whatwg/builder/host_error_port_delimiter.phpt b/ext/uri/tests/whatwg/builder/host_error_port_delimiter.phpt new file mode 100644 index 000000000000..97d357829695 --- /dev/null +++ b/ext/uri/tests/whatwg/builder/host_error_port_delimiter.phpt @@ -0,0 +1,16 @@ +--TEST-- +Test Uri\WhatWg\UrlBuilder::setHost() - error - contains a port delimiter +--FILE-- +setHost("example.com:8080"); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), "\n"; +} + +?> +--EXPECT-- +Uri\WhatWg\InvalidUrlException: The specified host is malformed (HostInvalidCodePoint) diff --git a/ext/uri/uri_parser_whatwg.c b/ext/uri/uri_parser_whatwg.c index ef2486bb9cf5..493cd9d8f8b7 100644 --- a/ext/uri/uri_parser_whatwg.c +++ b/ext/uri/uri_parser_whatwg.c @@ -899,7 +899,8 @@ ZEND_ATTRIBUTE_NONNULL zend_result php_uri_parser_whatwg_host_validate(const zen * https://url.spec.whatwg.org/#opaque-host-parser */ for (const char *p = first; p < last; p++) { if (*p == '/' || *p == '?' || *p == '#' || *p == '\\' - || *p == '\0' || *p == ' ' || *p == '<' || *p == '>' || *p == '@' || *p == '^' || *p == '|') { + || *p == '\0' || *p == ' ' || *p == '<' || *p == '>' || *p == '@' || *p == '^' || *p == '|' + || (*first != '[' && (*p == ':' || *p == '[' || *p == ']'))) { return php_uri_parser_whatwg_component_error("host", LXB_URL_ERROR_TYPE_HOST_INVALID_CODE_POINT); } }