From e0fbc7ee0237c3541e28af49189de9b0fa65fc21 Mon Sep 17 00:00:00 2001 From: Ivan Tse Date: Fri, 13 Sep 2024 14:02:49 -0400 Subject: [PATCH] [ExpressionLanguage] Fix matches to handle booleans being used as regexp --- .../ExpressionLanguage/Node/BinaryNode.php | 2 ++ .../Tests/Node/BinaryNodeTest.php | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php b/src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php index 68bce60c62862..8890d96a93d73 100644 --- a/src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php +++ b/src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php @@ -52,6 +52,8 @@ public function compile(Compiler $compiler): void if ('matches' == $operator) { if ($this->nodes['right'] instanceof ConstantNode) { $this->evaluateMatches($this->nodes['right']->evaluate([], []), ''); + } elseif ($this->nodes['right'] instanceof self && '~' !== $this->nodes['right']->attributes['operator']) { + throw new SyntaxError('The regex passed to "matches" must be a string.'); } $compiler diff --git a/src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php b/src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php index 36e3b9b4dee81..454a18132fbd8 100644 --- a/src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php +++ b/src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php @@ -221,6 +221,27 @@ public function testCompileMatchesWithInvalidRegexpAsExpression() eval('$regexp = "this is not a regexp"; '.$compiler->getSource().';'); } + public function testCompileMatchesWithBooleanBinaryNode() + { + $binaryNode = new BinaryNode('||', new ConstantNode(true), new ConstantNode(false)); + $node = new BinaryNode('matches', new ConstantNode('abc'), $binaryNode); + + $this->expectException(SyntaxError::class); + $this->expectExceptionMessage('The regex passed to "matches" must be a string'); + $compiler = new Compiler([]); + $node->compile($compiler); + } + + public function testCompileMatchesWithStringBinaryNode() + { + $binaryNode = new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b')); + $node = new BinaryNode('matches', new ConstantNode('abc'), $binaryNode); + + $compiler = new Compiler([]); + $node->compile($compiler); + $this->expectNotToPerformAssertions(); + } + public function testDivisionByZero() { $node = new BinaryNode('/', new ConstantNode(1), new ConstantNode(0));