mirror of
https://github.com/NotAShelf/watchdog.git
synced 2026-04-15 14:54:00 +00:00
api/event: remove legacy validate function; use domain map
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I9a68733cf16b09ef6381161452bda1e56a6a6964
This commit is contained in:
parent
02c4f11619
commit
4189d14d65
2 changed files with 17 additions and 67 deletions
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue