diff --git a/interfaces/CHANGELOG.md b/interfaces/CHANGELOG.md index 4c405f6d..601fda9a 100644 --- a/interfaces/CHANGELOG.md +++ b/interfaces/CHANGELOG.md @@ -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 diff --git a/interfaces/UriString.php b/interfaces/UriString.php index 59cb8571..6c33a754 100644 --- a/interfaces/UriString.php +++ b/interfaces/UriString.php @@ -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. @@ -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]]; diff --git a/interfaces/UriStringTest.php b/interfaces/UriStringTest.php index 3d0ec46a..cb76d4cb 100644 --- a/interfaces/UriStringTest.php +++ b/interfaces/UriStringTest.php @@ -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; @@ -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']; + } }