bug #28466 [Form] fail reverse transforming invalid RFC 3339 dates (xabbuh)

This PR was merged into the 2.8 branch.

Discussion
----------

[Form] fail reverse transforming invalid RFC 3339 dates

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28455
| License       | MIT
| Doc PR        |

Commits
-------

ee4ce43e91 fail reverse transforming invalid RFC 3339 dates
This commit is contained in:
Nicolas Grekas 2018-09-22 20:44:32 +02:00
commit 57a34131c6
2 changed files with 21 additions and 6 deletions

View File

@ -69,6 +69,10 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
return;
}
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})T\d{2}:\d{2}(?::\d{2})?(?:\.\d)?(?:Z|(?:(?:\+|-)\d{2}:\d{2}))$/', $rfc3339, $matches)) {
throw new TransformationFailedException(sprintf('The date "%s" is not a valid date.', $rfc3339));
}
try {
$dateTime = new \DateTime($rfc3339);
} catch (\Exception $e) {
@ -79,10 +83,8 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
}
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) {
if (!checkdate($matches[2], $matches[3], $matches[1])) {
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
}
if (!checkdate($matches[2], $matches[3], $matches[1])) {
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
}
return $dateTime;

View File

@ -134,12 +134,25 @@ class DateTimeToRfc3339TransformerTest extends TestCase
}
/**
* @dataProvider invalidDateStringProvider
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformExpectsValidDateString()
public function testReverseTransformExpectsValidDateString($date)
{
$transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC');
$transformer->reverseTransform('2010-2010-2010');
$transformer->reverseTransform($date);
}
public function invalidDateStringProvider()
{
return array(
'invalid month' => array('2010-2010-01'),
'invalid day' => array('2010-10-2010'),
'no date' => array('x'),
'cookie format' => array('Saturday, 01-May-2010 04:05:00 Z'),
'RFC 822 format' => array('Sat, 01 May 10 04:05:00 +0000'),
'RSS format' => array('Sat, 01 May 2010 04:05:00 +0000'),
);
}
}