Skip to content

Commit

Permalink
[Bug]: Fix domain name validation (pimcore#17685)
Browse files Browse the repository at this point in the history
* fix domain validation

* reduce condition

* apply suggestion: fix comment typo

Co-authored-by: Matthias Schuhmayer <[email protected]>

* sonarcloud tweaks

* Update PimcoreCoreExtension.php

* Apply php-cs-fixer changes

* Update PimcoreCoreExtension.php

---------

Co-authored-by: Matthias Schuhmayer <[email protected]>
Co-authored-by: kingjia90 <[email protected]>
  • Loading branch information
3 people authored Oct 8, 2024
1 parent 8a843a1 commit 0a3bd0d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 0 additions & 6 deletions bundles/CoreBundle/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,6 @@ private function addGeneralNode(ArrayNodeDefinition $rootNode): void
->end()
->scalarNode('domain')
->defaultValue('')
->validate()
->ifTrue(function ($v) {
return $v && !filter_var($v, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
})
->thenInvalid('Invalid domain name "%s"')
->end()
->end()
->booleanNode('redirect_to_maindomain')
->beforeNormalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\CoreBundle\DependencyInjection;

use InvalidArgumentException;
use Pimcore;
use Pimcore\Bundle\CoreBundle\EventListener\TranslationDebugListener;
use Pimcore\Extension\Document\Areabrick\Attribute\AsAreabrick;
Expand Down Expand Up @@ -84,7 +85,15 @@ public function loadInternal(array $config, ContainerBuilder $container): void

// set default domain for router to main domain if configured
// this will be overridden from the request in web context but is handy for CLI scripts
if (!empty($config['general']['domain'])) {
$domain = $config['general']['domain'] ?? '';
if ($domain) {
// when not an env variable, check if the domain is valid
if (
!str_starts_with($domain, 'env_') &&
!filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
) {
throw new InvalidArgumentException(sprintf('Invalid main domain name "%s"', $domain));
}
$container->setParameter('router.request_context.host', $config['general']['domain']);
}

Expand Down
4 changes: 3 additions & 1 deletion models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ public function setDomains(array|string $domains): static
$domains = Serialize::unserialize($domains);
}
array_map(static function ($domain) {
if (!filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
//replace all wildcards with a placeholder dummy string
$wildCardLessDomain = str_replace('*', 'anystring', $domain);
if (!filter_var($wildCardLessDomain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
throw new InvalidArgumentException(sprintf('Invalid domain name "%s"', $domain));
}
}, $domains);
Expand Down

0 comments on commit 0a3bd0d

Please sign in to comment.