Skip to content

Commit

Permalink
Include cookie values in mw context data
Browse files Browse the repository at this point in the history
fixes: #1544

When enabling context variables in api definition, we may access the
entire cookie via the header.

```
$tyk_context.headers_Cookie
```

However we cannot get individual components of the cookie via their
field names without custom middleware.

This PR allows the context variables to access the cookie by it's field
name for use within middleware.

```
$tyk_context.cookies_COOKIEFIELDNAME
```
  • Loading branch information
asoorm authored and buger committed Mar 20, 2018
1 parent e7aa7c2 commit 1c90a43
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
7 changes: 6 additions & 1 deletion mw_context_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ func (m *MiddlewareContextVars) ProcessRequest(w http.ResponseWriter, r *http.Re
//Correlation ID
contextDataObject["request_id"] = uuid.NewV4().String()

for _, c := range copiedRequest.Cookies() {
name := "cookies_" + strings.Replace(c.Name, "-", "_", -1)
contextDataObject[name] = c.Value
}

ctxSetData(r, contextDataObject)

return nil, 200
return nil, http.StatusOK
}
34 changes: 34 additions & 0 deletions mw_context_vars_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/TykTechnologies/tyk/apidef"
Expand Down Expand Up @@ -34,3 +37,34 @@ func TestContextVarsMiddleware(t *testing.T) {
{Path: "/test/path", Code: 200, BodyMatch: `"X-Request-Id":"`},
}...)
}

func TestMiddlewareContextVars_ProcessRequest_cookies(t *testing.T) {

req, _ := http.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()

req.Header.Set("Cookie", "abc=123; def=456")

err, code := (&MiddlewareContextVars{}).ProcessRequest(res, req, nil)
if err != nil {
t.Fatal(err)
}

if code != http.StatusOK {
t.Fatal(errors.New("non 200 status code"))
}

ctx := ctxGetData(req)

if ctx["cookies_abc"].(string) != "123" {
t.Error("abc should be 123")
}

if ctx["cookies_def"].(string) != "456" {
t.Error("def should be 456")
}

if ctx["cookies_ghi"] != nil {
t.Error("ghi should be nil")
}
}
4 changes: 3 additions & 1 deletion mw_ip_blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"net"
"net/http"

"github.com/TykTechnologies/tyk/request"
)

// IPBlackListMiddleware lets you define a list of IPs to block from upstream
Expand All @@ -21,7 +23,7 @@ func (i *IPBlackListMiddleware) EnabledForSpec() bool {

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (i *IPBlackListMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
remoteIP := net.ParseIP(requestIP(r))
remoteIP := net.ParseIP(request.RealIP(r))

// Enabled, check incoming IP address
for _, ip := range i.Spec.BlacklistedIPs {
Expand Down

0 comments on commit 1c90a43

Please sign in to comment.