Skip to content

Commit c419bd6

Browse files
committed
Server防SQL注入:校验@order
1 parent eadad0e commit c419bd6

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public String getGroupString() {
242242
if (keys != null && keys.length > 0) {
243243
for (int i = 0; i < keys.length; i++) {
244244
if (StringUtil.isName(keys[i]) == false) {
245-
throw new IllegalArgumentException("@group:value 中 value里面用 , 分割的每一项都必须是1个单词!");
245+
throw new IllegalArgumentException("@group:value 中 value里面用 , 分割的每一项都必须是1个单词!并且不要有空格!");
246246
}
247247
}
248248
}
@@ -299,6 +299,28 @@ public String getOrderString() {
299299
if (order.contains("-")) {
300300
order = order.replaceAll("-", " DESC ");
301301
}
302+
303+
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
304+
String[] keys = StringUtil.split(order);
305+
if (keys != null && keys.length > 0) {
306+
String origin;
307+
int index;
308+
for (int i = 0; i < keys.length; i++) {
309+
index = keys[i].trim().endsWith(" ASC") ? keys[i].lastIndexOf(" ASC") : -1; //StringUtil.split返回数组中,子项不会有null
310+
if (index < 0) {
311+
index = keys[i].trim().endsWith(" DESC") ? keys[i].lastIndexOf(" DESC") : -1;
312+
}
313+
origin = index < 0 ? keys[i] : keys[i].substring(0, index);
314+
315+
//这里既不对origin trim,也不对 ASC/DESC ignoreCase,希望前端严格传没有任何空格的字符串过来,减少传输数据量,节约服务器性能
316+
if (StringUtil.isName(origin) == false) {
317+
throw new IllegalArgumentException("预编译模式下 @order:value 中 value里面用 , 分割的每一项"
318+
+ " column+ / column- 中 column必须是1个单词!并且不要有多余的空格!");
319+
}
320+
}
321+
}
322+
}
323+
302324
return " ORDER BY " + order;
303325
}
304326

@@ -361,7 +383,8 @@ public String getColumnString() throws NotExistException {
361383
alias = index < 0 ? null : keys[i].substring(index + 1);
362384

363385
if (StringUtil.isName(origin) == false || (alias != null && StringUtil.isName(alias) == false)) {
364-
throw new IllegalArgumentException("GET请求: 预编译模式下 @column:value 中 value里面用 , 分割的每一项 column:alias 中 column必须是1个单词!如果有alias,则alias也必须为1个单词!");
386+
throw new IllegalArgumentException("GET请求: 预编译模式下 @column:value 中 value里面用 , 分割的每一项"
387+
+ " column:alias 中 column必须是1个单词!如果有alias,则alias也必须为1个单词!并且不要有多余的空格!");
365388
}
366389
}
367390
}
@@ -903,7 +926,6 @@ public String getContainString(String key, Object value) throws NotExistExceptio
903926
key = logic.getKey();
904927
Log.i(TAG, "getRangeString key = " + key);
905928

906-
//TODO 直接调Like性能最好!
907929
return getContainString(key, newJSONArray(value).toArray(), logic.getType());
908930
}
909931
/**WHERE key contains childs

0 commit comments

Comments
 (0)