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
A few fixes and improvements after feedback
  • Loading branch information
kocsismate committed May 25, 2025
commit 9de1271fd84ce0fa1566391a2a73270a900df45d
1 change: 1 addition & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ EMPTY_SWITCH_DEFAULT_CASE()
_(ZEND_STR_PORT, "port") \
_(ZEND_STR_USERINFO, "userinfo") \
_(ZEND_STR_USER, "user") \
_(ZEND_STR_USERNAME, "username") \
_(ZEND_STR_PASS, "pass") \
_(ZEND_STR_PASSWORD, "password") \
_(ZEND_STR_PATH, "path") \
Expand Down
9 changes: 2 additions & 7 deletions ext/filter/logical_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,28 +620,23 @@ void php_filter_validate_url(http://github.com/PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */

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 (uri->host == NULL) {
php_uri_struct_free(uri);
RETURN_VALIDATION_FAILED
}

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
* both uriparser and lexbor performs comprehensive validations. Also, the [] pair is removed by these
* libraries in case of ipv6 URIs, therefore php_filter_is_valid_ipv6_hostname() would case false positive
* failures. */
strcmp(uri_handler->name, "parse_url") == 0 &&
/* An IPv6 enclosed by square brackets is a valid hostname.*/
!php_filter_is_valid_ipv6_hostname(url->host) &&
!php_filter_is_valid_ipv6_hostname(uri->host) &&
/* Validate domain.
* This includes a loose check for an IPv4 address. */
!_php_filter_validate_domain(ZSTR_VAL(uri->host), l, FILTER_FLAG_HOSTNAME)
!php_filter_validate_domain_ex(uri->host, FILTER_FLAG_HOSTNAME)
) {
php_uri_struct_free(uri);
RETURN_VALIDATION_FAILED
Expand Down
18 changes: 16 additions & 2 deletions ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ static zend_result parse_url_read_scheme(const uri_internal_t *internal_uri, uri
return SUCCESS;
}

static zend_result parse_url_read_user(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
static zend_result parse_url_read_username(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
{
php_url *parse_url_uri = (php_url *) internal_uri->uri;

Expand Down Expand Up @@ -431,7 +431,7 @@ static zend_result parse_url_init_parser(void)
zend_hash_init(&parse_url_property_handlers, 8, NULL, NULL, true);

URI_REGISTER_PROPERTY_READ_HANDLER(&parse_url_property_handlers, ZSTR_KNOWN(ZEND_STR_SCHEME), parse_url_read_scheme);
URI_REGISTER_PROPERTY_READ_HANDLER(&parse_url_property_handlers, ZSTR_KNOWN(ZEND_STR_USER), parse_url_read_user);
URI_REGISTER_PROPERTY_READ_HANDLER(&parse_url_property_handlers, ZSTR_KNOWN(ZEND_STR_USER), parse_url_read_username);
URI_REGISTER_PROPERTY_READ_HANDLER(&parse_url_property_handlers, ZSTR_KNOWN(ZEND_STR_PASSWORD), parse_url_read_password);
URI_REGISTER_PROPERTY_READ_HANDLER(&parse_url_property_handlers, ZSTR_KNOWN(ZEND_STR_HOST), parse_url_read_host);
URI_REGISTER_PROPERTY_READ_HANDLER(&parse_url_property_handlers, ZSTR_KNOWN(ZEND_STR_PORT), parse_url_read_port);
Expand All @@ -456,6 +456,19 @@ static void *parse_url_parse_uri(const zend_string *uri_str, const zend_string *
return php_url_parse_ex2(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), &has_port);
}

static void parse_url_create_invalid_url_exception(zval *exception_zv, zval *errors)
{
object_init_ex(exception_zv, invalid_uri_exception_ce);

zend_update_property_string(
invalid_uri_exception_ce,
Z_OBJ_P(exception_zv),
ZSTR_VAL(ZSTR_KNOWN(ZEND_STR_MESSAGE)),
ZSTR_LEN(ZSTR_KNOWN(ZEND_STR_MESSAGE)),
"URI parsing failed"
);
}

static zend_string *parse_url_uri_to_string(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment)
{
ZEND_UNREACHABLE();
Expand All @@ -472,6 +485,7 @@ const uri_handler_t parse_url_uri_handler = {
"parse_url",
parse_url_init_parser,
parse_url_parse_uri,
parse_url_create_invalid_url_exception,
parse_url_clone_uri,
parse_url_uri_to_string,
parse_url_free_uri,
Expand Down
43 changes: 29 additions & 14 deletions ext/uri/php_lexbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

static zend_result lexbor_init_parser(void);
static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *base_url_str, zval *errors);
static void lexbor_create_invalid_uri_exception(zval *exception_zv, zval *errors);
static void *lexbor_clone_uri(void *uri);
static zend_string *lexbor_uri_to_string(void *uri, uri_recomposition_mode_t recomposition_mode, bool exclude_fragment);
static void lexbor_free_uri(void *uri);
Expand All @@ -39,6 +40,7 @@ const uri_handler_t lexbor_uri_handler = {
URI_PARSER_WHATWG,
lexbor_init_parser,
lexbor_parse_uri,
lexbor_create_invalid_uri_exception,
lexbor_clone_uri,
lexbor_uri_to_string,
lexbor_free_uri,
Expand Down Expand Up @@ -284,7 +286,7 @@ static zend_result lexbor_write_scheme(uri_internal_t *internal_uri, zval *value
CHECK_WRITE_RESULT(status, lexbor_uri, str, errors);
}

static zend_result lexbor_read_user(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
static zend_result lexbor_read_username(const uri_internal_t *internal_uri, uri_component_read_mode_t read_mode, zval *retval)
{
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

Expand All @@ -297,7 +299,7 @@ static zend_result lexbor_read_user(const uri_internal_t *internal_uri, uri_comp
return SUCCESS;
}

static zend_result lexbor_write_user(uri_internal_t *internal_uri, zval *value, zval *errors)
static zend_result lexbor_write_username(uri_internal_t *internal_uri, zval *value, zval *errors)
{
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;
lexbor_str_t str = {0};
Expand Down Expand Up @@ -339,22 +341,19 @@ static zend_result lexbor_read_host(const uri_internal_t *internal_uri, uri_comp
lxb_url_t *lexbor_uri = (lxb_url_t *) internal_uri->uri;

if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV4) {
struct in_addr myaddr;
myaddr.s_addr = htonl((zend_ulong) &lexbor_uri->host.u.ipv4);
char buffer[17];
smart_str host_str = {0};

const char* result = inet_ntop(AF_INET, &myaddr, buffer, sizeof(buffer));
ZEND_ASSERT(result != NULL);
lxb_url_serialize_host_ipv4((zend_ulong) lexbor_uri->host.u.ipv4, lexbor_serialize_callback, (void *) &host_str);

ZVAL_STRING(retval, buffer);
ZVAL_STR(retval, smart_str_extract(&host_str));
} else if (lexbor_uri->host.type == LXB_URL_HOST_TYPE_IPV6) {
char buffer[46];
smart_str host_str = {0};

php_sprintf(buffer, "[%hu:%hu:%hu:%hu:%hu:%hu:%hu:%hu]", lexbor_uri->host.u.ipv6[0], lexbor_uri->host.u.ipv6[1],
lexbor_uri->host.u.ipv6[2], lexbor_uri->host.u.ipv6[3], lexbor_uri->host.u.ipv6[4], lexbor_uri->host.u.ipv6[5],
lexbor_uri->host.u.ipv6[6], lexbor_uri->host.u.ipv6[7]);
smart_str_appendc(&host_str, '[');
lxb_url_serialize_host_ipv6(lexbor_uri->host.u.ipv6, lexbor_serialize_callback, (void *) &host_str);
smart_str_appendc(&host_str, ']');

ZVAL_STRING(retval, buffer);
ZVAL_STR(retval, smart_str_extract(&host_str));
} else if (lexbor_uri->host.type != LXB_URL_HOST_TYPE_EMPTY && lexbor_uri->host.type != LXB_URL_HOST_TYPE__UNDEF) {

switch (read_mode) {
Expand Down Expand Up @@ -522,7 +521,7 @@ static zend_result lexbor_init_parser(void)
zend_hash_init(&lexbor_property_handlers, 8, NULL, NULL, true);

URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_SCHEME), lexbor_read_scheme, lexbor_write_scheme);
URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_USER), lexbor_read_user, lexbor_write_user);
URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_USERNAME), lexbor_read_username, lexbor_write_username);
URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_PASSWORD), lexbor_read_password, lexbor_write_password);
URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_HOST), lexbor_read_host, lexbor_write_host);
URI_REGISTER_PROPERTY_READ_WRITE_HANDLER(&lexbor_property_handlers, ZSTR_KNOWN(ZEND_STR_PORT), lexbor_read_port, lexbor_write_port);
Expand Down Expand Up @@ -552,6 +551,22 @@ static void *lexbor_parse_uri(const zend_string *url_str, const zend_string *bas
return url;
}

static void lexbor_create_invalid_uri_exception(zval *exception_zv, zval *errors)
{
object_init_ex(exception_zv, whatwg_invalid_url_exception_ce);

zend_update_property_string(
whatwg_invalid_url_exception_ce,
Z_OBJ_P(exception_zv),
ZSTR_VAL(ZSTR_KNOWN(ZEND_STR_MESSAGE)),
ZSTR_LEN(ZSTR_KNOWN(ZEND_STR_MESSAGE)),
"URL parsing failed"
);

zend_update_property(whatwg_invalid_url_exception_ce, Z_OBJ_P(exception_zv), "errors", sizeof("errors") - 1, errors);
}


static void *lexbor_clone_uri(void *uri)
{
lxb_url_t *lexbor_uri = (lxb_url_t *) uri;
Expand Down
Loading