Skip to content

Commit 74509e1

Browse files
authored
Merge pull request #9043 from sbulen/21_query_check_fix3
[2.1] Fix string literal cleanup - take 3
2 parents c471a81 + 8ea7e7d commit 74509e1

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

Sources/Subs-Db-mysql.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,14 @@ function smf_db_query($identifier, $db_string, $db_values = array(), $connection
368368

369369
// Comments that are allowed in a query are preg_removed.
370370
static $allowed_comments_from = array(
371+
'~\'\X*?\'~s',
371372
'~\s+~s',
372373
'~/\*!40001 SQL_NO_CACHE \*/~',
373374
'~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~',
374375
'~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~',
375376
);
376377
static $allowed_comments_to = array(
378+
' %s ',
377379
' ',
378380
'',
379381
'',
@@ -415,19 +417,9 @@ function smf_db_query($identifier, $db_string, $db_values = array(), $connection
415417
// First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over.
416418
if (empty($modSettings['disableQueryCheck']))
417419
{
418-
$clean = preg_split('/(?:\\\\{2})*\K(?<![\'\\\\])\'(?![\'])/', $db_string);
419-
420-
for ($i = 0; $i < count($clean); $i++)
421-
{
422-
if ($i % 2 === 1)
423-
$clean[$i] = ' %s ';
424-
}
425-
426-
$clean = trim(strtolower(preg_replace(
427-
$allowed_comments_from,
428-
$allowed_comments_to,
429-
implode('', $clean)
430-
)));
420+
// Clear out escaped backslashes & single quotes first, to make it simpler to ID & remove string literals
421+
$clean = str_replace(array('\\\\', '\\\'', '\'\''), array('', '', ''), $db_string);
422+
$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean)));
431423

432424
// Comments? We don't use comments in our queries, we leave 'em outside!
433425
if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false)

0 commit comments

Comments
 (0)