Skip to content

Commit e6e235b

Browse files
committed
bug symfony#25781 [Form] Disallow transform dates beyond the year 9999 (curry684)
This PR was squashed before being merged into the 2.7 branch (closes symfony#25781). Discussion ---------- [Form] Disallow transform dates beyond the year 9999 Fixes symfony#14727 | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | not really | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#14727 | License | MIT Explicitly locked out submission of dates beyond December 31st 9999 in forms as PHP is highly incapable of consistently handling such dates. Before this patch dates were randomly transformed or mangled. Technically there is a BC break as this will now cause validation to fail on input that was *accepted* before, but it was mangled. Not my call but I prefer the rejection over data corruption: ``` // Old behavior $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd'); $result = $transformer->reverseTransform('20107-03-21'); // $result is now 2007-03-21 ``` Commits ------- 70cc969 [Form] Disallow transform dates beyond the year 9999
2 parents 3ed19dd + 70cc969 commit e6e235b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public function reverseTransform($value)
123123

124124
if (0 != intl_get_error_code()) {
125125
throw new TransformationFailedException(intl_get_error_message());
126+
} elseif ($timestamp > 253402214400) {
127+
// This timestamp represents UTC midnight of 9999-12-31 to prevent 5+ digit years
128+
throw new TransformationFailedException('Years beyond 9999 are not supported.');
126129
}
127130

128131
try {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,22 @@ public function testReverseTransformOutOfTimestampRange()
343343
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC');
344344
$transformer->reverseTransform('1789-07-14');
345345
}
346+
347+
/**
348+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
349+
*/
350+
public function testReverseTransformFiveDigitYears()
351+
{
352+
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
353+
$transformer->reverseTransform('20107-03-21');
354+
}
355+
356+
/**
357+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
358+
*/
359+
public function testReverseTransformFiveDigitYearsWithTimestamp()
360+
{
361+
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss');
362+
$transformer->reverseTransform('20107-03-21 12:34:56');
363+
}
346364
}

0 commit comments

Comments
 (0)