Skip to content

Commit 9dc02b6

Browse files
vincent4vxnicolas-grekas
authored andcommitted
[Validator] Regex bypass when match is false with too big input
1 parent 7c3897b commit 9dc02b6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Constraints/RegexValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public function validate(mixed $value, Constraint $constraint)
4747
$value = ($constraint->normalizer)($value);
4848
}
4949

50-
if ($constraint->match xor preg_match($constraint->pattern, $value)) {
50+
$expectedResult = $constraint->match ? 1 : 0;
51+
52+
if (preg_match($constraint->pattern, $value) !== $expectedResult) {
5153
$this->context->buildViolation($constraint->message)
5254
->setParameter('{{ value }}', $this->formatValue($value))
5355
->setParameter('{{ pattern }}', $constraint->pattern)

Tests/Constraints/RegexValidatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,19 @@ public function __toString(): string
152152
}],
153153
];
154154
}
155+
156+
public function testMatchFalseWithTooManyBacktrackingShouldNotPass()
157+
{
158+
$value = '<'.str_repeat('a', 1000000).'<a href="javascript:alert(1)">test</a>';
159+
$pattern = '/<script|([^>]*?)(on\w+\s*=\s*(["\']).*?\3|href\s*=\s*(["\'])javascript:.*?\4)[^>]*?>/is';
160+
$constraint = new Regex(pattern: $pattern, message: 'myMessage', match: false);
161+
162+
$this->validator->validate($value, $constraint);
163+
164+
$this->buildViolation('myMessage')
165+
->setParameter('{{ value }}', '"'.$value.'"')
166+
->setParameter('{{ pattern }}', $pattern)
167+
->setCode(Regex::REGEX_FAILED_ERROR)
168+
->assertRaised();
169+
}
155170
}

0 commit comments

Comments
 (0)