Skip to content
File

Blob: waf/settings.go

go122 lines
1// Package waf provides request protection for public HTTP tunnels.
2package waf
3 
4import (
5 "bytes"
6 "cmp"
7 "fmt"
8 "regexp"
9 "slices"
10 "strings"
11 "unicode"
12 
13 "go.miragespace.co/specter/config"
14 "go.miragespace.co/specter/spec/protocol"
15 
16 "google.golang.org/protobuf/proto"
17)
18 
19const (
20 ConfigName = "waf"
21 ConfigSchema = 1
22 MaxExclusions = 32
23 MaxPolicyBytes = 48 << 10
24)
25 
26var releaseVersion = regexp.MustCompile(`^4\.[0-9]+\.[0-9]+$`)
27 
28func DefaultSettings() *protocol.WAFSettings {
29 return &protocol.WAFSettings{DefaultMode: protocol.WAFPolicy_BLOCK, Channel: protocol.WAFSettings_LTS}
30}
31 
32// ValidateSettings returns an owned copy suitable for immutable publication.
33func ValidateSettings(s *protocol.WAFSettings) (*protocol.WAFSettings, error) {
34 if s == nil {
35 return nil, fmt.Errorf("settings are required")
36 }
37 if s.DefaultMode != protocol.WAFPolicy_BLOCK && s.DefaultMode != protocol.WAFPolicy_MONITOR {
38 return nil, fmt.Errorf("default_mode must be BLOCK or MONITOR")
39 }
40 if s.Channel != protocol.WAFSettings_LTS && s.Channel != protocol.WAFSettings_STABLE {
41 return nil, fmt.Errorf("channel must be LTS or STABLE")
42 }
43 if s.PinnedVersion != "" && !validReleaseVersion(s.PinnedVersion) {
44 return nil, fmt.Errorf("pinned_version must be a CRS 4 release, such as 4.25.2")
45 }
46 return proto.Clone(s).(*protocol.WAFSettings), nil
47}
48 
49func DecodeSettings(record config.Record) (*protocol.WAFSettings, error) {
50 if err := record.Validate(); err != nil {
51 return nil, err
52 }
53 if record.Name != ConfigName || record.Version.Schema != ConfigSchema {
54 return nil, fmt.Errorf("unsupported WAF configuration")
55 }
56 s := new(protocol.WAFSettings)
57 if err := DecodeJSON(bytes.NewReader(record.Value), s); err != nil {
58 return nil, err
59 }
60 return ValidateSettings(s)
61}
62 
63func DefaultPolicy() *protocol.WAFPolicy {
64 return &protocol.WAFPolicy{Mode: protocol.WAFPolicy_INHERIT}
65}
66 
67// ValidatePolicy returns an owned, normalized copy. Callers retain their input.
68func ValidatePolicy(p *protocol.WAFPolicy) (*protocol.WAFPolicy, error) {
69 if p == nil {
70 return nil, fmt.Errorf("policy is required")
71 }
72 switch p.Mode {
73 case protocol.WAFPolicy_INHERIT, protocol.WAFPolicy_BLOCK, protocol.WAFPolicy_MONITOR, protocol.WAFPolicy_OFF:
74 default:
75 return nil, fmt.Errorf("mode must be INHERIT, BLOCK, MONITOR, or OFF")
76 }
77 if len(p.Exclusions) > MaxExclusions {
78 return nil, fmt.Errorf("at most %d rule exceptions are allowed", MaxExclusions)
79 }
80 for _, e := range p.Exclusions {
81 if e == nil || e.RuleId < 100000 || e.RuleId > 999999 {
82 return nil, fmt.Errorf("rule_id must be a six-digit rule ID")
83 }
84 if len(e.Path) > 1024 || (e.Path != "" && !strings.HasPrefix(e.Path, "/")) || strings.ContainsAny(e.Path, "?#") || strings.ContainsFunc(e.Path, unicode.IsControl) {
85 return nil, fmt.Errorf("path must be an exact URL path without a query or fragment, up to 1024 bytes")
86 }
87 }
88 p = proto.Clone(p).(*protocol.WAFPolicy)
89 slices.SortFunc(p.Exclusions, func(a, b *protocol.WAFExclusion) int {
90 if a.RuleId != b.RuleId {
91 return cmp.Compare(a.RuleId, b.RuleId)
92 }
93 return strings.Compare(a.Path, b.Path)
94 })
95 p.Exclusions = slices.CompactFunc(p.Exclusions, func(a, b *protocol.WAFExclusion) bool {
96 return a.RuleId == b.RuleId && a.Path == b.Path
97 })
98 encoded, err := MarshalJSON(p)
99 if err != nil || len(encoded) > MaxPolicyBytes {
100 return nil, fmt.Errorf("rule exceptions exceed the %d-byte policy limit", MaxPolicyBytes)
101 }
102 return p, nil
103}
104 
105func EffectiveMode(p *protocol.WAFPolicy, defaultMode protocol.WAFPolicy_Mode) protocol.WAFPolicy_Mode {
106 if p.GetMode() == protocol.WAFPolicy_INHERIT {
107 return defaultMode
108 }
109 return p.GetMode()
110}
111 
112func DisabledRules(p *protocol.WAFPolicy, path string) []int {
113 var ids []int
114 for _, e := range p.GetExclusions() {
115 if e.Path == "" || e.Path == path {
116 ids = append(ids, int(e.RuleId))
117 }
118 }
119 slices.Sort(ids)
120 return slices.Compact(ids)
121}