Problem/Motivation
The following PHP warning occurs when invalid search keys are passed to the Terms parse mode.
Warning: Invalid argument supplied for foreach() in Drupal\search_api\Plugin\search_api\parse_mode\Terms->parseInput() (line 33
Steps to reproduce
- Create a search page View with a Fulltext search filter with the Parse mode of "Multiple words" (terms). Set the Views Filter identifier as "search".
- View the search page
- Pass an invalid string as the keywords. Example: For a Views Filter identifier as "search", set the url to "/some-search-page?search=1%00%C0%A7%C0%A2%252527%252522"
- The following PHP warning occurs:
Warning: Invalid argument supplied for foreach() in Drupal\search_api\Plugin\search_api\parse_mode\Terms->parseInput() (line 33
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | 3304560-8--keywords_parsing_invalid_utf8.patch | 2.88 KB | drunken monkey |
| #8 | 3304560-8--keywords_parsing_invalid_utf8--tests_only.patch | 568 bytes | drunken monkey |
Comments
Comment #2
recrit commentedthe attached patch adds an is_array() check after the preg_split()
Comment #4
recrit commentedThe failing test "IntegrationTest:: testInstallAndDefaultSetupWorking() line 116" is bit confusing since it is an empty search with no keys passed in. The change in this issue only affects the Terms parse_mode plugin. The parse_mode plugins are only called when there are keys.
Drupal\Tests\search_api_db_defaults\Functional\IntegrationTestComment #5
gertlor commentedWe have a different approach/solution for this problem, I have a patch that validates the input on the SearchApiFulltext field and gives an error message when the input is invalid.
Comment #6
recrit commented@Gertlor your patch #5 limits the coverage to only Search Api queries using Views. I suggest that we combine the 2 patches to also cover sites that may execute Search Api queries without views. The attached patch combines #5 and #2
Comment #7
recrit commentedComment #8
drunken monkeyThanks a lot for reporting this issue, and sorry it took me so long to respond!
I made the two fixes a bit more consistent, by avoiding to call
preg_split()with invalid UTF-8 strings in the first place, but also making sure we don’t try to iterate overFALSEeven if something slips through that check. I also added test coverage.Please test/review!
Comment #11
drunken monkeyMerged.
Thanks again!