2016-05-13 02:45:00 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-06-07 07:27:36 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-05-13 02:45:00 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
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()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-05-13 02:45:00 +02:00
|
|
|
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
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.POST, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-07-14 01:55:46 +02:00
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
assert.Error(t, h(c))
|
2016-05-13 02:45:00 +02:00
|
|
|
|
|
|
|
// Empty/invalid CSRF token
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.POST, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-05-13 02:45:00 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-09-23 07:53:44 +02:00
|
|
|
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-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(echo.HeaderCookie, "_csrf="+token)
|
|
|
|
req.Header.Set(echo.HeaderXCSRFToken, token)
|
2016-06-07 07:27:36 +02:00
|
|
|
if assert.NoError(t, h(c)) {
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2016-06-07 07:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCSRFTokenFromForm(t *testing.T) {
|
|
|
|
f := make(url.Values)
|
|
|
|
f.Set("csrf", "token")
|
|
|
|
e := echo.New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
2016-06-07 07:27:36 +02:00
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
token, err := csrfTokenFromForm("csrf")(c)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "token", token)
|
|
|
|
}
|
2017-04-01 18:29:12 +02:00
|
|
|
_, err = csrfTokenFromForm("invalid")(c)
|
2016-06-07 07:27:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCSRFTokenFromQuery(t *testing.T) {
|
|
|
|
q := make(url.Values)
|
|
|
|
q.Set("csrf", "token")
|
|
|
|
e := echo.New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(echo.GET, "/?"+q.Encode(), nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
2016-06-07 07:27:36 +02:00
|
|
|
c := e.NewContext(req, nil)
|
|
|
|
token, err := csrfTokenFromQuery("csrf")(c)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "token", token)
|
|
|
|
}
|
2017-04-01 18:29:12 +02:00
|
|
|
_, err = csrfTokenFromQuery("invalid")(c)
|
2016-06-07 07:27:36 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
csrfTokenFromQuery("csrf")
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|