PHP throws ValueError when DateTimeImmutable::createFromFormat() receives a NUL byte. In affected Symfony versions, the DateTime validator can let that exception escape. Reject NUL bytes first and run the format check only when the first constraint passes.
Last updated: November 19, 2026.
<?php
namespace App\Dto;
use Symfony\Component\Validator\Constraints as Assert;
final class ScheduleInput
{
#[Assert\Sequentially([
new Assert\Regex(
pattern: '/\x00/',
match: false,
message: 'The date contains an invalid control character.',
),
new Assert\DateTime(format: 'Y-m-d H:i:s'),
])]
public string $startsAt = '';
}Sequentially stops after the Regex violation, so the DateTime constraint never receives the dangerous value. The pattern rejects the NUL character; it does not silently remove or normalize it.
Why constraint order matters
Symfony’s Sequentially constraint applies its nested rules in order and stops at the first violation. Two independent property constraints do not provide that guarantee. The initial Regex rule therefore must be inside the same ordered sequence as the documented DateTime constraint.
The PHP manual documents that DateTimeImmutable::createFromFormat() throws ValueError for input containing NUL bytes. A current Symfony report reproduces the uncaught exception with Symfony 6.4.50 and PHP 8.4.27. That report is version-specific and under review; check the release notes before removing the guard after an upgrade.
Treat hostile input as a validation error
Do not strip the byte and continue parsing. Changing hostile input can turn one value into another that appears valid. Return the normal validation message and keep detailed exception data out of the response. If the date is optional, remember that Symfony’s DateTime constraint accepts null and an empty string; add NotBlank when the field is required.
Apply the same boundary check to raw JSON, form values and command messages before using PHP date APIs directly. Add tests containing an actual "\0" byte, an empty value, a correctly formatted date and an impossible date. Assert that the hostile value produces a constraint violation rather than an exception, while the valid value reaches the application unchanged.