@@ -46,6 +46,7 @@ func NewMiddleware(trusted []string) (func(http.Handler) http.Handler, error) {
|
||||
r.Header.Del(h)
|
||||
}
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}, nil
|
||||
@@ -53,6 +54,7 @@ func NewMiddleware(trusted []string) (func(http.Handler) http.Handler, error) {
|
||||
|
||||
func parseTrusted(trusted []string) ([]net.IP, []*net.IPNet, error) {
|
||||
ips := make([]net.IP, 0, len(trusted))
|
||||
|
||||
nets := make([]*net.IPNet, 0, len(trusted))
|
||||
for _, entry := range trusted {
|
||||
if strings.Contains(entry, "/") {
|
||||
@@ -60,7 +62,9 @@ func parseTrusted(trusted []string) ([]net.IP, []*net.IPNet, error) {
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("cannot parse CIDR %q: %w", entry, err)
|
||||
}
|
||||
|
||||
nets = append(nets, ipNet)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -68,8 +72,10 @@ func parseTrusted(trusted []string) ([]net.IP, []*net.IPNet, error) {
|
||||
if ip == nil {
|
||||
return nil, nil, fmt.Errorf("cannot parse IP address %q", entry)
|
||||
}
|
||||
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
|
||||
return ips, nets, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -31,18 +31,22 @@ func runMiddleware(t *testing.T, trusted []string, remoteAddr string, headers ma
|
||||
require.NoError(t, err)
|
||||
|
||||
var captured *http.Request
|
||||
|
||||
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
captured = r
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
||||
req.RemoteAddr = remoteAddr
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
handler.ServeHTTP(httptest.NewRecorder(), req)
|
||||
|
||||
require.NotNil(t, captured)
|
||||
|
||||
return captured
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user