Skip to content

Commit 8f70a61

Browse files
authored
Merge pull request #716 from uyong/master
修复sql注入检验不正确问题 #707
2 parents bd57fad + b2abd2e commit 8f70a61

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/main/java/com/github/pagehelper/util/SqlSafeUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class SqlSafeUtil {
3636
* 参考: mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlInjectionUtils.java
3737
*/
3838
private static final Pattern SQL_SYNTAX_PATTERN = Pattern.compile("(insert|delete|update|select|create|drop|truncate|grant|alter|deny|revoke|call|execute|exec|declare|show|rename|set)" +
39-
".+(into|from|set|where|table|database|view|index|on|cursor|procedure|trigger|for|password|union|and|or)", Pattern.CASE_INSENSITIVE);
39+
"\\s+.*(into|from|set|where|table|database|view|index|on|cursor|procedure|trigger|for|password|union|and|or)|(select\\s*\\*\\s*from\\s+)", Pattern.CASE_INSENSITIVE);
4040
/**
4141
* 使用'、;或注释截断SQL检查正则
4242
* <p>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.pagehelper.util;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class SqlSafeUtilTest {
8+
9+
@Test
10+
public void check() {
11+
assertSql(true, "insert into xx");
12+
// 无空格
13+
assertSql(false, "insertxxinto xx");
14+
assertSql(false, "insert_into");
15+
assertSql(true, "SELECT aa FROM user");
16+
// 无空格
17+
assertSql(true, "SELECT*FROM user");
18+
// 左空格
19+
assertSql(true, "SELECT *FROM user");
20+
// 右空格
21+
assertSql(true, "SELECT* FROM user");
22+
// 左tab
23+
assertSql(true, "SELECT *FROM user");
24+
// 右tab
25+
assertSql(true, "SELECT* FROM user");
26+
assertSql(false, "SELECT*FROMuser");
27+
28+
// 验证 issue #707 问题
29+
assertSql(false, "databaseType desc,orderNum desc");
30+
}
31+
32+
private void assertSql(boolean injection, String sql) {
33+
assertEquals(injection, SqlSafeUtil.check(sql));
34+
}
35+
}

0 commit comments

Comments
 (0)