Problem/Motivation

$condition->condition('foobar', [1]);

works.

$condition->condition('foobar', [1, 2]);

doesn't.

Proposed resolution

There are a few options:

  1. Add an exception for array and no operator. At least there are no surprises.
  2. Add back automated IN support. ( automatic IN query support was one of several contributing factors that made SA-CORE-2014-005 easily exploitable. ).
  3. Roll the entire #2358079: Require a specific placeholder format in db_query() in order to trigger argument expansion, and require explicit 'IN' parameter for conditions back.

Remaining tasks

Decide.

User interface changes

none

API changes

Improve consistency

Data model changes

none

Comments

chx created an issue. See original summary.

pwolanin’s picture

So, it looks to me as though the condition code is lacking validation.

I agree there should be no surprise. Both should work or both shoudl fail - bet we also don't validate BETWEEN conditions which should take an array of exactly 2 elements

dawehner’s picture

Just a quick comment regarding the tone of the issue summary: http://xjmdrupal.org/blog/someone-worked-hard-on-it

chx’s picture

I know the Drupal thought police is working hard and I have made myself scarce from the issue queue for it but really? Really? We can't call useless, developer hostile features out for what they are? I have not namecalled anyone, we can't even criticize the code any more?

dawehner’s picture

@chx
This is purely about being nice to people and enable people to participate in the discussion. Here is an alternative version of the issue summary. It contains less sentences, which don't add anything to the issue itself,
while making it clearer for people to understand what is going on.

As of #2358079: Require a specific placeholder format in db_query() in order to trigger argument expansion, and require explicit 'IN' parameter for conditions db_query() requires :foo[] when you pass in an array as arguments. On top of that, it also requires to explicit define the 'IN' parameter, even when using the SELECT objects.

Let's have a look at an actual example:

// This part doesn't work.
$select->condition('foobar', [1, 2]);
// This part works.
$select->condition('foobar', [1]);
// This part works as well.
$select->condition('foobar', [1], 'IN');

There is a clear inconsistency between passing in one or multiple parameters.

Possible solutions:

  1. Don't make 'IN' requires when you pass in multiple values
  2. Make 'IN' optional again

@todo:
Describe security implications, which might be totally fine at the end.

chx’s picture

Issue summary: View changes

w/e

chx’s picture

Issue summary: View changes
chx’s picture

Issue summary: View changes
chx’s picture

Title: DBTNG condition no longer works » DBTNG/EQ condition no longer works with arrays
dawehner’s picture

Thank you

pwolanin’s picture

For my part I think solution 1 makes the most sense.

I say that based on the fact that automatic IN query support was one of several contributing factors that made SA-CORE-2014-005 easily exploitable. The developer should know whether they are looking up a single value or a list.

pwolanin’s picture

Status: Active » Needs review
StatusFileSize
new1.79 KB

Here's a rough start at adding more validation.

It's a little tricky since we access such random things for every parameter.

Status: Needs review » Needs work

The last submitted patch, 12: 2823910-12.patch, failed testing.

pwolanin’s picture

Title: DBTNG/EQ condition no longer works with arrays » DBTNG/EQ condition works inconsistently with arrays
pwolanin’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.81 KB
new885 bytes

Hmm, NULL is not scalar. Silly PHP.

Status: Needs review » Needs work

The last submitted patch, 15: 2823910-15.patch, failed testing.

pwolanin’s picture

Status: Needs work » Needs review
StatusFileSize
new2.4 KB
new597 bytes

Nice - uncovered a bug in UserStorage class

Status: Needs review » Needs work

The last submitted patch, 17: 2823910-17.patch, failed testing.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Issue tags: +Bug Smash Initiative
StatusFileSize
new2.79 KB

This came up as a daily Bug smash target

