| 1 | // Copyright © SAS Institute Inc. |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package realipscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ |
| 15 | |
| 16 | import ( |
| 17 | "contextscip-go gomod github.com/golang/go/src go1.18 context/" |
| 18 | "crypto/x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/" |
| 19 | "encoding/pemscip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 21 | "netscip-go gomod github.com/golang/go/src go1.18 net/" |
| 22 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 23 | "net/urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 25 | |
| 26 | "github.com/sassoftware/relic/v7/internal/zhttpscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/zhttp`/" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | forwardedForscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/forwardedFor. = "X-Forwarded-For" |
| 31 | forwardedHostscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/forwardedHost. = "X-Forwarded-Host" |
| 32 | forwardedProtoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/forwardedProto. = "X-Forwarded-Proto" |
| 33 | sslClientCertscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/sslClientCert. = "Ssl-Client-Cert" |
| 34 | ) |
| 35 | |
| 36 | // Middleware processes headers set by a trusted reverse proxy in front of the |
| 37 | // server. Client IPs are checked against the provided list of networks, and the |
| 38 | // "real" IP passed by the trusted proxy replaces req.RemoteAddr. |
| 39 | func Middlewarescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/Middleware().(trustedProxies []string) (func(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Handler#) httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Handler#, error) { |
| 40 | // parse net list |
| 41 | trustedNets, err := parseTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/parseTrusted().(trustedProxies) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | return func(next httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Handler#) httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Handlerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Handler# { |
| 46 | return httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.HandlerFuncscip-go gomod github.com/golang/go/src go1.18 `net/http`/HandlerFunc#(func(w httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.18 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) { |
| 47 | remoteIP, proxied := trustedClientscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/trustedClient().(trustedNets, r) |
| 48 | r.RemoteAddrscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#RemoteAddr. = remoteIP |
| 49 | if proxied { |
| 50 | ctx := contextscip-go gomod github.com/golang/go/src go1.18 context/.WithValuescip-go gomod github.com/golang/go/src go1.18 context/WithValue().(r.Contextscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Context().(), ctxKeyTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ctxKeyTrusted., proxied) |
| 51 | r = r.WithContextscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#WithContext().(ctx) |
| 52 | } |
| 53 | next.ServeHTTPscip-go gomod github.com/golang/go/src go1.18 `net/http`/Handler#ServeHTTP().(w, r) |
| 54 | }) |
| 55 | }, nil |
| 56 | } |
| 57 | |
| 58 | // BaseURL returns an estimate of the URL that the client used to access the |
| 59 | // service. Headers like X-Forwarded-Host are taken into account if the |
| 60 | // connection is from a trusted proxy. |
| 61 | func BaseURLscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/BaseURL().(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) *urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL# { |
| 62 | u := &urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#{Schemescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Scheme.: "http", Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host.: req.Hostscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Host.} |
| 63 | if req.TLSscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#TLS. != nil { |
| 64 | u.Schemescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Scheme. = "https" |
| 65 | } |
| 66 | if requestTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/requestTrusted().(req) { |
| 67 | if host := req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().(forwardedHostscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/forwardedHost.); host != "" { |
| 68 | u.Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host. = host |
| 69 | } |
| 70 | if scheme := req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().(forwardedProtoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/forwardedProto.); scheme != "" { |
| 71 | u.Schemescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Scheme. = scheme |
| 72 | } |
| 73 | } |
| 74 | return u |
| 75 | } |
| 76 | |
| 77 | // PeerCertificates returns the unverified set of certificates provided by the |
| 78 | // client, using the Ssl-Client-Cert header if the connection is from a trusted |
| 79 | // proxy. |
| 80 | func PeerCertificatesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/PeerCertificates().(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) ([]*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#, error) { |
| 81 | if !requestTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/requestTrusted().(req) { |
| 82 | // direct untrusted connection |
| 83 | if req.TLSscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#TLS. != nil { |
| 84 | return req.TLSscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#TLS..PeerCertificatesscip-go gomod github.com/golang/go/src go1.18 `crypto/tls`/ConnectionState#PeerCertificates., nil |
| 85 | } |
| 86 | return nil, nil |
| 87 | } |
| 88 | certString := req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Get().(sslClientCertscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/sslClientCert.) |
| 89 | if certString == "" { |
| 90 | return nil, nil |
| 91 | } |
| 92 | certString, err := urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/.PathUnescapescip-go gomod github.com/golang/go/src go1.18 `net/url`/PathUnescape().(certString) |
| 93 | if err != nil { |
| 94 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s: invalid URL encoding: %w", sslClientCertscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/sslClientCert., err) |
| 95 | } |
| 96 | pemBytes := []byte(certString) |
| 97 | var ret []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate# |
| 98 | for { |
| 99 | var b *pemscip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/.Blockscip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/Block# |
| 100 | b, pemBytes = pemscip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/Decode().(pemBytes) |
| 101 | if b == nil { |
| 102 | break |
| 103 | } else if b.Typescip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/Block#Type. != "CERTIFICATE" { |
| 104 | continue |
| 105 | } |
| 106 | cert, err := x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.ParseCertificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/ParseCertificate().(b.Bytesscip-go gomod github.com/golang/go/src go1.18 `encoding/pem`/Block#Bytes.) |
| 107 | if err != nil { |
| 108 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("%s: parsing certificate %d: %w", sslClientCertscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/sslClientCert., len(ret), err) |
| 109 | } |
| 110 | ret = append(ret, cert) |
| 111 | } |
| 112 | return ret, nil |
| 113 | } |
| 114 | |
| 115 | func parseTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/parseTrusted().(trustedProxies []string) ([]*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#, error) { |
| 116 | var trustedNets []*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet# |
| 117 | for _, v := range trustedProxies { |
| 118 | var ipnet *netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet# |
| 119 | if stringsscip-go gomod github.com/golang/go/src go1.18 strings/.ContainsRunescip-go gomod github.com/golang/go/src go1.18 strings/ContainsRune().(v, '/') { |
| 120 | var err error |
| 121 | _, ipnet, err = netscip-go gomod github.com/golang/go/src go1.18 net/.ParseCIDRscip-go gomod github.com/golang/go/src go1.18 net/ParseCIDR().(v) |
| 122 | if err != nil { |
| 123 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("trusted_proxies %q: %w", v, err) |
| 124 | } |
| 125 | } else { |
| 126 | ip := netscip-go gomod github.com/golang/go/src go1.18 net/.ParseIPscip-go gomod github.com/golang/go/src go1.18 net/ParseIP().(v) |
| 127 | if ip == nil { |
| 128 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("trusted_proxies %q: invalid IP or IP network", v) |
| 129 | } |
| 130 | ipnet = &netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#{IPscip-go gomod github.com/golang/go/src go1.18 net/IPNet#IP.: ip} |
| 131 | if ip.To4scip-go gomod github.com/golang/go/src go1.18 net/IP#To4().() != nil { |
| 132 | ipnet.Maskscip-go gomod github.com/golang/go/src go1.18 net/IPNet#Mask. = netscip-go gomod github.com/golang/go/src go1.18 net/.CIDRMaskscip-go gomod github.com/golang/go/src go1.18 net/CIDRMask().(32, 32) |
| 133 | } else { |
| 134 | ipnet.Maskscip-go gomod github.com/golang/go/src go1.18 net/IPNet#Mask. = netscip-go gomod github.com/golang/go/src go1.18 net/.CIDRMaskscip-go gomod github.com/golang/go/src go1.18 net/CIDRMask().(128, 128) |
| 135 | } |
| 136 | } |
| 137 | trustedNets = append(trustedNets, ipnet) |
| 138 | } |
| 139 | return trustedNets, nil |
| 140 | } |
| 141 | |
| 142 | func trustedClientscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/trustedClient().(trustedNets []*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#, req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) (string, bool) { |
| 143 | remoteIP := zhttpscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/zhttp`/.StripPortscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/zhttp`/StripPort().(req.RemoteAddrscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#RemoteAddr.) |
| 144 | if !hopTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/hopTrusted().(trustedNets, remoteIP) { |
| 145 | // first hop is not trusted |
| 146 | return remoteIP, false |
| 147 | } |
| 148 | // Parse all XFF headers into a single list of hops |
| 149 | var hops []string |
| 150 | for _, xff := range req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Valuesscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Values().(forwardedForscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/forwardedFor.) { |
| 151 | for _, hop := range stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(xff, ",") { |
| 152 | hop = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(hop) |
| 153 | if hop != "" { |
| 154 | hops = append(hops, hop) |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | // Check each hop, starting from the closest one to us |
| 159 | for i := len(hops) - 1; i >= 0; i-- { |
| 160 | nextHop := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(hops[i]) |
| 161 | if !hopTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/hopTrusted().(trustedNets, nextHop) { |
| 162 | // The previous hop is trusted but this one is not, so this is the |
| 163 | // best client IP |
| 164 | return nextHop, true |
| 165 | } |
| 166 | } |
| 167 | // No untrusted hops were found, so whatever the last one in the chain is is |
| 168 | // the client IP. |
| 169 | if len(hops) != 0 { |
| 170 | return hops[0], true |
| 171 | } |
| 172 | return remoteIP, false |
| 173 | } |
| 174 | |
| 175 | func hopTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/hopTrusted().(trustedNets []*netscip-go gomod github.com/golang/go/src go1.18 net/.IPNetscip-go gomod github.com/golang/go/src go1.18 net/IPNet#, addr string) bool { |
| 176 | if addr == "@" { |
| 177 | // UNIX socket |
| 178 | return true |
| 179 | } |
| 180 | ip := netscip-go gomod github.com/golang/go/src go1.18 net/.ParseIPscip-go gomod github.com/golang/go/src go1.18 net/ParseIP().(addr) |
| 181 | if ip == nil { |
| 182 | return false |
| 183 | } |
| 184 | for _, p := range trustedNets { |
| 185 | if p.Containsscip-go gomod github.com/golang/go/src go1.18 net/IPNet#Contains().(ip) { |
| 186 | return true |
| 187 | } |
| 188 | } |
| 189 | return false |
| 190 | } |
| 191 | |
| 192 | type ctxKeyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ctxKey# int |
| 193 | |
| 194 | var ctxKeyTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ctxKeyTrusted. ctxKeyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ctxKey# = 1 |
| 195 | |
| 196 | func requestTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/requestTrusted().(req *httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#) bool { |
| 197 | trusted, _ := req.Contextscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.18 context/Context#Value().(ctxKeyTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ctxKeyTrusted.).(bool) |
| 198 | return trusted |
| 199 | } |