Skip to content

Commit 41a4e99

Browse files
committed
fix: validate regex when matches operator is used in custom comparison
1 parent 61ff258 commit 41a4e99

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

internal/policywizard/forms.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ func (m *Model) applyFormValues(fv formValues) {
382382
case "exists":
383383
m.storeSubExpr(policy.ExistsExpr(m.exprContext))
384384
case "matches":
385+
if err := validateRegex(fv.str); err != nil {
386+
m.validationErr = err.Error()
387+
return
388+
}
385389
m.storeSubExpr(policy.MatchesExpr(m.exprContext, fv.str))
386390
default:
387391
m.storeSubExpr(policy.ComparisonExpr(m.exprContext, fv.operator, fv.str))

internal/policywizard/forms_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ func TestApply_ExprCustomOp_MatchesStoresPending(t *testing.T) {
413413
assert.Equal(t, `matches(flow.name, "^prod")`, m.pendingExprs[0])
414414
}
415415

416+
func TestApply_ExprCustomOp_MatchesInvalidRegex(t *testing.T) {
417+
m := newTestModel()
418+
m.step = stepExprCustomOp
419+
m.exprContext = "flow.name"
420+
421+
m.applyFormValues(formValues{operator: "matches", str: "[unclosed"})
422+
423+
assert.Empty(t, m.pendingExprs)
424+
assert.Contains(t, m.validationErr, "invalid regex")
425+
}
426+
416427
func TestApply_ExprCustomOp_ExistsStoresPending(t *testing.T) {
417428
m := newTestModel()
418429
m.step = stepExprCustomOp

0 commit comments

Comments
 (0)