Checks need the same protection as identity provider requests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package netguard
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestIsPublic(t *testing.T) {
|
|
tests := map[string]bool{
|
|
"8.8.8.8": true,
|
|
"2606:4700::1111": true,
|
|
"127.0.0.1": false,
|
|
"10.1.2.3": false,
|
|
"172.16.0.1": false,
|
|
"192.168.1.1": false,
|
|
"169.254.169.254": false,
|
|
"100.64.0.1": false,
|
|
"0.0.0.0": false,
|
|
"::1": false,
|
|
"fd00::1": false,
|
|
"fe80::1": false,
|
|
"::ffff:127.0.0.1": false,
|
|
}
|
|
for addr, want := range tests {
|
|
if got := IsPublic(netip.MustParseAddr(addr)); got != want {
|
|
t.Errorf("IsPublic(%s) = %t, want %t", addr, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDialerBlocksLoopback(t *testing.T) {
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = ln.Close() }()
|
|
ctx := context.Background()
|
|
if _, err := Dialer(false, time.Second).DialContext(ctx, "tcp", ln.Addr().String()); !errors.Is(err, ErrForbiddenAddress) {
|
|
t.Errorf("Dialer(false).Dial(loopback) error = %v, want %v", err, ErrForbiddenAddress)
|
|
}
|
|
conn, err := Dialer(true, time.Second).DialContext(ctx, "tcp", ln.Addr().String())
|
|
if err != nil {
|
|
t.Fatalf("Dialer(true).Dial(loopback) failed: %v", err)
|
|
}
|
|
_ = conn.Close()
|
|
}
|