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

Add isExternal for validating safe domains #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 21 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 35 additions & 10 deletions src/Domains/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(string $domain)
$this->parts = \explode('.', $this->domain);

if (empty(self::$list)) {
self::$list = include __DIR__.'/../../data/data.php';
self::$list = include __DIR__ . '/../../data/data.php';
}
}

Expand Down Expand Up @@ -115,8 +115,8 @@ public function getSuffix(): string
for ($i = 0; $i < count($this->parts); $i++) {
$joined = \implode('.', \array_slice($this->parts, $i));
$next = \implode('.', \array_slice($this->parts, $i + 1));
$exception = '!'.$joined;
$wildcard = '*.'.$next;
$exception = '!' . $joined;
$wildcard = '*.' . $next;

if (\array_key_exists($exception, self::$list)) {
$this->suffix = $next;
Expand Down Expand Up @@ -145,7 +145,7 @@ public function getSuffix(): string

public function getRule(): string
{
if (! $this->rule) {
if (!$this->rule) {
$this->getSuffix();
}
return $this->rule;
Expand All @@ -156,11 +156,11 @@ public function getRule(): string
*/
public function getRegisterable(): string
{
if (! $this->isKnown()) {
if (!$this->isKnown()) {
return '';
}

$registerable = $this->getName().'.'.$this->getSuffix();
$registerable = $this->getName() . '.' . $this->getSuffix();

return $registerable;
}
Expand All @@ -175,7 +175,7 @@ public function getName(): string
}

$suffix = $this->getSuffix();
$suffix = (! empty($suffix)) ? '.'.$suffix : '.'.$this->getTLD();
$suffix = (!empty($suffix)) ? '.' . $suffix : '.' . $this->getTLD();

$name = \explode('.', \mb_substr($this->domain, 0, \mb_strlen($suffix) * -1));

Expand All @@ -190,12 +190,12 @@ public function getName(): string
public function getSub(): string
{
$name = $this->getName();
$name = (! empty($name)) ? '.'.$name : '';
$name = (!empty($name)) ? '.' . $name : '';

$suffix = $this->getSuffix();
$suffix = (! empty($suffix)) ? '.'.$suffix : '.'.$this->getTLD();
$suffix = (!empty($suffix)) ? '.' . $suffix : '.' . $this->getTLD();

$domain = $name.$suffix;
$domain = $name . $suffix;

$sub = \explode('.', \mb_substr($this->domain, 0, \mb_strlen($domain) * -1));

Expand Down Expand Up @@ -240,6 +240,31 @@ public function isPrivate(): bool
return false;
}

/**
* Performs a DNS Lookup to check if the domain is valid and isn't within the private or reserved IP ranges
*/
public function isExternal(): bool
{
$ipV4s = dns_get_record($this->get(), DNS_A);
$ipV6s = dns_get_record($this->get(), DNS_AAAA);

$ips = array_merge($ipV4s, $ipV6s);

if (empty($ips)) {
return false;
}

foreach ($ips as $record) {
$ipAddress = $record['ip'] ?? $record['ipv6'] ?? null;

if ($ipAddress && !filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
return false;
}
}

return true;
}

/**
* Returns true if the public suffix is reserved for testing purpose
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,13 @@ public function testPrivateDomain(): void
$this->assertEquals(true, $domain->isPrivate());
$this->assertEquals(false, $domain->isTest());
}

public function testIsExternal()
{
$internalDomain = new Domain('127.0.0.1.nip.io');
$this->assertFalse($internalDomain->isExternal());

$externalDomain = new Domain('example.com');
$this->assertTrue($externalDomain->isExternal());
}
}
Loading