Skip to content

Commit d29d079

Browse files
committed
条件组合:解决 @combine:"(date> | tag&$) & name*~" 解析异常,解决 @combine:"id | userId{}" 可绕过权限控制
1 parent 2cc13da commit d29d079

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

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

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,25 +2342,22 @@ public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String
23422342

23432343
String key = "";
23442344
Set<String> usedKeySet = new HashSet<>(where.size());
2345-
while (i < n) { // "date> | (contactIdList<> & (name*~ | tag&$))"
2346-
char c = s.charAt(i);
2347-
boolean isLast = i >= n - 1;
2345+
while (i <= n) { // "date> | (contactIdList<> & (name*~ | tag&$))"
2346+
boolean isOver = i >= n;
2347+
char c = isOver ? 0 : s.charAt(i);
23482348
boolean isBlankOrRightParenthesis = c == ' ' || c == ')';
2349-
if (isLast || isBlankOrRightParenthesis) {
2350-
if (isBlankOrRightParenthesis == false) {
2351-
key += c;
2352-
}
2353-
2349+
if (isOver || isBlankOrRightParenthesis) {
23542350
boolean isEmpty = StringUtil.isEmpty(key, true);
23552351
if (isEmpty && last != ')') {
2356-
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + s.substring(i)
2352+
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + (isOver ? s : s.substring(i))
23572353
+ "' 不合法!" + (c == ' ' ? "空格 ' ' " : "右括号 ')'") + " 左边缺少条件 key !逻辑连接符 & | 左右必须各一个相邻空格!"
23582354
+ "空格不能多也不能少!不允许首尾有空格,也不允许连续空格!左括号 ( 的右边 和 右括号 ) 的左边 都不允许有相邻空格!");
23592355
}
23602356

23612357
if (isEmpty == false) {
23622358
if (first == false && lastLogic <= 0) {
2363-
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + s.substring(i - key.length()) + "' 不合法!左边缺少 & | 其中一个逻辑连接符!");
2359+
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + s.substring(i - key.length() - (isOver ? 1 : 0))
2360+
+ "' 不合法!左边缺少 & | 其中一个逻辑连接符!");
23642361
}
23652362

23662363
Object value = where.get(key);
@@ -2380,7 +2377,7 @@ public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String
23802377

23812378
key = "";
23822379

2383-
if (isLast) {
2380+
if (isOver) {
23842381
break;
23852382
}
23862383
}
@@ -2389,8 +2386,8 @@ public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String
23892386
}
23902387
else if (c == '&') {
23912388
if (last == ' ') {
2392-
if (i >= n || s.charAt(i + 1) != ' ') {
2393-
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + s.substring(0, i + 1)
2389+
if (i >= n - 1 || s.charAt(i + 1) != ' ') {
2390+
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + (i >= n - 1 ? s : s.substring(0, i + 1))
23942391
+ "' 不合法!逻辑连接符 & 右边缺少一个空格 !逻辑连接符 & | 左右必须各一个相邻空格!空格不能多也不能少!"
23952392
+ "不允许首尾有空格,也不允许连续空格!左括号 ( 的右边 和 右括号 ) 的左边 都不允许有相邻空格!");
23962393
}
@@ -2399,14 +2396,14 @@ else if (c == '&') {
23992396
lastLogic = c;
24002397
i ++;
24012398
}
2402-
else if (isLast == false) {
2399+
else {
24032400
key += c;
24042401
}
24052402
}
24062403
else if (c == '|') {
24072404
if (last == ' ') {
2408-
if (i >= n || s.charAt(i + 1) != ' ') {
2409-
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + s.substring(0, i + 1)
2405+
if (i >= n - 1 || s.charAt(i + 1) != ' ') {
2406+
throw new IllegalArgumentException(table + ":{ @combine: '" + combine + "' } 中字符 '" + (i >= n - 1 ? s : s.substring(0, i + 1))
24102407
+ "' 不合法!逻辑连接符 | 右边缺少一个空格 !逻辑连接符 & | 左右必须各一个相邻空格!空格不能多也不能少!"
24112408
+ "不允许首尾有空格,也不允许连续空格!左括号 ( 右边和右括号 ) 左边都不允许有相邻空格!");
24122409
}
@@ -2415,7 +2412,7 @@ else if (c == '|') {
24152412
lastLogic = c;
24162413
i ++;
24172414
}
2418-
else if (isLast == false) {
2415+
else {
24192416
key += c;
24202417
}
24212418
}
@@ -2438,16 +2435,12 @@ else if (c == ')') {
24382435
whereString += c;
24392436
lastLogic = 0;
24402437
}
2441-
else if (isLast == false) {
2438+
else {
24422439
key += c;
24432440
}
24442441

24452442
last = c;
24462443
i ++;
2447-
2448-
if (i >= n) {
2449-
i = n - 1;
2450-
}
24512444
}
24522445

24532446
if (depth != 0) {
@@ -2477,8 +2470,8 @@ else if (isLast == false) {
24772470
if (StringUtil.isEmpty(whereString, true)) {
24782471
whereString = andWhere;
24792472
}
2480-
else if (StringUtil.isNotEmpty(andWhere, true)) {
2481-
whereString = andWhere + AND + "( " + whereString + " )";
2473+
else if (StringUtil.isNotEmpty(andWhere, true)) { // andWhere 必须放后面,否则 prepared 值顺序错误
2474+
whereString = "( " + whereString + " )" + AND + andWhere;
24822475
}
24832476

24842477
if (joinList != null) {
@@ -4127,7 +4120,24 @@ else if (id instanceof Subquery) {}
41274120
List<String> orList = combineMap == null ? null : new ArrayList<>();
41284121
List<String> notList = combineMap == null ? null : new ArrayList<>();
41294122

4130-
if (combineMap != null) {
4123+
if (combineMap == null) {
4124+
if (StringUtil.isNotEmpty(combineExpression, true)) {
4125+
List<String> banKeyList = Arrays.asList(idKey, idInKey, userIdKey, userIdInKey);
4126+
4127+
for (String key : banKeyList) {
4128+
int index = combineExpression.indexOf(key);
4129+
if (index >= 0) {
4130+
char left = index <= 0 ? ' ' : combineExpression.charAt(index - 1);
4131+
char right = index >= combineExpression.length() - key.length() ? ' ' : combineExpression.charAt(index + key.length());
4132+
if ((left == ' ' || left == '(') && (right == ' ' || right == ')')) {
4133+
throw new UnsupportedOperationException(table + ":{} 里的 @combine:value 中的 value 里 " + key + " 不合法!"
4134+
+ "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!");
4135+
}
4136+
}
4137+
}
4138+
}
4139+
}
4140+
else {
41314141
//强制作为条件且放在最前面优化性能
41324142
if (id != null) {
41334143
tableWhere.put(idKey, id);
@@ -4178,7 +4188,7 @@ else if (w.startsWith("!")) {
41784188
}
41794189
else {
41804190
if (idKey.equals(w) || idInKey.equals(w) || userIdKey.equals(w) || userIdInKey.equals(w)) {
4181-
throw new UnsupportedOperationException(table + ":{} 里的 @combine:value 中的value里 " + ws[i] + " 不合法!"
4191+
throw new UnsupportedOperationException(table + ":{} 里的 @combine:value 中的 value 里 " + ws[i] + " 不合法!"
41824192
+ "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!");
41834193
}
41844194
}

0 commit comments

Comments
 (0)