Skip to content

Commit c555a3e

Browse files
committed
Server防SQL注入:校验@column@group
1 parent 75227ad commit c555a3e

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,22 @@ public AbstractSQLConfig setGroup(String group) {
233233
@JSONField(serialize = false)
234234
public String getGroupString() {
235235
group = StringUtil.getTrimedString(group);
236-
return group.isEmpty() ? "" : " GROUP BY " + group;
236+
if (group.isEmpty()) {
237+
return "";
238+
}
239+
240+
if (isPrepared()) { //不能通过 ? 来代替,因为SQLExecutor statement.setString后 GROUP BY 'userId' 有单引号,只能返回一条数据,必须去掉单引号才行!
241+
String[] keys = StringUtil.split(group);
242+
if (keys != null && keys.length > 0) {
243+
for (int i = 0; i < keys.length; i++) {
244+
if (StringUtil.isName(keys[i]) == false) {
245+
throw new IllegalArgumentException("@group:value 中 value里面用 , 分割的每一项都必须是1个单词!");
246+
}
247+
}
248+
}
249+
}
250+
251+
return " GROUP BY " + group;
237252
}
238253

239254
@Override
@@ -306,18 +321,52 @@ public String getColumnString() throws NotExistException {
306321
switch (getMethod()) {
307322
case HEAD:
308323
case HEADS:
324+
if (StringUtil.isEmpty(column, true) == false && StringUtil.isName(column) == false) {
325+
throw new IllegalArgumentException("HEAD请求: @column:value 中 value必须是1个单词!");
326+
}
309327
return SQL.count(column);
310328
case POST:
311329
if (StringUtil.isEmpty(column, true)) {
312330
throw new NotExistException(TAG + "getColumnString getMethod() = POST"
313331
+ " >> StringUtil.isEmpty(column, true)");
314332
}
333+
334+
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
335+
String[] keys = StringUtil.split(column);
336+
if (keys != null && keys.length > 0) {
337+
for (int i = 0; i < keys.length; i++) {
338+
if (StringUtil.isName(keys[i]) == false) {
339+
throw new IllegalArgumentException("POST请求: 每一个 key:value 中的key都必须是1个单词!");
340+
}
341+
}
342+
}
343+
}
344+
315345
return "(" + column + ")";
316346
default:
317347
column = StringUtil.getString(column);
318348
if (column.isEmpty()) {
319349
return "*";
320350
}
351+
352+
if (isPrepared()) { //不能通过 ? 来代替,SELECT 'id','name' 返回的就是 id:"id", name:"name",而不是数据库里的值!
353+
String[] keys = StringUtil.split(column);
354+
if (keys != null && keys.length > 0) {
355+
String origin;
356+
String alias;
357+
int index;
358+
for (int i = 0; i < keys.length; i++) {
359+
index = keys[i].indexOf(":"); //StringUtil.split返回数组中,子项不会有null
360+
origin = index < 0 ? keys[i] : keys[i].substring(0, index);
361+
alias = index < 0 ? null : keys[i].substring(index + 1);
362+
363+
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个单词!");
365+
}
366+
}
367+
}
368+
}
369+
321370
return column.contains(":") == false ? column : column.replaceAll(":", " AS ");//不能在这里改,后续还要用到:
322371
}
323372
}
@@ -806,11 +855,11 @@ public String getRangeString(String key, Object range) throws Exception {
806855
if (condition.isEmpty()) {
807856
return "";
808857
}
809-
858+
810859
if (isPrepared()) {
811860
throw new UnsupportedOperationException("预编译模式下不允许传 key{}:\"condition\" !");
812861
}
813-
862+
814863
return getCondition(logic.isNot(), condition);
815864
}
816865

0 commit comments

Comments
 (0)