Skip to content

Commit 553829d

Browse files
committed
fix reported problems
1 parent 3d5a9b6 commit 553829d

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

api/src/Db.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ public static function from_bool($val)
757757
* @throws Db\Exception\InvalidSql for SQL syntax errors
758758
* @throws Db\Exception with $this->Link_ID->ErrorNo() as code for all other errors
759759
*/
760-
function query($Query_String, $line = '', $file = '', $offset=0, $num_rows=-1, $inputarr=false, $fetchmode=self::FETCH_BOTH, $reconnect=true)
760+
function query($Query_String, $line = '', $file = '', int $offset=0, int $num_rows=-1, $inputarr=false, $fetchmode=self::FETCH_BOTH, $reconnect=true)
761761
{
762762
if ($Query_String == '')
763763
{
@@ -2291,7 +2291,7 @@ function select($table,$cols,$where,$line,$file,$offset=False,$append='',$app=Fa
22912291
{
22922292
return $sql;
22932293
}
2294-
return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows,false,$fetchmode);
2294+
return $this->query($sql, $line, $file, (int)$offset, $offset===False ? -1 : (int)$num_rows, false, $fetchmode);
22952295
}
22962296

22972297
/**

api/src/Etemplate/Widget/Nextmatch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static public function ajax_get_rows($exec_id, array $queriedRange, array $filte
336336
// Parse sort into something that get_rows functions are expecting: db_field in order, ASC/DESC in sort
337337
if(is_array($value['sort']))
338338
{
339-
$value['order'] = $value['sort']['id'];
339+
$value['order'] = preg_match('/^[a-z0-9_]+$/', $value['sort']['id']) ? $value['sort']['id'] : '';
340340
$value['sort'] = $value['sort']['asc'] ? 'ASC' : 'DESC';
341341
}
342342

api/src/Link.php

+5
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ static function query($app, $pattern, &$options = array())
812812
$options['num_rows'] = max((int)$GLOBALS['egw_info']['user']['preference']['common']['maxmatchs'], self::DEFAULT_NUM_ROWS);
813813
}
814814

815+
if (isset($options['order']) && !preg_match(preg_match('/^[a-z0-9_]+$/', $options['order'])))
816+
{
817+
unset($options['order'], $options['sort']);
818+
}
819+
815820
$result = self::exec($method, array($pattern, &$options));
816821

817822
if (!isset($options['total']))

api/src/Storage/Base.php

+35
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,8 @@ function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard
966966
$num_rows = 0; // as spec. in max_matches in the user-prefs
967967
if (is_array($start)) list($start,$num_rows) = $start+[null,null];
968968

969+
$order_by = self::sanitizeOrderBy($order_by);
970+
969971
// fix GROUP BY clause to contain all non-aggregate selected columns
970972
if ($order_by && stripos($order_by,'GROUP BY') !== false)
971973
{
@@ -1066,6 +1068,39 @@ function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard
10661068
return $arr;
10671069
}
10681070

1071+
1072+
/**
1073+
* Sanitize (currently just remove) not understood ORDER BY clause
1074+
*
1075+
* @param string $fragment SQL fragment containing ORDER BY clause, could also contain GROUP BY etc
1076+
* @return string sanitized version of $_order_by
1077+
*/
1078+
static function sanitizeOrderBy(string $fragment)
1079+
{
1080+
// check fragment contains ORDER BY --> just operate on what's behind
1081+
if (stripos($fragment,'ORDER BY') !== false)
1082+
{
1083+
[$group_by, $order_by] = preg_split('/order +by +/i', $fragment);
1084+
}
1085+
// check fragment not just contain GROUP BY or HAVING --> nothing to do
1086+
elseif ($fragment === '' || stripos($fragment,'GROUP BY')!==false || stripos($fragment,'HAVING')!==false)
1087+
{
1088+
return $fragment;
1089+
}
1090+
// fragment is ORDER BY clause
1091+
else
1092+
{
1093+
$order_by = $fragment;
1094+
}
1095+
if (!preg_match_all("/(#?[a-zA-Z_.]+) *(<> *''|IS NULL|IS NOT NULL|& *\d+)? *(ASC|DESC)?(,|$)/ui", $order_by, $all_matches) ||
1096+
$order_by !== implode('', $all_matches[0]))
1097+
{
1098+
//error_log(__METHOD__."(".json_encode($fragment).") REMOVED");
1099+
return $group_by??'';
1100+
}
1101+
return $fragment;
1102+
}
1103+
10691104
/**
10701105
* Parse an array of search criteria into something that can be passed on
10711106
* to the DB

infolog/inc/class.infolog_so.inc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ function search(&$query, $no_acl=false)
821821
}
822822
}
823823
$sortbycf='';
824-
if (!empty($query['order']) && (preg_match('/^[a-z_0-9, ]+$/i',$query['order']) || stripos($query['order'],'#')!==FALSE ) &&
824+
if (!empty($query['order']) && preg_match('/^#?[a-z_0-9, ]+$/i',$query['order']) &&
825825
(empty($query['sort']) || is_string($query['sort']) && preg_match('/^(DESC|ASC)$/i',$query['sort'])))
826826
{
827827
$order = array();

0 commit comments

Comments
 (0)