- Issue created by @enchufe
- @enchufe opened merge request.
- Status changed to Needs review
about 2 years ago 8:38am 5 April 2023 - 🇮🇳India sakthi_dev
Instead of hard coding (casting the variable), suggested another method based on value. Please review.
- 🇪🇸Spain enchufe Spain
#3 Your patch seems to be more secure by adding additional checks for variables. Do I manually add it to my MR or is there a way to merge the patch? I'm relatively new to contributing code to Drupal.
- 🇮🇳India sakthi_dev
Pushed the changes to the same branch. Please review.
- Status changed to Needs work
about 2 years ago 2:18pm 5 April 2023 - 🇺🇸United States smustgrave
Tickets like these appear to be covering up a large problem by addressing the symptom.
Did not verify the issue in the issue summary (but thanks for the steps!)
This will need a test case to show the problem.
- 🇫🇷France Chris64 France
$operator ?? ''
instead of
!empty($operator) ? $operator : ''
- First commit to issue fork.
- 🇫🇷France Chris64 France
Same kind of things for the other line,
- if (stripos($condition['operator'], 'UNION') !== FALSE || strpbrk($condition['operator'], '[-\'"();') !== FALSE) { + if (stripos(!empty($condition['operator']) ? $condition['operator']: '', 'UNION') !== FALSE || strpbrk(!empty($condition['operator']) ? $condition['operator']: '', '[-\'"();') !== FALSE) {
but in this case rather,
if (isset($condition['operator']) && (stripos($condition['operator'], 'UNION') !== FALSE || strpbrk($condition['operator'], '[-\'"();') !== FALSE)) {
But there is some thing weird here: did
$condition['operator']
may be null? Is it a regular case? Since we are here because$ignore_operato
r isFALSE
, and if!isset($condition['operator'])
then$ignore_operator = TRUE
, exceptif ($condition['field'] instanceof ConditionInterface)
. If then$ignore_operator = $condition['operator'] === '=' && $condition['value'] === NULL
, henceFALSE
if$condition['operator']
is null. On an other hand, after,$operator = $connection->mapConditionOperator($condition['operator']);
$operator += ['operator' => $condition['operator']];
hence null is passed. On an other hand, if$ignore_operator
isTRUE
then$operator = ['operator' => '', 'use_value' => FALSE];
, looking nice. We may think that in all cases if!isset($condition['operator'])
then$ignore_operator = TRUE
, and we can not be here. - 🇫🇷France Chris64 France
Yes indeed. I've changed it.
For the other after thinking should be enough,
if ($condition['operator'] !== NULL && (stripos($condition['operator'], 'UNION') !== FALSE || strpbrk($condition['operator'], '[-\'"();') !== FALSE)) {
- First commit to issue fork.
- 🇪🇸Spain enchufe Spain
Wouldn't it be more efficient to use
$condition['operator'] ?? ''
instead of!empty($condition['operator']) ? $condition['operator'] : ''
? The empty function and the ternary operator are two steps, while the null merge operator (??) is a native PHP operator. - 🇫🇷France Chris64 France
@enchufe I am agree that is better. But I think #13 is better. Since
!== FALSE
means exists, and if so thenTRUE
. On the other hand, if$condition['operator'] === NULL
then it can not exist, and the condition isFALSE
. The problem comes from$condition['operator'] === NULL
.But in the exemple,
$query2 = $this->database->condition('t.field', 'value', '=');
, the operator should be '='. Then why$condition['operator'] === NULL
? This after #11.About the test of #7 it should fail without the patch and succeed with the patch.
Did some tests exist for the function with the problem? The function is
compile
incore/lib/Drupal/Core/Database/Query/Condition.php
. May be intests/Drupal/Tests/Core/Database/ConditionTest.php
. However nothing seems provide forNULL
operator. And'IS NULL'
operator is some thing else. - 🇪🇸Spain enchufe Spain
So, the current MR is slightly safer by first checking if the variable is defined and not null before applying PHP functions. In my specific case it works correctly.
- Status changed to Closed: works as designed
about 2 years ago 12:31pm 1 May 2023 - 🇳🇱Netherlands daffie
$query = $this->database->select('table', 't'); $query->fields('t', ['column_1', 'column_2']); $query2 = $this->database->condition('t.field', 'value', '='); $query->where($query2); $query->execute()->fetchAll();
The given example from the issue summary fails, because the function
where()
only accepts string values. In the given example an instance of a Condition object is not a string value.It works as designed! Use
condition()
instead ofwhere()
.