This appears like it could be still relevant in D9.5 (Correct me if I'm wrong)

Rerolled but leaving in NW as tests are needed.

Error generating interdiff as the starting patch was done a while ago. Started with #17

amber himes matz’s picture

Issue tags: +Needs tests

Adding “needs tests” tag.

smustgrave’s picture

Also before fixing the existing tests is the validation still needed?

daffie’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2.74 KB

Added the testing that was needed and made the change more simple.

daffie’s picture

StatusFileSize
new2.74 KB

Style guide fix.

daffie’s picture

StatusFileSize
new2.94 KB

Small improvement.

The last submitted patch, 34: 2823910-34.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 35: 2823910-35.patch, failed testing. View results

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new3.62 KB

Fixed the test failure for Drupal\Tests\Core\Database\ConditionTest.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

FYI please try to include interdiffs, just helps.

Ran the tests local without the fix and confirmed they failed
Failed asserting that exception of type "Drupal\Core\Database\InvalidQueryException" is thrown.

I like the idea of throwing an exception.

Since this doesn't work I don't think it needs a change record. But will leave for committer to decide that.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: 2823910-37.patch, failed testing. View results

daffie’s picture

Status: Needs work » Reviewed & tested by the community

Back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 38: 2823910-37.patch, failed testing. View results

daffie’s picture

Status: Needs work » Reviewed & tested by the community
larowlan’s picture

This needs a change notice and a release note snippet, as the behaviour change could break sites. And for that reason we'll only commit it to 10.1.x.

I'll have a go at both.

Additionally, test providers should use keyed arrays to convey additional information in the case of a fail, that can be fixed on commit though.

larowlan’s picture

I added a change notice and added the behaviour change to the draft release notes.

  • larowlan committed 330d393e on 10.1.x
    Issue #2823910 by daffie, pwolanin, smustgrave, dawehner, larowlan:...
larowlan’s picture

Fixed on commit

diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
index 621f06c24b4..798b4dc5640 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php
@@ -600,15 +600,15 @@ public function testEmptyInCondition() {
    */
   public function providerNonArrayOperatorWithArrayValueCondition() {
     return [
-      ['=', '='],
-      ['>', '>'],
-      ['<', '<'],
-      ['>=', '>='],
-      ['<=', '<='],
-      ['IS NULL', 'IS NULL'],
-      ['IS NOT NULL', 'IS NOT NULL'],
-      ['', '='],
-      [NULL, '='],
+      '=' => ['=', '='],
+      '>' => ['>', '>'],
+      '<' => ['<', '<'],
+      '>=' => ['>=', '>='],
+      '<=' => ['<=', '<='],
+      'IS NULL' => ['IS NULL', 'IS NULL'],
+      'IS NOT NULL' => ['IS NOT NULL', 'IS NOT NULL'],
+      'Empty string' => ['', '='],
+      'Not set' => [NULL, '='],
     ];
   }

Published the change record

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

neclimdul’s picture

Status: Closed (fixed) » Needs review

I'm that person who's site was broken. I have a view argument that works in 10.0 but throws this exception in 10.1. Wouldn't that sort of behavior normally be deprecated instead of break sites on a minor release?

Compromise, if count > 1, throw exception, else trigger deprecation?

neclimdul’s picture

StatusFileSize
new2.37 KB

Patch with the deprecation middle ground.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

neclimdul’s picture

Version: 11.x-dev » 10.1.x-dev

trying to fix the regression and provide a proper deprecation in 10.1.

ghost of drupal past’s picture

Add back automated IN support. ( automatic IN query support was one of several contributing factors that made SA-CORE-2014-005 easily exploitable. ).

That was in query and it is no reason to cripple the query builder. There was no need to do that then or now. We have added more Drupalisms and degraded DX for no reason whatsoever. The job of the query builder is to do the sensible thing and it can easily do so seeing an array -- it's much harder for the string thing. So my vote is: a serious analysis should be made whether #2 is really detrimental to security for I believe it is not.

catch’s picture

Status: Needs review » Reviewed & tested by the community

Since removing automatic IN() support didn't happen in this issue, putting it back should be its own issue rather than keeping going in this one.

I do think the main issue with SA-CORE-2014-005 was committing patches from needs work at midnight on Christmas eve more than trying to support array expansion as such.

#52 looks good to me. I wonder a bit if we want to trigger_error() E_USER_WARNING when count > 1 too, but if there are no known cases of this being relied upon, the exception is still clearer.

  • larowlan committed 485604fe on 11.x
    Issue #2823910 by daffie, pwolanin, smustgrave, neclimdul, larowlan,...

  • larowlan committed 7e1dcd65 on 10.1.x
    Issue #2823910 by daffie, pwolanin, smustgrave, neclimdul, larowlan,...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 11.x and cherry-picked to 10.1.x

Thanks for testing the new release before it comes out @neclimdul 💪

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.