Skip to content

[RFC] Add RFC 3986 and WHATWG compliant URL parsing support #14461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
196957b
Create separate lexbor extension
kocsismate May 11, 2025
0334e22
Add RFC 3986 and WHATWG compliant URL parsing support
kocsismate Jun 3, 2024
a09a457
Serialization
kocsismate Oct 22, 2024
3abdb0a
Improve error handling
kocsismate Oct 26, 2024
1445e55
Lot of fixes and added support for equalsTo()
kocsismate Nov 11, 2024
e4de160
Add normalization support
kocsismate Nov 13, 2024
77184ce
SOAP test fixes
kocsismate Nov 13, 2024
3b0449d
Fix some memory leaks
kocsismate Nov 13, 2024
5f90823
Some cleanups
kocsismate Nov 18, 2024
b7011b8
Changes based on discussion
kocsismate Nov 30, 2024
f20842a
Removal of Uri\Uri
kocsismate Dec 30, 2024
c88e92b
A lot of fixes and API changes
kocsismate Jan 6, 2025
c743ead
Updates
kocsismate Feb 5, 2025
19b7180
Add new tests, path fixes
kocsismate Feb 9, 2025
fb0c929
Add more tests for verifying the behavior of withers
kocsismate Feb 15, 2025
cf7ca4e
Fix code review comments
kocsismate Feb 19, 2025
9de1271
A few fixes and improvements after feedback
kocsismate Apr 14, 2025
db3b79d
Test fixes
kocsismate Apr 14, 2025
feefccf
Remove WHATWG non-raw getters
kocsismate Apr 18, 2025
190bbfd
Rename WHATWG getters again
kocsismate Apr 26, 2025
d1df694
Add UriComparisonMode
kocsismate Apr 28, 2025
0d1fc02
Expose $softErrors for Uri\WhatWg\Url::resolve()
kocsismate Apr 30, 2025
df1d0b9
Add SensitiveParameter support
kocsismate May 3, 2025
e8261f7
Proper build support
kocsismate May 19, 2025
7530dd9
Review fixes and serialization update according to the RFC
kocsismate May 20, 2025
b75cc5b
Some extension fixes
kocsismate May 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updates
  • Loading branch information
kocsismate committed May 25, 2025
commit c743ead51fe65e79c8d8d3271dd0ee635843e622
1 change: 1 addition & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ EMPTY_SWITCH_DEFAULT_CASE()
_(ZEND_STR_SCHEME, "scheme") \
_(ZEND_STR_HOST, "host") \
_(ZEND_STR_PORT, "port") \
_(ZEND_STR_USERINFO, "userinfo") \
_(ZEND_STR_USER, "user") \
_(ZEND_STR_PASS, "pass") \
_(ZEND_STR_PASSWORD, "password") \
Expand Down
109 changes: 22 additions & 87 deletions ext/filter/logical_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ static bool php_filter_is_valid_ipv6_hostname(const zend_string *s)

