forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec_check.go
58 lines (52 loc) · 1.14 KB
/
sec_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gen
import (
"fmt"
"gorm.io/gorm/clause"
"gorm.io/hints"
)
func checkConds(conds []clause.Expression) error {
for _, cond := range conds {
if err := checkClause(cond); err != nil {
return err
}
}
return nil
}
var banClauses = map[string]bool{
"INSERT": true,
"VALUES": true,
// "ON CONFLICT": true,
"SELECT": true,
"FROM": true,
"WHERE": true,
"GROUP BY": true,
"ORDER BY": true,
"LIMIT": true,
"FOR": true,
"UPDATE": true,
"SET": true,
"DELETE": true,
}
func checkClause(cond clause.Expression) error {
switch cond := cond.(type) {
case hints.Hints, hints.IndexHint:
return nil
case clause.OnConflict:
return checkOnConflict(cond)
case clause.Interface:
if banClauses[cond.Name()] {
return fmt.Errorf("clause %s is banned", cond.Name())
}
return nil
}
return fmt.Errorf("unknown clause %v", cond)
}
func checkOnConflict(cond clause.OnConflict) error {
for _, item := range cond.DoUpdates {
switch item.Value.(type) {
case clause.Expr, *clause.Expr:
return fmt.Errorf("OnConflict clause assignment with gorm.Expr is banned for security reasons for now")
}
}
return nil
}