Skip to content

Commit 409862f

Browse files
committed
Parser: Don't swallow chars if a logical operator is missing
1 parent 9020afc commit 409862f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Filter/Parser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ protected function readFilters($nestingLevel = 0, $op = null, $filters = null, $
134134
$this->pos--;
135135
} else {
136136
$this->termIndex++;
137+
$next = $this->nextChar();
138+
if ($next !== false && ! in_array($next, ['&', '|', ')'])) {
139+
$this->pos++;
140+
$this->parseError($next, 'Expected logical operator');
141+
}
137142
}
138143

139144
break;
@@ -227,6 +232,11 @@ protected function readFilters($nestingLevel = 0, $op = null, $filters = null, $
227232
$this->pos--;
228233
} else {
229234
$this->termIndex++;
235+
$next = $this->nextChar();
236+
if ($next !== false && ! in_array($next, ['&', '|', ')'])) {
237+
$this->pos++;
238+
$this->parseError($next, 'Expected logical operator');
239+
}
230240
}
231241

232242
break;

tests/ParserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,31 @@
33
namespace ipl\Tests\Web;
44

55
use ipl\Stdlib\Filter;
6+
use ipl\Web\Filter\ParseException;
67
use ipl\Web\Filter\Parser;
78

89
class ParserTest extends TestCase
910
{
11+
public function testMissingLogicalOperatorsAfterConditionsAreDetected()
12+
{
13+
$this->expectException(ParseException::class);
14+
$this->expectExceptionMessage(
15+
'Invalid filter "(a=b|c=d)e=f", unexpected e at pos 10: Expected logical operator'
16+
);
17+
18+
(new Parser('(a=b|c=d)e=f'))->parse();
19+
}
20+
21+
public function testMissingLogicalOperatorsAfterOperatorsAreDetected()
22+
{
23+
$this->expectException(ParseException::class);
24+
$this->expectExceptionMessage(
25+
'Invalid filter "(a=b|c=d|)e=f", unexpected e at pos 11: Expected logical operator'
26+
);
27+
28+
(new Parser('(a=b|c=d|)e=f'))->parse();
29+
}
30+
1031
public function testSimpleIndexRecognition()
1132
{
1233
$filter = (new Parser('a=b&c=d|e=|g'))->parse();

0 commit comments

Comments
 (0)