I got this message after typing in address bar of my browser: example.com/?q[]
Warning: trim() expects parameter 1 to be string, array given in request_path() (line 2436 of .../includes/bootstrap.inc).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

StevenPatz’s picture

galooph’s picture

Status: Active » Needs review
FileSize
510 bytes

Cast $path as a string - $path = trim((string) $path, '/');

catch’s picture

Issue tags: +Needs tests

Fix looks right, should be simple enough to add tests for this though.

galooph’s picture

Thanks catch.

Just trying to work out where's best to add the test. Should I add another test module in /modules/simpletest/tests or add a test to one of the existing test modules? If the latter, which test module would you recommend I extend?

galooph’s picture

First stab at a test. This one should fail, then I'll submit the test together with the fix to see if it passes.

galooph’s picture

Right, worked out how to run tests locally :-)

Next try at a failing test.

galooph’s picture

Third time lucky for a failing test.

Status: Needs review » Needs work

The last submitted patch, invalid_type_of_GET_variable_test-1242472-7.patch, failed testing.

galooph’s picture

The test correctly failed, so now adding in the patch to fix the problem.

Chi’s picture

Status: Needs work » Needs review
xjm’s picture

There's some trailing whitespace in the second hunk, but this looks fine other than that.

xjm’s picture

FileSize
1.25 KB

Attached patch is a reroll of #9 with the following changes:

  • Removed trailing whitespace.
  • Removed unneeded reference to this issue.
  • Added a comment explaining why we are typecasting as a string.
xjm’s picture

Issue tags: -Needs tests

Oh, and it's got tests, so untagging. :)

galooph’s picture

Thanks for the tweaks, xjm - still finding my feet on core patches :-)

Dave Reid’s picture

Shouldn't this be fixed a little farther up in request_path()?

  if (isset($_GET['q'])) {
    // This is a request with a ?q=foo/bar query string. $_GET['q'] is
    // overwritten in drupal_path_initialize(), but request_path() is called
    // very early in the bootstrap process, so the original value is saved in
    // $path and returned in later calls.
    $path = $_GET['q'];
  }

Should maybe have an is_string() check on $_GET['q']. We shouldn't let $_GET['q'] = 'Array' which would happen with the current patch.

xjm’s picture

@Dave Reid's review is relevant to these related issues as well:
#1242060: Invalid value of page GET variable causes PHP warning
#1242480: Invalid type of $_GET[sort] variable causes PHP warning
#1226480: Invalid search query string causes PHP notice on D7 (warning in D6)

Edit: Basically, we should cast all these values as strings immediately when we assign them from $_GET.

xjm’s picture

Assigned: Unassigned » xjm
xjm’s picture

So we thought #15 might resolve all four of these issues, but I realize it doesn't (the other three tests fail). I do think @Dave Reid is correct that we should fix these issues systematically upstream rather than type-checking and typecasting in the specific places that have been found to throw errors.

I'm going to mark the others as duplicates and look into a more complete solution.

Chi’s picture

@xjm There are some reasons against combining these issues raised in that comment.

But since we are looking for a more complete solution I've marked as duplicate
#1242488: Invalid type of $_GET[target] variable causes PHP warning

Also we have rename the issue or file new one.

xjm’s picture

Title: Invalid type of $_GET[q] variable causes PHP warning » Invalid type of $_GET['foo'] variables causes PHP warnings and notices when treated as strings

For reference, an all-inclusive list of specific $_GET parameters used in core. A lot of these should only ever be strings.

  • $_GET['q']
  • $_GET['destination']
  • $_GET['language'] or custom value
  • $_GET['page']
  • $_GET['sort']
  • $_GET['order']
  • $_GET['parent']
  • $_GET['token']
  • $_GET['weight']
  • $_GET['render']
  • $_GET['link']
  • $_GET['name']
  • $_GET['trigger_actions_on_watchdog']
  • $_GET['format']
  • $_GET['theme']
  • $_GET['translation']
  • $_GET['target']
  • $_GET['pass-reset-token']
  • $_GET['name']
xjm’s picture

Status: Needs review » Needs work
sun’s picture

Status: Needs work » Closed (won't fix)

Garbage in, garbage out. Unless there's a security issue involved, this won't fix. Fix the calling code instead. Or, as in the case of the OP, enter a proper URL into your browser's address bar.

Dave Reid’s picture

Yep, I'm ok with that.

Chi’s picture

galooph’s picture

I've just watched Crell's Core Conversation update on http://groups.drupal.org/wscci and it looks like this will solve the issue.

xjm’s picture

Assigned: xjm » Unassigned
Chi’s picture

Title: Invalid type of $_GET['foo'] variables causes PHP warnings and notices when treated as strings » Invalid type of $_GET and $_POST variables causes PHP warnings and notices when treated as strings
Version: 8.0.x-dev » 8.6.x-dev
Issue summary: View changes
Status: Closed (won't fix) » Active

Garbage in, garbage out.

This case is different because we are getting garbage inside.

Unless there's a security issue involved, this won't fix.

Reopening, since we have just got a security issue involved and it is being exploited by passing array to the $_GET['q'] variable.

enter a proper URL into your browser's address bar

Unfortunately there is no way to make attackers enter a proper URL to the address bar. At present, much of Drupal sites are logging error messages like follows.
Warning: trim() expects parameter 1 to be string, array given в функции user_pass_validate()

Chi’s picture

Title: Invalid type of $_GET and $_POST variables causes PHP warnings and notices when treated as strings » Invalid type of $_GET variables causes PHP warnings and notices when treated as strings

Reverting the original title since for $_POST variables there is a separate issue. #1253858: Invalid type of $_POST['foo'] variables causes PHP warnings and notices when treated as strings

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.

catch’s picture