-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
realip.go
310 lines (260 loc) · 8.58 KB
/
realip.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package chix
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"github.com/lrstanley/go-bogon"
)
const (
OptTrustBogon RealIPOptions = 1 << iota // Trust bogon IP ranges (private IP ranges).
OptTrustAny // Trust any proxy (DON'T USE THIS!).
OptTrustCloudflare // Trust Cloudflare's origin IPs.
OptUseXForwardedFor // Allow using the X-Forwarded-For header.
OptUseXRealIP // Allow using the X-Real-IP header.
OptUseTrueClientIP // Allow using the True-Client-IP header.
OptUseCFConnectingIP // Allow using the CF-Connecting-IP header.
OptDefaultTrust = OptTrustBogon | OptUseXForwardedFor // Default trust options.
xForwardedFor = "X-Forwarded-For"
xRealIP = "X-Real-IP"
trueClientIP = "True-Client-IP"
)
// RealIPOptions is a bitmask of options that can be passed to RealIP.
type RealIPOptions int
// UseRealIPDefault is a convenience function that wraps RealIP with the default
// options (OptTrustBogon and OptUseXForwardedFor).
func UseRealIPDefault(next http.Handler) http.Handler {
return UseRealIP(nil, OptDefaultTrust)(next)
}
// UseRealIPCLIOpts is a convenience function that wraps RealIP, with support for
// configuring the middleware via CLI flags. You can pass in an array that contains
// a mix of different supported headers, "cloudflare", "*" (or "any", "all") to
// trust anything, "local" (or "localhost", "bogon", "internal") for bogon IPs,
// and anything else gets passed in as allowed CIDRs.
//
// If no options are passed in, the default will use the same as chix.UseRealIPDefault
// (OptTrustBogon and OptUseXForwardedFor).
func UseRealIPCLIOpts(options []string) func(next http.Handler) http.Handler {
if len(options) == 0 {
return UseRealIPDefault
}
var flags RealIPOptions
var proxies []string
for _, option := range options {
switch strings.ToLower(option) {
case "cloudflare", "cf-connecting-ip":
flags |= OptTrustCloudflare | OptUseCFConnectingIP
case "x-forwarded-for":
flags |= OptUseXForwardedFor
case "x-real-ip":
flags |= OptUseXRealIP
case "true-client-ip":
flags |= OptUseTrueClientIP
case "*", "any", "all":
flags |= OptTrustAny
case "local", "localhost", "bogon", "internal":
flags |= OptTrustBogon
default:
proxies = append(proxies, option)
}
}
if flags == 0 {
flags = OptDefaultTrust
}
return UseRealIP(proxies, flags)
}
// UseRealIP is a middleware that allows passing the real IP address of the client
// only if the request headers that include an override, come from a trusted
// proxy. Pass an optional list of trusted proxies to trust, as well as
// any additional options to control the behavior of the middleware. See the
// related Opt* constants for more information. Will panic if invalid IP's or
// ranges are specified.
//
// NOTE: if multiple headers are configured to be trusted, the lookup order is:
// - CF-Connecting-IP
// - X-Real-IP
// - True-Client-IP
// - X-Forwarded-For
//
// Examples:
//
// router.Use(chix.UseRealIP([]string{"1.2.3.4", "10.0.0.0/24"}, chix.OptUseXForwardedFor))
// router.Use(nil, chix.OptTrustBogon|chix.OptUseXForwardedFor))
func UseRealIP(trusted []string, flags RealIPOptions) func(next http.Handler) http.Handler {
if flags == 0 {
panic(ErrRealIPNoOpts)
}
// Must provide at least one proxy header type.
if flags&(OptUseXForwardedFor|OptUseXRealIP|OptUseTrueClientIP|OptUseCFConnectingIP) == 0 {
panic(ErrRealIPNoSource)
}
// ¯\_(ツ)_/¯.
if flags&OptTrustAny != 0 {
trusted = append(trusted, "0.0.0.0/0")
}
rip := &realIP{
trusted: []*net.IPNet{},
}
// Add all known bogon IP ranges.
if flags&OptTrustBogon != 0 {
rip.trusted = append(rip.trusted, bogon.DefaultRanges()...)
}
if flags&OptTrustCloudflare != 0 {
rip.trusted = append(rip.trusted, cloudflareRanges()...)
}
// Start parsing user-provided CIDR's and/or IP's.
for _, proxy := range trusted {
if !strings.Contains(proxy, "/") {
ip := parseIP(proxy)
if ip == nil {
panic(&ErrRealIPInvalidIP{Err: &net.ParseError{Type: "IP address", Text: proxy}})
}
switch len(ip) {
case net.IPv4len:
proxy += "/32"
case net.IPv6len:
proxy += "/128"
}
}
_, cidr, err := net.ParseCIDR(proxy)
if err != nil {
panic(fmt.Errorf("chix: realip: invalid CIDR %w", err))
}
rip.trusted = append(rip.trusted, cidr)
}
if len(rip.trusted) == 0 {
panic(ErrRealIPNoTrusted)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
if err != nil {
goto nexthandler // Fallback and don't modify.
}
if trusted := rip.isTrustedProxy(net.ParseIP(ip)); !trusted {
goto nexthandler // Fallback and don't modify.
}
// Parse enabled headers by most specific (and common) to least.
if flags&OptUseCFConnectingIP != 0 {
if value := parseIP(r.Header.Get("CF-Connecting-IP")); value != nil {
r.RemoteAddr = value.String()
goto nexthandler
}
}
if flags&OptUseXRealIP != 0 {
if value := parseIP(r.Header.Get(xRealIP)); value != nil {
r.RemoteAddr = value.String()
goto nexthandler
}
}
if flags&OptUseTrueClientIP != 0 {
if value := parseIP(r.Header.Get(trueClientIP)); value != nil {
r.RemoteAddr = value.String()
goto nexthandler
}
}
if flags&OptUseXForwardedFor != 0 {
if value, valid := rip.parseForwardedFor(r.Header.Get(xForwardedFor)); valid && value != "" {
r.RemoteAddr = value
goto nexthandler
}
}
nexthandler:
next.ServeHTTP(w, r)
})
}
}
type realIP struct {
trusted []*net.IPNet
}
// isTrustedProxy will check whether the IP address is included in the trusted
// list according to realIP.trusted.
func (rip *realIP) isTrustedProxy(ip net.IP) bool {
if ip == nil || rip.trusted == nil {
return false
}
for _, cidr := range rip.trusted {
if cidr.Contains(ip) {
return true
}
}
return false
}
// parseForwardedFor will parse the X-Forwarded-For header in the proper
// direction (reversed).
func (rip *realIP) parseForwardedFor(value string) (clientIP string, valid bool) {
if value == "" {
return "", false
}
items := strings.Split(value, ",")
// X-Forwarded-For is appended by each proxy. Check IPs in reverse order
// and stop when find untrusted proxy.
for i := len(items) - 1; i >= 0; i-- {
raw := strings.TrimSpace(items[i])
ip := net.ParseIP(raw)
if ip == nil {
break
}
if (i == 0) || (!rip.isTrustedProxy(ip)) {
return raw, true
}
}
return "", false
}
// parseIP parse a string representation of an IP and returns a net.IP with
// the appropriate byte representation or nil, if the input is invalid.
func parseIP(ip string) net.IP {
parsedIP := net.ParseIP(strings.TrimSpace(ip))
if parsedIP != nil {
if v4 := parsedIP.To4(); v4 != nil {
return v4
}
}
return parsedIP
}
// UsePrivateIP can be used to allow only private IP's to access specific
// routes. Make sure to register this middleware after UseRealIP, otherwise
// the IP checking may be incorrect.
func UsePrivateIP(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ok, _ := bogon.Is(sanitizeIP(r.RemoteAddr)); ok {
next.ServeHTTP(w, r)
return
}
_ = Error(w, r, WrapError(ErrAccessDenied, http.StatusForbidden))
})
}
// UseContextIP can be used to add the requests IP to the context. This is beneficial
// for passing the request context to a request-unaware function/method/service, that
// does not have access to the original request. Ensure that this middleware is
// registered after UseRealIP, otherwise the stored IP may be incorrect.
func UseContextIP(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(
context.WithValue(
r.Context(),
contextIP,
parseIP(sanitizeIP(r.RemoteAddr)),
),
))
})
}
// GetContextIP can be used to retrieve the IP from the context, that was previously
// set by UseContextIP. If no IP was set, nil is returned.
func GetContextIP(ctx context.Context) net.IP {
if ip, ok := ctx.Value(contextIP).(net.IP); ok {
return ip
}
return nil
}
func sanitizeIP(input string) (ip string) {
ip, _, err := net.SplitHostPort(strings.TrimSpace(input))
if err != nil || ip == "" {
ip = input
}
return ip
}