Skip to content

Commit c7ca33a

Browse files
committed
bug symfony#54306 Throw TransformationFailedException when there is a null bytes injection (sormes)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- Throw TransformationFailedException when there is a null bytes injection | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | - <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT On one hand, in PHP 7, DateTime::createFromFormat allows null byte injection, and on the other hand, in PHP 8, it throws a ValueError that is not caught. This PR prevents injection when using version 5.4 under PHP 7 and onwards, throwing a TransformationFailedException. Commits ------- dd3c254 Throw TransformationFailedException when there is a null bytes injection
2 parents 8391d6d + dd3c254 commit c7ca33a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public function reverseTransform($value)
118118
throw new TransformationFailedException('Expected a string.');
119119
}
120120

121+
if (str_contains($value, "\0")) {
122+
throw new TransformationFailedException('Null bytes not allowed');
123+
}
124+
121125
$outputTz = new \DateTimeZone($this->outputTimezone);
122126
$dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz);
123127

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ public function testReverseTransformEmpty()
133133
$this->assertNull($reverseTransformer->reverseTransform(''));
134134
}
135135

136+
public function testReverseTransformWithNullBytes()
137+
{
138+
$transformer = new DateTimeToStringTransformer();
139+
140+
$nullByte = \chr(0);
141+
$value = '2024-03-15 21:11:00'.$nullByte;
142+
143+
$this->expectException(TransformationFailedException::class);
144+
$this->expectExceptionMessage('Null bytes not allowed');
145+
146+
$transformer->reverseTransform($value);
147+
}
148+
136149
public function testReverseTransformWithDifferentTimezones()
137150
{
138151
$reverseTransformer = new DateTimeToStringTransformer('America/New_York', 'Asia/Hong_Kong', 'Y-m-d H:i:s');

0 commit comments

Comments
 (0)