Skip to content

Commit

Permalink
Do not allow lowercase country codes
Browse files Browse the repository at this point in the history
Country codes in ISO 3166-1 should be in uppercase, and the
`AbstractSearcher` should not change the input to search for a value.

While working on this fix, I also discovered that the
"PublicDomainSuffix" rule would throw an exception if it got a
non-scalar value as an input.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 2, 2024
1 parent debf6c5 commit 5b7ea86
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
3 changes: 1 addition & 2 deletions library/Rules/AbstractSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use function in_array;
use function is_scalar;
use function mb_strtoupper;

/**
* Abstract class for searches into arrays.
Expand Down Expand Up @@ -45,6 +44,6 @@ public function validate($input): bool
return false;
}

return in_array(mb_strtoupper((string) $input), $dataSource, true);
return in_array((string) $input, $dataSource, true);
}
}
31 changes: 18 additions & 13 deletions library/Rules/PublicDomainSuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,34 @@

namespace Respect\Validation\Rules;

use Respect\Validation\Helpers\CanValidateUndefined;
use Respect\Validation\Helpers\DomainInfo;

use function array_pop;
use function explode;
use function in_array;
use function is_scalar;
use function strtoupper;

final class PublicDomainSuffix extends AbstractSearcher
final class PublicDomainSuffix extends AbstractRule
{
/**
* @var string[]
*/
private $domainInfo;

/**
* {@inheritDoc}
*/
protected function getDataSource($input = null): array
use CanValidateUndefined;

public function validate($input): bool
{
$parts = explode('.', $input);
if (!is_scalar($input)) {
return false;
}

$parts = explode('.', (string) $input);
$tld = array_pop($parts);

$domainInfo = new DomainInfo($tld);
$this->domainInfo = $domainInfo->getPublicSuffixes();
$dataSource = $domainInfo->getPublicSuffixes();
if ($this->isUndefined($input) && empty($dataSource)) {
return true;
}

return $this->domainInfo;
return in_array(strtoupper((string) $input), $dataSource, true);
}
}
1 change: 1 addition & 0 deletions tests/unit/Rules/CountryCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static function providerForValidInput(): array
public static function providerForInvalidInput(): array
{
return [
[new CountryCode(), 'ca'],
[new CountryCode(CountryCode::ALPHA2), 'USA'],
[new CountryCode(CountryCode::ALPHA3), 'US'],
[new CountryCode(CountryCode::NUMERIC), '000'],
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Rules/PublicDomainSuffixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Test\RuleTestCase;
use stdClass;

/**
* @covers \Respect\Validation\Exceptions\PublicDomainSuffixException
Expand Down Expand Up @@ -41,6 +42,9 @@ public static function providerForInvalidInput(): array
$rule = new PublicDomainSuffix();

return [
[$rule, []],
[$rule, null],
[$rule, new stdClass()],
[$rule, 'NONONONONONONONONON'],
[$rule, 'NONONONONONONONONON.uk'],
[$rule, 'invalid.com'],
Expand Down

0 comments on commit 5b7ea86

Please sign in to comment.