From e963fc027a05a4ce3660b395b22d74738aaf40ce Mon Sep 17 00:00:00 2001 From: Patrick McDougle Date: Sat, 12 Mar 2016 09:30:07 -0800 Subject: [PATCH 1/3] Introduce flag to prevent DNS lookups --- .../Validator/Constraints/EmailValidator.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index f804f17cabfcf..03ff1553a66a6 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -26,9 +26,15 @@ class EmailValidator extends ConstraintValidator */ private $isStrict; - public function __construct($strict = false) + /** + * @var bool + */ + private $preventDNSLookups; + + public function __construct($strict = false, $preventDNSLookups = false) { $this->isStrict = $strict; + $this->preventDNSLookups = $preventDNSLookups; } /** @@ -81,7 +87,7 @@ public function validate($value, Constraint $constraint) $host = substr($value, strpos($value, '@') + 1); // Check for host DNS resource records - if ($constraint->checkMX) { + if (!$this->preventDNSLookups && $constraint->checkMX) { if (!$this->checkMX($host)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) @@ -92,7 +98,7 @@ public function validate($value, Constraint $constraint) return; } - if ($constraint->checkHost && !$this->checkHost($host)) { + if (!$this->preventDNSLookups && $constraint->checkHost && !$this->checkHost($host)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Email::HOST_CHECK_FAILED_ERROR) From b9070e9f2b6c5fdd7452bff6a38ee2ae1bd842d8 Mon Sep 17 00:00:00 2001 From: Patrick McDougle Date: Sat, 12 Mar 2016 09:39:29 -0800 Subject: [PATCH 2/3] Utilize the prevent DNS lookup option --- .../Bundle/FrameworkBundle/DependencyInjection/Configuration.php | 1 + .../FrameworkBundle/DependencyInjection/FrameworkExtension.php | 1 + .../Bundle/FrameworkBundle/Resources/config/validator.xml | 1 + .../Tests/DependencyInjection/ConfigurationTest.php | 1 + 4 files changed, 4 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index ab55275b20b2c..e218ae3e1595c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -480,6 +480,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode) ->end() ->scalarNode('translation_domain')->defaultValue('validators')->end() ->booleanNode('strict_email')->defaultFalse()->end() + ->booleanNode('prevent_dns_lookups')->defaultFalse()->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 324915ecb211d..1bd4a07053a93 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -770,6 +770,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder $definition = $container->findDefinition('validator.email'); $definition->replaceArgument(0, $config['strict_email']); + $definition->replaceArgument(1, $config['prevent_dns_lookups']); if (array_key_exists('enable_annotations', $config) && $config['enable_annotations']) { $validatorBuilder->addMethodCall('enableAnnotationMapping', array(new Reference('annotation_reader'))); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml index ce317f35e1abc..b35efaa9bcbb7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml @@ -48,6 +48,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 9a73e95890d19..e03d103af3c23 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -211,6 +211,7 @@ protected static function getBundleDefaultConfig() 'static_method' => array('loadValidatorMetadata'), 'translation_domain' => 'validators', 'strict_email' => false, + 'prevent_dns_lookups' => false, ), 'annotations' => array( 'cache' => 'file', From 4d10fcaaf23c556c02adb8367b8c698867c3a85a Mon Sep 17 00:00:00 2001 From: Patrick McDougle Date: Sun, 13 Mar 2016 17:08:25 -0700 Subject: [PATCH 3/3] Address PR Comments --- .../DependencyInjection/Configuration.php | 2 +- .../FrameworkExtension.php | 2 +- .../DependencyInjection/ConfigurationTest.php | 2 +- .../Validator/Constraints/EmailValidator.php | 34 ++++++++++--------- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index e218ae3e1595c..40115dcc09f46 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -480,7 +480,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode) ->end() ->scalarNode('translation_domain')->defaultValue('validators')->end() ->booleanNode('strict_email')->defaultFalse()->end() - ->booleanNode('prevent_dns_lookups')->defaultFalse()->end() + ->booleanNode('use_dns')->defaultTrue()->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 1bd4a07053a93..adb7dac39afda 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -770,7 +770,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder $definition = $container->findDefinition('validator.email'); $definition->replaceArgument(0, $config['strict_email']); - $definition->replaceArgument(1, $config['prevent_dns_lookups']); + $definition->replaceArgument(1, $config['use_dns']); if (array_key_exists('enable_annotations', $config) && $config['enable_annotations']) { $validatorBuilder->addMethodCall('enableAnnotationMapping', array(new Reference('annotation_reader'))); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index e03d103af3c23..0ed513d50ebbc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -211,7 +211,7 @@ protected static function getBundleDefaultConfig() 'static_method' => array('loadValidatorMetadata'), 'translation_domain' => 'validators', 'strict_email' => false, - 'prevent_dns_lookups' => false, + 'use_dns' => true, ), 'annotations' => array( 'cache' => 'file', diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index 03ff1553a66a6..3eb6f4194c08c 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -29,12 +29,12 @@ class EmailValidator extends ConstraintValidator /** * @var bool */ - private $preventDNSLookups; + private $useDNS; - public function __construct($strict = false, $preventDNSLookups = false) + public function __construct($strict = false, $useDNS = true) { $this->isStrict = $strict; - $this->preventDNSLookups = $preventDNSLookups; + $this->useDNS = $useDNS; } /** @@ -86,23 +86,25 @@ public function validate($value, Constraint $constraint) $host = substr($value, strpos($value, '@') + 1); - // Check for host DNS resource records - if (!$this->preventDNSLookups && $constraint->checkMX) { - if (!$this->checkMX($host)) { + if ($this->useDNS) { + // Check for host DNS resource records + if ($constraint->checkMX) { + if (!$this->checkMX($host)) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(Email::MX_CHECK_FAILED_ERROR) + ->addViolation(); + } + + return; + } + + if ($constraint->checkHost && !$this->checkHost($host)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(Email::MX_CHECK_FAILED_ERROR) + ->setCode(Email::HOST_CHECK_FAILED_ERROR) ->addViolation(); } - - return; - } - - if (!$this->preventDNSLookups && $constraint->checkHost && !$this->checkHost($host)) { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(Email::HOST_CHECK_FAILED_ERROR) - ->addViolation(); } }