Skip to content

Commit

Permalink
minor #31681 [Validator] Remove checkDNS option in Url (ro0NL)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.0-dev branch.

Discussion
----------

[Validator] Remove checkDNS option in Url

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

See #25516

Commits
-------

885703f [Validator] Remove checkDNS option in Url
  • Loading branch information
nicolas-grekas committed May 29, 2019
2 parents 80f5850 + 885703f commit 52756a5
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 219 deletions.
84 changes: 0 additions & 84 deletions src/Symfony/Component/Validator/Constraints/Url.php
Expand Up @@ -22,103 +22,19 @@
*/
class Url extends Constraint
{
/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_ANY = 'ANY';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_NONE = false;

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_A = 'A';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_A6 = 'A6';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_AAAA = 'AAAA';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_CNAME = 'CNAME';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_MX = 'MX';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_NAPTR = 'NAPTR';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_NS = 'NS';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_PTR = 'PTR';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_SOA = 'SOA';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_SRV = 'SRV';

/**
* @deprecated since Symfony 4.1
*/
const CHECK_DNS_TYPE_TXT = 'TXT';

const INVALID_URL_ERROR = '57c2f299-1154-4870-89bb-ef3b1f5ad229';

protected static $errorNames = [
self::INVALID_URL_ERROR => 'INVALID_URL_ERROR',
];

public $message = 'This value is not a valid URL.';

/**
* @deprecated since Symfony 4.1
*/
public $dnsMessage = 'The host could not be resolved.';
public $protocols = ['http', 'https'];

/**
* @deprecated since Symfony 4.1
*/
public $checkDNS = self::CHECK_DNS_TYPE_NONE;
public $relativeProtocol = false;
public $normalizer;

public function __construct($options = null)
{
if (\is_array($options)) {
if (\array_key_exists('checkDNS', $options)) {
@trigger_error(sprintf('The "checkDNS" option in "%s" is deprecated since Symfony 4.1. Its false-positive rate is too high to be relied upon.', self::class), E_USER_DEPRECATED);
}
if (\array_key_exists('dnsMessage', $options)) {
@trigger_error(sprintf('The "dnsMessage" option in "%s" is deprecated since Symfony 4.1.', self::class), E_USER_DEPRECATED);
}
}

parent::__construct($options);

if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
Expand Down
29 changes: 0 additions & 29 deletions src/Symfony/Component/Validator/Constraints/UrlValidator.php
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

Expand Down Expand Up @@ -77,33 +76,5 @@ public function validate($value, Constraint $constraint)

return;
}

if ($constraint->checkDNS) {
if (!\in_array($constraint->checkDNS, [
Url::CHECK_DNS_TYPE_ANY,
Url::CHECK_DNS_TYPE_A,
Url::CHECK_DNS_TYPE_A6,
Url::CHECK_DNS_TYPE_AAAA,
Url::CHECK_DNS_TYPE_CNAME,
Url::CHECK_DNS_TYPE_MX,
Url::CHECK_DNS_TYPE_NAPTR,
Url::CHECK_DNS_TYPE_NS,
Url::CHECK_DNS_TYPE_PTR,
Url::CHECK_DNS_TYPE_SOA,
Url::CHECK_DNS_TYPE_SRV,
Url::CHECK_DNS_TYPE_TXT,
], true)) {
throw new InvalidOptionsException(sprintf('Invalid value for option "checkDNS" in constraint %s', \get_class($constraint)), ['checkDNS']);
}

$host = parse_url($value, PHP_URL_HOST);

if (!\is_string($host) || !checkdnsrr($host, $constraint->checkDNS)) {
$this->context->buildViolation($constraint->dnsMessage)
->setParameter('{{ value }}', $this->formatValue($host))
->setCode(Url::INVALID_URL_ERROR)
->addViolation();
}
}
}
}
106 changes: 0 additions & 106 deletions src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php
Expand Up @@ -11,14 +11,10 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Bridge\PhpUnit\DnsMock;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\UrlValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @group dns-sensitive
*/
class UrlValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
Expand Down Expand Up @@ -283,108 +279,6 @@ public function getValidCustomUrls()
['git://[::1]/'],
];
}

/**
* @dataProvider getCheckDns
* @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
* @group legacy
* @expectedDeprecation The "checkDNS" option in "Symfony\Component\Validator\Constraints\Url" is deprecated since Symfony 4.1. Its false-positive rate is too high to be relied upon.
*/
public function testCheckDns($violation)
{
DnsMock::withMockedHosts(['example.com' => [['type' => $violation ? '' : 'A']]]);

$constraint = new Url([
'checkDNS' => 'ANY',
'dnsMessage' => 'myMessage',
]);

$this->validator->validate('http://example.com', $constraint);

if (!$violation) {
$this->assertNoViolation();
} else {
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"example.com"')
->setCode(Url::INVALID_URL_ERROR)
->assertRaised();
}
}

public function getCheckDns()
{
return [[true], [false]];
}

/**
* @dataProvider getCheckDnsTypes
* @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
* @group legacy
* @expectedDeprecation The "checkDNS" option in "Symfony\Component\Validator\Constraints\Url" is deprecated since Symfony 4.1. Its false-positive rate is too high to be relied upon.
*/
public function testCheckDnsByType($type)
{
DnsMock::withMockedHosts(['example.com' => [['type' => $type]]]);

$constraint = new Url([
'checkDNS' => $type,
'dnsMessage' => 'myMessage',
]);

$this->validator->validate('http://example.com', $constraint);

$this->assertNoViolation();
}

public function getCheckDnsTypes()
{
return [
['ANY'],
['A'],
['A6'],
['AAAA'],
['CNAME'],
['MX'],
['NAPTR'],
['NS'],
['PTR'],
['SOA'],
['SRV'],
['TXT'],
];
}

/**
* @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
* @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
* @group legacy
* @expectedDeprecation The "checkDNS" option in "Symfony\Component\Validator\Constraints\Url" is deprecated since Symfony 4.1. Its false-positive rate is too high to be relied upon.
* @expectedDeprecation The "dnsMessage" option in "Symfony\Component\Validator\Constraints\Url" is deprecated since Symfony 4.1.
*/
public function testCheckDnsWithInvalidType()
{
DnsMock::withMockedHosts(['example.com' => [['type' => 'A']]]);

$constraint = new Url([
'checkDNS' => 'BOGUS',
'dnsMessage' => 'myMessage',
]);

$this->validator->validate('http://example.com', $constraint);
}

/**
* @group legacy
* @expectedDeprecation The "checkDNS" option in "Symfony\Component\Validator\Constraints\Url" is deprecated since Symfony 4.1. Its false-positive rate is too high to be relied upon.
*/
public function testCheckDnsOptionIsDeprecated()
{
$constraint = new Url([
'checkDNS' => Url::CHECK_DNS_TYPE_NONE,
]);

$this->validator->validate('http://example.com', $constraint);
}
}

class EmailProvider
Expand Down

0 comments on commit 52756a5

Please sign in to comment.