Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee4ce43

Browse files
committed
fail reverse transforming invalid RFC 3339 dates
1 parent d5a366f commit ee4ce43

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function reverseTransform($rfc3339)
6969
return;
7070
}
7171

72+
if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})T\d{2}:\d{2}(?::\d{2})?(?:\.\d)?(?:Z|(?:(?:\+|-)\d{2}:\d{2}))$/', $rfc3339, $matches)) {
73+
throw new TransformationFailedException(sprintf('The date "%s" is not a valid date.', $rfc3339));
74+
}
75+
7276
try {
7377
$dateTime = new \DateTime($rfc3339);
7478
} catch (\Exception $e) {
@@ -79,10 +83,8 @@ public function reverseTransform($rfc3339)
7983
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
8084
}
8185

82-
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) {
83-
if (!checkdate($matches[2], $matches[3], $matches[1])) {
84-
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
85-
}
86+
if (!checkdate($matches[2], $matches[3], $matches[1])) {
87+
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
8688
}
8789

8890
return $dateTime;

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToRfc3339TransformerTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,25 @@ public function testReverseTransformWithNonExistingDate()
134134
}
135135

136136
/**
137+
* @dataProvider invalidDateStringProvider
137138
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
138139
*/
139-
public function testReverseTransformExpectsValidDateString()
140+
public function testReverseTransformExpectsValidDateString($date)
140141
{
141142
$transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC');
142143

143-
$transformer->reverseTransform('2010-2010-2010');
144+
$transformer->reverseTransform($date);
145+
}
146+
147+
public function invalidDateStringProvider()
148+
{
149+
return array(
150+
'invalid month' => array('2010-2010-01'),
151+
'invalid day' => array('2010-10-2010'),
152+
'no date' => array('x'),
153+
'cookie format' => array('Saturday, 01-May-2010 04:05:00 Z'),
154+
'RFC 822 format' => array('Sat, 01 May 10 04:05:00 +0000'),
155+
'RSS format' => array('Sat, 01 May 2010 04:05:00 +0000'),
156+
);
144157
}
145158
}

0 commit comments

Comments
 (0)