hostenv: CARLOS_INGRESS_PROXY_SOURCES with validation
The region NLB's private node addresses the edge will trust to send a PROXY protocol v2 header (spec §4.2). A new Optional key; the exported ParseProxySources (1 to 16 distinct RFC 1918 IPv4 addresses, empty is none) refuses a bad value at Assemble, Seed and AssembleGroup with ErrValue naming the variable only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+197
−0
internal/hostenv/group.go+3 −0internal/hostenv/hostenv.go+55 −0internal/hostenv/hostenv_test.go+139 −0
Intent
diff --git a/internal/hostenv/group.go b/internal/hostenv/group.go| index bcec60eb7..f8965b2e3 100644 |
| --- a/internal/hostenv/group.go |
| +++ b/internal/hostenv/group.go |
| @@ -138,6 +138,9 @@ func AssembleGroup(shared, regional map[string]string, c GroupConfig, queue stri |
| if err := validateIngressTargetGroups(env, c.Account, c.AWSRegion); err != nil { |
| return nil, err |
| } |
| + if err := validateProxySources(env); err != nil { |
| + return nil, err |
| + } |
| if _, err := Render(env); err != nil { |
| return nil, err |
| } |
diff --git a/internal/hostenv/hostenv.go b/internal/hostenv/hostenv.go| index d9d6e5c87..195d23e9c 100644 |
| --- a/internal/hostenv/hostenv.go |
| +++ b/internal/hostenv/hostenv.go |
| @@ -22,6 +22,7 @@ import ( |
| "github.com/carlosframework/platform/internal/controlname" |
| "github.com/carlosframework/platform/internal/demo" |
| "github.com/carlosframework/platform/internal/ingresshealth" |
| + "net/netip" |
| "regexp" |
| "slices" |
| "sort" |
| @@ -94,6 +95,7 @@ var Optional = []string{ |
| "CARLOS_HIBERNATE_PREFIX", |
| "CARLOS_HOST_CLAIM_RETIRED_LOCATIONS", |
| "CARLOS_HOST_METRICS", |
| + "CARLOS_INGRESS_PROXY_SOURCES", |
| "CARLOS_INGRESS_TARGET_GROUPS", |
| "CARLOS_LAYOUT_DEFAULT", |
| "CARLOS_MOVES_PHASE1", |
| @@ -234,6 +236,9 @@ func Assemble(shared, regional map[string]string) (map[string]string, error) { |
| if err := validateIngressTargetGroups(env, "", ""); err != nil { |
| return nil, err |
| } |
| + if err := validateProxySources(env); err != nil { |
| + return nil, err |
| + } |
| return env, nil |
| } |
| @@ -283,6 +288,53 @@ func validateIngressTargetGroups(env map[string]string, account, region string) |
| return nil |
| } |
| +// proxySources is the region NLB's private node addresses, the only |
| +// socket peers the edge trusts to send a PROXY protocol v2 header (edge |
| +// PROXY protocol spec §4.2). |
| +const proxySources = "CARLOS_INGRESS_PROXY_SOURCES" |
| + |
| +// maxProxySources bounds the list: one address per enabled zone of the |
| +// region NLB. |
| +const maxProxySources = 16 |
| + |
| +// ParseProxySources reads a CARLOS_INGRESS_PROXY_SOURCES value: a |
| +// comma-separated list of 1 to 16 distinct RFC 1918 IPv4 addresses |
| +// (10/8, 172.16/12, 192.168/16), in the order given. An empty or |
| +// whitespace-only value is no list and gives nil, nil: the edge then |
| +// trusts no peer and reads no header. Anything else, a public or IPv6 |
| +// address, a prefix, an empty entry, a repeat or a 17th entry, is |
| +// ErrValue naming the variable only, never the value. The edge reads its |
| +// list with this parser, and write and seed refuse with it, so a value |
| +// the edge would refuse never reaches a box. |
| +func ParseProxySources(value string) ([]netip.Addr, error) { |
| + value = strings.TrimSpace(value) |
| + if value == "" { |
| + return nil, nil |
| + } |
| + refused := fmt.Errorf("%w: %s", ErrValue, proxySources) |
| + entries := strings.Split(value, ",") |
| + if len(entries) > maxProxySources { |
| + return nil, refused |
| + } |
| + out := make([]netip.Addr, 0, len(entries)) |
| + for _, raw := range entries { |
| + addr, err := netip.ParseAddr(strings.TrimSpace(raw)) |
| + if err != nil || !addr.Is4() || !addr.IsPrivate() || slices.Contains(out, addr) { |
| + return nil, refused |
| + } |
| + out = append(out, addr) |
| + } |
| + return out, nil |
| +} |
| + |
| +// validateProxySources refuses, at write and seed, a |
| +// CARLOS_INGRESS_PROXY_SOURCES the edge could not read. An empty or |
| +// absent value passes: the edge reads no header, as before the variable. |
| +func validateProxySources(env map[string]string) error { |
| + _, err := ParseProxySources(env[proxySources]) |
| + return err |
| +} |
| + |
| // Missing lists the Required variables env has no value for, sorted. |
| func Missing(env map[string]string) []string { |
| var out []string |
| @@ -465,6 +517,9 @@ func Seed(ctx context.Context, p Params, deployment, region string, env map[stri |
| if err := validateIngressTargetGroups(env, "", ""); err != nil { |
| return Seeded{}, err |
| } |
| + if err := validateProxySources(env); err != nil { |
| + return Seeded{}, err |
| + } |
| shared, regional := Split(env) |
| delete(regional, BoxReference) |
| existing, err := p.ByPath(ctx, sharedPrefix) |
diff --git a/internal/hostenv/hostenv_test.go b/internal/hostenv/hostenv_test.go| index 21d9bd89f..243af5f94 100644 |
| --- a/internal/hostenv/hostenv_test.go |
| +++ b/internal/hostenv/hostenv_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "context" |
| "errors" |
| "fmt" |
| + "net/netip" |
| "os" |
| "path/filepath" |
| "reflect" |
| @@ -636,3 +637,141 @@ func TestIngressTargetGroupsInAGroupBoxHostEnv(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// The region NLB's private node addresses the edge trusts to send a PROXY |
| +// protocol v2 header (edge PROXY protocol spec §4.2). Seed and Fetch |
| +// carry the value through; anything but 1 to 16 distinct private IPv4 |
| +// addresses is refused by write and by seed alike, by name, never by |
| +// value. |
| +func TestProxySourcesHostEnv(t *testing.T) { |
| + const key = "CARLOS_INGRESS_PROXY_SOURCES" |
| + if !Allowed(key) { |
| + t.Fatalf("%s is not in the host.env allowlist", key) |
| + } |
| + for _, value := range []string{"", "172.31.29.110", "172.31.29.110,172.31.11.189"} { |
| + env := today() |
| + if value != "" { |
| + env[key] = value |
| + } |
| + p := &fakeParams{vals: map[string]string{}} |
| + if _, err := Seed(t.Context(), p, "shop", "ie", env); err != nil { |
| + t.Fatalf("seed %q: %v", value, err) |
| + } |
| + if value != "" && p.vals["/carlos/shop/ie/"+key] != value { |
| + t.Fatalf("seed %q put it %v", value, p.vals) |
| + } |
| + body, err := Fetch(t.Context(), p, "shop", "ie") |
| + if err != nil { |
| + t.Fatalf("fetch %q: %v", value, err) |
| + } |
| + got, err := Parse(body) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got[key] != value { |
| + t.Errorf("round trip of %q gave %q", value, got[key]) |
| + } |
| + } |
| +} |
| + |
| +func TestProxySourcesParse(t *testing.T) { |
| + sixteen := make([]string, 16) |
| + var sixteenAddrs []netip.Addr |
| + for i := range sixteen { |
| + sixteen[i] = fmt.Sprintf("10.0.0.%d", i+1) |
| + sixteenAddrs = append(sixteenAddrs, netip.MustParseAddr(sixteen[i])) |
| + } |
| + for value, want := range map[string][]netip.Addr{ |
| + "": nil, |
| + " ": nil, |
| + "172.31.29.110": {netip.MustParseAddr("172.31.29.110")}, |
| + "172.31.29.110,172.31.11.189": {netip.MustParseAddr("172.31.29.110"), netip.MustParseAddr("172.31.11.189")}, |
| + "10.1.2.3,192.168.0.9": {netip.MustParseAddr("10.1.2.3"), netip.MustParseAddr("192.168.0.9")}, |
| + strings.Join(sixteen, ","): sixteenAddrs, |
| + } { |
| + got, err := ParseProxySources(value) |
| + if err != nil || !slices.Equal(got, want) { |
| + t.Errorf("ParseProxySources(%q) = %v, %v; want %v", value, got, err, want) |
| + } |
| + } |
| +} |
| + |
| +func TestProxySourcesRefused(t *testing.T) { |
| + const key = "CARLOS_INGRESS_PROXY_SOURCES" |
| + seventeen := make([]string, 17) |
| + for i := range seventeen { |
| + seventeen[i] = fmt.Sprintf("172.31.1.%d", i+1) |
| + } |
| + for name, value := range map[string]string{ |
| + "public": "54.77.110.223", |
| + "repeated": "172.31.1.1,172.31.1.1", |
| + "ipv6": "fe80::1", |
| + "emptyEntry": "172.31.1.1,", |
| + "seventeen": strings.Join(seventeen, ","), |
| + "cidr": "172.31.1.1/32", |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + checkMessage := func(what string, err error) { |
| + t.Helper() |
| + if !strings.Contains(err.Error(), key) { |
| + t.Errorf("%s: %q does not name %s", what, err, key) |
| + } |
| + if strings.Contains(err.Error(), value) { |
| + t.Errorf("%s: %q prints the value", what, err) |
| + } |
| + } |
| + addrs, err := ParseProxySources(value) |
| + if !errors.Is(err, ErrValue) || addrs != nil { |
| + t.Fatalf("parse: %v, %v; want ErrValue", addrs, err) |
| + } |
| + checkMessage("parse", err) |
| + env := today() |
| + env[key] = value |
| + p := &fakeParams{vals: map[string]string{}} |
| + _, err = Seed(t.Context(), p, "shop", "ie", env) |
| + if !errors.Is(err, ErrValue) { |
| + t.Fatalf("seed: %v, want ErrValue", err) |
| + } |
| + checkMessage("seed", err) |
| + if len(p.sets) != 0 { |
| + t.Fatalf("seed wrote %v before refusing", p.sets) |
| + } |
| + shared, regional := Split(env) |
| + got, err := Assemble(shared, regional) |
| + if !errors.Is(err, ErrValue) || got != nil { |
| + t.Fatalf("assemble: %v, want ErrValue", err) |
| + } |
| + checkMessage("assemble", err) |
| + }) |
| + } |
| +} |
| + |
| +// A group box's host.env is assembled by AssembleGroup, which must refuse |
| +// a bad value the same way: group members are the boxes the region NLB |
| +// sends PROXY headers to. |
| +func TestProxySourcesInAGroupBoxHostEnv(t *testing.T) { |
| + const key = "CARLOS_INGRESS_PROXY_SOURCES" |
| + for name, c := range map[string]struct { |
| + value string |
| + valid bool |
| + }{ |
| + "absent": {"", true}, |
| + "pair": {"172.31.29.110,172.31.11.189", true}, |
| + "public": {"54.77.110.223", false}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + gc, env := groupEnvFixture() |
| + if c.value != "" { |
| + env[key] = c.value |
| + } |
| + got, err := AssembleGroup(nil, env, gc, "queue") |
| + if c.valid { |
| + if err != nil || got[key] != c.value { |
| + t.Fatalf("valid %q: %v", c.value, err) |
| + } |
| + } else if !errors.Is(err, ErrValue) || got != nil || !strings.Contains(err.Error(), key) || strings.Contains(err.Error(), c.value) { |
| + t.Fatalf("invalid %q: %v, want ErrValue naming %s only", c.value, err, key) |
| + } |
| + }) |
| + } |
| +} |