diff --git a/internal/infrastructure/controlplanestore/task_policy.go b/internal/infrastructure/controlplanestore/task_policy.go index c5360f823..a43ad87da 100644 --- a/internal/infrastructure/controlplanestore/task_policy.go +++ b/internal/infrastructure/controlplanestore/task_policy.go @@ -28,6 +28,12 @@ func taskPolicyDefinition() Definition { if err != nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") { return fmt.Errorf("notify_webhook must be an HTTP or HTTPS URL") } + if parsed.User != nil { + return fmt.Errorf("notify_webhook must not carry credentials in the URL") + } + if parsed.RawQuery != "" || parsed.ForceQuery { + return fmt.Errorf("notify_webhook must not carry a query string") + } } if !alert.Severity(doc.NotifyMinSeverity).Valid() && doc.NotifyMinSeverity != "" { return fmt.Errorf("notify_min_severity must be info, warning or action") diff --git a/internal/infrastructure/controlplanestore/task_policy_test.go b/internal/infrastructure/controlplanestore/task_policy_test.go index 0109d83b8..99431ea00 100644 --- a/internal/infrastructure/controlplanestore/task_policy_test.go +++ b/internal/infrastructure/controlplanestore/task_policy_test.go @@ -2,11 +2,39 @@ package controlplanestore import ( "errors" + "fmt" "testing" "github.com/samcharles93/archie-core/internal/domain/storecontract" ) +func TestTaskPolicyNotifyWebhookValidation(t *testing.T) { + tests := []struct { + name string + webhook string + refused bool + }{ + {"empty disables delivery", "", false}, + {"plain https URL", "https://hooks.example.com/services/abc", false}, + {"plain http URL", "http://hooks.example.com/hook", false}, + {"credentials in the URL", "https://user:pass@hooks.example.com/hook", true}, + {"userinfo without password", "https://user@hooks.example.com/hook", true}, + {"query string", "https://hooks.example.com/hook?token=secret", true}, + {"bare question mark", "https://hooks.example.com/hook?", true}, + {"non-http scheme", "ftp://hooks.example.com/hook", true}, + {"no host", "https:///hook", true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + doc := fmt.Sprintf(`{"notify_webhook":%q}`, tt.webhook) + err := taskPolicyDefinition().Validate([]byte(doc)) + if refused := err != nil; refused != tt.refused { + t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err) + } + }) + } +} + func TestTaskPolicyNamesNoInstanceSecretForAnotherOrg(t *testing.T) { const signed = `{"notify_webhook":"https://example.com/hook","notify_secret":{"engine":"env","key":"INSTANCE_KEY"}}` const unsigned = `{"notify_webhook":"https://example.com/hook"}`