Skip to content

Commit bc7c219

Browse files
committed
add proxyInit conditional
Signed-off-by: Kevin Button <kevin@thinkingmachines.ai>
1 parent e9d3d72 commit bc7c219

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

proxy-init/cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,16 @@ func BuildFirewallConfiguration(options *RootOptions) (*iptables.FirewallConfigu
195195
sanitizedSubnets := []string{}
196196
for _, subnet := range options.SubnetsToIgnore {
197197
subnet := strings.TrimSpace(subnet)
198-
_, _, err := net.ParseCIDR(subnet)
198+
_, cidr, err := net.ParseCIDR(subnet)
199199
if err != nil {
200200
return nil, fmt.Errorf("%s is not a valid CIDR address", subnet)
201201
}
202202

203+
isIPv6Subnet := cidr.IP.To4() == nil
204+
if isIPv6Subnet != options.IPv6 {
205+
continue
206+
}
207+
203208
sanitizedSubnets = append(sanitizedSubnets, subnet)
204209
}
205210

proxy-init/cmd/root_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,62 @@ func TestBuildFirewallConfiguration(t *testing.T) {
122122
}
123123
}
124124
})
125+
126+
t.Run("It filters subnets by address family for dual-stack", func(t *testing.T) {
127+
mixedSubnets := []string{"172.16.0.0/12", "fd00::/8", "10.0.0.0/8", "2001:db8::/32"}
128+
129+
// IPv4 pass (IPv6=false) should only include IPv4 subnets
130+
optIPv4 := &RootOptions{
131+
IncomingProxyPort: 1234,
132+
OutgoingProxyPort: 2345,
133+
SubnetsToIgnore: mixedSubnets,
134+
IPTablesMode: IPTablesModeLegacy,
135+
IPv6: false,
136+
}
137+
config, err := BuildFirewallConfiguration(optIPv4)
138+
if err != nil {
139+
t.Fatalf("Unexpected error: %s", err)
140+
}
141+
expectedIPv4 := []string{"172.16.0.0/12", "10.0.0.0/8"}
142+
if !reflect.DeepEqual(config.SubnetsToIgnore, expectedIPv4) {
143+
t.Fatalf("Expected IPv4 subnets %v but got %v", expectedIPv4, config.SubnetsToIgnore)
144+
}
145+
146+
// IPv6 pass (IPv6=true) should only include IPv6 subnets
147+
optIPv6 := &RootOptions{
148+
IncomingProxyPort: 1234,
149+
OutgoingProxyPort: 2345,
150+
SubnetsToIgnore: mixedSubnets,
151+
IPTablesMode: IPTablesModeLegacy,
152+
IPv6: true,
153+
}
154+
config, err = BuildFirewallConfiguration(optIPv6)
155+
if err != nil {
156+
t.Fatalf("Unexpected error: %s", err)
157+
}
158+
expectedIPv6 := []string{"fd00::/8", "2001:db8::/32"}
159+
if !reflect.DeepEqual(config.SubnetsToIgnore, expectedIPv6) {
160+
t.Fatalf("Expected IPv6 subnets %v but got %v", expectedIPv6, config.SubnetsToIgnore)
161+
}
162+
})
163+
164+
t.Run("It handles IPv4-only subnets with dual-stack enabled", func(t *testing.T) {
165+
ipv4Only := []string{"172.16.0.0/12"}
166+
167+
// IPv6 pass should produce empty subnets, not error
168+
opt := &RootOptions{
169+
IncomingProxyPort: 1234,
170+
OutgoingProxyPort: 2345,
171+
SubnetsToIgnore: ipv4Only,
172+
IPTablesMode: IPTablesModeLegacy,
173+
IPv6: true,
174+
}
175+
config, err := BuildFirewallConfiguration(opt)
176+
if err != nil {
177+
t.Fatalf("Unexpected error: %s", err)
178+
}
179+
if len(config.SubnetsToIgnore) != 0 {
180+
t.Fatalf("Expected empty subnets for IPv6 pass with IPv4-only input, got %v", config.SubnetsToIgnore)
181+
}
182+
})
125183
}

0 commit comments

Comments
 (0)