Skip to content

Commit 8b122a8

Browse files
committed
Chore: validate search value against RegEx
1 parent f08c864 commit 8b122a8

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

cmd/rhoas/pkged.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

locales/kafka/active.en.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ Invalid Kafka instance name. Valid names must satisfy the following conditions:
1111
[kafka.validation.name.error.lengthError]
1212
one = 'Kafka instance name must be between 1 and 32 characters'
1313

14+
[kafka.validation.error.invalidSearchValue]
15+
description = 'Error message when invalid search input is provided'
16+
one = '''
17+
Illegal search value "{{.Search}}", search input must satisfy the following conditions:
18+
19+
- must be of 1 or more characters
20+
- must only consist of alphanumeric characters, '-', '_' and '%'
21+
'''
1422

1523
[kafka.common.error.notFoundErrorById]
1624
one = 'Kafka instance with ID "{{.ID}}" not found'

pkg/cmd/kafka/list/list.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
1010
flagutil "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil/flags"
1111
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"
12+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
1213

1314
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/dump"
1415

@@ -101,6 +102,11 @@ func runList(opts *options) error {
101102
a = a.Size(strconv.Itoa(opts.limit))
102103

103104
if opts.search != "" {
105+
106+
if err = kafka.ValidateSearchInput(opts.search); err != nil {
107+
return err
108+
}
109+
104110
logger.Debug(localizer.MustLocalize(&localizer.Config{
105111
MessageID: "kafka.list.log.debug.filteringKafkaList",
106112
TemplateData: map[string]interface{}{

pkg/kafka/kafka_util.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
var (
14-
validNameRegexp = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
14+
validNameRegexp = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
15+
validSearchRegexp = regexp.MustCompile(`^([a-zA-Z0-9-_%]*[a-zA-Z0-9-_%])?$`)
1516
)
1617

1718
// ValidateName validates the proposed name of a Kafka instance
@@ -69,3 +70,30 @@ func TransformKafkaRequest(kafka *kasclient.KafkaRequest) *kasclient.KafkaReques
6970

7071
return kafka
7172
}
73+
74+
func ValidateSearchInput(val interface{}) error {
75+
search, ok := val.(string)
76+
77+
if !ok {
78+
return errors.New(localizer.MustLocalize(&localizer.Config{
79+
MessageID: "common.error.castError",
80+
TemplateData: map[string]interface{}{
81+
"Value": val,
82+
"Type": "string",
83+
},
84+
}))
85+
}
86+
87+
matched := validSearchRegexp.MatchString(search)
88+
89+
if matched {
90+
return nil
91+
}
92+
93+
return errors.New(localizer.MustLocalize(&localizer.Config{
94+
MessageID: "kafka.validation.error.invalidSearchValue",
95+
TemplateData: map[string]interface{}{
96+
"Search": search,
97+
},
98+
}))
99+
}

0 commit comments

Comments
 (0)