generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webhookURLValidator.go
210 lines (198 loc) · 5.85 KB
/
webhookURLValidator.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"errors"
"fmt"
"net"
"net/url"
"strings"
)
var (
errInvalidURL = errors.New("invalid Config URL")
errInvalidFailureURL = errors.New("invalid Failure URL")
errInvalidAlternativeURL = errors.New("invalid Alternative URL(s)")
errURLIsNotHTTPS = errors.New("URL scheme is not HTTPS")
errInvalidHost = errors.New("host is blocked")
errIPGivenAsHost = errors.New("cannot use IP as host")
errLoopbackGivenAsHost = errors.New("cannot use loopback host")
errIPinInvalidSubnets = errors.New("IP is within a blocked subnet")
errInvalidSubnet = errors.New("invalid subnet")
errNoSuchHost = errors.New("host does not exist")
errBadURLProtocol = errors.New("bad URL protocol")
errEmptyURL = errors.New("url cannot be an empty string")
)
// filterNil takes out all entries of Nil value from the slice.
func filterNil(vs []ValidURLFunc) (filtered []ValidURLFunc) {
for _, v := range vs {
if v != nil {
filtered = append(filtered, v)
}
}
return
}
// GoodConfigURL parses the given webhook's Config.URL
// and returns as soon as the URL is considered invalid. It returns nil if the URL is
// valid.
func GoodConfigURL(vs []ValidURLFunc) ValidatorFunc {
vs = filterNil(vs)
return func(w Webhook) error {
if w.Config.URL == "" {
return fmt.Errorf("%w: %v",
errInvalidURL, errEmptyURL)
}
parsedURL, err := url.ParseRequestURI(w.Config.URL)
if err != nil {
return fmt.Errorf("%w: %v", errInvalidURL, err)
}
for _, f := range vs {
err = f(parsedURL)
if err != nil {
return fmt.Errorf("%w: %v", errInvalidURL, err)
}
}
return nil
}
}
// GoodFailureURL parses the given webhook's FailureURL
// and returns as soon as the URL is considered invalid. It returns nil if the URL is
// valid.
func GoodFailureURL(vs []ValidURLFunc) ValidatorFunc {
vs = filterNil(vs)
return func(w Webhook) error {
if w.FailureURL == "" {
return nil
}
parsedFailureURL, err := url.ParseRequestURI(w.FailureURL)
if err != nil {
return fmt.Errorf("%w: %v", errInvalidFailureURL, err)
}
for _, f := range vs {
if err = f(parsedFailureURL); err != nil {
return fmt.Errorf("%w: %v", errInvalidFailureURL, err)
}
}
return nil
}
}
// GoodAlternativeURLs parses the given webhook's Config.AlternativeURLs
// and returns as soon as the URL is considered invalid. It returns nil if the URL is
// valid.
func GoodAlternativeURLs(vs []ValidURLFunc) ValidatorFunc {
vs = filterNil(vs)
return func(w Webhook) error {
for _, u := range w.Config.AlternativeURLs {
if u == "" {
return fmt.Errorf("%w: %v",
errInvalidAlternativeURL, errEmptyURL)
}
parsedAlternativeURL, err := url.ParseRequestURI(u)
if err != nil {
return fmt.Errorf("%w '%s': %v",
errInvalidAlternativeURL, u, err)
}
for _, f := range vs {
err = f(parsedAlternativeURL)
if err != nil {
return fmt.Errorf("%w '%s': %v",
errInvalidAlternativeURL, u, err)
}
}
}
return nil
}
}
// GoodURLScheme creates a ValidURLFunc that checks the scheme of the URL.
// If httpsOnly is true, then it will only allow URLs with "https" schemes.
// If httpsOnly is false, it will only allow URLs with "https" and "http" schemes.
func GoodURLScheme(httpsOnly bool) ValidURLFunc {
return func(u *url.URL) error {
if u.Scheme != "https" && u.Scheme != "http" {
return fmt.Errorf("%w: %s", errBadURLProtocol, u.Scheme)
}
if httpsOnly && u.Scheme != "https" {
return fmt.Errorf("%w: %s", errURLIsNotHTTPS, u.Scheme)
}
return nil
}
}
// RejectHosts creates a ValidURLFunc that checks the URL and ensures the
// host does not contain any strings in the list of invalid hosts. It returns an error
// if the host does include an invalid host name.
func RejectHosts(invalidHosts []string) ValidURLFunc {
ih := []string{}
for _, v := range invalidHosts {
if v != "" {
ih = append(ih, v)
}
}
return func(u *url.URL) error {
for _, v := range ih {
if strings.Contains(u.Host, v) {
return fmt.Errorf("%w: %s", errInvalidHost, u.Host)
}
}
return nil
}
}
// RejectALLIPs creates a ValidURLFunc that checks if the URL is an IP and returns an error
// if it is.
func RejectAllIPs() ValidURLFunc {
return func(u *url.URL) error {
host := u.Hostname()
ip := net.ParseIP(host)
if ip != nil {
return fmt.Errorf("%w: %v", errIPGivenAsHost, host)
}
return nil
}
}
// RejectLoopback creates a ValidURLFunc that returns an error if the given URL is
// a loopback address.
func RejectLoopback() ValidURLFunc {
return func(u *url.URL) error {
host := u.Hostname()
ip := net.ParseIP(host)
if ip != nil && ip.IsLoopback() {
return fmt.Errorf("%w: %v", errLoopbackGivenAsHost, ip)
}
ips, err := net.LookupIP(host)
if err != nil {
return fmt.Errorf("%w: %v", errNoSuchHost, err)
}
for _, i := range ips {
if i.IsLoopback() {
return fmt.Errorf("%w: %v lookup includes %v",
errLoopbackGivenAsHost, host, i)
}
}
return nil
}
}
// InvalidSubnets checks if the given URL is in any subnets we are blocking and returns
// an error if it is. SpecialIPs will return nil if the URL is not in the subnet.
func InvalidSubnets(i []string) (ValidURLFunc, error) {
invalidSubnets := []*net.IPNet{}
for _, sp := range i {
_, n, err := net.ParseCIDR(sp)
if err != nil {
return nil, fmt.Errorf("%w %s: %v", errInvalidSubnet, sp, err)
}
invalidSubnets = append(invalidSubnets, n)
}
return func(u *url.URL) error {
ips, err := net.LookupIP(u.Hostname())
if err != nil {
return fmt.Errorf("%w: %v", errInvalidURL, err)
}
for _, d := range ips {
for _, s := range invalidSubnets {
if s.Contains(d) {
return fmt.Errorf("%w: ip %s in %s",
errIPinInvalidSubnets, d, s)
}
}
}
return nil
}, nil
}