|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/BackendStack21/odek/internal/danger" |
| 11 | +) |
| 12 | + |
| 13 | +// recordingDial returns a dialFunc that records the addr it was asked to dial |
| 14 | +// and never actually connects (returns nil, nil). Tests assert on *what* the |
| 15 | +// guard decided to dial, not on a live connection. |
| 16 | +func recordingDial(dialed *[]string) dialFunc { |
| 17 | + return func(_ context.Context, _ string, addr string) (net.Conn, error) { |
| 18 | + *dialed = append(*dialed, addr) |
| 19 | + return nil, nil |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func stubLookup(ips ...string) ipLookupFunc { |
| 24 | + return func(_ context.Context, _ string) ([]net.IPAddr, error) { |
| 25 | + out := make([]net.IPAddr, 0, len(ips)) |
| 26 | + for _, s := range ips { |
| 27 | + out = append(out, net.IPAddr{IP: net.ParseIP(s)}) |
| 28 | + } |
| 29 | + return out, nil |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestSSRFGuardedDial_ImplicitlyInternalDialedDirect(t *testing.T) { |
| 34 | + // A literal internal IP was already gated as SystemWrite by ClassifyURL; |
| 35 | + // the guard must dial it through unchanged without a DNS lookup (this is |
| 36 | + // what keeps httptest-on-127.0.0.1 tests working). |
| 37 | + var dialed []string |
| 38 | + lookupCalled := false |
| 39 | + lookup := func(_ context.Context, _ string) ([]net.IPAddr, error) { |
| 40 | + lookupCalled = true |
| 41 | + return nil, nil |
| 42 | + } |
| 43 | + guard := ssrfGuardedDial(recordingDial(&dialed), lookup) |
| 44 | + |
| 45 | + if _, err := guard(context.Background(), "tcp", "127.0.0.1:8080"); err != nil { |
| 46 | + t.Fatalf("unexpected error: %v", err) |
| 47 | + } |
| 48 | + if lookupCalled { |
| 49 | + t.Error("lookup must NOT be called for an implicitly-internal literal IP") |
| 50 | + } |
| 51 | + if len(dialed) != 1 || dialed[0] != "127.0.0.1:8080" { |
| 52 | + t.Errorf("dialed = %v, want [127.0.0.1:8080]", dialed) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestSSRFGuardedDial_ExternalResolvingInternalRefused(t *testing.T) { |
| 57 | + // The SSRF / rebinding core case: a host that presents as external but |
| 58 | + // resolves to an internal address must be refused, and no dial attempted. |
| 59 | + cases := []struct { |
| 60 | + name string |
| 61 | + ip string |
| 62 | + }{ |
| 63 | + {"cloud metadata", "169.254.169.254"}, |
| 64 | + {"rfc1918", "10.1.2.3"}, |
| 65 | + {"loopback", "127.0.0.1"}, |
| 66 | + {"ipv6 ula", "fd00::1"}, |
| 67 | + {"unspecified", "0.0.0.0"}, |
| 68 | + } |
| 69 | + for _, tc := range cases { |
| 70 | + t.Run(tc.name, func(t *testing.T) { |
| 71 | + var dialed []string |
| 72 | + guard := ssrfGuardedDial(recordingDial(&dialed), stubLookup(tc.ip)) |
| 73 | + |
| 74 | + _, err := guard(context.Background(), "tcp", "evil.example.com:80") |
| 75 | + if err == nil { |
| 76 | + t.Fatal("expected the connection to be refused, got nil error") |
| 77 | + } |
| 78 | + if !strings.Contains(err.Error(), "internal address") { |
| 79 | + t.Errorf("error %q should mention the internal address", err) |
| 80 | + } |
| 81 | + if len(dialed) != 0 { |
| 82 | + t.Errorf("no dial must be attempted, dialed = %v", dialed) |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestSSRFGuardedDial_MixedAnswersFailClosed(t *testing.T) { |
| 89 | + // An answer set mixing a safe and an internal IP must be refused entirely — |
| 90 | + // an attacker cannot smuggle an internal target past the guard by padding |
| 91 | + // the DNS response with a public address. |
| 92 | + var dialed []string |
| 93 | + guard := ssrfGuardedDial(recordingDial(&dialed), stubLookup("93.184.216.34", "10.0.0.1")) |
| 94 | + |
| 95 | + if _, err := guard(context.Background(), "tcp", "evil.example.com:80"); err == nil { |
| 96 | + t.Fatal("expected refusal for mixed safe+internal answers") |
| 97 | + } |
| 98 | + if len(dialed) != 0 { |
| 99 | + t.Errorf("no dial must be attempted, dialed = %v", dialed) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestSSRFGuardedDial_ExternalPinnedToValidatedIP(t *testing.T) { |
| 104 | + // A genuinely external host is dialed — but pinned to the validated IP, not |
| 105 | + // the hostname, so the kernel cannot re-resolve to a rebound address. |
| 106 | + var dialed []string |
| 107 | + guard := ssrfGuardedDial(recordingDial(&dialed), stubLookup("93.184.216.34")) |
| 108 | + |
| 109 | + if _, err := guard(context.Background(), "tcp", "example.com:443"); err != nil { |
| 110 | + t.Fatalf("unexpected error: %v", err) |
| 111 | + } |
| 112 | + if len(dialed) != 1 || dialed[0] != "93.184.216.34:443" { |
| 113 | + t.Errorf("dialed = %v, want [93.184.216.34:443] (pinned IP, not hostname)", dialed) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestBrowser_SSRF_ResolvesInternal exercises the guard through the real |
| 118 | +// browser navigate path: the hostname classifies as NetworkEgress (so the |
| 119 | +// policy gate lets it through) but resolves to the cloud-metadata IP, and the |
| 120 | +// dial guard refuses it. |
| 121 | +func TestBrowser_SSRF_ResolvesInternal(t *testing.T) { |
| 122 | + b := &browserTool{state: &browserState{nextRef: 1}} |
| 123 | + b.client = &http.Client{ |
| 124 | + CheckRedirect: b.checkRedirect, |
| 125 | + Transport: &http.Transport{ |
| 126 | + DialContext: ssrfGuardedDial((&net.Dialer{}).DialContext, stubLookup("169.254.169.254")), |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + result := callJSON(t, b, `{"action":"navigate","url":"http://internal-disguised.example.com/"}`) |
| 131 | + var r struct { |
| 132 | + Error string `json:"error"` |
| 133 | + } |
| 134 | + mustUnmarshal(t, result, &r) |
| 135 | + if r.Error == "" { |
| 136 | + t.Fatal("expected navigate to be blocked by the dial guard") |
| 137 | + } |
| 138 | + if !strings.Contains(r.Error, "internal address") && !strings.Contains(r.Error, "SSRF") { |
| 139 | + t.Errorf("error %q should explain the SSRF block", r.Error) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// TestHTTPBatch_SSRF_ResolvesInternal exercises the guard through the real |
| 144 | +// http_batch fetch path. |
| 145 | +func TestHTTPBatch_SSRF_ResolvesInternal(t *testing.T) { |
| 146 | + tool := newHTTPBatchTool(danger.DangerousConfig{}) |
| 147 | + tool.client = &http.Client{ |
| 148 | + CheckRedirect: tool.checkRedirect, |
| 149 | + Transport: &http.Transport{ |
| 150 | + DialContext: ssrfGuardedDial((&net.Dialer{}).DialContext, stubLookup("10.0.0.1")), |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + result := callJSON(t, tool, `{"requests":[{"url":"http://internal-disguised.example.com/"}]}`) |
| 155 | + var r struct { |
| 156 | + Results []struct { |
| 157 | + Error string `json:"error"` |
| 158 | + } `json:"results"` |
| 159 | + } |
| 160 | + mustUnmarshal(t, result, &r) |
| 161 | + if len(r.Results) != 1 { |
| 162 | + t.Fatalf("Results = %d, want 1", len(r.Results)) |
| 163 | + } |
| 164 | + if r.Results[0].Error == "" { |
| 165 | + t.Fatal("expected fetch to be blocked by the dial guard") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// TestSSRFGuardedTransport_Installed is a guard against regressions that would |
| 170 | +// silently drop the SSRF protection from the production constructors. |
| 171 | +func TestSSRFGuardedTransport_Installed(t *testing.T) { |
| 172 | + b := newBrowserTool(danger.DangerousConfig{}) |
| 173 | + if b.client.Transport == nil { |
| 174 | + t.Error("browser tool client has no Transport — SSRF guard not installed") |
| 175 | + } |
| 176 | + if tr, ok := b.client.Transport.(*http.Transport); !ok || tr.DialContext == nil { |
| 177 | + t.Error("browser tool Transport is missing the guarded DialContext") |
| 178 | + } |
| 179 | + |
| 180 | + h := newHTTPBatchTool(danger.DangerousConfig{}) |
| 181 | + if h.client.Transport == nil { |
| 182 | + t.Error("http_batch tool client has no Transport — SSRF guard not installed") |
| 183 | + } |
| 184 | + if tr, ok := h.client.Transport.(*http.Transport); !ok || tr.DialContext == nil { |
| 185 | + t.Error("http_batch tool Transport is missing the guarded DialContext") |
| 186 | + } |
| 187 | +} |
0 commit comments