Skip to content

Commit 6f40be4

Browse files
committed
Password value is computed for Uri class
1 parent 10e6eb8 commit 6f40be4

File tree

6 files changed

+90
-26
lines changed

6 files changed

+90
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All Notable changes to `League\Uri` will be documented in this file
1111
### Fixed
1212

1313
- Null byte handling see [GH-20366](https://github.com/php/php-src/pull/20489)
14+
- Align password component handling for the Uri class with `Uri\Rfc3986\Uri` implementation [GH-20545](https://github.com/php/php-src/issues/20545)
1415

1516
### Deprecated
1617

Polyfill/UriTest.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ public function it_can_update_the_user_info_component(): void
296296

297297
self::assertSame('apple', $uriWithUser->getUserInfo());
298298
self::assertSame('apple', $uriWithUser->getUsername());
299-
self::assertNull($uriWithUser->getPassword());
300-
self::assertNull($uriWithUser->getRawPassword());
299+
self::assertSame('', $uriWithUser->getPassword());
300+
self::assertSame('', $uriWithUser->getRawPassword());
301301

302302
$uriWithUserAndPassword = $uriWithUser->withUserInfo('banana:cream');
303303
self::assertSame('banana:cream', $uriWithUserAndPassword->getUserInfo());
@@ -554,9 +554,63 @@ public function it_does_not_guarantee_roundtripping_on_modification_part_3(): vo
554554

555555
public function test_it_fails_using_exception_with_null_bytes(): void
556556
{
557-
$uri = new Uri("https://example.com");
557+
$uri = new Uri('https://example.com');
558558

559559
$this->expectException(InvalidUriException::class);
560560
$uri->resolve("/f\0o");
561561
}
562+
563+
public function test_userinfo_password_is_always_set_when_userinfo_is_set(): void
564+
{
565+
$uri = new Uri('http://example.com#foobar');
566+
$uri1 = $uri->withUserInfo('appple');
567+
$uri2 = $uri1->withUserInfo(null);
568+
569+
self::assertNull($uri->getPassword());
570+
self::assertSame('', $uri1->getPassword());
571+
self::assertNull($uri2->getPassword());
572+
}
573+
574+
#[DataProvider('providesUriWithUserInfoAndPassword')]
575+
public function test_uri_is_not_affected_by_password_value(
576+
string $input,
577+
?string $password,
578+
?string $username
579+
): void {
580+
$uri = new Uri($input);
581+
582+
self::assertSame($password, $uri->getPassword());
583+
self::assertSame($password, $uri->__debugInfo()['password']);
584+
self::assertSame($username, $uri->getUsername());
585+
self::assertSame($username, $uri->__debugInfo()['username']);
586+
self::assertSame($input, $uri->toRawString());
587+
self::assertSame($input, $uri->toString());
588+
}
589+
590+
public static function providesUriWithUserInfoAndPassword(): iterable
591+
{
592+
yield 'userinfo is missing' => [
593+
'input' => 'http://example.com#foobar',
594+
'password' => null,
595+
'username' => null,
596+
];
597+
598+
yield 'userinfo password is missing' => [
599+
'input' => 'http://[email protected]#foobar',
600+
'password' => '',
601+
'username' => 'user',
602+
];
603+
604+
yield 'userinfo user is missing' => [
605+
'input' => 'http://:[email protected]#foobar',
606+
'password' => 'password',
607+
'username' => '',
608+
];
609+
610+
yield 'userinfo all components are presents' => [
611+
'input' => 'http://user:[email protected]#foobar',
612+
'password' => 'password',
613+
'username' => 'user',
614+
];
615+
}
562616
}

Polyfill/UrlTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -369,60 +369,60 @@ public function test_it_will_return_the_host_correct_format(): void
369369

370370
public function test_it_can_handle_null_bytes_in_fragment(): void
371371
{
372-
$url1 = new Url("https://example.com");
372+
$url1 = new Url('https://example.com');
373373
$url2 = $url1->withFragment("frag\0ment");
374374

375-
self::assertSame("frag%00ment", $url2->getFragment());
376-
self::assertSame("https://example.com/#frag%00ment", $url2->toAsciiString());
375+
self::assertSame('frag%00ment', $url2->getFragment());
376+
self::assertSame('https://example.com/#frag%00ment', $url2->toAsciiString());
377377
}
378378

379379
public function test_it_can_handle_null_bytes_in_query(): void
380380
{
381-
$url1 = new Url("https://example.com");
381+
$url1 = new Url('https://example.com');
382382
$url2 = $url1->withQuery("f\0o=bar&baz=q\0x");
383383

384-
self::assertSame("f%00o=bar&baz=q%00x", $url2->getQuery());
385-
self::assertSame("https://example.com/?f%00o=bar&baz=q%00x", $url2->toAsciiString());
384+
self::assertSame('f%00o=bar&baz=q%00x', $url2->getQuery());
385+
self::assertSame('https://example.com/?f%00o=bar&baz=q%00x', $url2->toAsciiString());
386386
}
387387

388388
public function test_it_can_handle_null_bytes_in_path(): void
389389
{
390-
$url1 = new Url("https://example.com");
390+
$url1 = new Url('https://example.com');
391391
$url2 = $url1->withPath("/p\0th\0");
392392

393-
self::assertSame("/p%00th%00", $url2->getPath());
394-
self::assertSame("https://example.com/p%00th%00", $url2->toAsciiString());
393+
self::assertSame('/p%00th%00', $url2->getPath());
394+
self::assertSame('https://example.com/p%00th%00', $url2->toAsciiString());
395395
}
396396

397397
public function test_it_can_handle_null_bytes_in_password(): void
398398
{
399-
$url1 = new Url("https://example.com");
399+
$url1 = new Url('https://example.com');
400400
$url2 = $url1->withPassword("pass\0word");
401401

402-
self::assertSame("pass%00word", $url2->getPassword());
403-
self::assertSame("https://:pass%[email protected]/", $url2->toAsciiString());
402+
self::assertSame('pass%00word', $url2->getPassword());
403+
self::assertSame('https://:pass%[email protected]/', $url2->toAsciiString());
404404
}
405405

406406
public function test_it_can_handle_null_bytes_in_username(): void
407407
{
408-
$url1 = new Url("https://example.com");
408+
$url1 = new Url('https://example.com');
409409
$url2 = $url1->withUsername("usern\0me");
410410

411-
self::assertSame("usern%00me", $url2->getUsername());
412-
self::assertSame("https://usern%[email protected]/", $url2->toAsciiString());
411+
self::assertSame('usern%00me', $url2->getUsername());
412+
self::assertSame('https://usern%[email protected]/', $url2->toAsciiString());
413413
}
414414

415415
public function test_it_cannot_handle_null_bytes_in_host(): void
416416
{
417-
$url1 = new Url("https://example.com");
417+
$url1 = new Url('https://example.com');
418418
$this->expectException(InvalidUrlException::class);
419419

420420
$url1->withHost("usern\0me");
421421
}
422422

423423
public function test_it_cannot_handle_null_bytes_in_scheme(): void
424424
{
425-
$url1 = new Url("https://example.com");
425+
$url1 = new Url('https://example.com');
426426
$this->expectException(InvalidUrlException::class);
427427

428428
$url1->withScheme("usern\0me");

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
5-
[![Latest Version](https://img.shields.io/github/release/thephpleague/uri-pollyfill.svg?style=flat-square)](https://github.com/thephpleague/uri-interfaces/releases)
5+
[![Latest Version](https://img.shields.io/github/release/thephpleague/uri-polyfill.svg?style=flat-square)](https://github.com/thephpleague/uri-interfaces/releases)
66
[![Total Downloads](https://img.shields.io/packagist/dt/league/uri-polyfill.svg?style=flat-square)](https://packagist.org/packages/league/uri-interfaces)
77

88
````php

lib/Rfc3986/Uri.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function parse(string $uri, ?self $baseUri = null): ?Uri
7171
public function __construct(string $uri, ?self $baseUri = null)
7272
{
7373
if (!UriString::containsRfc3986Chars($uri)) {
74-
$formatter = fn (string $input): ?string => preg_replace_callback('/[\x00-\x1F\x7F]/', fn (array $m): string => '\\x' . bin2hex($m[0]), $input);
74+
$formatter = fn (string $input): ?string => preg_replace_callback('/[\x00-\x1F\x7F]/', fn (array $m): string => '\\x'.bin2hex($m[0]), $input);
7575

7676
throw new InvalidUriException('The URI `'.$formatter($uri).'` contains invalid RFC3986 characters.');
7777
}
@@ -286,12 +286,22 @@ public function getUsername(): ?string
286286

287287
public function getRawPassword(): ?string
288288
{
289-
return $this->getComponent(self::TYPE_RAW, 'pass');
289+
$parsedValue = $this->getComponent(self::TYPE_RAW, 'pass');
290+
if (null !== $this->getUsername()) {
291+
return (string) $parsedValue;
292+
}
293+
294+
return $parsedValue;
290295
}
291296

292297
public function getPassword(): ?string
293298
{
294-
return $this->getComponent(self::TYPE_NORMALIZED, 'pass');
299+
$parsedValue = $this->getComponent(self::TYPE_NORMALIZED, 'pass');
300+
if (null !== $this->getUsername()) {
301+
return (string) $parsedValue;
302+
}
303+
304+
return $parsedValue;
295305
}
296306

297307
public function getRawHost(): ?string
@@ -470,7 +480,7 @@ public function __debugInfo(): array
470480
return [
471481
'scheme' => $this->rawComponents['scheme'],
472482
'username' => $this->rawComponents['user'],
473-
'password' => $this->rawComponents['pass'],
483+
'password' => null !== $this->rawComponents['user'] ? ((string) $this->rawComponents['pass']) : $this->rawComponents['pass'],
474484
'host' => $this->rawComponents['host'],
475485
'port' => $this->rawComponents['port'],
476486
'path' => $this->rawComponents['path'],

lib/WhatWg/Url.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Rowbot\URL\URLRecord;
2626
use SensitiveParameter;
2727
use Uri\UriComparisonMode;
28-
use ValueError;
2928

3029
use function in_array;
3130
use function preg_match;

0 commit comments

Comments
 (0)