feat: check malformed domain names

This commit is contained in:
Maël Gangloff
2025-10-16 14:16:58 +02:00
parent a20344816c
commit a143039925
8 changed files with 77 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Config\EventAction;
use App\Exception\MalformedDomainException;
use App\Repository\DomainRepository;
use App\Service\RDAPService;
use App\State\AutoRegisterDomainProvider;
@@ -177,6 +178,9 @@ class Domain
return $this->ldhName;
}
/**
* @throws MalformedDomainException
*/
public function setLdhName(string $ldhName): static
{
$this->ldhName = RDAPService::convertToIdn($ldhName);

View File

@@ -6,6 +6,6 @@ class MalformedDomainException extends \Exception
{
public static function fromDomain(string $ldhName): self
{
return new self("Domain name ($ldhName) must contain at least one dot");
return new self("Malformed domain name ($ldhName)");
}
}

View File

@@ -197,9 +197,18 @@ class RDAPService
return $tldEntity;
}
/**
* @throws MalformedDomainException
*/
public static function convertToIdn(string $fqdn): string
{
return strtolower(idn_to_ascii($fqdn));
$ascii = strtolower(idn_to_ascii($fqdn));
if (OfficialDataService::DOMAIN_DOT !== $fqdn && !preg_match('/^(xn--)?[a-z0-9-]+(\.[a-z0-9-]+)*$/', $ascii)) {
throw MalformedDomainException::fromDomain($fqdn);
}
return $ascii;
}
/**

View File

@@ -52,7 +52,6 @@ readonly class AutoRegisterDomainProvider implements ProviderInterface
if (null !== $domain
&& !$domain->getDeleted()
&& !$domain->isToBeUpdated(true, true)
&& !$this->kernel->isDebug()
&& ($request && !filter_var($request->get('forced', false), FILTER_VALIDATE_BOOLEAN))
) {
$this->logger->debug('It is not necessary to update the domain name', [