Files
sam 59827b19bd fix(controlplane): reject userinfo and query in notify_webhook
A webhook URL with embedded credentials or a query string can leak
secrets through logs, proxies and referrer headers. The task policy
validator now refuses both.
2026-10-28 00:36:33 +11:00

58 lines
2.0 KiB
Go

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"}`
tests := []struct {
name, org, value string
refused bool
}{
{"system org signs with a secret", storecontract.DefaultOrgID, signed, false},
{"another org names an instance secret", "acme", signed, true},
{"another org sets only its webhook", "acme", unsigned, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, TaskPolicyKind, []byte(tt.value))
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
}
})
}
}