Skip to content

Commit c759013

Browse files
Merge pull request SimpleMachines#8992 from Sesquipedalian/2.1/query_clean
[2.1] Simplifies code in db query check
2 parents aa1499f + fe407fd commit c759013

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Sources/Subs-Db-mysql.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,12 @@ 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*?(?<![\'\\\\])\'~',
372371
'~\s+~s',
373372
'~/\*!40001 SQL_NO_CACHE \*/~',
374373
'~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~',
375374
'~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~',
376375
);
377376
static $allowed_comments_to = array(
378-
' %s ',
379377
' ',
380378
'',
381379
'',
@@ -417,7 +415,19 @@ function smf_db_query($identifier, $db_string, $db_values = array(), $connection
417415
// First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over.
418416
if (empty($modSettings['disableQueryCheck']))
419417
{
420-
$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $db_string)));
418+
$clean = preg_split('/(?<![\'\\\\])\'(?![\'])/', $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+
)));
421431

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

Sources/Subs-Db-postgresql.php

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

380380
// Comments that are allowed in a query are preg_removed.
381381
static $allowed_comments_from = array(
382-
'~(?<![\'\\\\])\'\X*?(?<![\'\\\\])\'~',
383382
'~\s+~s',
384383
'~/\*!40001 SQL_NO_CACHE \*/~',
385384
'~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~',
386385
'~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~',
387386
);
388387
static $allowed_comments_to = array(
389-
' %s ',
390388
' ',
391389
'',
392390
'',
@@ -415,7 +413,19 @@ function smf_db_query($identifier, $db_string, $db_values = array(), $connection
415413
// First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over.
416414
if (empty($modSettings['disableQueryCheck']))
417415
{
418-
$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $db_string)));
416+
$clean = preg_split('/(?<![\'\\\\])\'(?![\'])/', $db_string);
417+
418+
for ($i = 0; $i < count($clean); $i++)
419+
{
420+
if ($i % 2 === 1)
421+
$clean[$i] = ' %s ';
422+
}
423+
424+
$clean = trim(strtolower(preg_replace(
425+
$allowed_comments_from,
426+
$allowed_comments_to,
427+
implode('', $clean)
428+
)));
419429

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

0 commit comments

Comments
 (0)