From 67a313cef32e36f951d8209e47ea600329b793f3 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:50:48 +0100 Subject: [PATCH] fix: tests --- app/controllers/general.php | 3 +- src/Appwrite/Network/Validator/Origin.php | 62 ++++++++++---------- src/Appwrite/Network/Validator/Redirect.php | 2 +- tests/unit/Network/Validators/OriginTest.php | 3 + 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 2e7833d9aa..be9b165ac9 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -734,7 +734,8 @@ App::init() $originValidator = new Origin($platforms); if ( - !$originValidator->isValid($origin) + !empty($origin) + && !$originValidator->isValid($origin) && \in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE]) && $route->getLabel('origin', false) !== '*' && empty($request->getHeader('x-appwrite-key', '')) diff --git a/src/Appwrite/Network/Validator/Origin.php b/src/Appwrite/Network/Validator/Origin.php index 9a3d2d6e23..78d0db221b 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -3,6 +3,7 @@ namespace Appwrite\Network\Validator; use Appwrite\Network\Platform; +use Utopia\CLI\Console; use Utopia\Validator; use Utopia\Validator\Hostname; @@ -26,37 +27,32 @@ class Origin extends Validator /** - * Check if Redirect URI is valid. - * @param mixed $redirect The redirect URI. + * Check if Origin is valid. + * @param mixed $origin The Origin URI. * @return bool */ - public function isValid($redirect): bool + public function isValid($origin): bool { $this->scheme = null; $this->host = null; - if (!is_string($redirect) || empty($redirect)) { + if (!is_string($origin) || empty($origin)) { return false; } - $parts = $this->parseUrl($redirect); - $scheme = $parts['scheme']; - $host = $parts['host']; + $this->scheme = $this->parseScheme($origin); + $this->host = parse_url($origin, PHP_URL_HOST); - if (!empty($scheme) && in_array($scheme, $this->schemes, true)) { + if (!empty($this->scheme) && in_array($this->scheme, $this->schemes, true)) { return true; } - if (empty($host)) { - return true; - } else { - $validator = new Hostname($this->hostnames); - if ($validator->isValid($host)) { - return true; - } + if (!in_array($this->scheme, ['http', 'https'])) { + return false; } - return false; + $validator = new Hostname($this->hostnames); + return $validator->isValid($this->host); } /** @@ -66,7 +62,7 @@ class Origin extends Validator public function getDescription(): string { $platform = $this->scheme ? Platform::getNameByScheme($this->scheme) : null; - $host = $this->host ? '(' . htmlspecialchars($this->host) . ')' : ''; + $host = $this->host ? '(' . $this->host . ')' : ''; if (empty($this->host) && empty($this->scheme)) { return 'Invalid Origin.'; @@ -95,20 +91,26 @@ class Origin extends Validator } /** - * Parses a URI string to extract scheme and host. - * Stores extracted parts in $this->scheme and $this->host. - * @param string $uri - * @return array{scheme: string|null, host: string|null} + * Parses the scheme from a URI string. + * + * @param string $uri The URI string to parse. + * @return string|null The extracted scheme string (e.g., "http", "exp", "mailto") */ - protected function parseUrl(string $uri): array - { - if (str_ends_with($uri, '://')) { - $uri .= 'placeholder'; + function parseScheme(string $uri): ?string { + $uri = trim($uri); + if ($uri === '') { + return null; // No scheme in empty string + } + + $scheme = parse_url($uri, PHP_URL_SCHEME); + if ($scheme === false) { + if (preg_match('/^([a-z][a-z0-9+.-]*):/i', $uri, $matches)) { + return $matches[1]; + } else { + return null; + } + } else { + return $scheme; } - $scheme = \parse_url($uri, PHP_URL_SCHEME); - $host = \parse_url($uri, PHP_URL_HOST); - $this->scheme = $scheme ?: null; - $this->host = $host ?: null; - return ['scheme' => $this->scheme,'host' => $this->host]; } } diff --git a/src/Appwrite/Network/Validator/Redirect.php b/src/Appwrite/Network/Validator/Redirect.php index 58bb955ad2..43750f4cbd 100644 --- a/src/Appwrite/Network/Validator/Redirect.php +++ b/src/Appwrite/Network/Validator/Redirect.php @@ -13,7 +13,7 @@ class Redirect extends Origin public function getDescription(): string { $platform = $this->scheme ? Platform::getNameByScheme($this->scheme) : null; - $host = $this->host ? '(' . htmlspecialchars($this->host) . ')' : ''; + $host = $this->host ? '(' . $this->host . ')' : ''; if (empty($this->host) && empty($this->scheme)) { return 'Invalid URI.'; diff --git a/tests/unit/Network/Validators/OriginTest.php b/tests/unit/Network/Validators/OriginTest.php index 839960b671..989c06da71 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -50,6 +50,9 @@ class OriginTest extends TestCase ], ]); + $this->assertEquals(false, $validator->isValid('')); + $this->assertEquals(false, $validator->isValid('/')); + $this->assertEquals(true, $validator->isValid('https://localhost')); $this->assertEquals(true, $validator->isValid('http://localhost')); $this->assertEquals(true, $validator->isValid('http://localhost:80'));