void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
{
zend_result result;
size_t old_len = Z_STRLEN_P(value);

php_filter_url(http://github.com/value,%20flags,%20option_array,%20charset);
Expand All @@ -613,41 +612,24 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
RETURN_VALIDATION_FAILED
}

/* Use parse_url - if it returns false, we return NULL */
uri_internal_t *internal_uri = php_uri_parse(uri_handler, Z_STR_P(value), NULL);
if (internal_uri == NULL) {
/* Parse the URI - if it fails, we return NULL */
php_uri *uri = php_uri_parse_to_struct(uri_handler, Z_STR_P(value), URI_COMPONENT_READ_NORMALIZED_MACHINE_FRIENDLY, NULL);
if (uri == NULL) {
RETURN_VALIDATION_FAILED
}

zval scheme;
result = php_uri_get_scheme(internal_uri, URI_COMPONENT_READ_RAW, &scheme);
if (result == FAILURE) {
php_uri_free(internal_uri);
RETURN_VALIDATION_FAILED
}

zval host;
result = php_uri_get_host(internal_uri, URI_COMPONENT_READ_RAW, &host);
if (result == FAILURE) {
zval_ptr_dtor(&scheme);
php_uri_free(internal_uri);
RETURN_VALIDATION_FAILED
}

if (Z_TYPE(scheme) == IS_STRING &&
(zend_string_equals_literal_ci(Z_STR(scheme), "http") || zend_string_equals_literal_ci(Z_STR(scheme), "https"))) {
if (uri->scheme != NULL &&
(zend_string_equals_literal_ci(uri->scheme, "http") || zend_string_equals_literal_ci(uri->scheme, "https"))) {
const char *s;
size_t l;

if (Z_TYPE(host) != IS_STRING) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
zval_ptr_dtor(&host);
if (uri->host == NULL) {
php_uri_struct_free(uri);
RETURN_VALIDATION_FAILED
}

s = Z_STRVAL(host);
l = Z_STRLEN(host);
s = ZSTR_VAL(uri->host);
l = ZSTR_LEN(uri->host);

if (
/* @todo Find a better solution than hardcoding the uri handler name. Skipping these checks is needed because
Expand All @@ -659,80 +641,33 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
!php_filter_is_valid_ipv6_hostname(url->host) &&
/* Validate domain.
* This includes a loose check for an IPv4 address. */
!_php_filter_validate_domain(Z_STRVAL(host), l, FILTER_FLAG_HOSTNAME)
!_php_filter_validate_domain(ZSTR_VAL(uri->host), l, FILTER_FLAG_HOSTNAME)
) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
zval_ptr_dtor(&host);
php_uri_struct_free(uri);
RETURN_VALIDATION_FAILED
}
}

zval path;
result = php_uri_get_path(internal_uri, URI_COMPONENT_READ_RAW, &path);
if (result == FAILURE) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
zval_ptr_dtor(&host);
RETURN_VALIDATION_FAILED
}

zval query;
result = php_uri_get_query(internal_uri, URI_COMPONENT_READ_RAW, &query);
if (result == FAILURE) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
zval_ptr_dtor(&host);
zval_ptr_dtor(&path);
RETURN_VALIDATION_FAILED
}

if (
Z_TYPE(scheme) == IS_NULL ||
if (uri->scheme == NULL ||
/* some schemes allow the host to be empty */
(Z_TYPE(host) == IS_NULL && (!zend_string_equals_literal(Z_STR(scheme), "mailto") && !zend_string_equals_literal(Z_STR(scheme), "news") && !zend_string_equals_literal(Z_STR(scheme), "file"))) ||
((flags & FILTER_FLAG_PATH_REQUIRED) && Z_TYPE(path) == IS_NULL) || ((flags & FILTER_FLAG_QUERY_REQUIRED) && Z_TYPE(query) == IS_NULL)
(uri->host == NULL && (!zend_string_equals_literal(uri->scheme, "mailto") && !zend_string_equals_literal(uri->scheme, "news") && !zend_string_equals_literal(uri->scheme, "file"))) ||
((flags & FILTER_FLAG_PATH_REQUIRED) && uri->path == NULL) || ((flags & FILTER_FLAG_QUERY_REQUIRED) && uri->query == NULL)
) {
php_uri_free(internal_uri);
zval_ptr_dtor(&scheme);
zval_ptr_dtor(&host);
zval_ptr_dtor(&path);
zval_ptr_dtor(&query);
RETURN_VALIDATION_FAILED
}

zval_ptr_dtor(&scheme);
zval_ptr_dtor(&host);
zval_ptr_dtor(&path);
zval_ptr_dtor(&query);

zval user;
result = php_uri_get_user(internal_uri, URI_COMPONENT_READ_RAW, &user);
if (result == FAILURE) {
php_uri_free(internal_uri);
RETURN_VALIDATION_FAILED
}

zval password;
result = php_uri_get_password(internal_uri, URI_COMPONENT_READ_RAW, &password);
if (result == FAILURE) {
php_uri_free(internal_uri);
zval_ptr_dtor(&user);
php_uri_struct_free(uri);
RETURN_VALIDATION_FAILED
}

if ((Z_TYPE(user) != IS_NULL && !is_userinfo_valid(Z_STR(user)))
|| (Z_TYPE(password) != IS_NULL && !is_userinfo_valid(Z_STR(password)))
if (strcmp(uri_handler->name, "parse_url") == 0 &&
(
(uri->user != NULL && !is_userinfo_valid(uri->user)) ||
(uri->password != NULL && !is_userinfo_valid(uri->password))
)
) {
php_uri_free(internal_uri);
zval_ptr_dtor(&user);
zval_ptr_dtor(&password);
php_uri_struct_free(uri);
RETURN_VALIDATION_FAILED
}

php_uri_free(internal_uri);
zval_ptr_dtor(&user);
zval_ptr_dtor(&password);
php_uri_struct_free(uri);
}
/* }}} */

Expand Down
4 changes: 2 additions & 2 deletions ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ int make_http_soap_request(zval *this_ptr,
zend_argument_value_error(6, "must be a valid URI parser name");
return FALSE;
}
uri = php_uri_parse_to_struct(uri_handler, location, NULL);
uri = php_uri_parse_to_struct(uri_handler, location, URI_COMPONENT_READ_NORMALIZED_MACHINE_FRIENDLY, NULL);
}

