| 1 | package realipscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/ |
| 2 | |
| 3 | import ( |
| 4 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 5 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 7 | "testingscip-go gomod github.com/golang/go/src go1.18 testing/" |
| 8 | |
| 9 | "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/" |
| 10 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/require`/" |
| 11 | ) |
| 12 | |
| 13 | func TestTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/TestTrusted().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 14 | nets, err := parseTrustedscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/parseTrusted().([]string{"10.0.0.0/8"}) |
| 15 | requirescip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 16 | cases := []struct { |
| 17 | Case, IP, XFF, Expected string |
| 18 | Trusted bool |
| 19 | }{ |
| 20 | // direct from an untrusted IP |
| 21 | {"Direct", "172.16.0.1", "", "172.16.0.1", false}, |
| 22 | // direct from a trusted IP |
| 23 | {"DirectInternal", "10.0.0.1", "", "10.0.0.1", false}, |
| 24 | // direct from an untrusted IP with an untrusted hop |
| 25 | {"DirectUntrusted", "10.0.0.1", "", "10.0.0.1", false}, |
| 26 | // connected via trusted proxy |
| 27 | {"OneHop", "10.0.0.1", "192.168.100.1", "192.168.100.1", true}, |
| 28 | // connected via trusted proxy and the client is also a trusted IP |
| 29 | {"OneHopInternal", "10.0.0.1", "10.0.0.2", "10.0.0.2", true}, |
| 30 | // connected via trusted proxy with an untrusted hop |
| 31 | {"OneHopUntrusted", "10.0.0.1", "172.16.0.1, 192.168.100.1", "192.168.100.1", true}, |
| 32 | // connected via two trusted proxies |
| 33 | {"TwoHops", "10.0.0.1", "192.168.100.1, 10.0.0.2", "192.168.100.1", true}, |
| 34 | } |
| 35 | for _, c := range cases { |
| 36 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(c.Case, func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 37 | req := httptestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/.NewRequestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/NewRequest().(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.18 `net/http`/MethodGet., "/", nil) |
| 38 | req.RemoteAddrscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#RemoteAddr. = c.IP + ":12345" |
| 39 | if c.XFF != "" { |
| 40 | req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Set().("X-Forwarded-For", c.XFF) |
| 41 | } |
| 42 | actual, trusted := trustedClientscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/trustedClient().(nets, req) |
| 43 | assertscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/Equal().(t, c.Expected, actual) |
| 44 | assertscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/Equal().(t, c.Trusted, trusted) |
| 45 | req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Delscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Del().("X-Forwarded-For") |
| 46 | |
| 47 | }) |
| 48 | if !stringsscip-go gomod github.com/golang/go/src go1.18 strings/.ContainsRunescip-go gomod github.com/golang/go/src go1.18 strings/ContainsRune().(c.XFF, ',') { |
| 49 | continue |
| 50 | } |
| 51 | // do it again with separate XFF headers |
| 52 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(c.Case+"Multi", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 53 | req := httptestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/.NewRequestscip-go gomod github.com/golang/go/src go1.18 `net/http/httptest`/NewRequest().(httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.18 `net/http`/MethodGet., "/", nil) |
| 54 | req.RemoteAddrscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#RemoteAddr. = c.IP + ":12345" |
| 55 | for _, v := range stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(c.XFF, ",") { |
| 56 | req.Headerscip-go gomod github.com/golang/go/src go1.18 `net/http`/Request#Header..Addscip-go gomod github.com/golang/go/src go1.18 `net/http`/Header#Add().("X-Forwarded-For", v) |
| 57 | } |
| 58 | actual, trusted := trustedClientscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/internal/realip`/trustedClient().(nets, req) |
| 59 | assertscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/Equal().(t, c.Expected, actual) |
| 60 | assertscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.8.2 `github.com/stretchr/testify/assert`/Equal().(t, c.Trusted, trusted) |
| 61 | }) |
| 62 | } |
| 63 | } |