Skip to content

Commit c9f809f

Browse files
authored
feat(policy): allow arguments with comma character (#2306)
Signed-off-by: Sylwester Piskozub <[email protected]>
1 parent c38f3c1 commit c9f809f

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

pkg/policies/policies.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ func getInputArguments(inputs map[string]string) map[string]any {
393393
args[k] = value
394394
}
395395

396-
// Single string, let's check for CSV
397-
lines = strings.Split(s, ",")
396+
// Single string, let's check for CSV and escaped commas `\,`
397+
lines = splitArgs(s)
398398
value = getValue(lines)
399399
if value == nil {
400400
continue
@@ -425,6 +425,42 @@ func getValue(values []string) any {
425425
return lines[0]
426426
}
427427

428+
func splitArgs(s string) []string {
429+
var result []string
430+
var current strings.Builder
431+
escaped := false
432+
433+
for i := 0; i < len(s); i++ {
434+
c := s[i]
435+
436+
if escaped {
437+
current.WriteByte(c)
438+
escaped = false
439+
continue
440+
}
441+
442+
if c == '\\' {
443+
escaped = true
444+
continue
445+
}
446+
447+
if c == ',' {
448+
// Unescaped comma: split here
449+
result = append(result, strings.TrimSpace(current.String()))
450+
current.Reset()
451+
} else {
452+
current.WriteByte(c)
453+
}
454+
}
455+
456+
// Add the final part
457+
if current.Len() > 0 {
458+
result = append(result, strings.TrimSpace(current.String()))
459+
}
460+
461+
return result
462+
}
463+
428464
func engineEvaluationsToAPIViolations(results []*engine.EvaluationResult) []*v12.PolicyEvaluation_Violation {
429465
res := make([]*v12.PolicyEvaluation_Violation, 0)
430466
for _, r := range results {

pkg/policies/policies_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,11 @@ func (s *testSuite) TestGetInputArguments() {
772772
inputs: map[string]string{"foo": "bar1,bar2,bar3"},
773773
expected: map[string]any{"foo": []string{"bar1", "bar2", "bar3"}},
774774
},
775+
{
776+
name: "csv input with escaped comma",
777+
inputs: map[string]string{"foo": "bar1\\,bar2,bar3"},
778+
expected: map[string]any{"foo": []string{"bar1,bar2", "bar3"}},
779+
},
775780
{
776781
name: "csv input with empty slots",
777782
inputs: map[string]string{"foo": ",bar1,,,bar2,bar3,,"},

0 commit comments

Comments
 (0)