Skip to content

Commit af82c33

Browse files
authored
Merge pull request #2463 from actiontech/issue2448-1
Cherry-Pick From 2.9999.x: Fix the issue of SQL blacklist invalidation in multiple rows of SQL
2 parents e2c9b92 + c22c755 commit af82c33

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

sqle/api/controller/v1/audit_plan_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ func TestIsSqlInBlackList(t *testing.T) {
1515
}, {
1616
FilterContent: "table_1",
1717
FilterType: "SQL",
18+
},{
19+
FilterContent: "ignored_service",
20+
FilterType: "SQL",
1821
},
1922
})
2023

2124
matchSqls := []string{
2225
"SELECT * FROM users",
2326
"DELETE From tAble_1",
2427
"SELECT COUNT(*) FROM table_2",
28+
`/* this is a comment, Service: ignored_service */
29+
select * from table_ignored where id < 123;`,
30+
`/* this is a comment, Service: ignored_service */ update * from table_ignored where id < 123;`,
2531
}
2632
for _, matchSql := range matchSqls {
2733
if !filter.IsSqlInBlackList(matchSql) {
@@ -32,6 +38,8 @@ func TestIsSqlInBlackList(t *testing.T) {
3238
"INSERT INTO users VALUES (1, 'John')",
3339
"DELETE From schools",
3440
"SHOW CREATE TABLE table_2",
41+
`/* this is a comment, Service: ignored_
42+
service */ update * from table_ignored where id < 123;`,
3543
}
3644
for _, notMatchSql := range notMatchSqls {
3745
if filter.IsSqlInBlackList(notMatchSql) {

sqle/utils/util.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,20 @@ func IsPrefixSubStrArray(arr []string, prefix []string) bool {
318318
return true
319319
}
320320

321-
// 全模糊匹配字符串,并且对大小写不敏感
321+
// 全模糊匹配字符串,对大小写不敏感,匹配多行,且防止正则注入
322322
func FullFuzzySearchRegexp(str string) *regexp.Regexp {
323-
return regexp.MustCompile(`^.*(?i)` + regexp.QuoteMeta(str) + `.*$`)
323+
/*
324+
1. (?is)是一个正则表达式修饰符,其中:
325+
i表示忽略大小写(case-insensitive)
326+
s表示开启单行模式,开启后.可以匹配换行符,让整个字符串作为一行
327+
2. ^.*匹配字符串的开头,其中:
328+
^表示起始位置,
329+
.表示匹配任何字符(除了换行符)
330+
*表示匹配前面的模式零次或多次
331+
3. .*$匹配字符串的结尾,其中:
332+
$表示结束位置
333+
*/
334+
return regexp.MustCompile(`(?is)^.*` + regexp.QuoteMeta(str) + `.*$`)
324335
}
325336

326337
var ErrUnknownEncoding = errors.New("unknown encoding")

sqle/utils/util_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ func TestFullFuzzySearchRegexp(t *testing.T) {
261261
".*(?i)",
262262
[]string{"GoLang .*(?i) awesome", "I love GO^.*(?i)SING", "GoLangGO.*(?i)Golang"},
263263
[]string{"language", "hi", "heyHelloCode", "HElLO", "Sun_hello", "HelLo_Jack"},
264+
},{
265+
"ignored_service",
266+
[]string{`/* this is a comment, Service: ignored_service */
267+
select * from table_ignored where id < 123;'
268+
`,`/* this is a comment, Service: ignored_service */ select * from table_ignored where id < 123;`},
269+
[]string{"any sql","",`/* this is a comment, Service: ignored
270+
_service */ select * from table_ignored where id < 123;`},
264271
},
265272
}
266273

0 commit comments

Comments
 (0)