[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
This commit is contained in:
parent
8f54413a69
commit
73524c2f81
2 changed files with 35 additions and 12 deletions
|
|
@ -29,7 +29,6 @@
|
|||
/**
|
||||
* Class to stock current configuration
|
||||
*/
|
||||
#[AllowDynamicProperties]
|
||||
class Conf
|
||||
{
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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)) . "%'";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue