From 9cfeb169ddce087ca6da5c7e8992a20febda7e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 4 Jan 2024 13:24:25 +0000 Subject: [PATCH] Wildcard host validator --- src/Validator/Host.php | 8 +++++--- tests/Validator/HostTest.php | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) mode change 100644 => 100755 src/Validator/Host.php diff --git a/src/Validator/Host.php b/src/Validator/Host.php old mode 100644 new mode 100755 index 1f7fb06c..77078b2c --- a/src/Validator/Host.php +++ b/src/Validator/Host.php @@ -51,11 +51,13 @@ public function isValid($value): bool return false; } - if (\in_array(\parse_url($value, PHP_URL_HOST), $this->whitelist)) { - return true; + $hostnameValidator = new Hostname($this->whitelist); + + if (!$hostnameValidator->isValid(\parse_url($value, PHP_URL_HOST))) { + return false; } - return false; + return true; } /** diff --git a/tests/Validator/HostTest.php b/tests/Validator/HostTest.php index 6ad38299..de62d4bf 100644 --- a/tests/Validator/HostTest.php +++ b/tests/Validator/HostTest.php @@ -21,7 +21,7 @@ class HostTest extends TestCase public function setUp(): void { - $this->host = new Host(['example.io', 'subdomain.example.test', 'localhost']); + $this->host = new Host(['example.io', 'subdomain.example.test', 'localhost', '*.appwrite.io']); } public function testIsValid() @@ -32,6 +32,10 @@ public function testIsValid() $this->assertEquals($this->host->isValid('localhost'), false); $this->assertEquals($this->host->isValid('http://subdomain.example.test/path'), true); $this->assertEquals($this->host->isValid('http://test.subdomain.example.test/path'), false); + $this->assertEquals($this->host->isValid('http://appwrite.io/path'), false); + $this->assertEquals($this->host->isValid('http://me.appwrite.io/path'), true); + $this->assertEquals($this->host->isValid('http://you.appwrite.io/path'), true); + $this->assertEquals($this->host->isValid('http://us.together.appwrite.io/path'), true); $this->assertEquals($this->host->getType(), 'string'); } }