2016-05-13 02:45:00 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-06-07 07:27:36 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-05-13 02:45:00 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/test"
|
2016-07-19 08:24:50 +02:00
|
|
|
"github.com/labstack/gommon/random"
|
2016-05-13 02:45:00 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCSRF(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
req := test.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
2016-06-07 07:27:36 +02:00
|
|
|
csrf := CSRFWithConfig(CSRFConfig{
|
2016-07-17 05:13:27 +02:00
|
|
|
TokenLength: 16,
|
2016-06-07 07:27:36 +02:00
|
|
|
})
|
2016-05-13 02:45:00 +02:00
|
|
|
h := csrf(func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Generate CSRF token
|
|
|
|
h(c)
|
2016-07-14 01:55:46 +02:00
|
|
|
assert.Contains(t, rec.Header().Get(echo.HeaderSetCookie), "_csrf")
|
|
|
|
|
|
|
|
// Without CSRF cookie
|
|
|
|
req = test.NewRequest(echo.POST, "/", nil)
|
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
assert.Error(t, h(c))
|
2016-05-13 02:45:00 +02:00
|
|
|
|
|
|
|
// Empty/invalid CSRF token
|
|
|
|
req = test.NewRequest(echo.POST, "/", nil)
|
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
req.Header().Set(echo.HeaderXCSRFToken, "")
|
2016-07-14 01:55:46 +02:00
|
|
|
assert.Error(t, h(c))
|
2016-05-13 02:45:00 +02:00
|
|
|
|
|
|
|
// Valid CSRF token
|
2016-07-19 08:24:50 +02:00
|
|
|
token := random.String(16)
|
2016-07-14 01:55:46 +02:00
|
|
|
req.Header().Set(echo.HeaderCookie, "_csrf="+token)
|
2016-05-13 02:45:00 +02:00
|
|
|
req.Header().Set(echo.HeaderXCSRFToken, token)
|
2016-06-07 07:27:36 +02:00
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Status())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCSRFTokenFromForm(t *testing.T) {
|
|
|
|
f := make(url.Values)
|
|
|
|
f.Set("csrf", "token")
|
|
|
|
e := echo.New()
|
|
|
|
req := test.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
|
|
|
|
req.Header().Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
token, err := csrfTokenFromForm("csrf")(c)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "token", token)
|
|
|
|
}
|
|
|
|
token, err = csrfTokenFromForm("invalid")(c)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCSRFTokenFromQuery(t *testing.T) {
|
|
|
|
q := make(url.Values)
|
|
|
|
q.Set("csrf", "token")
|
|
|
|
e := echo.New()
|
|
|
|
req := test.NewRequest(echo.GET, "/?"+q.Encode(), nil)
|
|
|
|
req.Header().Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
token, err := csrfTokenFromQuery("csrf")(c)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "token", token)
|
|
|
|
}
|
|
|
|
token, err = csrfTokenFromQuery("invalid")(c)
|
|
|
|
assert.Error(t, err)
|
|
|
|
csrfTokenFromQuery("csrf")
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|