Skip to content

Commit

Permalink
validate subdomains (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
stri8ed authored Apr 19, 2024
1 parent c5edfb2 commit f6062eb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/disposable-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@

'whitelist' => [],

/*
|--------------------------------------------------------------------------
| Include Subdomains
|--------------------------------------------------------------------------
|
| Determines whether subdomains should be validated based on the disposability
| status of their parent domains. Enabling this will treat any subdomain of
| a disposable domain as disposable too (e.g., 'temp.abc.com' if 'abc.com' is
| disposable).
|
*/
'include_subdomains' => false,

/*
|--------------------------------------------------------------------------
| Cache Configuration
Expand Down
35 changes: 35 additions & 0 deletions src/DisposableDomains.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class DisposableDomains
*/
protected $cacheKey;

/**
* Whether to include subdomains.
*
* @var bool
*/
protected $includeSubdomains = false;

/**
* Disposable constructor.
*/
Expand Down Expand Up @@ -164,6 +171,12 @@ public function flushStorage()
public function isDisposable($email)
{
if ($domain = Str::lower(Arr::get(explode('@', $email, 2), 1))) {
if($this->includeSubdomains) {
$domain_parts = explode('.', $domain);
if(count($domain_parts) > 2) {
$domain = $domain_parts[count($domain_parts) - 2] . '.' . $domain_parts[count($domain_parts) - 1];
}
}
return in_array($domain, $this->domains);
}

Expand Down Expand Up @@ -225,6 +238,28 @@ public function setWhitelist(array $whitelist)
return $this;
}

/**
* Get whether to include subdomains.
*
* @return bool
*/
public function getIncludeSubdomains()
{
return $this->includeSubdomains;
}

/**
* Set whether to include subdomains.
*
* @return $this
*/
public function setIncludeSubdomains(bool $includeSubdomains)
{
$this->includeSubdomains = $includeSubdomains;

return $this;
}

/**
* Get the storage path.
*
Expand Down
1 change: 1 addition & 0 deletions src/DisposableEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function register()

$instance = new DisposableDomains($cache ?? null);

$instance->setIncludeSubdomains($app['config']['disposable-email.include_subdomains']);
$instance->setStoragePath($app['config']['disposable-email.storage']);
$instance->setCacheKey($app['config']['disposable-email.cache.key']);
$instance->setWhitelist($app['config']['disposable-email.whitelist']);
Expand Down
25 changes: 25 additions & 0 deletions tests/DisposableDomainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public function it_can_set_storage_path()
$this->assertEquals('foo', $this->disposable()->getStoragePath());
}

#[Test]
public function it_can_get_include_subdomains()
{
$this->assertEquals(
$this->app['config']['disposable-email.include_subdomains'],
$this->disposable()->getIncludeSubdomains()
);
}

#[Test]
public function it_can_set_include_subdomains()
{
$this->disposable()->setIncludeSubdomains(true);

$this->assertEquals(true, $this->disposable()->getIncludeSubdomains());
}

#[Test]
public function it_can_get_cache_key()
{
Expand Down Expand Up @@ -162,6 +179,14 @@ public function it_checks_the_full_email_domain()
$this->assertTrue($this->disposable()->isNotDisposable('[email protected]'));
}

#[Test]
public function it_can_check_subdomains()
{
$this->disposable()->setIncludeSubdomains(true);

$this->assertTrue($this->disposable()->isDisposable('[email protected]'));
}

#[Test]
public function it_can_exclude_whitelisted_domains()
{
Expand Down

0 comments on commit f6062eb

Please sign in to comment.