Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix: changes how we validate OAuth State (#559)

Guilherme Oenning657aecea31eb
diff --git a/app/handlers/images.go b/app/handlers/images.go
index 89e7ae417..948f00fad 100644
--- a/app/handlers/images.go
+++ b/app/handlers/images.go
@@ -8,9 +8,9 @@ import (
"net/http"
"strings"
+ "github.com/getfider/fider/app/pkg/crypto"
"github.com/getfider/fider/app/pkg/img"
"github.com/getfider/fider/app/pkg/log"
- "github.com/getfider/fider/app/pkg/md5"
"github.com/getfider/fider/app/pkg/web"
"github.com/goenning/letteravatar"
@@ -27,7 +27,7 @@ func Avatar() web.HandlerFunc {
user, err := c.Services().Users.GetByID(id)
if err == nil && user.Tenant.ID == c.Tenant().ID {
if user.Email != "" {
- url := fmt.Sprintf("https://www.gravatar.com/avatar/%s?s=%d&d=404", md5.Hash(strings.ToLower(user.Email)), size)
+ url := fmt.Sprintf("https://www.gravatar.com/avatar/%s?s=%d&d=404", crypto.MD5(strings.ToLower(user.Email)), size)
c.Logger().Debugf("Requesting gravatar: @{GravatarURL}", log.Props{
"GravatarURL": url,
})
diff --git a/app/handlers/oauth.go b/app/handlers/oauth.go
index d882f42c8..ee868f149 100644
--- a/app/handlers/oauth.go
+++ b/app/handlers/oauth.go
@@ -8,9 +8,9 @@ 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/rand"
"github.com/getfider/fider/app/pkg/web"
"github.com/getfider/fider/app/pkg/web/util"
)
@@ -25,14 +25,11 @@ func OAuthEcho() web.HandlerFunc {
return c.Redirect("/")
}
- identifier := c.QueryParam("identifier")
- cookie, err := c.Request.Cookie(web.CookieOAuthIdentifier)
- if err != nil {
- return c.Failure(errors.Wrap(err, "failed to get oauth identifier cookie"))
- }
+ originIdentifier := c.QueryParam("identifier")
+ currentIdentifier := getOAuthIdentifier(c)
- c.RemoveCookie(web.CookieOAuthIdentifier)
- if identifier == "" || cookie.Value == "" || identifier != cookie.Value {
+ if originIdentifier != currentIdentifier {
+ c.Logger().Warn("OAuth identifier doesn't match. Aborting sign in process.")
return c.Redirect("/")
}
@@ -72,15 +69,11 @@ func OAuthToken() web.HandlerFunc {
return c.Redirect(redirectURL.String())
}
- identifier := c.QueryParam("identifier")
- cookie, err := c.Request.Cookie(web.CookieOAuthIdentifier)
- if err != nil {
- c.Logger().Warn(err.Error())
- return c.Redirect(redirectURL.String())
- }
+ originIdentifier := c.QueryParam("identifier")
+ currentIdentifier := getOAuthIdentifier(c)
- c.RemoveCookie(web.CookieOAuthIdentifier)
- if identifier == "" || cookie.Value == "" || identifier != cookie.Value {
+ if originIdentifier != currentIdentifier {
+ c.Logger().Warn("OAuth identifier doesn't match. Aborting sign in process.")
return c.Redirect(redirectURL.String())
}
@@ -210,14 +203,17 @@ func OAuthCallback() web.HandlerFunc {
func SignInByOAuth() web.HandlerFunc {
return func(c web.Context) error {
provider := c.Param("provider")
- identifier := rand.String(16)
-
- c.AddCookie(web.CookieOAuthIdentifier, identifier, time.Now().Add(24*time.Hour))
- authURL, err := c.Services().OAuth.GetAuthURL(provider, c.QueryParam("redirect"), identifier)
+ authURL, err := c.Services().OAuth.GetAuthURL(provider, c.QueryParam("redirect"), getOAuthIdentifier(c))
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"))
+ c.Logger().Info(identifier)
+ return crypto.SHA512(identifier)
+}
diff --git a/app/handlers/oauth_test.go b/app/handlers/oauth_test.go
index dceda7a72..adfb644fa 100644
--- a/app/handlers/oauth_test.go
+++ b/app/handlers/oauth_test.go
@@ -38,10 +38,12 @@ func TestSignInByOAuthHandler(t *testing.T) {
server, _ := mock.NewServer()
code, response := server.
AddParam("provider", oauth.FacebookProvider).
+ AddHeader("User-Agent", "Chrome").
+ WithClientIP("1.1.1.1").
Execute(handlers.SignInByOAuth())
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).ContainsSubstring("http://avengers.test.fider.io/oauth/token?provider=facebook&redirect=|")
+ Expect(response.Header().Get("Location")).Equals("http://avengers.test.fider.io/oauth/token?provider=facebook&redirect=|656e0c3864ae014bcaf370c2b2d265a7cc969ac50fe82a67c97f3042393a84dfd251596997d09e4f0f5a184e1b3dfccf24b193c2347e22c2b895d251290df162")
}
func TestCallbackHandler_InvalidState(t *testing.T) {
@@ -123,8 +125,7 @@ func TestOAuthTokenHandler_ExistingUserAndProvider(t *testing.T) {
server, _ := mock.NewServer()
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=888&redirect=/hello").
- AddCookie(web.CookieOAuthIdentifier, "888").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/hello").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.FacebookProvider).
Execute(handlers.OAuthToken())
@@ -139,8 +140,7 @@ 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=888&redirect=/hello").
- AddCookie(web.CookieOAuthIdentifier, "888").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/hello").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.FacebookProvider).
Execute(handlers.OAuthToken())
@@ -169,8 +169,7 @@ func TestOAuthTokenHandler_NewUserWithoutEmail(t *testing.T) {
})
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=798&identifier=888&redirect=/").
- AddCookie(web.CookieOAuthIdentifier, "888").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=798&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.FacebookProvider).
Execute(handlers.OAuthToken())
@@ -210,8 +209,7 @@ func TestOAuthTokenHandler_ExistingUser_WithoutEmail(t *testing.T) {
})
code, response := server.
- WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=888&redirect=/").
- AddCookie(web.CookieOAuthIdentifier, "888").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=456&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.FacebookProvider).
Execute(handlers.OAuthToken())
@@ -233,8 +231,7 @@ 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=888&redirect=/").
- AddCookie(web.CookieOAuthIdentifier, "888").
+ WithURL("http://demo.test.fider.io/oauth/facebook/token?code=123&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
OnTenant(mock.DemoTenant).
AddParam("provider", oauth.GoogleProvider).
Execute(handlers.OAuthToken())
@@ -255,8 +252,7 @@ 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=888&redirect=/").
- AddCookie(web.CookieOAuthIdentifier, "888").
+ WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=7c0b0d99a6e4c33cda0f6f63547f878f4dd9f486dfe5d0446ce004b1c0ff28f191ff86f5d5933d3614cceee6fbbdc17e658881d3a164dfa5d6f4c699b2126e3d&redirect=/").
OnTenant(mock.AvengersTenant).
AddParam("provider", oauth.FacebookProvider).
Execute(handlers.OAuthToken())
@@ -276,8 +272,7 @@ func TestOAuthTokenHandler_InvalidIdentifier(t *testing.T) {
mock.AvengersTenant.IsPrivate = true
code, response := server.
- WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=888&redirect=/").
- AddCookie(web.CookieOAuthIdentifier, "777").
+ WithURL("http://feedback.theavengers.com/oauth/facebook/token?code=456&identifier=WRONG&redirect=/").
OnTenant(mock.AvengersTenant).
AddParam("provider", oauth.FacebookProvider).
Execute(handlers.OAuthToken())
diff --git a/app/pkg/crypto/md5.go b/app/pkg/crypto/md5.go
new file mode 100644
index 000000000..63f3ee45d
--- /dev/null
+++ b/app/pkg/crypto/md5.go
@@ -0,0 +1,11 @@
+package crypto
+
+import (
+ "crypto/md5"
+ "fmt"
+)
+
+//MD5 returns the MD5 hash of a given string
+func MD5(input string) string {
+ return fmt.Sprintf("%x", md5.Sum([]byte(input)))
+}
diff --git a/app/pkg/md5/md5_test.go b/app/pkg/crypto/md5_test.go
similarity index 65%
rename from app/pkg/md5/md5_test.go
rename to app/pkg/crypto/md5_test.go
index c2554d6f1..f217558bc 100644
--- a/app/pkg/md5/md5_test.go
+++ b/app/pkg/crypto/md5_test.go
@@ -1,16 +1,16 @@
-package md5_test
+package crypto_test
import (
"testing"
. "github.com/getfider/fider/app/pkg/assert"
- "github.com/getfider/fider/app/pkg/md5"
+ "github.com/getfider/fider/app/pkg/crypto"
)
func TestMD5Hash(t *testing.T) {
RegisterT(t)
- hash := md5.Hash("Fider")
+ hash := crypto.MD5("Fider")
Expect(hash).Equals("3734538c8b650e4f354a55a436566bb6")
}
diff --git a/app/pkg/crypto/sha512.go b/app/pkg/crypto/sha512.go
new file mode 100644
index 000000000..030203690
--- /dev/null
+++ b/app/pkg/crypto/sha512.go
@@ -0,0 +1,12 @@
+package crypto
+
+import (
+ "crypto/sha512"
+
+ "fmt"
+)
+
+//SHA512 returns the SHA512 hash of a given string
+func SHA512(input string) string {
+ return fmt.Sprintf("%x", sha512.Sum512([]byte(input)))
+}
diff --git a/app/pkg/crypto/sha512_test.go b/app/pkg/crypto/sha512_test.go
new file mode 100644
index 000000000..c58480069
--- /dev/null
+++ b/app/pkg/crypto/sha512_test.go
@@ -0,0 +1,16 @@
+package crypto_test
+
+import (
+ "testing"
+
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/crypto"
+)
+
+func TestSHA512Hash(t *testing.T) {
+ RegisterT(t)
+
+ hash := crypto.SHA512("Fider")
+
+ Expect(hash).Equals("262d21f30715f2b226264844c4ab2a934a4c0241321f77bebbca191e172df93da71c939c56fcb4bbdd8895fa8c496882d38e3ce66d9d4e3dee5bacde01e73988")
+}
diff --git a/app/pkg/md5/md5.go b/app/pkg/md5/md5.go
deleted file mode 100644
index 31deadbc4..000000000
--- a/app/pkg/md5/md5.go
+++ /dev/null
@@ -1,11 +0,0 @@
-package md5
-
-import (
- "crypto/md5"
- "fmt"
-)
-
-//Hash returns the MD5 hash of a given string
-func Hash(input string) string {
- return fmt.Sprintf("%x", md5.Sum([]byte(input)))
-}
diff --git a/app/pkg/mock/server.go b/app/pkg/mock/server.go
index 775fabdc8..eeda9f79d 100644
--- a/app/pkg/mock/server.go
+++ b/app/pkg/mock/server.go
@@ -81,6 +81,12 @@ func (s *Server) AddCookie(name string, value string) *Server {
return s
}
+// WithClientIP set current ClientIP address
+func (s *Server) WithClientIP(clientIP string) *Server {
+ s.context.Request.ClientIP = clientIP
+ return s
+}
+
// WithClaims set current context user claims
func (s *Server) WithClaims(claims *jwt.FiderClaims) *Server {
s.context.SetClaims(claims)
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index 4f932510a..f99d81be1 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -53,9 +53,6 @@ const CookieAuthName = "auth"
// CookieSignUpAuthName is the name of the cookie that holds the temporary Authentication Token
const CookieSignUpAuthName = "__signup_auth"
-// CookieOAuthIdentifier is the name of the cookie that holds the temporary OAuth identifier string
-const CookieOAuthIdentifier = "__oauth_identifier"
-
var (
preffixKey = "__CTX_"
tenantContextKey = preffixKey + "TENANT"
diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.go
index b8efa2777..7e9c1fd60 100644
--- a/app/pkg/web/renderer.go
+++ b/app/pkg/web/renderer.go
@@ -9,17 +9,17 @@ import (
"io/ioutil"
"github.com/getfider/fider/app/models"
+ "github.com/getfider/fider/app/pkg/crypto"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/markdown"
- "github.com/getfider/fider/app/pkg/md5"
"github.com/getfider/fider/app/pkg/oauth"
)
var templateFunctions = template.FuncMap{
"md5": func(input string) string {
- return md5.Hash(input)
+ return crypto.MD5(input)
},
"markdown": func(input string) template.HTML {
return markdown.Parse(input)