forked from alphagov/cdn-acceptance-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdn_req_headers_test.go
142 lines (117 loc) · 3.72 KB
/
cdn_req_headers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"net"
"net/http"
"strings"
"testing"
)
// Should set an `X-Forwarded-For` header for requests that don't already
// have one and append to requests that already have the header. This test
// will not work if run from behind a proxy that also sets XFF.
func TestReqHeaderXFFCreateAndAppend(t *testing.T) {
ResetBackends(backendsByPriority)
const headerName = "X-Forwarded-For"
const sentHeaderVal = "203.0.113.99"
var ourReportedIP net.IP
var receivedHeaderVal string
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
receivedHeaderVal = r.Header.Get(headerName)
})
// First request with no existing XFF.
req := NewUniqueEdgeGET(t)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if receivedHeaderVal == "" {
t.Fatalf("Origin didn't receive request with %q header", headerName)
}
ourReportedIP = net.ParseIP(receivedHeaderVal)
if ourReportedIP == nil {
t.Fatalf(
"Expected origin to receive %q header with single IP. Got %q",
headerName,
receivedHeaderVal,
)
}
// Use the IP returned by the first response to predict the second.
expectedHeaderVals := []string{sentHeaderVal, ourReportedIP.String()}
// Second request with existing XFF.
req = NewUniqueEdgeGET(t)
req.Header.Set(headerName, sentHeaderVal)
resp = RoundTripCheckError(t, req)
defer resp.Body.Close()
receivedHeaderVals := strings.Split(receivedHeaderVal, ",")
if count := len(receivedHeaderVals); count != len(expectedHeaderVals) {
t.Fatalf(
"Origin received %q header with wrong count of IPs. Expected %q, got %d: %q",
headerName,
expectedHeaderVals,
count,
receivedHeaderVal,
)
}
for count, expectedVal := range expectedHeaderVals {
receivedVal := strings.TrimSpace(receivedHeaderVals[count])
if receivedVal != expectedVal {
t.Errorf(
"Origin received %q header with wrong IP #%d. Expected %q, got %q",
headerName,
count+1,
expectedVal,
receivedVal,
)
}
}
}
// Should create a True-Client-IP header containing the client's IP
// address, discarding the value provided in the original request. The name
// of this header must be consistent across all vendors.
func TestReqHeaderUnspoofableClientIP(t *testing.T) {
ResetBackends(backendsByPriority)
const sentHeaderVal = "203.0.113.99"
const headerName = "True-Client-IP"
var receivedHeaderVal string
sentHeaderIP := net.ParseIP(sentHeaderVal)
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
receivedHeaderVal = r.Header.Get(headerName)
})
req := NewUniqueEdgeGET(t)
req.Header.Set(headerName, sentHeaderVal)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
receivedHeaderIP := net.ParseIP(receivedHeaderVal)
if receivedHeaderIP == nil {
t.Fatalf("Origin received %q header with non-IP value %q", headerName, receivedHeaderVal)
}
if receivedHeaderIP.Equal(sentHeaderIP) {
t.Errorf("Origin received %q header with unmodified value %q", headerName, receivedHeaderIP)
}
}
// Should not modify `Host` header from original request.
func TestReqHeaderHostUnmodified(t *testing.T) {
const headerName = "Host"
var sentHeaderVal = *edgeHost
var receivedHeaderVal string
ResetBackends(backendsByPriority)
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
receivedHeaderVal = r.Host
})
req := NewUniqueEdgeGET(t)
if req.Host != sentHeaderVal {
t.Errorf(
"Constructed request contains wrong %q header. Expected %q, got %q",
headerName,
sentHeaderVal,
req.Host,
)
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if receivedHeaderVal != sentHeaderVal {
t.Errorf(
"Origin received %q header with modified value. Expected %q, got %q",
headerName,
sentHeaderVal,
receivedHeaderVal,
)
}
}