Skip to content

Commit

Permalink
fix regex security check
Browse files Browse the repository at this point in the history
Signed-off-by: xliuqq <xlzq1992@gmail.com>
  • Loading branch information
xliuqq committed May 7, 2024
1 parent 605fe2d commit 916828c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/common/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ const (
)

var (
// fluid cache label for scheduling pod, format: 'fluid.io/dataset.{dataset name}.sched]'
LabelAnnotationPodSchedRegex = regexp.MustCompile("^" + LabelAnnotationDataset + "\\.([A-Za-z0-9.-]*)\\.sched$")
// LabelAnnotationPodSchedRegex is the fluid cache label for scheduling pod, format: 'fluid.io/dataset.{dataset name}.sched]'
// use string literal to meet security check.
LabelAnnotationPodSchedRegex = regexp.MustCompile("^fluid\\.io/dataset\\.([A-Za-z0-9.-]*)\\.sched$")

Check failure on line 56 in pkg/common/label.go

View workflow job for this annotation

GitHub Actions / lint

S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple)

Check failure on line 56 in pkg/common/label.go

View workflow job for this annotation

GitHub Actions / staticcheck

should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (S1007)
)

type OperationType string
Expand Down
30 changes: 30 additions & 0 deletions pkg/common/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,33 @@ func TestHitTarget(t *testing.T) {
}

}

func TestLabelAnnotationPodSchedRegex(t *testing.T) {
testCases := map[string]struct {
target string
got string
match bool
}{
"correct": {
target: LabelAnnotationDataset + ".dsA.sched",
match: true,
got: "dsA",
},
"wrong": {
target: "fluidaio/dataset.dsA.sched",
match: false,
},
}

for index, item := range testCases {
submatch := LabelAnnotationPodSchedRegex.FindStringSubmatch(item.target)

if !item.match && len(submatch) == 2 {
t.Errorf("[%s] check match, want:%t, got:%t", index, item.match, len(submatch) == 2)
}

if len(submatch) == 2 && submatch[1] != item.got {
t.Errorf("[%s] check failure, want:%s, got:%s", index, item.got, submatch[1])
}
}
}

0 comments on commit 916828c

Please sign in to comment.