feat(deploy): restrict the public perimeter and resolve the client address
This commit is contained in:
1 parent
e52ffe7bcb
commit
57174dffba
25 files changed
+1224
-25
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// loadTrustedProxies reads TRUSTED_PROXY_CIDRS, a comma-separated list of
|
||||
// CIDR prefixes or single addresses of the reverse proxies in front of the
|
||||
// API. Unset or empty means no proxy is trusted: the client address is the
|
||||
// connecting peer and X-Forwarded-For is ignored.
|
||||
func (config *Config) loadTrustedProxies(lookup LookupEnv) error {
|
||||
value, _ := lookup("TRUSTED_PROXY_CIDRS")
|
||||
for entry := range strings.SplitSeq(value, ",") {
|
||||
entry = strings.TrimSpace(entry)
|
||||
if entry == "" {
|
||||
continue
|
||||
}
|
||||
prefix, err := parseProxyPrefix(entry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Config.loadTrustedProxies - TRUSTED_PROXY_CIDRS entry %q: %w", entry, err)
|
||||
}
|
||||
config.TrustedProxyCIDRs = append(config.TrustedProxyCIDRs, prefix)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseProxyPrefix(entry string) (netip.Prefix, error) {
|
||||
if !strings.Contains(entry, "/") {
|
||||
addr, err := netip.ParseAddr(entry)
|
||||
if err != nil {
|
||||
return netip.Prefix{}, fmt.Errorf("must be an IP address or a CIDR prefix")
|
||||
}
|
||||
addr = addr.Unmap()
|
||||
return netip.PrefixFrom(addr, addr.BitLen()), nil
|
||||
}
|
||||
prefix, err := netip.ParsePrefix(entry)
|
||||
if err != nil {
|
||||
return netip.Prefix{}, fmt.Errorf("must be an IP address or a CIDR prefix")
|
||||
}
|
||||
if prefix.Bits() == 0 {
|
||||
// A prefix that matches every address would trust every client and
|
||||
// let it choose its own address.
|
||||
return netip.Prefix{}, fmt.Errorf("must not match every address")
|
||||
}
|
||||
return prefix.Masked(), nil
|
||||
}
|
||||
Reference in new issue
Block a user