Skip to content

Commit 4338833

Browse files
committed
add tests for 0.0.0.0/0 detection + fix
1 parent 2b23561 commit 4338833

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

packages/orchestrator/internal/sandbox/network/firewall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func addCIDRToSet(conn *nftables.Conn, ipset set.Set, cidr string) error {
266266
return err
267267
}
268268

269-
if len(current) == 1 && current[0].AddressRangeStart == netip.MustParseAddr("0.0.0.0") && current[0].AddressRangeEnd == netip.MustParseAddr("255.255.255.255") {
269+
if len(current) == 1 && current[0].AddressRangeStart == netip.MustParseAddr("0.0.0.0") && current[0].AddressRangeEnd == netip.MustParseAddr("255.255.255.254") {
270270
// Because 0.0.0.0/0 is not valid IP per GoLang, we can't add new addresses to the set.
271271
return nil
272272
}

tests/integration/internal/tests/api/sandboxes/sandbox_network_out_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,83 @@ func TestEgressFirewallPrivateIPRangesAlwaysBlocked(t *testing.T) {
406406
})
407407
}
408408
}
409+
410+
// TestEgressFirewallAllowAllDuplicate tests that adding 0.0.0.0/0 twice works correctly
411+
func TestEgressFirewallAllowAllDuplicate(t *testing.T) {
412+
ctx := t.Context()
413+
client := setup.GetAPIClient()
414+
415+
// Add 0.0.0.0/0 twice in the allowOut list
416+
allowAll := []string{"0.0.0.0/0", "0.0.0.0/0"}
417+
418+
sbx := utils.SetupSandboxWithCleanup(t, client,
419+
utils.WithTimeout(60),
420+
utils.WithNetwork(&api.SandboxNetworkConfig{
421+
AllowOut: &allowAll,
422+
DenyOut: &[]string{internetBlockAddress},
423+
}),
424+
)
425+
426+
envdClient := setup.GetEnvdClient(t, ctx)
427+
428+
// Test that various IPs are accessible (duplicate 0.0.0.0/0 should work like a single one)
429+
err := utils.ExecCommand(t, ctx, sbx, envdClient, "curl", "--connect-timeout", "3", "--max-time", "5", "-Iks", "https://8.8.8.8")
430+
require.NoError(t, err, "Expected curl to 8.8.8.8 to succeed with duplicate 0.0.0.0/0 allow")
431+
432+
err = utils.ExecCommand(t, ctx, sbx, envdClient, "curl", "--connect-timeout", "3", "--max-time", "5", "-Iks", "https://1.1.1.1")
433+
require.NoError(t, err, "Expected curl to 1.1.1.1 to succeed with duplicate 0.0.0.0/0 allow")
434+
}
435+
436+
// TestEgressFirewallRegularIPThenAllowAll tests that adding a regular IP and then 0.0.0.0/0 works correctly
437+
func TestEgressFirewallRegularIPThenAllowAll(t *testing.T) {
438+
ctx := t.Context()
439+
client := setup.GetAPIClient()
440+
441+
// Add a specific IP followed by 0.0.0.0/0
442+
allowList := []string{"8.8.8.8", "0.0.0.0/0"}
443+
444+
sbx := utils.SetupSandboxWithCleanup(t, client,
445+
utils.WithTimeout(60),
446+
utils.WithNetwork(&api.SandboxNetworkConfig{
447+
AllowOut: &allowList,
448+
DenyOut: &[]string{internetBlockAddress},
449+
}),
450+
)
451+
452+
envdClient := setup.GetEnvdClient(t, ctx)
453+
454+
// Test that the specific IP is accessible
455+
err := utils.ExecCommand(t, ctx, sbx, envdClient, "curl", "--connect-timeout", "3", "--max-time", "5", "-Iks", "https://8.8.8.8")
456+
require.NoError(t, err, "Expected curl to 8.8.8.8 to succeed")
457+
458+
// Test that other IPs are also accessible (0.0.0.0/0 allows everything)
459+
err = utils.ExecCommand(t, ctx, sbx, envdClient, "curl", "--connect-timeout", "3", "--max-time", "5", "-Iks", "https://1.1.1.1")
460+
require.NoError(t, err, "Expected curl to 1.1.1.1 to succeed (0.0.0.0/0 allows all)")
461+
}
462+
463+
// TestEgressFirewallAllowAllThenRegularIP tests that adding 0.0.0.0/0 and then a regular IP works correctly
464+
func TestEgressFirewallAllowAllThenRegularIP(t *testing.T) {
465+
ctx := t.Context()
466+
client := setup.GetAPIClient()
467+
468+
// Add 0.0.0.0/0 followed by a specific IP
469+
allowList := []string{"0.0.0.0/0", "8.8.8.8"}
470+
471+
sbx := utils.SetupSandboxWithCleanup(t, client,
472+
utils.WithTimeout(60),
473+
utils.WithNetwork(&api.SandboxNetworkConfig{
474+
AllowOut: &allowList,
475+
DenyOut: &[]string{internetBlockAddress},
476+
}),
477+
)
478+
479+
envdClient := setup.GetEnvdClient(t, ctx)
480+
481+
// Test that the specific IP is accessible
482+
err := utils.ExecCommand(t, ctx, sbx, envdClient, "curl", "--connect-timeout", "3", "--max-time", "5", "-Iks", "https://8.8.8.8")
483+
require.NoError(t, err, "Expected curl to 8.8.8.8 to succeed")
484+
485+
// Test that other IPs are also accessible (0.0.0.0/0 allows everything)
486+
err = utils.ExecCommand(t, ctx, sbx, envdClient, "curl", "--connect-timeout", "3", "--max-time", "5", "-Iks", "https://1.1.1.1")
487+
require.NoError(t, err, "Expected curl to 1.1.1.1 to succeed (0.0.0.0/0 allows all)")
488+
}

0 commit comments

Comments
 (0)