Skip to content

Commit

Permalink
Merge pull request #85 from utopia-php/improve-hostname-validator
Browse files Browse the repository at this point in the history
Feat: Improve hostname validator
  • Loading branch information
eldadfux authored Feb 27, 2023
2 parents b8d0447 + 74c4595 commit 9b2e1fc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
34 changes: 8 additions & 26 deletions src/Validator/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,14 @@ public function isValid(mixed $value): bool
}

// If wildcard symbol used
if (\str_contains($allowedHostname, '*')) {
// Split hostnames into sections (subdomains)
$allowedSections = \explode('.', $allowedHostname);
$valueSections = \explode('.', $value);

// Only if amount of sections matches
if (\count($allowedSections) === \count($valueSections)) {
$matchesAmount = 0;

// Loop through all sections
for ($sectionIndex = 0; $sectionIndex < \count($allowedSections); $sectionIndex++) {
$allowedSection = $allowedSections[$sectionIndex];

// If section matches, or wildcard symbol is used, increment match count
if ($allowedSection === '*' || $allowedSection === $valueSections[$sectionIndex]) {
$matchesAmount++;
} else {
// If one fails, the whole check always fails; we can skip iterations
break;
}
}

// If every section matched; allow
if ($matchesAmount === \count($allowedSections)) {
return true;
}
if(\str_starts_with($allowedHostname, '*')) {
// Remove starting * symbol before comparing
$allowedHostname = substr($allowedHostname, 1);

// If rest of hostname match; allow
// Notice allowedHostname still includes starting dot. Root domain is NOT allowed by wildcard.
if(\str_ends_with($value, $allowedHostname)) {
return true;
}
}
}
Expand Down
23 changes: 11 additions & 12 deletions tests/Validator/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function testCanValidateHostnamesWithAllowList(): void
'myweb.vercel.app',
'myweb.com',
'*.myapp.com',
'*.*.myrepo.com',
]);

$this->assertTrue($validator->isValid('myweb.vercel.app'));
Expand All @@ -73,15 +72,9 @@ public function testCanValidateHostnamesWithAllowList(): void
$this->assertTrue($validator->isValid('project2.myapp.com'));
$this->assertTrue($validator->isValid('project-with-dash.myapp.com'));
$this->assertTrue($validator->isValid('anything.myapp.com'));
$this->assertTrue($validator->isValid('commit.anything.myapp.com'));
$this->assertFalse($validator->isValid('anything.myapp.eu'));
$this->assertFalse($validator->isValid('commit.anything.myapp.com'));

$this->assertTrue($validator->isValid('commit1.project1.myrepo.com'));
$this->assertTrue($validator->isValid('commit2.project3.myrepo.com'));
$this->assertTrue($validator->isValid('commit-with-dash.project-with-dash.myrepo.com'));
$this->assertFalse($validator->isValid('myrepo.com'));
$this->assertFalse($validator->isValid('project1.myrepo.com'));
$this->assertFalse($validator->isValid('line1.commit1.project1.myrepo.com'));
$this->assertFalse($validator->isValid('myapp.com'));

$validator = new Hostname(['localhost']);
$this->assertTrue($validator->isValid('localhost'));
Expand All @@ -93,9 +86,15 @@ public function testCanValidateHostnamesWithWildcard(): void
$this->assertTrue($validator->isValid('*'));

$validator = new Hostname(['netlify.*']);
$this->assertTrue($validator->isValid('netlify.com'));
$this->assertTrue($validator->isValid('netlify.eu'));
$this->assertTrue($validator->isValid('netlify.app'));
$this->assertFalse($validator->isValid('netlify.com'));
$this->assertFalse($validator->isValid('netlify.eu'));
$this->assertFalse($validator->isValid('netlify.app'));

$validator = new Hostname(['*.*.app.io']);
$this->assertFalse($validator->isValid('app.io'));
$this->assertFalse($validator->isValid('project.app.io'));
$this->assertFalse($validator->isValid('commit.project.app.io'));
$this->assertFalse($validator->isValid('api.commit.project.app.io'));

$validator = new Hostname(['*']);
$this->assertTrue($validator->isValid('*'));
Expand Down

0 comments on commit 9b2e1fc

Please sign in to comment.