Skip to content

Commit 79fb554

Browse files
committed
#10: OR filter bypasses all doctrine extensions -> Potential security problem
- Applies workaround only with OR
1 parent e60889a commit 79fb554

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/Filter/FilterLogic.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,24 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
122122
$this->replaceInnerJoinsByLeftJoins($queryBuilder);
123123
}
124124

125-
// if $existingWhere empty no problem
126-
// if $filterWhere empty nest OR in an extra AND
127-
if (empty($existingWhere) || empty($filterWhere) ) {
125+
// if $existingWhere empty it does not matter how applied
126+
// if combinator == AND no problem
127+
// if $filterWhere empty use andWhere
128+
if (empty($existingWhere) || empty($filterWhere) || $combinator == 'AND') {
128129
$queryBuilder->andWhere($logicExp);
129130
return;
130131
}
131132
// elseif only criteria from filters, apply according to operator
132133
if ($existingWhere == $filterWhere) {
133-
if ($combinator == 'OR') {
134-
$queryBuilder->orWhere($logicExp);
135-
} else {
136-
$queryBuilder->andWhere($logicExp);
137-
}
134+
$queryBuilder->orWhere($logicExp);
138135
return;
139136
}
140-
// elseif criteria from filters follow AND, replace them
137+
138+
// Criteria from both extensions and filters, should OR only with those from filters,
139+
// replace them if criteria from filters follow AND
141140
if(false!==strpos($existingWhere, " AND $filterWhere")) {
142141
$queryBuilder->add('where',
143-
str_replace($filterWhere, "($filterWhere $combinator ($logicExp))", $existingWhere)
142+
str_replace($filterWhere, "($filterWhere OR ($logicExp))", $existingWhere)
144143
);
145144
return;
146145
}

tests/Filter/FilterLogicWithAnnotationTest.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ public function testDdFilterOr()
133133
'Parameter dd_p2');
134134
}
135135

136-
public function testDdFilterAndWithExtsionCriterium()
136+
public function testDdFilterAndWithExtsionCriteria()
137137
{
138-
$this->testEntityQb->andWhere('o.numb >= 0');
138+
$this->testEntityQb->orWhere('o.numb >= 0');
139+
$this->testEntityQb->orWhere('o.numb <= 999');
139140
$reqData = null;
140141
parse_str('exists[bool]=true&and[or][dd][after]=2021-01-01', $reqData);
141142
// var_dump($reqData);
@@ -147,10 +148,10 @@ public function testDdFilterAndWithExtsionCriterium()
147148
$this->assertEquals(
148149
str_replace('
149150
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o WHERE
150-
o.numb >= 0 AND (
151+
(o.numb >= 0 OR o.numb <= 999) AND
151152
o.bool IS NOT NULL
152153
AND (o.dd >= :dd_p1 OR o.dd IS NULL)
153-
)"),
154+
"),
154155
$this->testEntityQb->getDQL(),
155156
'DQL');
156157
$this->assertEquals(
@@ -160,9 +161,10 @@ public function testDdFilterAndWithExtsionCriterium()
160161

161162
}
162163

163-
public function testDdFilterNotWithExtsionCriterium()
164+
public function testDdFilterNotWithExtsionCriteria()
164165
{
165-
$this->testEntityQb->andWhere('o.numb >= 0');
166+
$this->testEntityQb->orWhere('o.numb >= 0');
167+
$this->testEntityQb->orWhere('o.numb <= 999');
166168
$reqData = null;
167169
parse_str('exists[bool]=true&not[dd][after]=2021-01-01', $reqData);
168170
// var_dump($reqData);
@@ -174,9 +176,9 @@ public function testDdFilterNotWithExtsionCriterium()
174176
$this->assertEquals(
175177
str_replace('
176178
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o WHERE
177-
o.numb >= 0 AND (
179+
(o.numb >= 0 OR o.numb <= 999) AND
178180
o.bool IS NOT NULL
179-
AND (NOT(o.dd >= :dd_p1 OR o.dd IS NULL)))"),
181+
AND (NOT(o.dd >= :dd_p1 OR o.dd IS NULL))"),
180182
$this->testEntityQb->getDQL(),
181183
'DQL');
182184
$this->assertEquals(
@@ -185,9 +187,10 @@ public function testDdFilterNotWithExtsionCriterium()
185187
'Parameter dd_p1');
186188
}
187189

188-
public function testDdFilterOrWithExtsionCriterium()
190+
public function testDdFilterOrWithExtsionCriteria()
189191
{
190-
$this->testEntityQb->andWhere('o.numb >= 0');
192+
$this->testEntityQb->orWhere('o.numb >= 0');
193+
$this->testEntityQb->orWhere('o.numb <= 999');
191194
$reqData = null;
192195
parse_str('exists[bool]=true&or[dd][after]=2021-01-01&or[dd][before]=2010-02-02', $reqData);
193196
// var_dump($reqData);
@@ -199,7 +202,7 @@ public function testDdFilterOrWithExtsionCriterium()
199202
$this->assertEquals(
200203
str_replace('
201204
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o WHERE
202-
o.numb >= 0 AND (
205+
(o.numb >= 0 OR o.numb <= 999) AND (
203206
o.bool IS NOT NULL
204207
OR (
205208
(o.dd <= :dd_p1 AND o.dd IS NOT NULL)

0 commit comments

Comments
 (0)