Files
llmgw/internal/safedial/safedial_test.go
T

70 lines
2.3 KiB
Go

package safedial
import (
"errors"
"net/http"
"net/http/httptest"
"net/netip"
"testing"
)
func TestWhatEachPolicyRefuses(t *testing.T) {
cases := []struct {
addr string
internet, control, private bool // blocked under Policy{}, ControlPlane, all allowed
}{
{"203.0.113.7", false, false, false},
{"2606:4700::1111", false, false, false},
{"169.254.169.254", true, true, false},
{"fe80::1", true, true, false},
{"127.0.0.1", true, false, false},
{"::1", true, false, false},
{"10.0.0.146", true, false, false},
{"fd00::5", true, false, false},
{"100.64.1.1", true, false, false},
{"0.0.0.0", true, true, false},
// An IPv4 address carried in an IPv6 one is judged as itself.
{"::ffff:169.254.169.254", true, true, false},
{"64:ff9b::a9fe:a9fe", true, true, false},
{"64:ff9b::cb00:7107", false, false, false},
{"2002:a9fe:a9fe::1", true, true, false},
{"2002:0a00:0001::1", true, false, false},
{"::a9fe:a9fe", true, true, false},
}
all := Policy{Private: true, Loopback: true, LinkLocal: true}
for _, c := range cases {
a := netip.MustParseAddr(c.addr)
if got := Blocked(a, Policy{}); got != c.internet {
t.Errorf("%s under the internet-only policy: blocked=%v", c.addr, got)
}
if got := Blocked(a, ControlPlane); got != c.control {
t.Errorf("%s under the control plane's: blocked=%v", c.addr, got)
}
if got := Blocked(a, all); got != c.private {
t.Errorf("%s with everything allowed: blocked=%v", c.addr, got)
}
}
}
func TestTheAddressDialledIsWhatIsChecked(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {}))
defer srv.Close()
if _, err := Client(Policy{}, 0).Get(srv.URL); !errors.Is(err, ErrBlocked) {
t.Errorf("loopback reached under the internet-only policy: %v", err)
}
resp, err := Client(ControlPlane, 0).Get(srv.URL)
if err != nil {
t.Fatalf("the control plane's own machine: %v", err)
}
resp.Body.Close()
// A redirect to a scheme that reads the disk is refused.
redir := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "file:///etc/passwd", http.StatusFound)
}))
defer redir.Close()
if _, err := Client(ControlPlane, 0).Get(redir.URL); err == nil {
t.Error("followed a redirect to file://")
}
}