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 Dec 29, 2024
1 parent 0586b61 commit b793a81
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 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

- None
- `UriString::parse` will fail if the URI contains whitespace.

### Deprecated

Expand Down
3 changes: 2 additions & 1 deletion UriString.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class UriString
*
* @var string
*/
private const REGEXP_INVALID_URI_CHARS = '/[\x00-\x1f\x7f]/';
private const REGEXP_INVALID_URI_CHARS = '/[\x00-\x1f\x7f\s]/';

/**
* RFC3986 regular expression URI splitter.
Expand Down Expand Up @@ -486,6 +486,7 @@ private static function resolvePathAndQuery(array $uri, array $baseUri): array
public static function parse(Stringable|string|int $uri): array
{
$uri = (string) $uri;

if (isset(self::URI_SHORTCUTS[$uri])) {
/** @var ComponentMap $components */
$components = [...self::URI_COMPONENTS, ...self::URI_SHORTCUTS[$uri]];
Expand Down
20 changes: 20 additions & 0 deletions UriStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace League\Uri;

use League\Uri\Contracts\UriException;
use League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Stringable;

Expand Down Expand Up @@ -1027,4 +1029,22 @@ public static function resolveProvider(): array
'not same origin' => [self::BASE_URI, 'ftp://a/b/c/d', 'ftp://a/b/c/d'],
];
}

#[Test]
#[DataProvider('invalidUriWithWhitespaceProvider')]
public function it_fails_parsing_uri_string_with_whitespace(string $uri): void
{
$this->expectException(UriException::class);

UriString::parse($uri);
}

public static function invalidUriWithWhitespaceProvider(): iterable
{
yield 'uri containing only whitespaces' => ['uri' => ' '];
yield 'uri stating with whitespace' => ['uri' => ' https://a/b?c'];
yield 'uri ending with whitespace' => ['uri' => 'https://a/b?c '];
yield 'uri surrounded with whitespace' => ['uri' => ' https://a/b?c '];
yield 'uri containing with whitespace' => ['uri' => 'https://a/b ?c'];
}
}

0 comments on commit b793a81

Please sign in to comment.