Skip to content

Commit 99376a8

Browse files
authored
fix: cwe89 sql injection (#8762)
1 parent 611556c commit 99376a8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

backend/server/services/pushapi.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,32 @@ limitations under the License.
1818
package services
1919

2020
import (
21+
"regexp"
22+
"strings"
23+
2124
"github.com/apache/incubator-devlake/core/dal"
2225
"github.com/apache/incubator-devlake/core/errors"
2326
)
2427

2528
// InsertRow FIXME ...
2629
func InsertRow(table string, rows []map[string]interface{}) (int64, errors.Error) {
30+
if !regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString(table) {
31+
return 0, errors.BadInput.New("table name invalid")
32+
}
33+
34+
if allowedTables := cfg.GetString("PUSH_API_ALLOWED_TABLES"); allowedTables != "" {
35+
allow := false
36+
for _, t := range strings.Split(allowedTables, ",") {
37+
if strings.TrimSpace(t) == table {
38+
allow = true
39+
break
40+
}
41+
}
42+
if !allow {
43+
return 0, errors.Forbidden.New("table name is not in the allowed list")
44+
}
45+
}
46+
2747
err := db.Create(rows, dal.From(table))
2848
if err != nil {
2949
return 0, err

env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SKIP_SUBTASK_PROGRESS=false
3434
PORT=8080
3535
MODE=release
3636

37+
# PUSH_API_ALLOWED_TABLES=table1,table2
3738
NOTIFICATION_ENDPOINT=
3839
NOTIFICATION_SECRET=
3940

0 commit comments

Comments
 (0)