From 4189d14d6576fed4358dae8b46447124c42e90f8 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sat, 7 Mar 2026 12:44:35 +0300 Subject: [PATCH] api/event: remove legacy validate function; use domain map Signed-off-by: NotAShelf Change-Id: I9a68733cf16b09ef6381161452bda1e56a6a6964 --- internal/api/event.go | 37 ++---------------------------- internal/api/event_test.go | 47 ++++++++++++-------------------------- 2 files changed, 17 insertions(+), 67 deletions(-) diff --git a/internal/api/event.go b/internal/api/event.go index 7cb828f..7f71ce9 100644 --- a/internal/api/event.go +++ b/internal/api/event.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "slices" "notashelf.dev/watchdog/internal/limits" ) @@ -40,40 +39,8 @@ func ParseEvent(body io.Reader) (*Event, error) { return &event, nil } -// Validate checks if the event is valid for the given domains -func (e *Event) Validate(allowedDomains []string) error { - if e.Domain == "" { - return fmt.Errorf("domain required") - } - - // Check if domain is in allowed list - allowed := slices.Contains(allowedDomains, e.Domain) - if !allowed { - return fmt.Errorf("domain not allowed") - } - - if e.Path == "" { - return fmt.Errorf("path required") - } - - if len(e.Path) > limits.MaxPathLen { - return fmt.Errorf("path too long") - } - - if len(e.Referrer) > limits.MaxRefLen { - return fmt.Errorf("referrer too long") - } - - // Validate screen width is in reasonable range - if e.Width < 0 || e.Width > limits.MaxWidth { - return fmt.Errorf("invalid width") - } - - return nil -} - -// ValidateWithMap checks if the event is valid using a domain map (O(1) lookup) -func (e *Event) ValidateWithMap(allowedDomains map[string]bool) error { +// Validate checks if the event is valid using a domain map +func (e *Event) Validate(allowedDomains map[string]bool) error { if e.Domain == "" { return fmt.Errorf("domain required") } diff --git a/internal/api/event_test.go b/internal/api/event_test.go index 0a075fe..f14d0e2 100644 --- a/internal/api/event_test.go +++ b/internal/api/event_test.go @@ -115,7 +115,7 @@ func TestValidateEvent(t *testing.T) { tests := []struct { name string event Event - domains []string + domains map[string]bool wantErr bool }{ { @@ -124,7 +124,7 @@ func TestValidateEvent(t *testing.T) { Domain: "example.com", Path: "/home", }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: false, }, { @@ -134,7 +134,7 @@ func TestValidateEvent(t *testing.T) { Path: "/signup", Event: "signup", }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: false, }, { @@ -143,7 +143,7 @@ func TestValidateEvent(t *testing.T) { Domain: "wrong.com", Path: "/home", }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: true, }, { @@ -152,7 +152,7 @@ func TestValidateEvent(t *testing.T) { Domain: "", Path: "/home", }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: true, }, { @@ -161,7 +161,7 @@ func TestValidateEvent(t *testing.T) { Domain: "example.com", Path: "", }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: true, }, { @@ -170,7 +170,7 @@ func TestValidateEvent(t *testing.T) { Domain: "example.com", Path: "/" + strings.Repeat("a", 3000), }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: true, }, { @@ -180,7 +180,7 @@ func TestValidateEvent(t *testing.T) { Path: "/home", Referrer: strings.Repeat("a", 3000), }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: true, }, { @@ -189,7 +189,7 @@ func TestValidateEvent(t *testing.T) { Domain: "example.com", Path: "/" + strings.Repeat("a", 2000), }, - domains: []string{"example.com"}, + domains: map[string]bool{"example.com": true}, wantErr: false, }, { @@ -198,7 +198,7 @@ func TestValidateEvent(t *testing.T) { Domain: "site1.com", Path: "/home", }, - domains: []string{"site1.com", "site2.com"}, + domains: map[string]bool{"site1.com": true, "site2.com": true}, wantErr: false, }, { @@ -207,7 +207,7 @@ func TestValidateEvent(t *testing.T) { Domain: "site2.com", Path: "/about", }, - domains: []string{"site1.com", "site2.com"}, + domains: map[string]bool{"site1.com": true, "site2.com": true}, wantErr: false, }, { @@ -216,7 +216,7 @@ func TestValidateEvent(t *testing.T) { Domain: "site3.com", Path: "/home", }, - domains: []string{"site1.com", "site2.com"}, + domains: map[string]bool{"site1.com": true, "site2.com": true}, wantErr: true, }, } @@ -231,24 +231,7 @@ func TestValidateEvent(t *testing.T) { } } -func BenchmarkValidate_SliceLookup(b *testing.B) { - // Simulate multi-site with 50 domains - domains := make([]string, 50) - for i := range 50 { - domains[i] = strings.Repeat("site", i) + ".com" - } - - event := Event{ - Domain: domains[49], // Worst case - last in list - Path: "/test", - } - - for b.Loop() { - _ = event.Validate(domains) - } -} - -func BenchmarkValidate_MapLookup(b *testing.B) { +func BenchmarkValidate(b *testing.B) { // Simulate multi-site with 50 domains domainMap := make(map[string]bool, 50) for i := range 50 { @@ -256,11 +239,11 @@ func BenchmarkValidate_MapLookup(b *testing.B) { } event := Event{ - Domain: strings.Repeat("site", 49) + ".com", // any position + Domain: strings.Repeat("site", 49) + ".com", Path: "/test", } for b.Loop() { - _ = event.ValidateWithMap(domainMap) + _ = event.Validate(domainMap) } }