115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package auth
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"net"
|
||
|
|
"net/http"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ParseTrustedProxies parses the TRUSTED_PROXIES setting: a comma-separated
|
||
|
|
// list of CIDR ranges or bare IP addresses. Only a request whose TCP peer is
|
||
|
|
// inside one of these ranges may have its X-Forwarded-For header honoured. An
|
||
|
|
// empty value trusts no proxy.
|
||
|
|
func ParseTrustedProxies(value string) ([]*net.IPNet, error) {
|
||
|
|
var networks []*net.IPNet
|
||
|
|
for _, entry := range strings.Split(value, ",") {
|
||
|
|
entry = strings.TrimSpace(entry)
|
||
|
|
if entry == "" {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if !strings.Contains(entry, "/") {
|
||
|
|
ip := net.ParseIP(entry)
|
||
|
|
if ip == nil {
|
||
|
|
return nil, fmt.Errorf("trusted proxy %q is neither an IP address nor a CIDR range", entry)
|
||
|
|
}
|
||
|
|
bits := 8 * net.IPv6len
|
||
|
|
if ip.To4() != nil {
|
||
|
|
bits = 8 * net.IPv4len
|
||
|
|
}
|
||
|
|
entry = fmt.Sprintf("%s/%d", ip.String(), bits)
|
||
|
|
}
|
||
|
|
_, network, err := net.ParseCIDR(entry)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("trusted proxy %q is not a valid CIDR range: %w", entry, err)
|
||
|
|
}
|
||
|
|
networks = append(networks, network)
|
||
|
|
}
|
||
|
|
return networks, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// clientAddress returns the address a login attempt is counted against. The
|
||
|
|
// TCP peer is used unless it is a trusted proxy. For a trusted peer,
|
||
|
|
// X-Forwarded-For is read from the right and trusted hops are skipped, so the
|
||
|
|
// first untrusted address is the one the proxy chain actually saw. A malformed
|
||
|
|
// entry makes the peer address the answer, so a bad header can never choose the
|
||
|
|
// key.
|
||
|
|
func (s *Service) clientAddress(r *http.Request) string {
|
||
|
|
peer := peerHost(r)
|
||
|
|
if !s.isTrustedProxy(peer) {
|
||
|
|
return peer
|
||
|
|
}
|
||
|
|
hops := forwardedHops(r.Header.Values("X-Forwarded-For"))
|
||
|
|
if len(hops) == 0 {
|
||
|
|
return peer
|
||
|
|
}
|
||
|
|
for i := len(hops) - 1; i >= 0; i-- {
|
||
|
|
ip := net.ParseIP(hops[i])
|
||
|
|
if ip == nil {
|
||
|
|
return peer
|
||
|
|
}
|
||
|
|
if !s.isTrustedProxyIP(ip) {
|
||
|
|
return ip.String()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// Every hop is a trusted proxy, so the left-most one is closest to the client.
|
||
|
|
return net.ParseIP(hops[0]).String()
|
||
|
|
}
|
||
|
|
|
||
|
|
// withClientIP returns the request context carrying clientAddress, so the
|
||
|
|
// credential calls to AuthGuard report the end user's address (as
|
||
|
|
// X-Forwarded-For) instead of this service's. AuthGuard honours that header
|
||
|
|
// only for peers in its own TRUSTED_PROXIES list.
|
||
|
|
func (s *Service) withClientIP(r *http.Request) context.Context {
|
||
|
|
return authclient.WithClientIP(r.Context(), s.clientAddress(r))
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Service) isTrustedProxy(host string) bool {
|
||
|
|
ip := net.ParseIP(host)
|
||
|
|
return ip != nil && s.isTrustedProxyIP(ip)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Service) isTrustedProxyIP(ip net.IP) bool {
|
||
|
|
for _, network := range s.trustedProxies {
|
||
|
|
if network.Contains(ip) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
// peerHost is the host part of the address the TCP connection came from.
|
||
|
|
func peerHost(r *http.Request) string {
|
||
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||
|
|
if err != nil {
|
||
|
|
return r.RemoteAddr
|
||
|
|
}
|
||
|
|
return host
|
||
|
|
}
|
||
|
|
|
||
|
|
// forwardedHops flattens every X-Forwarded-For header line into its entries,
|
||
|
|
// left to right.
|
||
|
|
func forwardedHops(values []string) []string {
|
||
|
|
var hops []string
|
||
|
|
for _, value := range values {
|
||
|
|
for _, part := range strings.Split(value, ",") {
|
||
|
|
if part = strings.TrimSpace(part); part != "" {
|
||
|
|
hops = append(hops, part)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return hops
|
||
|
|
}
|