2019-01-15 08:44:03 +00:00
|
|
|
package forwardedheaders
|
|
|
|
|
|
|
|
import (
|
2019-04-02 14:56:05 +00:00
|
|
|
"crypto/tls"
|
2019-01-15 08:44:03 +00:00
|
|
|
"net/http"
|
2021-02-02 10:40:04 +00:00
|
|
|
"net/http/httptest"
|
2019-01-15 08:44:03 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServeHTTP(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2024-09-16 09:10:04 +00:00
|
|
|
desc string
|
|
|
|
insecure bool
|
|
|
|
trustedIps []string
|
|
|
|
connectionHeaders []string
|
|
|
|
incomingHeaders map[string][]string
|
|
|
|
remoteAddr string
|
|
|
|
expectedHeaders map[string]string
|
|
|
|
tls bool
|
|
|
|
websocket bool
|
|
|
|
host string
|
2019-01-15 08:44:03 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "all Empty",
|
|
|
|
insecure: true,
|
|
|
|
trustedIps: nil,
|
|
|
|
remoteAddr: "",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{},
|
2019-01-15 08:44:03 +00:00
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "",
|
|
|
|
xForwardedURI: "",
|
|
|
|
xForwardedMethod: "",
|
|
|
|
xForwardedTLSClientCert: "",
|
|
|
|
xForwardedTLSClientCertInfo: "",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-08 15:56:04 +00:00
|
|
|
desc: "insecure true with incoming X-Forwarded headers",
|
2019-01-15 08:44:03 +00:00
|
|
|
insecure: true,
|
|
|
|
trustedIps: nil,
|
|
|
|
remoteAddr: "",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {"10.0.1.0, 10.0.1.12"},
|
|
|
|
xForwardedURI: {"/bar"},
|
|
|
|
xForwardedMethod: {"GET"},
|
|
|
|
xForwardedTLSClientCert: {"Cert"},
|
|
|
|
xForwardedTLSClientCertInfo: {"CertInfo"},
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "10.0.1.0, 10.0.1.12",
|
|
|
|
xForwardedURI: "/bar",
|
|
|
|
xForwardedMethod: "GET",
|
|
|
|
xForwardedTLSClientCert: "Cert",
|
|
|
|
xForwardedTLSClientCertInfo: "CertInfo",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-08 15:56:04 +00:00
|
|
|
desc: "insecure false with incoming X-Forwarded headers",
|
2019-01-15 08:44:03 +00:00
|
|
|
insecure: false,
|
|
|
|
trustedIps: nil,
|
|
|
|
remoteAddr: "",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {"10.0.1.0, 10.0.1.12"},
|
|
|
|
xForwardedURI: {"/bar"},
|
|
|
|
xForwardedMethod: {"GET"},
|
|
|
|
xForwardedTLSClientCert: {"Cert"},
|
|
|
|
xForwardedTLSClientCertInfo: {"CertInfo"},
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "",
|
|
|
|
xForwardedURI: "",
|
|
|
|
xForwardedMethod: "",
|
|
|
|
xForwardedTLSClientCert: "",
|
|
|
|
xForwardedTLSClientCertInfo: "",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-08 15:56:04 +00:00
|
|
|
desc: "insecure false with incoming X-Forwarded headers and valid Trusted Ips",
|
2019-01-15 08:44:03 +00:00
|
|
|
insecure: false,
|
|
|
|
trustedIps: []string{"10.0.1.100"},
|
|
|
|
remoteAddr: "10.0.1.100:80",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {"10.0.1.0, 10.0.1.12"},
|
|
|
|
xForwardedURI: {"/bar"},
|
|
|
|
xForwardedMethod: {"GET"},
|
|
|
|
xForwardedTLSClientCert: {"Cert"},
|
|
|
|
xForwardedTLSClientCertInfo: {"CertInfo"},
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "10.0.1.0, 10.0.1.12",
|
|
|
|
xForwardedURI: "/bar",
|
|
|
|
xForwardedMethod: "GET",
|
|
|
|
xForwardedTLSClientCert: "Cert",
|
|
|
|
xForwardedTLSClientCertInfo: "CertInfo",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-08 15:56:04 +00:00
|
|
|
desc: "insecure false with incoming X-Forwarded headers and invalid Trusted Ips",
|
2019-01-15 08:44:03 +00:00
|
|
|
insecure: false,
|
|
|
|
trustedIps: []string{"10.0.1.100"},
|
|
|
|
remoteAddr: "10.0.1.101:80",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {"10.0.1.0, 10.0.1.12"},
|
|
|
|
xForwardedURI: {"/bar"},
|
|
|
|
xForwardedMethod: {"GET"},
|
|
|
|
xForwardedTLSClientCert: {"Cert"},
|
|
|
|
xForwardedTLSClientCertInfo: {"CertInfo"},
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "",
|
|
|
|
xForwardedURI: "",
|
|
|
|
xForwardedMethod: "",
|
|
|
|
xForwardedTLSClientCert: "",
|
|
|
|
xForwardedTLSClientCertInfo: "",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-08 15:56:04 +00:00
|
|
|
desc: "insecure false with incoming X-Forwarded headers and valid Trusted Ips CIDR",
|
2019-01-15 08:44:03 +00:00
|
|
|
insecure: false,
|
|
|
|
trustedIps: []string{"1.2.3.4/24"},
|
|
|
|
remoteAddr: "1.2.3.156:80",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {"10.0.1.0, 10.0.1.12"},
|
|
|
|
xForwardedURI: {"/bar"},
|
|
|
|
xForwardedMethod: {"GET"},
|
|
|
|
xForwardedTLSClientCert: {"Cert"},
|
|
|
|
xForwardedTLSClientCertInfo: {"CertInfo"},
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "10.0.1.0, 10.0.1.12",
|
|
|
|
xForwardedURI: "/bar",
|
|
|
|
xForwardedMethod: "GET",
|
|
|
|
xForwardedTLSClientCert: "Cert",
|
|
|
|
xForwardedTLSClientCertInfo: "CertInfo",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-08 15:56:04 +00:00
|
|
|
desc: "insecure false with incoming X-Forwarded headers and invalid Trusted Ips CIDR",
|
2019-01-15 08:44:03 +00:00
|
|
|
insecure: false,
|
|
|
|
trustedIps: []string{"1.2.3.4/24"},
|
|
|
|
remoteAddr: "10.0.1.101:80",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {"10.0.1.0, 10.0.1.12"},
|
|
|
|
xForwardedURI: {"/bar"},
|
|
|
|
xForwardedMethod: {"GET"},
|
|
|
|
xForwardedTLSClientCert: {"Cert"},
|
|
|
|
xForwardedTLSClientCertInfo: {"CertInfo"},
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
2021-12-14 14:36:07 +00:00
|
|
|
xForwardedFor: "",
|
|
|
|
xForwardedURI: "",
|
|
|
|
xForwardedMethod: "",
|
|
|
|
xForwardedTLSClientCert: "",
|
|
|
|
xForwardedTLSClientCertInfo: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedFor with multiple header(s) values",
|
|
|
|
insecure: true,
|
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedFor: {
|
|
|
|
"10.0.0.4, 10.0.0.3",
|
|
|
|
"10.0.0.2, 10.0.0.1",
|
|
|
|
"10.0.0.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedFor: "10.0.0.4, 10.0.0.3, 10.0.0.2, 10.0.0.1, 10.0.0.0",
|
2019-01-15 08:44:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-02 14:56:05 +00:00
|
|
|
{
|
|
|
|
desc: "xRealIP populated from remote address",
|
|
|
|
remoteAddr: "10.0.1.101:80",
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xRealIP: "10.0.1.101",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xRealIP was already populated from previous headers",
|
|
|
|
insecure: true,
|
|
|
|
remoteAddr: "10.0.1.101:80",
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xRealIP: {"10.0.1.12"},
|
2019-04-02 14:56:05 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xRealIP: "10.0.1.12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedProto with no tls",
|
|
|
|
tls: false,
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedProto with tls",
|
|
|
|
tls: true,
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "https",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedProto with websocket",
|
|
|
|
tls: false,
|
|
|
|
websocket: true,
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "ws",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedProto with websocket and tls",
|
|
|
|
tls: true,
|
|
|
|
websocket: true,
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "wss",
|
|
|
|
},
|
|
|
|
},
|
2020-04-30 16:00:04 +00:00
|
|
|
{
|
|
|
|
desc: "xForwardedProto with websocket and tls and already x-forwarded-proto with wss",
|
|
|
|
tls: true,
|
|
|
|
websocket: true,
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedProto: {"wss"},
|
2020-04-30 16:00:04 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "wss",
|
|
|
|
},
|
|
|
|
},
|
2019-04-02 14:56:05 +00:00
|
|
|
{
|
|
|
|
desc: "xForwardedPort with explicit port",
|
|
|
|
host: "foo.com:8080",
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedPort: "8080",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedPort with implicit tls port from proto header",
|
|
|
|
// setting insecure just so our initial xForwardedProto does not get cleaned
|
|
|
|
insecure: true,
|
2021-12-14 14:36:07 +00:00
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
xForwardedProto: {"https"},
|
2019-04-02 14:56:05 +00:00
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "https",
|
|
|
|
xForwardedPort: "443",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedPort with implicit tls port from TLS in req",
|
|
|
|
tls: true,
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedPort: "443",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "xForwardedHost from req host",
|
|
|
|
host: "foo.com:8080",
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedHost: "foo.com:8080",
|
|
|
|
},
|
2020-07-07 12:42:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-02 14:56:05 +00:00
|
|
|
desc: "xForwardedServer from req XForwarded",
|
|
|
|
host: "foo.com:8080",
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedServer: "foo.com:8080",
|
|
|
|
},
|
|
|
|
},
|
2024-09-16 09:10:04 +00:00
|
|
|
{
|
|
|
|
desc: "Untrusted: Connection header has no effect on X- forwarded headers",
|
|
|
|
insecure: false,
|
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
connection: {
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
|
|
|
xRealIP,
|
|
|
|
},
|
|
|
|
xForwardedProto: {"foo"},
|
|
|
|
xForwardedFor: {"foo"},
|
|
|
|
xForwardedURI: {"foo"},
|
|
|
|
xForwardedMethod: {"foo"},
|
|
|
|
xForwardedHost: {"foo"},
|
|
|
|
xForwardedPort: {"foo"},
|
|
|
|
xForwardedTLSClientCert: {"foo"},
|
|
|
|
xForwardedTLSClientCertInfo: {"foo"},
|
|
|
|
xRealIP: {"foo"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "http",
|
|
|
|
xForwardedFor: "",
|
|
|
|
xForwardedURI: "",
|
|
|
|
xForwardedMethod: "",
|
|
|
|
xForwardedHost: "",
|
|
|
|
xForwardedPort: "80",
|
|
|
|
xForwardedTLSClientCert: "",
|
|
|
|
xForwardedTLSClientCertInfo: "",
|
|
|
|
xRealIP: "",
|
|
|
|
connection: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Trusted (insecure): Connection header has no effect on X- forwarded headers",
|
|
|
|
insecure: true,
|
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
connection: {
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
|
|
|
xRealIP,
|
|
|
|
},
|
|
|
|
xForwardedProto: {"foo"},
|
|
|
|
xForwardedFor: {"foo"},
|
|
|
|
xForwardedURI: {"foo"},
|
|
|
|
xForwardedMethod: {"foo"},
|
|
|
|
xForwardedHost: {"foo"},
|
|
|
|
xForwardedPort: {"foo"},
|
|
|
|
xForwardedTLSClientCert: {"foo"},
|
|
|
|
xForwardedTLSClientCertInfo: {"foo"},
|
|
|
|
xRealIP: {"foo"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "foo",
|
|
|
|
xForwardedFor: "foo",
|
|
|
|
xForwardedURI: "foo",
|
|
|
|
xForwardedMethod: "foo",
|
|
|
|
xForwardedHost: "foo",
|
|
|
|
xForwardedPort: "foo",
|
|
|
|
xForwardedTLSClientCert: "foo",
|
|
|
|
xForwardedTLSClientCertInfo: "foo",
|
|
|
|
xRealIP: "foo",
|
|
|
|
connection: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Untrusted and Connection: Connection header has no effect on X- forwarded headers",
|
|
|
|
insecure: false,
|
|
|
|
connectionHeaders: []string{
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
|
|
|
xRealIP,
|
|
|
|
},
|
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
connection: {
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
|
|
|
xRealIP,
|
|
|
|
},
|
|
|
|
xForwardedProto: {"foo"},
|
|
|
|
xForwardedFor: {"foo"},
|
|
|
|
xForwardedURI: {"foo"},
|
|
|
|
xForwardedMethod: {"foo"},
|
|
|
|
xForwardedHost: {"foo"},
|
|
|
|
xForwardedPort: {"foo"},
|
|
|
|
xForwardedTLSClientCert: {"foo"},
|
|
|
|
xForwardedTLSClientCertInfo: {"foo"},
|
|
|
|
xRealIP: {"foo"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "http",
|
|
|
|
xForwardedFor: "",
|
|
|
|
xForwardedURI: "",
|
|
|
|
xForwardedMethod: "",
|
|
|
|
xForwardedHost: "",
|
|
|
|
xForwardedPort: "80",
|
|
|
|
xForwardedTLSClientCert: "",
|
|
|
|
xForwardedTLSClientCertInfo: "",
|
|
|
|
xRealIP: "",
|
|
|
|
connection: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Trusted (insecure) and Connection: Connection header has no effect on X- forwarded headers",
|
|
|
|
insecure: true,
|
|
|
|
connectionHeaders: []string{
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
|
|
|
xRealIP,
|
|
|
|
},
|
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
connection: {
|
|
|
|
xForwardedProto,
|
|
|
|
xForwardedFor,
|
|
|
|
xForwardedURI,
|
|
|
|
xForwardedMethod,
|
|
|
|
xForwardedHost,
|
|
|
|
xForwardedPort,
|
|
|
|
xForwardedTLSClientCert,
|
|
|
|
xForwardedTLSClientCertInfo,
|
|
|
|
xRealIP,
|
|
|
|
},
|
|
|
|
xForwardedProto: {"foo"},
|
|
|
|
xForwardedFor: {"foo"},
|
|
|
|
xForwardedURI: {"foo"},
|
|
|
|
xForwardedMethod: {"foo"},
|
|
|
|
xForwardedHost: {"foo"},
|
|
|
|
xForwardedPort: {"foo"},
|
|
|
|
xForwardedTLSClientCert: {"foo"},
|
|
|
|
xForwardedTLSClientCertInfo: {"foo"},
|
|
|
|
xRealIP: {"foo"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
xForwardedProto: "foo",
|
|
|
|
xForwardedFor: "foo",
|
|
|
|
xForwardedURI: "foo",
|
|
|
|
xForwardedMethod: "foo",
|
|
|
|
xForwardedHost: "foo",
|
|
|
|
xForwardedPort: "foo",
|
|
|
|
xForwardedTLSClientCert: "foo",
|
|
|
|
xForwardedTLSClientCertInfo: "foo",
|
|
|
|
xRealIP: "foo",
|
|
|
|
connection: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Connection: one remove, and one passthrough header",
|
|
|
|
connectionHeaders: []string{
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
incomingHeaders: map[string][]string{
|
|
|
|
connection: {
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
"Foo": {"bar"},
|
|
|
|
"Bar": {"foo"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string]string{
|
|
|
|
"Bar": "foo",
|
|
|
|
},
|
|
|
|
},
|
2019-01-15 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req.RemoteAddr = test.remoteAddr
|
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
if test.tls {
|
|
|
|
req.TLS = &tls.ConnectionState{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.websocket {
|
|
|
|
req.Header.Set(connection, "upgrade")
|
|
|
|
req.Header.Set(upgrade, "websocket")
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.host != "" {
|
|
|
|
req.Host = test.host
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:36:07 +00:00
|
|
|
for k, values := range test.incomingHeaders {
|
|
|
|
for _, value := range values {
|
|
|
|
req.Header.Add(k, value)
|
|
|
|
}
|
2019-01-15 08:44:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 09:10:04 +00:00
|
|
|
m, err := NewXForwarded(test.insecure, test.trustedIps, test.connectionHeaders,
|
2019-04-02 14:56:05 +00:00
|
|
|
http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
|
2019-01-15 08:44:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-02 14:56:05 +00:00
|
|
|
if test.host != "" {
|
|
|
|
m.hostname = test.host
|
|
|
|
}
|
|
|
|
|
2019-01-15 08:44:03 +00:00
|
|
|
m.ServeHTTP(nil, req)
|
|
|
|
|
|
|
|
for k, v := range test.expectedHeaders {
|
|
|
|
assert.Equal(t, v, req.Header.Get(k))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 10:40:04 +00:00
|
|
|
|
|
|
|
func Test_isWebsocketRequest(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
connectionHeader string
|
|
|
|
upgradeHeader string
|
|
|
|
assert assert.BoolAssertionFunc
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "connection Header multiple values middle",
|
|
|
|
connectionHeader: "foo,upgrade,bar",
|
|
|
|
upgradeHeader: "websocket",
|
|
|
|
assert: assert.True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connection Header multiple values end",
|
|
|
|
connectionHeader: "foo,bar,upgrade",
|
|
|
|
upgradeHeader: "websocket",
|
|
|
|
assert: assert.True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connection Header multiple values begin",
|
|
|
|
connectionHeader: "upgrade,foo,bar",
|
|
|
|
upgradeHeader: "websocket",
|
|
|
|
assert: assert.True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connection Header no upgrade",
|
|
|
|
connectionHeader: "foo,bar",
|
|
|
|
upgradeHeader: "websocket",
|
|
|
|
assert: assert.False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "connection Header empty",
|
|
|
|
connectionHeader: "",
|
|
|
|
upgradeHeader: "websocket",
|
|
|
|
assert: assert.False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no header values",
|
|
|
|
connectionHeader: "foo,bar",
|
|
|
|
upgradeHeader: "foo,bar",
|
|
|
|
assert: assert.False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "upgrade header multiple values",
|
|
|
|
connectionHeader: "upgrade",
|
|
|
|
upgradeHeader: "foo,bar,websocket",
|
|
|
|
assert: assert.True,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
|
|
|
|
req.Header.Set(connection, test.connectionHeader)
|
|
|
|
req.Header.Set(upgrade, test.upgradeHeader)
|
|
|
|
|
|
|
|
ok := isWebsocketRequest(req)
|
|
|
|
|
|
|
|
test.assert(t, ok)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-09-16 09:10:04 +00:00
|
|
|
|
|
|
|
func TestConnection(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
reqHeaders map[string]string
|
|
|
|
connectionHeaders []string
|
|
|
|
expected http.Header
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "simple remove",
|
|
|
|
reqHeaders: map[string]string{
|
|
|
|
"Foo": "bar",
|
|
|
|
connection: "foo",
|
|
|
|
},
|
|
|
|
expected: http.Header{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove and upgrade",
|
|
|
|
reqHeaders: map[string]string{
|
|
|
|
upgrade: "test",
|
|
|
|
"Foo": "bar",
|
|
|
|
connection: "upgrade,foo",
|
|
|
|
},
|
|
|
|
expected: http.Header{
|
|
|
|
upgrade: []string{"test"},
|
|
|
|
connection: []string{"Upgrade"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no remove",
|
|
|
|
reqHeaders: map[string]string{
|
|
|
|
"Foo": "bar",
|
|
|
|
connection: "fii",
|
|
|
|
},
|
|
|
|
expected: http.Header{
|
|
|
|
"Foo": []string{"bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no remove because connection header pass through",
|
|
|
|
reqHeaders: map[string]string{
|
|
|
|
"Foo": "bar",
|
|
|
|
connection: "Foo",
|
|
|
|
},
|
|
|
|
connectionHeaders: []string{"Foo"},
|
|
|
|
expected: http.Header{
|
|
|
|
"Foo": []string{"bar"},
|
|
|
|
connection: []string{"Foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
forwarded, err := NewXForwarded(true, nil, test.connectionHeaders, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "https://localhost", nil)
|
|
|
|
|
|
|
|
for k, v := range test.reqHeaders {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
forwarded.removeConnectionHeaders(req)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, req.Header)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|