-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstraints.go
177 lines (141 loc) · 4.88 KB
/
constraints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package CADDY_FILE_SERVER
import (
"encoding/json"
"fmt"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"net/url"
)
var constraintsRegistry = make(map[string]func() Constraint)
func RegisterConstraintType(factory func() Constraint) {
typ := factory().ID()
constraintsRegistry[typ] = factory
}
// Constraints represent constraints as {params:[{type:..., customConfig..}]}
type Constraints map[string][]Constraint
type Constraint interface {
Validate(param string) error
ValidateParam(param string, value string) error
UnmarshalCaddyfile(d *caddyfile.Dispenser) error
ID() string
}
// Temporary map to hold serialized constraints with type as a key
// MarshalJSON serializes Constraints to JSON, adding a `type` field to each entry.
func (cs *Constraints) MarshalJSON() ([]byte, error) {
type jsonConstraintsWrapper map[string][]map[string]Constraint
// Initialize the map for wrapped constraints
wrappedConstraints := make(jsonConstraintsWrapper, len(*cs))
for param, constraintList := range *cs {
var wrappedList []map[string]Constraint
for _, constraint := range constraintList {
wrappedList = append(wrappedList, map[string]Constraint{
constraint.ID(): constraint,
})
}
wrappedConstraints[param] = wrappedList
}
return json.Marshal(wrappedConstraints)
}
// UnmarshalJSON deserializes JSON into Constraints, dynamically instantiating types using the registry.
func (cs *Constraints) UnmarshalJSON(data []byte) error {
type jsonConstraintsUnwrapper map[string][]map[string]json.RawMessage
var wrappedConstraints jsonConstraintsUnwrapper
if err := json.Unmarshal(data, &wrappedConstraints); err != nil {
return err
}
*cs = make(Constraints, len(wrappedConstraints))
for param, wrappedList := range wrappedConstraints {
(*cs)[param] = make([]Constraint, len(wrappedList))
for idx, wrappedConstraint := range wrappedList {
for constraintType, constraintData := range wrappedConstraint {
// Look up the factory function for the given constraint type
factory, found := constraintsRegistry[constraintType]
if !found {
return fmt.Errorf("unknown constraint type: %s for param: %s", constraintType, param)
}
// Instantiate the correct constraint type
constraint := factory()
// Unmarshal the constraint data into the instantiated constraint
if err := json.Unmarshal(constraintData, constraint); err != nil {
return fmt.Errorf("error unmarshaling constraint for param %s: %v", param, err)
}
// Add the deserialized constraint to the slice for this parameter
(*cs)[param][idx] = constraint
}
}
}
return nil
}
func (cs *Constraints) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for nesting := d.Nesting(); d.NextBlock(nesting); {
param := d.Val()
var constraintsForParam []Constraint
var nested bool
// Try to detect and process a nested block if any (like `w { range ... , range ... }`)
for nesting := d.Nesting(); d.NextBlock(nesting); {
nested = true
constraintName := d.Val()
factory, found := constraintsRegistry[constraintName]
if !found {
return d.Errf("unknown constraint type: %s", constraintName)
}
constraint := factory()
if err := constraint.UnmarshalCaddyfile(d); err != nil {
return d.Errf("error unmarshaling parameters for %s constraint: %v", constraintName, err)
}
constraintsForParam = append(constraintsForParam, constraint)
}
// If no nested block was found, process inline arguments (like `range 10 20`)
if !nested {
if !d.NextArg() {
return d.Errf("missing constraint name for parameter %s", param)
}
constraintName := d.Val()
factory, found := constraintsRegistry[constraintName]
if !found {
return d.Errf("unknown constraint type: %s", constraintName)
}
constraint := factory()
if err := constraint.UnmarshalCaddyfile(d); err != nil {
return d.Errf("error unmarshaling parameters for %s constraint: %v", constraintName, err)
}
if d.NextArg() {
return d.ArgErr()
}
constraintsForParam = append(constraintsForParam, constraint)
}
(*cs)[param] = constraintsForParam
}
return nil
}
func (cs *Constraints) Validate() error {
for param, constraints := range *cs {
for _, constraint := range constraints {
if err := constraint.Validate(param); err != nil {
return err
}
}
}
return nil
}
func (cs *Constraints) ProcessRequestForm(form *url.Values, onSecurityFail OnSecurityFail) error {
for param, constraints := range *cs {
if !form.Has(param) {
continue
}
for _, constraint := range constraints {
if err := constraint.ValidateParam(param, form.Get(param)); err != nil {
if onSecurityFail == OnSecurityFailIgnore {
form.Del(param)
} else if onSecurityFail == OnSecurityFailBypass {
return BypassRequestError
} else if onSecurityFail == OnSecurityFailAbort {
return &AbortRequestError{
err.Error(),
}
}
return err
}
}
}
return nil
}