Skip to content

Commit b82d797

Browse files
committed
Fix GH-19780: InvalidUrlException should check $errors argument
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.
1 parent ded813b commit b82d797

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

ext/uri/php_uri.c

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

399+
static bool is_list_of_whatwg_validation_errors(const HashTable *array)
400+
{
401+
if (!zend_array_is_list(array)) {
402+
return false;
403+
}
404+
405+
ZEND_HASH_FOREACH_VAL(array, zval *val) {
406+
/* Do not allow references as they may change types after checking. */
407+
408+
if (Z_TYPE_P(val) != IS_OBJECT) {
409+
return false;
410+
}
411+
412+
if (!instanceof_function(Z_OBJCE_P(val), uri_whatwg_url_validation_error_ce)) {
413+
return false;
414+
}
415+
} ZEND_HASH_FOREACH_END();
416+
417+
return true;
418+
}
419+
399420
PHP_METHOD(Uri_Rfc3986_Uri, parse)
400421
{
401422
create_rfc3986_uri(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
@@ -430,6 +451,11 @@ PHP_METHOD(Uri_WhatWg_InvalidUrlException, __construct)
430451
ZVAL_EMPTY_ARRAY(&tmp);
431452
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), &tmp);
432453
} else {
454+
if (!is_list_of_whatwg_validation_errors(Z_ARR_P(errors))) {
455+
zend_argument_value_error(2, "must be a list of %s", ZSTR_VAL(uri_whatwg_url_validation_error_ce->name));
456+
RETURN_THROWS();
457+
}
458+
433459
zend_update_property(uri_whatwg_invalid_url_exception_ce, Z_OBJ_P(ZEND_THIS), ZEND_STRL("errors"), errors);
434460
}
435461
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)