Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code scaning alerts —— Incomplete regular expression for hostnames #4052

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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$`)
)

type OperationType string
Expand Down
34 changes: 34 additions & 0 deletions pkg/common/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,37 @@ 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 fluid.io": {
target: "fluidaio/dataset.dsA.sched",
match: false,
},
"wrong prefix": {
target: "a.fluid.io/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])
}
}
}
Loading