Skip to content

Commit

Permalink
fix: fix rule id check (#2709)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer authored Mar 14, 2024
1 parent 7800443 commit 2b1fee4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/processor/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,13 @@ func (p *RuleProcessor) GetRuleByJson(id, ruleJson string) (*api.Rule, error) {
}

var invalidRuleChars = []string{
"/",
"/", "#", "%",
}

func validateRuleID(id string) error {
if id != strings.TrimSpace(id) {
return fmt.Errorf("ruleID: %v should be trimed", id)
}
for _, invalidChar := range invalidRuleChars {
if strings.Contains(id, invalidChar) {
return fmt.Errorf("ruleID:%s contains invalidChar:%v", id, invalidChar)
Expand Down
16 changes: 16 additions & 0 deletions internal/processor/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,22 @@ func TestValidateRuleID(t *testing.T) {
"1/2",
fmt.Errorf("ruleID:%s contains invalidChar:%v", "1/2", "/"),
},
{
"1#2",
fmt.Errorf("ruleID:%s contains invalidChar:%v", "1#2", "#"),
},
{
"1%2",
fmt.Errorf("ruleID:%s contains invalidChar:%v", "1%2", "%"),
},
{
id: "\t123",
err: fmt.Errorf("ruleID: %v should be trimed", "\t123"),
},
{
id: "123\t",
err: fmt.Errorf("ruleID: %v should be trimed", "123\t"),
},
}
for _, tc := range testcases {
require.Equal(t, tc.err, validateRuleID(tc.id))
Expand Down

0 comments on commit 2b1fee4

Please sign in to comment.