| 1 | package ssrf |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestPrivateRanges(t *testing.T) { |
| 12 | blocked := []string{ |
| 13 | "0.0.0.0", "10.1.2.3", "127.0.0.1", "127.255.0.1", |
| 14 | "100.64.0.1", "100.100.100.200", "100.127.255.255", |
| 15 | "169.254.1.1", "169.254.169.254", "169.254.170.2", |
| 16 | "172.16.0.1", "172.31.255.255", "192.168.1.1", |
| 17 | "224.0.0.1", "240.0.0.1", "255.255.255.255", |
| 18 | "::", "::1", "fc00::1", "fd00:ec2::254", "fd12:3456::1", |
| 19 | "fe80::1", "fec0::1", "ff02::1", |
| 20 | "::ffff:10.1.2.3", "::ffff:127.0.0.1", |
| 21 | "2002:0a00:0001::", |
| 22 | "64:ff9b::a00:1", |
| 23 | } |
| 24 | for _, s := range blocked { |
| 25 | ip := net.ParseIP(s) |
| 26 | if ip == nil { |
| 27 | t.Fatalf("ongeldig testadres %s", s) |
| 28 | } |
| 29 | if !Private(ip) { |
| 30 | t.Errorf("%s hoort geblokkeerd", s) |
| 31 | } |
| 32 | } |
| 33 | allowed := []string{ |
| 34 | "1.1.1.1", "8.8.8.8", "203.0.113.5", "86.95.10.20", |
| 35 | "100.63.255.255", "172.15.255.255", "172.32.0.1", "11.0.0.1", |
| 36 | "2001:4860:4860::8888", |
| 37 | "2002:0808:0808::", |
| 38 | "64:ff9b::808:808", |
| 39 | } |
| 40 | for _, s := range allowed { |
| 41 | ip := net.ParseIP(s) |
| 42 | if ip == nil { |
| 43 | t.Fatalf("ongeldig testadres %s", s) |
| 44 | } |
| 45 | if Private(ip) { |
| 46 | t.Errorf("%s hoort publiek", s) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestPinLiteralAndDNS(t *testing.T) { |
| 52 | ctx := context.Background() |
| 53 | if _, err := Pin(ctx, "127.0.0.1", func(context.Context, string) ([]net.IP, error) { |
| 54 | t.Fatal("letterlijk IP hoort geen DNS te doen") |
| 55 | return nil, nil |
| 56 | }); err == nil { |
| 57 | t.Fatal("loopback hoort geweigerd") |
| 58 | } |
| 59 | if _, err := Pin(ctx, "169.254.169.254", nil); err == nil { |
| 60 | t.Fatal("metadata-adres hoort geweigerd") |
| 61 | } |
| 62 | got, err := Pin(ctx, "hooks.example", func(context.Context, string) ([]net.IP, error) { |
| 63 | return []net.IP{net.ParseIP("86.95.10.20")}, nil |
| 64 | }) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | if !got.Equal(net.ParseIP("86.95.10.20")) { |
| 69 | t.Fatalf("vastgezet IP %s", got) |
| 70 | } |
| 71 | if _, err := Pin(ctx, "evil.example", func(context.Context, string) ([]net.IP, error) { |
| 72 | return []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("10.0.0.1")}, nil |
| 73 | }); err == nil { |
| 74 | t.Fatal("een gemengd DNS-antwoord hoort geweigerd") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestRedirectChecksEachHop(t *testing.T) { |
| 79 | literal, err := url.Parse("http://192.168.1.9/hook") |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | if err := checkRedirect(&http.Request{URL: literal}, nil); err == nil { |
| 84 | t.Fatal("redirect naar een privé-IP hoort geweigerd") |
| 85 | } |
| 86 | next, err := url.Parse("https://hooks.example/hook") |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | via := make([]*http.Request, MaxRedirects) |
| 91 | for i := range via { |
| 92 | via[i] = &http.Request{URL: next} |
| 93 | } |
| 94 | if err := checkRedirect(&http.Request{URL: next}, via); err == nil { |
| 95 | t.Fatal("te veel redirects") |
| 96 | } |
| 97 | |
| 98 | origin, err := url.Parse("https://discord.example/api") |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | other, err := url.Parse("https://other.example/land") |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | req := &http.Request{URL: other, Header: http.Header{}, Response: &http.Response{StatusCode: http.StatusFound}} |
| 107 | req.Header.Set("Authorization", "Bearer geheim") |
| 108 | req.Header.Set("Content-Type", "application/json") |
| 109 | if err := checkRedirect(req, []*http.Request{{URL: origin}}); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if req.Header.Get("Authorization") != "" { |
| 113 | t.Fatal("header naar een andere host bleef staan") |
| 114 | } |
| 115 | if req.Header.Get("Content-Type") != "application/json" { |
| 116 | t.Fatal("content-type hoort te blijven") |
| 117 | } |
| 118 | keep := &http.Request{URL: other, Header: http.Header{}, Response: &http.Response{StatusCode: http.StatusTemporaryRedirect}} |
| 119 | if err := checkRedirect(keep, []*http.Request{{URL: origin}}); err == nil { |
| 120 | t.Fatal("307 naar een andere host hoort geweigerd") |
| 121 | } |
| 122 | } |