Skip to content

Commit b65bb4f

Browse files
committed
Server:子查询防SQL注入并优化@from的别名以及注释等
1 parent 7ef8505 commit b65bb4f

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoSQLConfig.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ public String getSchema() {
5959
String s = super.getSchema();
6060
return StringUtil.isEmpty(s, true) ? "sys" : s; //TODO 改成你自己的
6161
}
62-
63-
@Override
64-
public String getSubqueryString(Subquery subquery) throws Exception {
65-
//TODO 用 SQLExecutor 的 preparedStatement 返回的
66-
// String range = subquery.getRange();
67-
// return (range == null || range.isEmpty() ? "" : range) + "(" + subquery.getConfig().getSQL(false) + ") ";
68-
throw new UnsupportedOperationException("未解决 SQL 注入,暂不支持");
69-
}
7062

7163
public DemoSQLConfig() {
7264
this(RequestMethod.GET);

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ public SQLConfig setJoinList(List<Join> joinList) {
754754
public boolean hasJoin() {
755755
return joinList != null && joinList.isEmpty() == false;
756756
}
757-
757+
758758

759759
@Override
760760
public boolean isTest() {
@@ -1152,7 +1152,7 @@ public String getEqualString(String key, Object value) throws Exception {
11521152
if (StringUtil.isName(key) == false) {
11531153
throw new IllegalArgumentException(key + ":value 中key不合法!不支持 ! 以外的逻辑符 !");
11541154
}
1155-
1155+
11561156
return getKey(key) + (not ? "!=" : "=") + (value instanceof Subquery ? getSubqueryString((Subquery) value) : getValue(value));
11571157
}
11581158

@@ -1346,12 +1346,12 @@ public String getBetweenString(String key, Object[] values, int type) throws Ill
13461346
if (values[i] instanceof String == false) {
13471347
throw new IllegalArgumentException(key + "%:value 中 value 的类型只能为 String 或 String[] !");
13481348
}
1349-
1349+
13501350
vs = StringUtil.split((String) values[i]);
13511351
if (vs == null || vs.length != 2) {
13521352
throw new IllegalArgumentException(key + "%:value 中 value 不合法!类型为 String 时必须包括1个逗号 , 且左右两侧都有值!类型为 String[] 里面每个元素要符合前面类型为 String 的规则 !");
13531353
}
1354-
1354+
13551355
condition += (i <= 0 ? "" : (Logic.isAnd(type) ? AND : OR)) + "(" + getBetweenString(key, vs[0], vs[1]) + ")";
13561356
}
13571357

@@ -1522,6 +1522,27 @@ public String getContainString(String key, Object[] childs, int type) throws Ill
15221522
}
15231523
//<> contain >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
15241524

1525+
1526+
1527+
//key@:{} Subquery <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1528+
1529+
@Override
1530+
public String getSubqueryString(Subquery subquery) throws Exception {
1531+
String range = subquery.getRange();
1532+
SQLConfig cfg = subquery.getConfig();
1533+
1534+
cfg.setPreparedValueList(new ArrayList<>());
1535+
String sql = (range == null || range.isEmpty() ? "" : range) + "(" + cfg.getSQL(isPrepared()) + ") ";
1536+
1537+
preparedValueList.addAll(cfg.getPreparedValueList());
1538+
1539+
return sql;
1540+
}
1541+
1542+
//key@:{} Subquery >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1543+
1544+
1545+
15251546
/**拼接条件
15261547
* @param not
15271548
* @param condition
@@ -1689,9 +1710,9 @@ private static String getConditionString(String column, String table, AbstractSQ
16891710

16901711
Subquery from = config.getFrom();
16911712
if (from != null) {
1692-
table = config.getSubqueryString(from) + " AS " + config.getKey(from.getFrom()) + " "; //TODO Comment:c 转为 AS `Comment:c`
1713+
table = config.getSubqueryString(from) + " AS " + config.getAlias() + " "; //TODO Comment:c 转为 AS `Comment:c`
16931714
}
1694-
1715+
16951716
String condition = table + config.getJoinString() + where + (
16961717
RequestMethod.isGetMethod(config.getMethod(), true) == false ?
16971718
"" : config.getGroupString() + config.getHavingString() + config.getOrderString()

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
*/
2323
public class Subquery {
2424

25-
private String path; // any, all
26-
private String originKey;
27-
private JSONObject originValue;
25+
private String path; // []/0/User
26+
private String originKey; //id{}@
27+
private JSONObject originValue; // { "from": "Comment", "Comment": {...} }
2828

29-
private String from; // any, all
29+
private String from; // Comment
3030
private String range; // any, all
31-
private String key; //id
31+
private String key; //id{}
3232
private SQLConfig config;
3333

3434
@JSONField(serialize = false) //解决泄漏 SQLConfig 里的 dbPassword 等

0 commit comments

Comments
 (0)