Skip to content

Commit 834e92a

Browse files
authored
Fix GH-19780: InvalidUrlException should check $errors argument (#19781)
It makes sense to restrict the types used for $errors. This can also improve the types for static analysis tools as they can now rely on the array being a list of this class type. Closes GH-19781.
1 parent 03d984e commit 834e92a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ PHP NEWS
66
. Fixed bug GH-19765 (object_properties_load() bypasses readonly property
77
checks). (timwolla)
88

9+
- URI:
10+
. Fixed bug GH-19780 (InvalidUrlException should check $errors argument).
11+
(nielsdos)
12+
913
11 Sep 2025, PHP 8.5.0beta3
1014

1115
- Core:

ext/uri/php_uri.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,27 @@ static void create_rfc3986_uri(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor
391391
php_uri_instantiate_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, uri_str, base_url_object, is_constructor, is_constructor, NULL);
392392
}
393393

394+
static bool is_list_of_whatwg_validation_errors(const HashTable *array)
395+
{
396+
if (!zend_array_is_list(array)) {
397+
return false;
398+
}
399+
400+
ZEND_HASH_FOREACH_VAL(array, zval *val) {
401+
/* Do not allow references as they may change types after checking. */
402+
403+
if (Z_TYPE_P(val) != IS_OBJECT) {
404+
return false;
405+
}
406+
407+
if (!instanceof_function(Z_OBJCE_P(val), uri_whatwg_url_validation_error_ce)) {
408+
return false;
409+
}
410+
} ZEND_HASH_FOREACH_END();
411+
412+
return true;
413+
}
414+
394415
PHP_METHOD(Uri_Rfc3986_Uri, parse)
395416
{
396417
create_rfc3986_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
@@ -425,6 +446,11 @@ PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct)
425446
ZVAL_EMPTY_ARRAY(&tmp);
426447
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), &tmp);
427448
} else {
449+
if (!is_list_of_whatwg_validation_errors(Z_ARR_P(errors))) {
450+
zend_argument_value_error(2, "must be a list of %s", ZSTR_VAL(uri_whatwg_url_validation_error_ce->name));
451+
RETURN_THROWS();
452+
}
453+
428454
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), errors);
429455
}
430456
if (EG(exception)) {

ext/uri/tests/gh19780.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GH-19780 (InvalidUrlException should check $errors argument)
3+
--EXTENSIONS--
4+
uri
5+
--FILE--
6+
<?php
7+
8+
use Uri\WhatWg\InvalidUrlException;
9+
use Uri\WhatWg\UrlValidationError;
10+
use Uri\WhatWg\UrlValidationErrorType;
11+
12+
try {
13+
new InvalidUrlException('message', ['foo']);
14+
} catch (ValueError $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
18+
try {
19+
new InvalidUrlException('message', [
20+
1 => new UrlValidationError('context', UrlValidationErrorType::HostMissing, true)
21+
]);
22+
} catch (ValueError $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
26+
?>
27+
--EXPECT--
28+
Uri\WhatWg\InvalidUrlException::__construct(): Argument #2 ($errors) must be a list of Uri\WhatWg\UrlValidationError
29+
Uri\WhatWg\InvalidUrlException::__construct(): Argument #2 ($errors) must be a list of Uri\WhatWg\UrlValidationError

0 commit comments

Comments
 (0)