From 73524c2f815e7cd629ef8ac3a63172d0ee462fcf Mon Sep 17 00:00:00 2001 From: Florian Mortgat <50440633+atm-florianm@users.noreply.github.com> Date: Fri, 3 Nov 2023 23:01:57 +0100 Subject: [PATCH] [modulebuilder] FIX handling of the $filter parameter in fetchAll() method (#25931) * FIX handling of the $filter parameter for fetchAll: the previous version can result in invalid SQL * Remove #[AllowDynamicProperties] attribute from Conf class (cf. PR #25931) * Ensure sql-building strings are detectably sanitized to avoid PR rejection --- htdocs/core/class/conf.class.php | 1 - .../template/class/myobject.class.php | 46 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index c01a8e70367..b6e15e6e844 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -29,7 +29,6 @@ /** * Class to stock current configuration */ -#[AllowDynamicProperties] class Conf { /** diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 08f6a76764c..f4234e54944 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -438,7 +438,7 @@ class MyObject extends CommonObject * @param string $sortfield Sort field * @param int $limit limit * @param int $offset Offset - * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...) + * @param array $filter Filter array. Example array('mystringfield'=>'value', 'myintfield'=>4, 'customsql'=>...) * @param string $filtermode Filter mode (AND or OR) * @return array|int int <0 if KO, array of pages if OK */ @@ -463,16 +463,40 @@ class MyObject extends CommonObject $sqlwhere = array(); if (count($filter) > 0) { foreach ($filter as $key => $value) { - if ($key == 't.rowid') { - $sqlwhere[] = $key." = ".((int) $value); - } elseif (in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) { - $sqlwhere[] = $key." = '".$this->db->idate($value)."'"; - } elseif ($key == 'customsql') { - $sqlwhere[] = $value; // For this case, $value never come from a user input but is a hard coded value in code - } elseif (strpos($value, '%') === false) { - $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")"; - } else { - $sqlwhere[] = $key." LIKE '%".$this->db->escapeforlike($this->db->escape($value))."%'"; + $columnName = preg_replace('/^t\./', '', $key); + if ($key === 'customsql') { + // Never use 'customsql' with a value from user input since it is injected as is. The value must be hard coded. + $sqlwhere[] = $value; + continue; + } elseif (isset($this->fields[$columnName])) { + $type = $this->fields[$columnName]['type']; + if (preg_match('/^integer/', $type)) { + if (is_int($value)) { + // single value + $sqlwhere[] = $key . " = " . intval($value); + } elseif (is_array($value)) { + if (empty($value)) continue; + $sqlwhere[] = $key . ' IN (' . $this->db->sanitize(implode(',', array_map('intval', $value))) . ')'; + } + continue; + } elseif (in_array($type, array('date', 'datetime', 'timestamp'))) { + $sqlwhere[] = $key . " = '" . $this->db->idate($value) . "'"; + continue; + } + } + + // when the $key doesn't fall into the previously handled categories, we do as if the column were a varchar/text + if (is_array($value) && count($value)) { + $value = implode(',', array_map(function ($v) { + return "'" . $this->db->sanitize($this->db->escape($v)) . "'"; + }, $value)); + $sqlwhere[] = $key . ' IN (' . $this->db->sanitize($value, true) . ')'; + } elseif (is_scalar($value)) { + if (strpos($value, '%') === false) { + $sqlwhere[] = $key . " = '" . $this->db->sanitize($this->db->escape($value)) . "'"; + } else { + $sqlwhere[] = $key . " LIKE '%" . $this->db->escapeforlike($this->db->escape($value)) . "%'"; + } } } }