6
6
import javax .servlet .http .HttpServletRequest ;
7
7
import java .lang .reflect .Field ;
8
8
import java .util .Set ;
9
+ import java .util .regex .Matcher ;
9
10
import java .util .regex .Pattern ;
10
11
11
12
/**
@@ -20,7 +21,7 @@ public class SqlInjectionUtil {
20
21
* (上线修改值 20200501,同步修改前端的盐值)
21
22
*/
22
23
private final static String TABLE_DICT_SIGN_SALT = "20200501" ;
23
- private final static String XSS_STR = "and |exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |;|or |+|user()" ;
24
+ private final static String XSS_STR = "and |extractvalue|updatexml| exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |;|or |+|user()" ;
24
25
25
26
/**
26
27
* 正则 user() 匹配更严谨
@@ -29,6 +30,11 @@ public class SqlInjectionUtil {
29
30
/**正则 show tables*/
30
31
private final static String SHOW_TABLES = "show\\ s+tables" ;
31
32
33
+ /**
34
+ * sql注释的正则
35
+ */
36
+ private final static Pattern SQL_ANNOTATION = Pattern .compile ("/\\ *.*\\ */" );
37
+
32
38
/**
33
39
* 针对表字典进行额外的sign签名校验(增加安全机制)
34
40
* @param dictCode:
@@ -66,10 +72,12 @@ public static void filterContent(String value, String customXssString) {
66
72
if (value == null || "" .equals (value )) {
67
73
return ;
68
74
}
75
+ // 校验sql注释 不允许有sql注释
76
+ checkSqlAnnotation (value );
69
77
// 统一转为小写
70
78
value = value .toLowerCase ();
71
79
//SQL注入检测存在绕过风险 https://gitee.com/jeecg/jeecg-boot/issues/I4NZGE
72
- value = value .replaceAll ("/\\ *.*\\ */" ,"" );
80
+ // value = value.replaceAll("/\\*.*\\*/","");
73
81
74
82
String [] xssArr = XSS_STR .split ("\\ |" );
75
83
for (int i = 0 ; i < xssArr .length ; i ++) {
@@ -117,10 +125,12 @@ public static void filterContent(String[] values, String customXssString) {
117
125
if (value == null || "" .equals (value )) {
118
126
return ;
119
127
}
128
+ // 校验sql注释 不允许有sql注释
129
+ checkSqlAnnotation (value );
120
130
// 统一转为小写
121
131
value = value .toLowerCase ();
122
132
//SQL注入检测存在绕过风险 https://gitee.com/jeecg/jeecg-boot/issues/I4NZGE
123
- value = value .replaceAll ("/\\ *.*\\ */" ,"" );
133
+ // value = value.replaceAll("/\\*.*\\*/","");
124
134
125
135
for (int i = 0 ; i < xssArr .length ; i ++) {
126
136
if (value .indexOf (xssArr [i ]) > -1 ) {
@@ -157,15 +167,17 @@ public static void filterContent(String[] values, String customXssString) {
157
167
*/
158
168
//@Deprecated
159
169
public static void specialFilterContentForDictSql (String value ) {
160
- String specialXssStr = " exec | insert | select | delete | update | drop | count | chr | mid | master | truncate | char | declare |;|+|user()" ;
170
+ String specialXssStr = " exec |extractvalue|updatexml| insert | select | delete | update | drop | count | chr | mid | master | truncate | char | declare |;|+|user()" ;
161
171
String [] xssArr = specialXssStr .split ("\\ |" );
162
172
if (value == null || "" .equals (value )) {
163
173
return ;
164
174
}
175
+ // 校验sql注释 不允许有sql注释
176
+ checkSqlAnnotation (value );
165
177
// 统一转为小写
166
178
value = value .toLowerCase ();
167
179
//SQL注入检测存在绕过风险 https://gitee.com/jeecg/jeecg-boot/issues/I4NZGE
168
- value = value .replaceAll ("/\\ *.*\\ */" ,"" );
180
+ // value = value.replaceAll("/\\*.*\\*/","");
169
181
170
182
for (int i = 0 ; i < xssArr .length ; i ++) {
171
183
if (value .indexOf (xssArr [i ]) > -1 || value .startsWith (xssArr [i ].trim ())) {
@@ -189,15 +201,17 @@ public static void specialFilterContentForDictSql(String value) {
189
201
*/
190
202
//@Deprecated
191
203
public static void specialFilterContentForOnlineReport (String value ) {
192
- String specialXssStr = " exec | insert | delete | update | drop | chr | mid | master | truncate | char | declare |user()" ;
204
+ String specialXssStr = " exec |extractvalue|updatexml| insert | delete | update | drop | chr | mid | master | truncate | char | declare |user()" ;
193
205
String [] xssArr = specialXssStr .split ("\\ |" );
194
206
if (value == null || "" .equals (value )) {
195
207
return ;
196
208
}
209
+ // 校验sql注释 不允许有sql注释
210
+ checkSqlAnnotation (value );
197
211
// 统一转为小写
198
212
value = value .toLowerCase ();
199
213
//SQL注入检测存在绕过风险 https://gitee.com/jeecg/jeecg-boot/issues/I4NZGE
200
- value = value .replaceAll ("/\\ *.*\\ */" ,"" );
214
+ // value = value.replaceAll("/\\*.*\\*/"," ");
201
215
202
216
for (int i = 0 ; i < xssArr .length ; i ++) {
203
217
if (value .indexOf (xssArr [i ]) > -1 || value .startsWith (xssArr [i ].trim ())) {
@@ -256,4 +270,17 @@ public static boolean isClassField(Set<String> fieldSet, Class clazz){
256
270
}
257
271
return true ;
258
272
}
273
+
274
+ /**
275
+ * 校验是否有sql注释
276
+ * @return
277
+ */
278
+ public static void checkSqlAnnotation (String str ){
279
+ Matcher matcher = SQL_ANNOTATION .matcher (str );
280
+ if (matcher .find ()){
281
+ String error = "请注意,值可能存在SQL注入风险---> \\ *.*\\ " ;
282
+ log .error (error );
283
+ throw new RuntimeException (error );
284
+ }
285
+ }
259
286
}
0 commit comments