| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package policy
- import (
- "context"
- "errors"
- "net"
- "testing"
- )
- type resolver struct {
- ips []net.IPAddr
- err error
- }
- func (r resolver) LookupIPAddr(context.Context, string) ([]net.IPAddr, error) { return r.ips, r.err }
- func TestNavigationPolicy(t *testing.T) {
- ctx := context.Background()
- p := URLPolicy{Resolver: resolver{ips: []net.IPAddr{{IP: net.ParseIP("8.8.8.8")}}}, AllowOrigins: []string{"http://127.0.0.1:8000", "https://private.example"}}
- for _, raw := range []string{"https://example.org/path?q=test#section", "http://8.8.8.8/", "https://[2606:4700:4700::1111]/", "http://127.0.0.1:8000/path", "https://private.example:443/a"} {
- if _, e := p.Validate(ctx, raw); e != nil {
- t.Errorf("%s: %v", raw, e)
- }
- }
- for _, raw := range []string{"file:///etc/passwd", "javascript:alert(1)", "https://user:pass@example.org", "http://localhost/", "http://x.localhost/", "http://127.0.0.1:8001/", "http://10.0.0.1/", "http://169.254.169.254/", "http://100.64.1.1/", "http://192.0.2.1/", "http://[::1]/", "https://", "://bad"} {
- if _, e := p.Validate(ctx, raw); e == nil {
- t.Errorf("blocked URL accepted: %s", raw)
- }
- }
- }
- func TestDNSMustOnlyResolvePublicAddresses(t *testing.T) {
- for _, r := range []resolver{{err: errors.New("dns")}, {}, {ips: []net.IPAddr{{IP: net.ParseIP("8.8.8.8")}, {IP: net.ParseIP("10.0.0.1")}}}} {
- p := URLPolicy{Resolver: r}
- if _, e := p.Validate(context.Background(), "https://example.org"); e == nil {
- t.Fatal("unsafe DNS result accepted")
- }
- }
- if public(nil) || public(net.IPv4zero) || public(net.ParseIP("2001:db8::1")) {
- t.Fatal("non-public IP accepted")
- }
- }
|