Skip to content

Commit 82249dc

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: uri: Use the “includes credentials” rule for WhatWg user/password getters (php#20303)
2 parents 2f0a0c5 + 81f6ba5 commit 82249dc

7 files changed

+54
-10
lines changed

ext/uri/tests/whatwg/modification/password_success_unset_existing.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ var_dump($url2->toAsciiString());
1515
?>
1616
--EXPECT--
1717
string(8) "password"
18-
NULL
18+
string(0) ""
1919
string(29) "https://[email protected]/"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test Uri\WhatWg\Url component modification - password - unsetting existing
3+
--EXTENSIONS--
4+
uri
5+
--FILE--
6+
<?php
7+
8+
$url1 = Uri\WhatWg\Url::parse("https://:[email protected]");
9+
$url2 = $url1->withPassword(null);
10+
11+
var_dump($url1->getPassword());
12+
var_dump($url2->getPassword());
13+
var_dump($url2->toAsciiString());
14+
15+
?>
16+
--EXPECT--
17+
string(8) "password"
18+
NULL
19+
string(20) "https://example.com/"

ext/uri/tests/whatwg/modification/password_success_unset_non_existent2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var_dump($url2->toAsciiString());
1414

1515
?>
1616
--EXPECT--
17-
NULL
18-
NULL
17+
string(0) ""
18+
string(0) ""
1919
string(29) "https://[email protected]/"

ext/uri/tests/whatwg/modification/username_success_unset_existing.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ var_dump($url2->toAsciiString());
1515
?>
1616
--EXPECT--
1717
string(8) "username"
18-
NULL
18+
string(0) ""
1919
string(30) "https://:[email protected]/"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test Uri\WhatWg\Url component modification - username - unsetting existing
3+
--EXTENSIONS--
4+
uri
5+
--FILE--
6+
<?php
7+
8+
$url1 = Uri\WhatWg\Url::parse("https://username:@example.com");
9+
$url2 = $url1->withUsername(null);
10+
11+
var_dump($url1->getUsername());
12+
var_dump($url2->getUsername());
13+
var_dump($url2->toAsciiString());
14+
15+
?>
16+
--EXPECT--
17+
string(8) "username"
18+
NULL
19+
string(20) "https://example.com/"

ext/uri/tests/whatwg/modification/username_success_unset_non_existent2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var_dump($url2->toAsciiString());
1414

1515
?>
1616
--EXPECT--
17-
NULL
18-
NULL
17+
string(0) ""
18+
string(0) ""
1919
string(30) "https://:[email protected]/"

ext/uri/uri_parser_whatwg.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,18 @@ static zend_result php_uri_parser_whatwg_scheme_write(void *uri, zval *value, zv
274274
return SUCCESS;
275275
}
276276

277+
/* 4.2. URL miscellaneous: A URL includes credentials if its username or password is not the empty string. */
278+
static bool includes_credentials(const lxb_url_t *lexbor_uri)
279+
{
280+
return lexbor_uri->username.length > 0 || lexbor_uri->password.length > 0;
281+
}
282+
277283
static zend_result php_uri_parser_whatwg_username_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
278284
{
279285
const lxb_url_t *lexbor_uri = uri;
280286

281-
if (lexbor_uri->username.length) {
282-
ZVAL_STRINGL(retval, (const char *) lexbor_uri->username.data, lexbor_uri->username.length);
287+
if (includes_credentials(lexbor_uri)) {
288+
ZVAL_STRINGL_FAST(retval, (const char *) lexbor_uri->username.data, lexbor_uri->username.length);
283289
} else {
284290
ZVAL_NULL(retval);
285291
}
@@ -307,8 +313,8 @@ static zend_result php_uri_parser_whatwg_password_read(void *uri, php_uri_compon
307313
{
308314
const lxb_url_t *lexbor_uri = uri;
309315

310-
if (lexbor_uri->password.length > 0) {
311-
ZVAL_STRINGL(retval, (const char *) lexbor_uri->password.data, lexbor_uri->password.length);
316+
if (includes_credentials(lexbor_uri)) {
317+
ZVAL_STRINGL_FAST(retval, (const char *) lexbor_uri->password.data, lexbor_uri->password.length);
312318
} else {
313319
ZVAL_NULL(retval);
314320
}

0 commit comments

Comments
 (0)