Merge pull request #23951 from nextcloud/backport/23922/stable20

[stable20] Improve query type detection
This commit is contained in:
Roeland Jago Douma 2020-11-07 10:49:06 +01:00 committed by GitHub
commit 71de05f775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

View File

@ -73,8 +73,7 @@ class OC_DB {
throw new \OC\DatabaseException($e->getMessage()); throw new \OC\DatabaseException($e->getMessage());
} }
// differentiate between query and manipulation // differentiate between query and manipulation
$result = new OC_DB_StatementWrapper($result, $isManipulation); return new OC_DB_StatementWrapper($result, $isManipulation);
return $result;
} }
/** /**
@ -85,22 +84,26 @@ class OC_DB {
* @return bool * @return bool
*/ */
public static function isManipulation($sql) { public static function isManipulation($sql) {
$sql = trim($sql);
$selectOccurrence = stripos($sql, 'SELECT'); $selectOccurrence = stripos($sql, 'SELECT');
if ($selectOccurrence !== false && $selectOccurrence < 10) { if ($selectOccurrence === 0) {
return false; return false;
} }
$insertOccurrence = stripos($sql, 'INSERT'); $insertOccurrence = stripos($sql, 'INSERT');
if ($insertOccurrence !== false && $insertOccurrence < 10) { if ($insertOccurrence === 0) {
return true; return true;
} }
$updateOccurrence = stripos($sql, 'UPDATE'); $updateOccurrence = stripos($sql, 'UPDATE');
if ($updateOccurrence !== false && $updateOccurrence < 10) { if ($updateOccurrence === 0) {
return true; return true;
} }
$deleteOccurrence = stripos($sql, 'DELETE'); $deleteOccurrence = stripos($sql, 'DELETE');
if ($deleteOccurrence !== false && $deleteOccurrence < 10) { if ($deleteOccurrence === 0) {
return true; return true;
} }
\OC::$server->getLogger()->logException(new \Exception('Can not detect if query is manipulating: ' . $sql));
return false; return false;
} }