Skip to content

Commit

Permalink
Merge pull request #34 from utopia-php/feat-allow-certain-domains
Browse files Browse the repository at this point in the history
Allow certain domains to pass public domain validation check
  • Loading branch information
eldadfux authored Jan 3, 2024
2 parents 8151984 + a8f3f77 commit bf07f60
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Domains/Validator/PublicDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
class PublicDomain extends Validator
{
/**
* @var array
*/
protected static $allowedDomains = [];
/**
* Get Description
*
Expand All @@ -27,7 +31,7 @@ public function getDescription(): string
/**
* Is valid
*
* Validation will pass when $value is a public domain
* Validation will pass when $value is either a known domain or in the list of allowed domains
*
* @param mixed $value
* @return bool
Expand All @@ -40,11 +44,8 @@ public function isValid($value): bool
}

$domain = new Domain($value);
if (!$domain->isKnown()) {
return false;
}

return true;
return $domain->isKnown() || in_array($domain->get(), self::$allowedDomains);
}

/**
Expand All @@ -70,4 +71,16 @@ public function getType(): string
{
return self::TYPE_STRING;
}

/**
* Allow domains
*
* Add domains to the allowed domains array
*
* @param array $domains
*/
public static function allow(array $domains): void
{
self::$allowedDomains = array_merge(self::$allowedDomains, $domains);
}
}
23 changes: 23 additions & 0 deletions tests/Validator/PublicDomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ public function tearDown(): void
public function testIsValid(): void
{
$this->assertEquals('Value must be a public domain', $this->domain->getDescription());
// Known public domains
$this->assertEquals(true, $this->domain->isValid('example.com'));
$this->assertEquals(true, $this->domain->isValid('google.com'));
$this->assertEquals(true, $this->domain->isValid('bbc.co.uk'));
$this->assertEquals(true, $this->domain->isValid('appwrite.io'));
$this->assertEquals(true, $this->domain->isValid('usa.gov'));
$this->assertEquals(true, $this->domain->isValid('stanford.edu'));

// URLs
$this->assertEquals(true, $this->domain->isValid('http://google.com'));
$this->assertEquals(true, $this->domain->isValid('http://www.google.com'));
$this->assertEquals(true, $this->domain->isValid('https://example.com'));

// Private domains
$this->assertEquals(false, $this->domain->isValid('localhost'));
$this->assertEquals(false, $this->domain->isValid('http://localhost'));
$this->assertEquals(false, $this->domain->isValid('sub.demo.localhost'));
Expand All @@ -39,4 +44,22 @@ public function testIsValid(): void
$this->assertEquals(false, $this->domain->isValid('wiki.team.local'));
$this->assertEquals(false, $this->domain->isValid('example.test'));
}

public function testAllowDomains(): void
{
// Adding localhost to allowed domains
PublicDomain::allow(['localhost']);

// Now localhost should be valid
$this->assertEquals(true, $this->domain->isValid('localhost'));
$this->assertEquals(true, $this->domain->isValid('http://localhost'));
$this->assertEquals(false, $this->domain->isValid('test.app.internal'));

// Adding more domains to allowed domains
PublicDomain::allow(['test.app.internal', 'home.local']);

// Now these domains should be valid
$this->assertEquals(true, $this->domain->isValid('test.app.internal'));
$this->assertEquals(true, $this->domain->isValid('home.local'));
}
}

0 comments on commit bf07f60

Please sign in to comment.