Skip to content
This repository was archived by the owner on Jan 4, 2020. It is now read-only.

Commit 43d3d95

Browse files
committed
改进聚合查询安全性
1 parent eca555e commit 43d3d95

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

ThinkPHP/Library/Think/Db/Driver.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,12 @@ protected function bindParam($name, $value)
448448

449449
/**
450450
* 字段和表名处理
451-
* @access protected
451+
* @access public
452452
* @param string $key
453453
* @param bool $strict
454454
* @return string
455455
*/
456-
protected function parseKey($key, $strict = false)
456+
public function parseKey($key, $strict = false)
457457
{
458458
return $key;
459459
}

ThinkPHP/Library/Think/Db/Driver/Mysql.class.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,19 @@ public function getTables($dbName = '')
9494

9595
/**
9696
* 字段和表名处理
97-
* @access protected
97+
* @access public
9898
* @param string $key
9999
* @param bool $strict
100100
* @return string
101101
*/
102-
protected function parseKey($key, $strict = false)
102+
public function parseKey($key, $strict = false)
103103
{
104104
$key = trim($key);
105+
106+
if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
107+
E('not support data:' . $key);
108+
}
109+
105110
if ($strict || (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)`.\s]/', $key))) {
106111
$key = '`' . $key . '`';
107112
}

ThinkPHP/Library/Think/Db/Driver/Sqlsrv.class.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,19 @@ protected function parseOrder($order)
105105

106106
/**
107107
* 字段和表名处理
108-
* @access protected
108+
* @access public
109109
* @param string $key
110110
* @param bool $strict
111111
* @return string
112112
*/
113-
protected function parseKey($key, $strict = false)
113+
public function parseKey($key, $strict = false)
114114
{
115115
$key = trim($key);
116+
117+
if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
118+
E('not support data:' . $key);
119+
}
120+
116121
if ($strict || (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key))) {
117122
$key = '[' . $key . ']';
118123
}

ThinkPHP/Library/Think/Model.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function __call($method, $args)
239239
} elseif (in_array(strtolower($method), array('count', 'sum', 'min', 'max', 'avg'), true)) {
240240
// 统计查询的实现
241241
$field = isset($args[0]) ? $args[0] : '*';
242-
return $this->getField(strtoupper($method) . '(' . $field . ') AS tp_' . $method);
242+
return $this->getField(strtoupper($method) . '(' . $this->db->parseKey($field, true) . ') AS tp_' . $method);
243243
} elseif (strtolower(substr($method, 0, 5)) == 'getby') {
244244
// 根据某个字段获取记录
245245
$field = parse_name(substr($method, 5));

0 commit comments

Comments
 (0)