Skip to content

Commit

Permalink
Fix URI RFC3986 parsing and invalid characters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 2, 2025
1 parent fdb81ce commit 1fb9ecc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All Notable changes to `League\Uri\Interfaces` will be documented in this file

### Fixed

- `UriString::parse` will fail if the URI contains whitespace.
- `UriString::parse` will fail if the URI contains whitespace or is the empty string.

### Deprecated

Expand Down
9 changes: 6 additions & 3 deletions UriString.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ final class UriString
* @var array<string, array<string>>
*/
private const URI_SHORTCUTS = [
'' => [],
'#' => ['fragment' => ''],
'?' => ['query' => ''],
'?#' => ['query' => '', 'fragment' => ''],
Expand Down Expand Up @@ -278,7 +277,7 @@ public static function buildAuthority(array $components): ?string
*/
public static function normalize(Stringable|string $uri): string
{
$components = UriString::parse($uri);
$components = self::parse($uri);
if (null !== $components['scheme']) {
$components['scheme'] = strtolower($components['scheme']);
}
Expand Down Expand Up @@ -339,6 +338,10 @@ public static function normalizeAuthority(Stringable|string $authority): string
*/
public static function resolve(Stringable|string $uri, Stringable|string|null $baseUri = null): string
{
if ('' === $uri) {
$uri = $baseUri ?? throw new SyntaxError('The uri can not be the empty string when there\'s no base URI.');
}

$uri = self::parse($uri);
$baseUri = null !== $baseUri ? self::parse($baseUri) : $uri;
if (null === $baseUri['scheme']) {
Expand Down Expand Up @@ -492,7 +495,7 @@ public static function parse(Stringable|string|int $uri): array
return $components;
}

if (1 === preg_match(self::REGEXP_INVALID_URI_CHARS, $uri)) {
if ('' === $uri || 1 === preg_match(self::REGEXP_INVALID_URI_CHARS, $uri)) {
throw new SyntaxError(sprintf('The uri `%s` contains invalid characters', $uri));
}

Expand Down
18 changes: 1 addition & 17 deletions UriStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,19 +579,6 @@ public static function validUriProvider(): array
'fragment' => 'foo=1/bar=2',
],
],
'empty string' => [
'',
[
'scheme' => null,
'user' => null,
'pass' => null,
'host' => null,
'port' => null,
'path' => '',
'query' => null,
'fragment' => null,
],
],
'complex URI' => [
'htà+d/s:totot',
[
Expand Down Expand Up @@ -776,6 +763,7 @@ public static function invalidUriProvider(): array
'invalid host with fullwidth escaped' => ['http://%ef%bc%85%ef%bc%94%ef%bc%91.com],'],
//'invalid pseudo IDN to ASCII string' => ['http://xn--3/foo.'],
'invalid IDN' => ['//:�@�����������������������������������������������������������������������������������������/'],
'empty string' => [''],
];
}

Expand Down Expand Up @@ -944,10 +932,6 @@ public static function buildUriProvider(): array
'http://example.com#foo=1/bar=2',
'http://example.com#foo=1/bar=2',
],
'empty string' => [
'',
'',
],
'complex URI' => [
'htà+d/s:totot',
'htà+d/s:totot',
Expand Down

0 comments on commit 1fb9ecc

Please sign in to comment.