feat(deploy): restrict the public perimeter and resolve the client address

This commit is contained in:
shira committed 2026-10-06 21:32:03 +03:00
1 parent e52ffe7bcb
commit 57174dffba
25 files changed
+1224 -25

No files matched your search

+9
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"net/netip"
"os"
"strconv"
"strings"
@@ -118,6 +119,11 @@ type Config struct {
// PasswordHashConcurrency is the number of password hash derivations
// that may run at once; further logins and registrations wait.
PasswordHashConcurrency int
// TrustedProxyCIDRs are the reverse proxies in front of the API. The
// client address is taken from X-Forwarded-For only through them (see
// middleware.ClientIP). Empty trusts no proxy.
TrustedProxyCIDRs []netip.Prefix
}
type LookupEnv func(string) (string, bool)
@@ -167,6 +173,9 @@ func LoadFromEnv(lookup LookupEnv) (Config, error) {
if err := config.loadPasswordHash(lookup); err != nil {
return Config{}, err
}
if err := config.loadTrustedProxies(lookup); err != nil {
return Config{}, err
}
var err error
config.DatabaseMigrateOnStart, err = boolOrDefault(lookup, "DATABASE_MIGRATE_ON_START", defaultDatabaseMigrateOnStart)
+48
View File
@@ -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
}
+45
View File
@@ -0,0 +1,45 @@
package config
import (
"strings"
"testing"
)
func TestLoadFromEnvTrustedProxyCIDRs(t *testing.T) {
tests := map[string]struct {
value string
want []string
}{
"unset": {value: "", want: nil},
"list": {value: " 172.16.0.0/12, 10.79.0.1 ,fd00::/8", want: []string{"172.16.0.0/12", "10.79.0.1/32", "fd00::/8"}},
"masked": {value: "10.79.0.1/30", want: []string{"10.79.0.0/30"}},
"empty items": {value: ",,10.0.0.0/8,", want: []string{"10.0.0.0/8"}},
}
for name, test := range tests {
settings, err := LoadFromEnv(testEnvironment(map[string]string{"TRUSTED_PROXY_CIDRS": test.value}))
if err != nil {
t.Fatalf("LoadFromEnv(%s) error = %v", name, err)
}
got := make([]string, 0, len(settings.TrustedProxyCIDRs))
for _, prefix := range settings.TrustedProxyCIDRs {
got = append(got, prefix.String())
}
if strings.Join(got, ";") != strings.Join(test.want, ";") {
t.Fatalf("LoadFromEnv(%s) TrustedProxyCIDRs = %v, want %v", name, got, test.want)
}
}
}
func TestLoadFromEnvRejectsTrustedProxyCIDRs(t *testing.T) {
for name, value := range map[string]string{
"not an address": "proxy.internal",
"bad prefix": "10.0.0.0/33",
"match all v4": "0.0.0.0/0",
"match all v6": "::/0",
} {
_, err := LoadFromEnv(testEnvironment(map[string]string{"TRUSTED_PROXY_CIDRS": value}))
if err == nil || !strings.Contains(err.Error(), "Config.loadTrustedProxies - TRUSTED_PROXY_CIDRS") {
t.Fatalf("LoadFromEnv(%s) error = %v, want a TRUSTED_PROXY_CIDRS error", name, err)
}
}
}