Skip to content

Commit 81f6ba5

Browse files
authored
uri: Use the “includes credentials” rule for WhatWg user/password getters (php#20303)
* uri: Use the “includes credentials” rule for WhatWg user/password getters The URL serializing algorithm from the WHATWG URL Standard uses an “includes credentials” rule to decide whether or not to include the `@` in the output, indicating the presence of a userinfo component in RFC 3986 terminology. Use this rule to determine whether or not an empty username or password should be returned as the empty string (present but empty) or NULL (not present). * uri: Use ZVAL_STRINGL_FAST in `whatwg_(username|password)_read()` This nicely sidesteps the undefined behavior with passing a `(NULL, 0)` pair without needing manual logic. * NEWS
1 parent cda8daa commit 81f6ba5

8 files changed

+59
-10
lines changed

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ PHP NEWS
2828
. Fixed bug GH-19798: XP_SOCKET XP_SSL (Socket stream modules): Incorrect
2929
condition for Win32/Win64. (Jakub Zelenka)
3030

31+
- URI:
32+
. Use the "includes credentials" rule of the WHATWG URL Standard to
33+
decide whether Uri\WhatWg\Url::getUsername() and ::getPassword()
34+
getters should return null or an empty string. (timwolla)
35+
3136
- Zip:
3237
. Fixed missing zend_release_fcall_info_cache on the following methods
3338
ZipArchive::registerProgressCallback() and ZipArchive::registerCancelCallback()

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)