Skip to content

Commit eca294f

Browse files
committed
updated validator to be method off of config; updated structs
1 parent 4cc1849 commit eca294f

File tree

1 file changed

+31
-85
lines changed

1 file changed

+31
-85
lines changed

webhookValidationConfig.go

+31-85
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,31 @@
44
package ancla
55

66
import (
7-
"errors"
8-
"fmt"
97
"time"
10-
)
118

12-
var (
13-
SpecialUseIPs = []string{
14-
"0.0.0.0/8", //local ipv4
15-
"fe80::/10", //local ipv6
16-
"255.255.255.255/32", //broadcast to neighbors
17-
"2001::/32", //ipv6 TEREDO prefix
18-
"2001:5::/32", //EID space for lisp
19-
"2002::/16", //ipv6 6to4
20-
"fc00::/7", //ipv6 unique local
21-
"192.0.0.0/24", //ipv4 IANA
22-
"2001:0000::/23", //ipv6 IANA
23-
"224.0.0.1/32", //ipv4 multicast
24-
}
25-
SpecialUseHosts = []string{
26-
".example.",
27-
".invalid.",
28-
".test.",
29-
"localhost",
30-
}
31-
errFailedToBuildValidators = errors.New("failed to build validators")
32-
errFailedToBuildValidURLFuncs = errors.New("failed to build ValidURLFuncs")
9+
"github.com/xmidt-org/urlegit"
3310
)
3411

3512
type ValidatorConfig struct {
36-
URL URLVConfig
37-
TTL TTLVConfig
13+
URL URLVConfig
14+
TTL TTLVConfig
15+
IP IPConfig
16+
Domain DomainConfig
17+
}
18+
19+
type IPConfig struct {
20+
Allow bool
21+
ForbiddenSubnets []string
22+
}
23+
24+
type DomainConfig struct {
25+
AllowSpecialUseDomains bool
26+
ForbiddenDomains []string
3827
}
3928

4029
type URLVConfig struct {
41-
HTTPSOnly bool
42-
AllowLoopback bool
43-
AllowIP bool
44-
AllowSpecialUseHosts bool
45-
AllowSpecialUseIPs bool
46-
InvalidHosts []string
47-
InvalidSubnets []string
30+
Schemes []string
31+
AllowLoopback bool
4832
}
4933

5034
type TTLVConfig struct {
@@ -53,63 +37,25 @@ type TTLVConfig struct {
5337
Now func() time.Time
5438
}
5539

56-
// BuildValidURLFuncs translates the configuration into a list of ValidURLFuncs
57-
// to be run on the webhook.
58-
func buildValidURLFuncs(config ValidatorConfig) ([]ValidURLFunc, error) {
59-
var v []ValidURLFunc
60-
v = append(v, GoodURLScheme(config.URL.HTTPSOnly))
61-
if !config.URL.AllowLoopback {
62-
v = append(v, RejectLoopback())
63-
}
64-
if !config.URL.AllowIP {
65-
v = append(v, RejectAllIPs())
66-
}
67-
if !config.URL.AllowSpecialUseHosts {
68-
config.URL.InvalidHosts = append(config.URL.InvalidHosts, SpecialUseHosts...)
40+
// BuildURLChecker translates the configuration into url Checker to be run on the webhook.
41+
func (config *ValidatorConfig) BuildURLChecker() (*urlegit.Checker, error) {
42+
var o []urlegit.Option
43+
if len(config.URL.Schemes) > 0 {
44+
o = append(o, urlegit.OnlyAllowSchemes(config.URL.Schemes...))
6945
}
70-
if len(config.URL.InvalidHosts) > 0 {
71-
v = append(v, RejectHosts(config.URL.InvalidHosts))
72-
}
73-
if !config.URL.AllowSpecialUseIPs {
74-
config.URL.InvalidSubnets = append(config.URL.InvalidSubnets, SpecialUseIPs...)
75-
}
76-
if len(config.URL.InvalidSubnets) > 0 {
77-
fInvalidSubnets, err := InvalidSubnets(config.URL.InvalidSubnets)
78-
if err != nil {
79-
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidURLFuncs, err)
80-
}
81-
v = append(v, fInvalidSubnets)
82-
}
83-
return v, nil
84-
}
85-
86-
// BuildValidators translates the configuration into a list of validators to be run on the
87-
// webhook.
88-
func BuildValidators(config ValidatorConfig) (Validators, error) {
89-
v, err := buildValidURLFuncs(config)
90-
if err != nil {
91-
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidators, err)
46+
if !config.URL.AllowLoopback {
47+
o = append(o, urlegit.ForbidLoopback())
9248
}
93-
94-
vs := Validators{
95-
GoodConfigURL(v),
96-
GoodFailureURL(v),
97-
GoodAlternativeURLs(v),
98-
CheckEvents(),
99-
CheckDeviceID(),
100-
CheckUntilOrDurationExist(),
49+
if !config.IP.Allow {
50+
o = append(o, urlegit.ForbidAnyIPs())
10151
}
102-
fCheckDuration, err := CheckDuration(config.TTL.Max)
103-
if err != nil {
104-
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidators, err)
52+
if !config.Domain.AllowSpecialUseDomains {
53+
o = append(o, urlegit.ForbidSpecialUseDomains())
10554
}
106-
vs = append(vs, fCheckDuration)
107-
108-
fCheckUntil, err := CheckUntil(config.TTL.Jitter, config.TTL.Max, config.TTL.Now)
55+
checker, err := urlegit.New(o...)
10956
if err != nil {
110-
return nil, fmt.Errorf("%w: %v", errFailedToBuildValidators, err)
57+
return nil, err
11158
}
112-
vs = append(vs, fCheckUntil)
59+
return checker, nil
11360

114-
return vs, nil
11561
}

0 commit comments

Comments
 (0)