From 24059b0b8db2dcc64ea7b133f17ff4a5258834af Mon Sep 17 00:00:00 2001 From: ATM john Date: Wed, 6 Mar 2024 17:24:23 +0100 Subject: [PATCH 1/3] get rows security --- htdocs/core/db/DoliDB.class.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 3b1184c7f85..4c0bfcbd0f3 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -385,6 +385,7 @@ abstract class DoliDB implements Database if ($res) { $obj = $this->fetch_object($res); if ($obj) { + $this->free($res); return $obj; } else { return 0; @@ -399,12 +400,17 @@ abstract class DoliDB implements Database * Note : This method executes a given SQL query and retrieves all row of results as an array of objects. It should only be used with SELECT queries * be careful with this method use it only with some limit of results to avoid performances loss. * - * @param string $sql The sql query string - * @return bool|array Result - * @deprecated + * @param string $sql The sql query string + * @param bool $noLimitCheck set it to true to disable sql query check of limit + * @return bool|array Result */ - public function getRows($sql) + public function getRows($sql, $noLimitCheck = false) { + + if (!$noLimitCheck && strpos($sql, 'LIMIT') === false) { + return false; + } + $res = $this->query($sql); if ($res) { $results = array(); @@ -413,6 +419,7 @@ abstract class DoliDB implements Database $results[] = $obj; } } + $this->free($res); return $results; } From 5b921d0dc4326c38e8b8cdd36cf3bca420b88603 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 11 Mar 2024 16:26:17 +0100 Subject: [PATCH 2/3] Update DoliDB.class.php --- htdocs/core/db/DoliDB.class.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 4c0bfcbd0f3..1a21fb5c527 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -396,18 +396,16 @@ abstract class DoliDB implements Database } /** - * Return all results from query as an array of objects - * Note : This method executes a given SQL query and retrieves all row of results as an array of objects. It should only be used with SELECT queries - * be careful with this method use it only with some limit of results to avoid performances loss. + * Return all results from query as an array of objects. Using this is a bad practice and is discouraged. + * Note : It should only be used with SELECT queries and with a limit. If you are not able to defined/know what can be the limit, it + * just means this function is not what you need. Do not use it. * - * @param string $sql The sql query string - * @param bool $noLimitCheck set it to true to disable sql query check of limit - * @return bool|array Result + * @param string $sql The sql query string. Must end with "... LIMIT x" + * @return bool|array Result */ - public function getRows($sql, $noLimitCheck = false) + public function getRows($sql) { - - if (!$noLimitCheck && strpos($sql, 'LIMIT') === false) { + if (! preg_match('/LIMIT \d+$/', $sql)) { return false; } From 553587b6606f6f956883fba7be163315526f23c5 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 11 Mar 2024 16:27:46 +0100 Subject: [PATCH 3/3] Update DoliDB.class.php --- htdocs/core/db/DoliDB.class.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 1a21fb5c527..90cb227db73 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -381,11 +381,11 @@ abstract class DoliDB implements Database { $sql .= ' LIMIT 1'; - $res = $this->query($sql); - if ($res) { - $obj = $this->fetch_object($res); + $resql = $this->query($sql); + if ($resql) { + $obj = $this->fetch_object($resql); if ($obj) { - $this->free($res); + $this->free($resql); return $obj; } else { return 0; @@ -409,15 +409,15 @@ abstract class DoliDB implements Database return false; } - $res = $this->query($sql); - if ($res) { + $resql = $this->query($sql); + if ($resql) { $results = array(); - if ($this->num_rows($res) > 0) { - while ($obj = $this->fetch_object($res)) { + if ($this->num_rows($resql) > 0) { + while ($obj = $this->fetch_object($resql)) { $results[] = $obj; } } - $this->free($res); + $this->free($resql); return $results; }