Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 128efbb

Browse filesBrowse files
committed
minor #58261 [ExpressionLanguage] Fix matches to handle booleans being used as regexp (ivantsepp)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [ExpressionLanguage] Fix matches to handle booleans being used as regexp | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | n/a | License | MIT I'm marking this as a bug fix but this could be seen as a "new feature" as existing `matches` syntax functionality works just fine. I wanted to build on top of the work done at #45875. This handles invalid regular expressions and also does an extra check inside the `compile` method. It checks the right hand side for a `ConstantNode` and validates that it's a valid regexp. The idea is that we can go **even further** and check for `BinaryNode` because in most cases, this is an invalid regexp since a boolean usually returned. The exception is `~` which is for string concatenation since that could result in a valid regexp. This extra check could help prevent invalid expressions like `"a" matches ("/a/" || "/b/")` where one could mistake `"/a/" || "/b/"` as being a valid regexp (when the correct approach would've been `"a" matches "/a|b/"`) Commits ------- e0fbc7e [ExpressionLanguage] Fix matches to handle booleans being used as regexp
2 parents 85380cf + e0fbc7e commit 128efbb
Copy full SHA for 128efbb

File tree

Expand file treeCollapse file tree

2 files changed

+23
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+23
-0
lines changed

‎src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function compile(Compiler $compiler): void
5252
if ('matches' == $operator) {
5353
if ($this->nodes['right'] instanceof ConstantNode) {
5454
$this->evaluateMatches($this->nodes['right']->evaluate([], []), '');
55+
} elseif ($this->nodes['right'] instanceof self && '~' !== $this->nodes['right']->attributes['operator']) {
56+
throw new SyntaxError('The regex passed to "matches" must be a string.');
5557
}
5658

5759
$compiler

‎src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ public function testCompileMatchesWithInvalidRegexpAsExpression()
224224
eval('$regexp = "this is not a regexp"; '.$compiler->getSource().';');
225225
}
226226

227+
public function testCompileMatchesWithBooleanBinaryNode()
228+
{
229+
$binaryNode = new BinaryNode('||', new ConstantNode(true), new ConstantNode(false));
230+
$node = new BinaryNode('matches', new ConstantNode('abc'), $binaryNode);
231+
232+
$this->expectException(SyntaxError::class);
233+
$this->expectExceptionMessage('The regex passed to "matches" must be a string');
234+
$compiler = new Compiler([]);
235+
$node->compile($compiler);
236+
}
237+
238+
public function testCompileMatchesWithStringBinaryNode()
239+
{
240+
$binaryNode = new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'));
241+
$node = new BinaryNode('matches', new ConstantNode('abc'), $binaryNode);
242+
243+
$compiler = new Compiler([]);
244+
$node->compile($compiler);
245+
$this->expectNotToPerformAssertions();
246+
}
247+
227248
public function testDivisionByZero()
228249
{
229250
$node = new BinaryNode('/', new ConstantNode(1), new ConstantNode(0));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.