Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: use session id for OAuth validation (#633)

Guilherme Oenningafca0f6b64cc
diff --git a/app/handlers/oauth.go b/app/handlers/oauth.go
index e899df4ad..bec0babdf 100644
--- a/app/handlers/oauth.go
+++ b/app/handlers/oauth.go
@@ -8,7 +8,6 @@ import (
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
- "github.com/getfider/fider/app/pkg/crypto"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/web"
@@ -25,11 +24,9 @@ func OAuthEcho() web.HandlerFunc {
return c.Redirect("/")
}
- originIdentifier := c.QueryParam("identifier")
- currentIdentifier := getOAuthIdentifier(c)
-
- if originIdentifier != currentIdentifier {
- c.Logger().Warn("OAuth identifier doesn't match. Aborting sign in process.")
+ identifier := c.QueryParam("identifier")
+ if identifier == "" || identifier != c.SessionID() {
+ c.Logger().Warn("OAuth identifier doesn't match with user session ID. Aborting sign in process.")
return c.Redirect("/")
}
@@ -69,11 +66,9 @@ func OAuthToken() web.HandlerFunc {
return c.Redirect(redirectURL.String())
}
- originIdentifier := c.QueryParam("identifier")
- currentIdentifier := getOAuthIdentifier(c)
-
- if originIdentifier != currentIdentifier {
- c.Logger().Warn("OAuth identifier doesn't match. Aborting sign in process.")
+ identifier := c.QueryParam("identifier")
+ if identifier == "" || identifier != c.SessionID() {
+ c.Logger().Warn("OAuth identifier doesn't match with user session ID. Aborting sign in process.")
return c.Redirect(redirectURL.String())
}
@@ -216,15 +211,10 @@ func SignInByOAuth() web.HandlerFunc {
return c.Redirect(redirect)
}
- authURL, err := c.Services().OAuth.GetAuthURL(provider, redirect, getOAuthIdentifier(c))
+ authURL, err := c.Services().OAuth.GetAuthURL(provider, redirect, c.SessionID())
if err != nil {
return c.Failure(err)
}
return c.Redirect(authURL)
}
}
-
-func getOAuthIdentifier(c web.Context) string {
- identifier := fmt.Sprintf("%s-%s", c.Request.ClientIP, c.Request.GetHeader("User-Agent"))
- return crypto.SHA512(identifier)
-}
diff --git a/app/handlers/oauth_test.go b/app/handlers/oauth_test.go
index fe7d0ca7a..a6e5ed447 100644
--- a/app/handlers/oauth_test.go
+++ b/app/handlers/oauth_test.go
@@ -8,6 +8,7 @@ import (
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/handlers"
+ "github.com/getfider/fider/app/middlewares"
"github.com/getfider/fider/app/models"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/errors"
@@ -38,13 +39,13 @@ func TestSignInByOAuthHandler(t *testing.T) {
server, _ := mock.NewServer()
code, response := server.
AddParam("provider", oauth.FacebookProvider).
- AddHeader("User-Agent", "Chrome").
- WithClientIP("1.1.1.1").
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
WithURL("http://avengers.test.fider.io/oauth/facebook").
+ Use(middlewares.Session()).
Execute(handlers.SignInByOAuth())
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://avengers.test.fider.io/oauth/token?provider=facebook&redirect=http://avengers.test.fider.io|656e0c3864ae014bcaf370c2b2d265a7cc969ac50fe82a67c97f3042393a84dfd251596997d09e4f0f5a184e1b3dfccf24b193c2347e22c2b895d251290df162")
+ Expect(response.Header().Get("Location")).Equals("http://avengers.test.fider.io/oauth/token?provider=facebook&redirect=http://avengers.test.fider.io|MY_SESSION_ID")
}
func TestSignInByOAuthHandler_AuthenticatedUser(t *testing.T) {
@@ -54,9 +55,9 @@ func TestSignInByOAuthHandler_AuthenticatedUser(t *testing.T) {
code, response := server.
AsUser(mock.JonSnow).
AddParam("provider", oauth.FacebookProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
WithURL("http://avengers.test.fider.io/oauth/facebook?redirect=http://avengers.test.fider.io").
- AddHeader("User-Agent", "Chrome").
- WithClientIP("1.1.1.1").
+ Use(middlewares.Session()).
Execute(handlers.SignInByOAuth())
Expect(code).Equals(http.StatusTemporaryRedirect)
@@ -70,13 +71,13 @@ func TestSignInByOAuthHandler_AuthenticatedUser_UsingEcho(t *testing.T) {
code, response := server.
AsUser(mock.JonSnow).
AddParam("provider", oauth.FacebookProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
WithURL("http://avengers.test.fider.io/oauth/facebook?redirect=http://avengers.test.fider.io/oauth/facebook/echo").
- AddHeader("User-Agent", "Chrome").
- WithClientIP("1.1.1.1").
+ Use(middlewares.Session()).
Execute(handlers.SignInByOAuth())
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://avengers.test.fider.io/oauth/token?provider=facebook&redirect=http://avengers.test.fider.io/oauth/facebook/echo|656e0c3864ae014bcaf370c2b2d265a7cc969ac50fe82a67c97f3042393a84dfd251596997d09e4f0f5a184e1b3dfccf24b193c2347e22c2b895d251290df162")
+ Expect(response.Header().Get("Location")).Equals("http://avengers.test.fider.io/oauth/token?provider=facebook&redirect=http://avengers.test.fider.io/oauth/facebook/echo|MY_SESSION_ID")
}
func TestCallbackHandler_InvalidState(t *testing.T) {
@@ -158,9 +159,11 @@ func TestOAuthTokenHandler_ExistingUserAndProvider(t *testing.T) {
server, _ := mock.NewServer()
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/hello").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=MY_SESSION_ID&redirect=/hello").
OnTenant(mock.DemoTenant).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
AddParam("provider", oauth.FacebookProvider).
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
Expect(code).Equals(http.StatusTemporaryRedirect)
@@ -173,9 +176,11 @@ func TestOAuthTokenHandler_NewUser(t *testing.T) {
server, services := mock.NewServer()
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/hello").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=MY_SESSION_ID&redirect=/hello").
OnTenant(mock.DemoTenant).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
AddParam("provider", oauth.FacebookProvider).
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
Expect(code).Equals(http.StatusTemporaryRedirect)
@@ -202,9 +207,11 @@ func TestOAuthTokenHandler_NewUserWithoutEmail(t *testing.T) {
})
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=798&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=798&identifier=MY_SESSION_ID&redirect=/").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.FacebookProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
user, err := services.Users.GetByID(3)
@@ -242,9 +249,11 @@ func TestOAuthTokenHandler_ExistingUser_WithoutEmail(t *testing.T) {
})
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=MY_SESSION_ID&redirect=/").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.FacebookProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
Expect(code).Equals(http.StatusTemporaryRedirect)
@@ -264,9 +273,11 @@ func TestOAuthTokenHandler_ExistingUser_NewProvider(t *testing.T) {
server, services := mock.NewServer()
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=MY_SESSION_ID&redirect=/").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.GoogleProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
Expect(code).Equals(http.StatusTemporaryRedirect)
@@ -285,9 +296,11 @@ func TestOAuthTokenHandler_NewUser_PrivateTenant(t *testing.T) {
mock.AvengersTenant.IsPrivate = true
code, response := server.
- WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
+ WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=MY_SESSION_ID&redirect=/").
OnTenant(mock.AvengersTenant).
AddParam("provider", oauth.FacebookProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
user, err := services.Users.GetByEmail("[email protected]")
@@ -305,9 +318,11 @@ func TestOAuthTokenHandler_InvalidIdentifier(t *testing.T) {
mock.AvengersTenant.IsPrivate = true
code, response := server.
- WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=WRONG&redirect=/").
+ WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=SOME_OTHER_ID&redirect=/").
OnTenant(mock.AvengersTenant).
AddParam("provider", oauth.FacebookProvider).
+ AddCookie(web.CookieSessionName, "MY_SESSION_ID").
+ Use(middlewares.Session()).
Execute(handlers.OAuthToken())
Expect(code).Equals(http.StatusTemporaryRedirect)
diff --git a/app/pkg/mock/server.go b/app/pkg/mock/server.go
index eeda9f79d..59b13154b 100644
--- a/app/pkg/mock/server.go
+++ b/app/pkg/mock/server.go
@@ -45,8 +45,9 @@ func (s *Server) Engine() *web.Engine {
}
// Use adds a new middleware to pipeline
-func (s *Server) Use(middleware web.MiddlewareFunc) {
+func (s *Server) Use(middleware web.MiddlewareFunc) *Server {
s.middleware = append(s.middleware, middleware)
+ return s
}
// OnTenant set current context tenant
diff --git a/scripts/e2e.sh b/scripts/e2e.sh
index 35dea4f91..b4e3aa99b 100755
--- a/scripts/e2e.sh
+++ b/scripts/e2e.sh
@@ -12,6 +12,8 @@ start_fider () {
docker run \
-d \
-p 3000:3000 \
+ -e SSL_CERT=development.crt \
+ -e SSL_CERT_KEY=development.key \
-e HOST_MODE=$1 \
-e DATABASE_URL=postgres://fider_e2e:fider_e2e_pw@$PG_CONTAINER:5432/fider_e2e?sslmode=disable \
-v `pwd`/etc:/app/etc \