tmp = Z_CLIENT_STREAM_CONTEXT_P(this_ptr);
Expand Down Expand Up @@ -1161,7 +1161,7 @@ int make_http_soap_request(zval *this_ptr,
}

zend_string *loc_str = zend_string_init(loc, strlen(loc), false);
php_uri *new_uri = php_uri_parse_to_struct(uri_handler, loc_str, NULL);
php_uri *new_uri = php_uri_parse_to_struct(uri_handler, loc_str, URI_COMPONENT_READ_NORMALIZED_MACHINE_FRIENDLY, NULL);
zend_string_release(loc_str);

if (new_uri != NULL) {
Expand Down
33 changes: 33 additions & 0 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,93 +114,123 @@ static void fill_errors(zval *errors)
zend_update_property_string(whatwg_error_ce, Z_OBJ(error), "context", sizeof("context") - 1, (const char *) lxb_error->data);

zend_string *error_str;
zval failure;
switch (lxb_error->id) {
case LXB_URL_ERROR_TYPE_DOMAIN_TO_ASCII:
error_str = zend_string_init("DomainToAscii", sizeof("DomainToAscii"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_DOMAIN_TO_UNICODE:
error_str = zend_string_init("DomainToUnicode", sizeof("DomainToUnicode"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_DOMAIN_INVALID_CODE_POINT:
error_str = zend_string_init("DomainInvalidCodePoint", sizeof("DomainInvalidCodePoint"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_HOST_INVALID_CODE_POINT:
error_str = zend_string_init("HostInvalidCodePoint", sizeof("HostInvalidCodePoint"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_EMPTY_PART:
error_str = zend_string_init("Ipv4EmptyPart", sizeof("Ipv4EmptyPart"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_TOO_MANY_PARTS:
error_str = zend_string_init("Ipv4TooManyParts", sizeof("Ipv4TooManyParts"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_NON_NUMERIC_PART:
error_str = zend_string_init("Ipv4NonNumericPart", sizeof("Ipv4NonNumericPart"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_NON_DECIMAL_PART:
error_str = zend_string_init("Ipv4NonDecimalPart", sizeof("Ipv4NonDecimalPart"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_OUT_OF_RANGE_PART:
error_str = zend_string_init("Ipv4OutOfRangePart", sizeof("Ipv4OutOfRangePart"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV6_UNCLOSED:
error_str = zend_string_init("Ipv6Unclosed", sizeof("Ipv6Unclosed"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV6_INVALID_COMPRESSION:
error_str = zend_string_init("Ipv6InvalidCompression", sizeof("Ipv6InvalidCompression"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV6_TOO_MANY_PIECES:
error_str = zend_string_init("Ipv6TooManyPieces", sizeof("Ipv6TooManyPieces"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV6_MULTIPLE_COMPRESSION:
error_str = zend_string_init("Ipv6MultipleCompression", sizeof("Ipv6MultipleCompression"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV6_INVALID_CODE_POINT:
error_str = zend_string_init("Ipv6InvalidCodePoint", sizeof("Ipv6InvalidCodePoint"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV6_TOO_FEW_PIECES:
error_str = zend_string_init("Ipv6TooFewPieces", sizeof("Ipv6TooFewPieces"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_TOO_MANY_PIECES:
error_str = zend_string_init("Ipv4InIpv6TooManyPieces", sizeof("Ipv4InIpv6TooManyPieces"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_INVALID_CODE_POINT:
error_str = zend_string_init("Ipv4InIpv6InvalidCodePoint", sizeof("Ipv4InIpv6InvalidCodePoint"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_OUT_OF_RANGE_PART:
error_str = zend_string_init("Ipv4InIpv6OutOfRangePart", sizeof("Ipv4InIpv6OutOfRangePart"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_IPV4_IN_IPV6_TOO_FEW_PARTS:
error_str = zend_string_init("Ipv4InIpv6TooFewParts", sizeof("Ipv4InIpv6TooFewParts"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_INVALID_URL_UNIT:
error_str = zend_string_init("InvalidUrlUnit", sizeof("InvalidUrlUnit"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_SPECIAL_SCHEME_MISSING_FOLLOWING_SOLIDUS:
error_str = zend_string_init("SpecialSchemeMissingFollowingSolidus", sizeof("SpecialSchemeMissingFollowingSolidus"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_MISSING_SCHEME_NON_RELATIVE_URL:
error_str = zend_string_init("MissingSchemeNonRelativeUrl", sizeof("MissingSchemeNonRelativeUrl"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_INVALID_REVERSE_SOLIDUS:
error_str = zend_string_init("InvalidReverseSoldius", sizeof("InvalidReverseSoldius"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_INVALID_CREDENTIALS:
error_str = zend_string_init("InvalidCredentials", sizeof("InvalidCredentials"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_HOST_MISSING:
error_str = zend_string_init("HostMissing", sizeof("HostMissing"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_PORT_OUT_OF_RANGE:
error_str = zend_string_init("PortOfOfRange", sizeof("PortOfOfRange"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_PORT_INVALID:
error_str = zend_string_init("PortInvalid", sizeof("PortInvalid"), false);
ZVAL_TRUE(&failure);
break;
case LXB_URL_ERROR_TYPE_FILE_INVALID_WINDOWS_DRIVE_LETTER:
error_str = zend_string_init("FileInvalidWindowsDriveLetter", sizeof("FileInvalidWindowsDriveLetter"), false);
ZVAL_FALSE(&failure);
break;
case LXB_URL_ERROR_TYPE_FILE_INVALID_WINDOWS_DRIVE_LETTER_HOST:
error_str = zend_string_init("FileInvalidWindowsDriveLetterHost", sizeof("FileInvalidWindowsDriveLetterHost"), false);
ZVAL_FALSE(&failure);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
Expand All @@ -211,6 +241,9 @@ static void fill_errors(zval *errors)
zend_string_release(error_str);
zval_ptr_dtor(&error_type);

zend_update_property(whatwg_error_ce, Z_OBJ(error), "failure", sizeof("failure") - 1, &failure);
zval_ptr_dtor(&failure);

add_next_index_zval(errors, &error);
}

Expand Down
Loading