Skip to content

Commit

Permalink
all: remove the last few direct r.RemoteAddr uses
Browse files Browse the repository at this point in the history
They should all be using either requestIP or requestAddrs, depending on
whether they want the real IP or all the addresses of all the hops.

This fixes an issue found in the context vars code. While at it, fix a
couple of other occurences.

Fixes #1017.
  • Loading branch information
mvdan committed Aug 21, 2017
1 parent 80f1615 commit 093257a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion instrumentation_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func InstrumentationMW(next http.Handler) http.Handler {

next.ServeHTTP(w, r)
job.EventKv("called", health.Kvs{
"from_ip": r.RemoteAddr,
"from_ip": requestIP(r),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func CreateMiddleware(mw TykMiddleware) func(http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
job := instrument.NewJob("MiddlewareCall")
meta := health.Kvs{
"from_ip": r.RemoteAddr,
"from_ip": requestIP(r),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
Expand Down
2 changes: 1 addition & 1 deletion mw_context_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m *MiddlewareContextVars) ProcessRequest(w http.ResponseWriter, r *http.Re
contextDataObject["path"] = copiedRequest.URL.Path

// IP:Port
contextDataObject["remote_addr"] = copiedRequest.RemoteAddr
contextDataObject["remote_addr"] = requestIP(copiedRequest)

//Correlation ID
contextDataObject["request_id"] = uuid.NewV4().String()
Expand Down
6 changes: 5 additions & 1 deletion mw_context_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestContextVarsMiddleware(t *testing.T) {
req := testReq(t, method, uri+param.Encode(), nil)
req.RemoteAddr = "127.0.0.1:80"
req.Header.Set("authorization", "1234wer")
req.Header.Set("x-forwarded-for", "1.2.3.4, 5.6.7.8")

chain := getChain(spec)
chain.ServeHTTP(recorder, req)
Expand All @@ -65,8 +66,11 @@ func TestContextVarsMiddleware(t *testing.T) {
t.Fatal("Could not find Path in header")
}

if req.Header.Get("X-Remote-Addr") == "" {
addr := req.Header.Get("X-Remote-Addr")
if addr == "" {
t.Fatal("Could not find Remote-Addr in header")
} else if addr != "1.2.3.4" {
t.Fatal("Remote-Addr is not the expected value")
}

if req.Header.Get("X-Request-ID") == "" {
Expand Down
2 changes: 1 addition & 1 deletion mw_hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (hm *HMACMiddleware) authorizationError(r *http.Request) (error, int) {
log.WithFields(logrus.Fields{
"prefix": "hmac",
"path": r.URL.Path,
"origin": r.RemoteAddr,
"origin": requestIP(r),
}).Info("Authorization field missing or malformed")

AuthFailed(hm.BaseMiddleware, r, r.Header.Get("Authorization"))
Expand Down

0 comments on commit 093257a

Please sign in to comment.