|
| 1 | +package ssrf |
| 2 | + |
| 3 | +import ( |
| 4 | + "main/context" |
| 5 | + "main/utils" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestCheckResolvedIpForSSRF_NoStoredInterceptorResult_ReturnsNil(t *testing.T) { |
| 10 | + context.ResetEventContext() |
| 11 | + t.Cleanup(func() { context.ResetEventContext() }) |
| 12 | + |
| 13 | + if res := CheckResolvedIpForSSRF("127.0.0.1"); res != nil { |
| 14 | + t.Fatalf("expected nil, got %#v", res) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestCheckResolvedIpForSSRF_PublicIp_ReturnsNil(t *testing.T) { |
| 19 | + context.ResetEventContext() |
| 20 | + t.Cleanup(func() { context.ResetEventContext() }) |
| 21 | + |
| 22 | + ir := &utils.InterceptorResult{ |
| 23 | + Operation: "curl_exec", |
| 24 | + Kind: utils.Ssrf, |
| 25 | + Source: "body", |
| 26 | + Metadata: map[string]string{}, |
| 27 | + Payload: "http://example.test", |
| 28 | + } |
| 29 | + context.EventContextSetCurrentSsrfInterceptorResult(ir) |
| 30 | + |
| 31 | + if res := CheckResolvedIpForSSRF("8.8.8.8"); res != nil { |
| 32 | + t.Fatalf("expected nil, got %#v", res) |
| 33 | + } |
| 34 | + if _, ok := ir.Metadata["isPrivateIp"]; ok { |
| 35 | + t.Fatalf("expected isPrivateIp to not be set for public IP") |
| 36 | + } |
| 37 | + if _, ok := ir.Metadata["resolvedIp"]; ok { |
| 38 | + t.Fatalf("expected resolvedIp to not be set for public IP") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestCheckResolvedIpForSSRF_PrivateIp_ReturnsInterceptorResultWithMetadata(t *testing.T) { |
| 43 | + context.ResetEventContext() |
| 44 | + t.Cleanup(func() { context.ResetEventContext() }) |
| 45 | + |
| 46 | + ir := &utils.InterceptorResult{ |
| 47 | + Operation: "curl_exec", |
| 48 | + Kind: utils.Ssrf, |
| 49 | + Source: "body", |
| 50 | + Metadata: map[string]string{}, |
| 51 | + Payload: "http://example.test", |
| 52 | + } |
| 53 | + context.EventContextSetCurrentSsrfInterceptorResult(ir) |
| 54 | + |
| 55 | + res := CheckResolvedIpForSSRF("127.0.0.1") |
| 56 | + if res == nil { |
| 57 | + t.Fatalf("expected non-nil interceptor result") |
| 58 | + } |
| 59 | + if res != ir { |
| 60 | + t.Fatalf("expected returned interceptor result to be the stored one") |
| 61 | + } |
| 62 | + if got := res.Metadata["resolvedIp"]; got != "127.0.0.1" { |
| 63 | + t.Fatalf("expected resolvedIp=127.0.0.1, got %q", got) |
| 64 | + } |
| 65 | + if got := res.Metadata["isPrivateIp"]; got != "true" { |
| 66 | + t.Fatalf("expected isPrivateIp=true, got %q", got) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestCheckEffectiveHostnameForSSRF_PrivateIpHostname_ReturnsInterceptorResultWithMetadata(t *testing.T) { |
| 71 | + context.ResetEventContext() |
| 72 | + t.Cleanup(func() { context.ResetEventContext() }) |
| 73 | + |
| 74 | + ir := &utils.InterceptorResult{ |
| 75 | + Operation: "curl_exec", |
| 76 | + Kind: utils.Ssrf, |
| 77 | + Source: "body", |
| 78 | + Metadata: map[string]string{}, |
| 79 | + Payload: "http://example.test", |
| 80 | + } |
| 81 | + context.EventContextSetCurrentSsrfInterceptorResult(ir) |
| 82 | + |
| 83 | + res := CheckEffectiveHostnameForSSRF("127.0.0.1") |
| 84 | + if res == nil { |
| 85 | + t.Fatalf("expected non-nil interceptor result") |
| 86 | + } |
| 87 | + if res != ir { |
| 88 | + t.Fatalf("expected returned interceptor result to be the stored one") |
| 89 | + } |
| 90 | + if got := res.Metadata["effectiveHostname"]; got != "127.0.0.1" { |
| 91 | + t.Fatalf("expected effectiveHostname=127.0.0.1, got %q", got) |
| 92 | + } |
| 93 | + if got := res.Metadata["resolvedIp"]; got != "127.0.0.1" { |
| 94 | + t.Fatalf("expected resolvedIp=127.0.0.1, got %q", got) |
| 95 | + } |
| 96 | + if got := res.Metadata["isPrivateIp"]; got != "true" { |
| 97 | + t.Fatalf("expected isPrivateIp=true, got %q", got) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestCheckEffectiveHostnameForSSRF_IMDSHostname_ReturnsInterceptorResultWithIMDSMetadata(t *testing.T) { |
| 102 | + context.ResetEventContext() |
| 103 | + t.Cleanup(func() { context.ResetEventContext() }) |
| 104 | + |
| 105 | + ir := &utils.InterceptorResult{ |
| 106 | + Operation: "curl_exec", |
| 107 | + Kind: utils.Ssrf, |
| 108 | + Source: "body", |
| 109 | + Metadata: map[string]string{}, |
| 110 | + Payload: "http://example.test", |
| 111 | + } |
| 112 | + context.EventContextSetCurrentSsrfInterceptorResult(ir) |
| 113 | + |
| 114 | + res := CheckEffectiveHostnameForSSRF("169.254.169.254") |
| 115 | + if res == nil { |
| 116 | + t.Fatalf("expected non-nil interceptor result") |
| 117 | + } |
| 118 | + if res != ir { |
| 119 | + t.Fatalf("expected returned interceptor result to be the stored one") |
| 120 | + } |
| 121 | + if got := res.Metadata["effectiveHostname"]; got != "169.254.169.254" { |
| 122 | + t.Fatalf("expected effectiveHostname=169.254.169.254, got %q", got) |
| 123 | + } |
| 124 | + if got := res.Metadata["resolvedIp"]; got != "169.254.169.254" { |
| 125 | + t.Fatalf("expected resolvedIp=169.254.169.254, got %q", got) |
| 126 | + } |
| 127 | + if got := res.Metadata["isIMDSIp"]; got != "true" { |
| 128 | + t.Fatalf("expected isIMDSIp=true, got %q", got) |
| 129 | + } |
| 130 | + // IMDS IPv4 is also in private ranges in our implementation. |
| 131 | + if got := res.Metadata["isPrivateIp"]; got != "true" { |
| 132 | + t.Fatalf("expected isPrivateIp=true, got %q", got) |
| 133 | + } |
| 134 | +} |
0 commit comments