4
4
package ancla
5
5
6
6
import (
7
- "errors"
8
- "fmt"
9
7
"time"
10
- )
11
8
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"
33
10
)
34
11
35
12
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
38
27
}
39
28
40
29
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
48
32
}
49
33
50
34
type TTLVConfig struct {
@@ -53,63 +37,25 @@ type TTLVConfig struct {
53
37
Now func () time.Time
54
38
}
55
39
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 ... ))
69
45
}
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 ())
92
48
}
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 ())
101
51
}
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 ())
105
54
}
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 ... )
109
56
if err != nil {
110
- return nil , fmt . Errorf ( "%w: %v" , errFailedToBuildValidators , err )
57
+ return nil , err
111
58
}
112
- vs = append ( vs , fCheckUntil )
59
+ return checker , nil
113
60
114
- return vs , nil
115
61
}
0 commit comments