Skip to content

Commit

Permalink
Change: no XFF header from Couper (#139)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Schneider <alex.schneider@sevenval.com>
  • Loading branch information
Alex Schneider and Alex Schneider authored Mar 15, 2021
1 parent 96edaec commit 6857b5f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/request/context_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const (
RoundTripProxy
ServerName
Wildcard
XFF
)
8 changes: 8 additions & 0 deletions handler/transport/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ func (b *Backend) RoundTrip(req *http.Request) (*http.Response, error) {
}
}

if xff, ok := req.Context().Value(request.XFF).(string); ok {
if xff != "" {
req.Header.Set("X-Forwarded-For", xff)
} else {
req.Header.Del("X-Forwarded-For")
}
}

setUserAgent(req)
req.Close = false
beresp, err := t.RoundTrip(req)
Expand Down
1 change: 1 addition & 0 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (s *HTTPServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

uid := s.uidFn()
ctx := context.WithValue(req.Context(), request.UID, uid)
ctx = context.WithValue(ctx, request.XFF, req.Header.Get("X-Forwarded-For"))
*req = *req.WithContext(ctx)

req.Host = s.getHost(req)
Expand Down
8 changes: 7 additions & 1 deletion server/http_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestEndpoints_Res(t *testing.T) {
}
}

func TestEndpoints_UpstreamBasicAuth(t *testing.T) {
func TestEndpoints_UpstreamBasicAuthAndXFF(t *testing.T) {
client := newClient()
helper := test.New(t)

Expand All @@ -88,6 +88,8 @@ func TestEndpoints_UpstreamBasicAuth(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "http://example.com:8080/anything", nil)
helper.Must(err)

req.Header.Set("X-Forwarded-For", "1.2.3.4")

res, err := client.Do(req)
helper.Must(err)

Expand All @@ -111,4 +113,8 @@ func TestEndpoints_UpstreamBasicAuth(t *testing.T) {
if v := jsonResult.Headers.Get("Authorization"); v != "Basic dXNlcjpwYXNz" {
t.Errorf("Expected a valid 'Authorization' header, given '%s'", v)
}

if v := jsonResult.Headers.Get("X-Forwarded-For"); v != "1.2.3.4" {
t.Errorf("Unexpected XFF header given '%s'", v)
}
}

0 comments on commit 6857b5f

Please sign in to comment.