Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow certain domains to pass public domain validation check #34

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
vermakhushboo marked this conversation as resolved.
Show resolved Hide resolved
}
}
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'));
}
}