Skip to content

Commit 6831cb6

Browse files
committed
增强安全:对 DELETE 和 PUT 强制加 LIMIT;简化包含选项的写法:解决 "key<>": "a" 这种包含字符串的格式报错 Data truncation: Invalid JSON text,原来必须里面再用 "" 包装一次,JSON 中还得转义,现在直接写即可;
1 parent cf1cca0 commit 6831cb6

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ public SQLConfig newSQLConfig(boolean isProcedure) throws Exception {
650650
*/
651651
@Override
652652
public AbstractObjectParser setSQLConfig() throws Exception {
653-
return setSQLConfig(1, 0, 0);
653+
return setSQLConfig(RequestMethod.isQueryMethod(method) ? 1 : 0, 0, 0);
654654
}
655655

656656
@Override
@@ -668,7 +668,7 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
668668
return this;
669669
}
670670
}
671-
sqlConfig.setCount(count).setPage(page).setPosition(position);
671+
sqlConfig.setCount(sqlConfig.getCount() <= 0 ? count : sqlConfig.getCount()).setPage(page).setPosition(position);
672672

673673
parser.onVerifyRole(sqlConfig);
674674

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,11 +1284,11 @@ public String getLimitString() {
12841284
public static String getLimitString(int page, int count, boolean isTSQL) {
12851285
int offset = getOffset(page, count);
12861286

1287-
if (isTSQL) {
1287+
if (isTSQL) { // OFFSET FECTH 中所有关键词都不可省略
12881288
return " OFFSET " + offset + " ROWS FETCH FIRST " + count + " ROWS ONLY";
12891289
}
12901290

1291-
return " LIMIT " + count + " OFFSET " + offset;
1291+
return " LIMIT " + count + (offset <= 0 ? "" : " OFFSET " + offset); // DELETE, UPDATE 不支持 OFFSET
12921292
}
12931293

12941294
//WHERE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -2179,20 +2179,23 @@ public String getContainString(String key, Object[] childs, int type) throws Ill
21792179
String condition = "";
21802180
if (childs != null) {
21812181
for (int i = 0; i < childs.length; i++) {
2182-
if (childs[i] != null) {
2183-
if (childs[i] instanceof JSON) {
2182+
Object c = childs[i];
2183+
if (c != null) {
2184+
if (c instanceof JSON) {
21842185
throw new IllegalArgumentException(key + "<>:value 中value类型不能为JSON!");
21852186
}
21862187

21872188
condition += (i <= 0 ? "" : (Logic.isAnd(type) ? AND : OR));
21882189
if (isPostgreSQL()) {
2189-
condition += (getKey(key) + " @> " + getValue(newJSONArray(childs[i]))); //operator does not exist: jsonb @> character varying "[" + childs[i] + "]");
2190+
condition += (getKey(key) + " @> " + getValue(newJSONArray(c))); //operator does not exist: jsonb @> character varying "[" + c + "]");
21902191
}
21912192
else if (isOracle()) {
2192-
condition += ("json_textcontains(" + getKey(key) + ", '$', " + getValue(childs[i].toString()) + ")");
2193+
condition += ("json_textcontains(" + getKey(key) + ", '$', " + getValue(c.toString()) + ")");
21932194
}
21942195
else {
2195-
condition += ("json_contains(" + getKey(key) + ", " + getValue(childs[i].toString()) + ")");
2196+
boolean isNum = c instanceof Number;
2197+
String v = (isNum ? "" : "\"") + childs[i] + (isNum ? "" : "\"");
2198+
condition += ("json_contains(" + getKey(key) + ", " + getValue(v) + ")");
21962199
}
21972200
}
21982201
}
@@ -2390,9 +2393,9 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
23902393
case POST:
23912394
return "INSERT INTO " + tablePath + config.getColumnString() + " VALUES" + config.getValuesString();
23922395
case PUT:
2393-
return "UPDATE " + tablePath + config.getSetString() + config.getWhereString(true);
2396+
return "UPDATE " + tablePath + config.getSetString() + config.getWhereString(true) + config.getLimitString();
23942397
case DELETE:
2395-
return "DELETE FROM " + tablePath + config.getWhereString(true);
2398+
return "DELETE FROM " + tablePath + config.getWhereString(true) + config.getLimitString();
23962399
default:
23972400
String explain = (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : "");
23982401
if (config.isTest() && RequestMethod.isGetMethod(config.getMethod(), true)) {
@@ -2635,6 +2638,10 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
26352638
throw new NotExistException(TAG + ": newSQLConfig idIn instanceof List >> 去掉无效 id 后 newIdIn.isEmpty()");
26362639
}
26372640
idIn = newIdIn;
2641+
2642+
if (method == DELETE || method == PUT) {
2643+
config.setCount(newIdIn.size());
2644+
}
26382645
}
26392646

26402647
//对id和id{}处理,这两个一定会作为条件
@@ -2670,6 +2677,10 @@ else if (id instanceof Subquery) {}
26702677
throw new NotExistException(TAG + ": newSQLConfig idIn != null && (((List<?>) idIn).contains(id) == false");
26712678
}
26722679
}
2680+
2681+
if (method == DELETE || method == PUT) {
2682+
config.setCount(1);
2683+
}
26732684
}
26742685

26752686

0 commit comments

Comments
 (0)