CSRF Token Implementation for Plugins (#9192)
deleted test config fix test config Dont wipe the session token for plugins Simplified Tokens; Generate CSRF for other sessions Remove CSRF from Access Token; Remove Getter/Setter from Context fix removed setter remove getcsrf helper from plugin api enforce csrf only for cookie auth
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
90e84d76ef
Коммит
2936dc87d0
@@ -126,7 +126,7 @@ func (a *App) GetUserForLogin(id, loginId string) (*model.User, *model.AppError)
|
|||||||
|
|
||||||
func (a *App) DoLogin(w http.ResponseWriter, r *http.Request, user *model.User, deviceId string) (*model.Session, *model.AppError) {
|
func (a *App) DoLogin(w http.ResponseWriter, r *http.Request, user *model.User, deviceId string) (*model.Session, *model.AppError) {
|
||||||
session := &model.Session{UserId: user.Id, Roles: user.GetRawRoles(), DeviceId: deviceId, IsOAuth: false}
|
session := &model.Session{UserId: user.Id, Roles: user.GetRawRoles(), DeviceId: deviceId, IsOAuth: false}
|
||||||
|
session.GenerateCSRF()
|
||||||
maxAge := *a.Config().ServiceSettings.SessionLengthWebInDays * 60 * 60 * 24
|
maxAge := *a.Config().ServiceSettings.SessionLengthWebInDays * 60 * 60 * 24
|
||||||
|
|
||||||
if len(deviceId) > 0 {
|
if len(deviceId) > 0 {
|
||||||
|
|||||||
@@ -334,6 +334,7 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
|||||||
func (a *App) newSession(appName string, user *model.User) (*model.Session, *model.AppError) {
|
func (a *App) newSession(appName string, user *model.User) (*model.Session, *model.AppError) {
|
||||||
// set new token an session
|
// set new token an session
|
||||||
session := &model.Session{UserId: user.Id, Roles: user.Roles, IsOAuth: true}
|
session := &model.Session{UserId: user.Id, Roles: user.Roles, IsOAuth: true}
|
||||||
|
session.GenerateCSRF()
|
||||||
session.SetExpireInDays(*a.Config().ServiceSettings.SessionLengthSSOInDays)
|
session.SetExpireInDays(*a.Config().ServiceSettings.SessionLengthSSOInDays)
|
||||||
session.AddProp(model.SESSION_PROP_PLATFORM, appName)
|
session.AddProp(model.SESSION_PROP_PLATFORM, appName)
|
||||||
session.AddProp(model.SESSION_PROP_OS, "OAuth2")
|
session.AddProp(model.SESSION_PROP_OS, "OAuth2")
|
||||||
|
|||||||
@@ -65,6 +65,16 @@ func (api *PluginAPI) UnregisterCommand(teamId, trigger string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *PluginAPI) GetSession(sessionId string) (*model.Session, *model.AppError) {
|
||||||
|
session, err := api.app.GetSessionById(sessionId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return session, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (api *PluginAPI) GetConfig() *model.Config {
|
func (api *PluginAPI) GetConfig() *model.Config {
|
||||||
return api.app.GetConfig()
|
return api.app.GetConfig()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"bytes"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/mattermost/mattermost-server/mlog"
|
"github.com/mattermost/mattermost-server/mlog"
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"github.com/mattermost/mattermost-server/model"
|
||||||
"github.com/mattermost/mattermost-server/plugin"
|
"github.com/mattermost/mattermost-server/plugin"
|
||||||
"github.com/mattermost/mattermost-server/utils"
|
"github.com/mattermost/mattermost-server/utils"
|
||||||
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
|
func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -38,22 +40,44 @@ func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler func(*plugin.Context, http.ResponseWriter, *http.Request)) {
|
func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler func(*plugin.Context, http.ResponseWriter, *http.Request)) {
|
||||||
token := ""
|
token := ""
|
||||||
|
context := &plugin.Context{}
|
||||||
|
cookieAuth := false
|
||||||
|
|
||||||
authHeader := r.Header.Get(model.HEADER_AUTH)
|
authHeader := r.Header.Get(model.HEADER_AUTH)
|
||||||
if strings.HasPrefix(strings.ToUpper(authHeader), model.HEADER_BEARER+" ") {
|
if strings.HasPrefix(strings.ToUpper(authHeader), model.HEADER_BEARER+" ") {
|
||||||
token = authHeader[len(model.HEADER_BEARER)+1:]
|
token = authHeader[len(model.HEADER_BEARER)+1:]
|
||||||
} else if strings.HasPrefix(strings.ToLower(authHeader), model.HEADER_TOKEN+" ") {
|
} else if strings.HasPrefix(strings.ToLower(authHeader), model.HEADER_TOKEN+" ") {
|
||||||
token = authHeader[len(model.HEADER_TOKEN)+1:]
|
token = authHeader[len(model.HEADER_TOKEN)+1:]
|
||||||
} else if cookie, _ := r.Cookie(model.SESSION_COOKIE_TOKEN); cookie != nil && (r.Method == "GET" || r.Header.Get(model.HEADER_REQUESTED_WITH) == model.HEADER_REQUESTED_WITH_XML) {
|
} else if cookie, _ := r.Cookie(model.SESSION_COOKIE_TOKEN); cookie != nil {
|
||||||
token = cookie.Value
|
token = cookie.Value
|
||||||
|
cookieAuth = true
|
||||||
} else {
|
} else {
|
||||||
token = r.URL.Query().Get("access_token")
|
token = r.URL.Query().Get("access_token")
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Header.Del("Mattermost-User-Id")
|
r.Header.Del("Mattermost-User-Id")
|
||||||
if token != "" {
|
if token != "" {
|
||||||
if session, err := a.GetSession(token); session != nil && err == nil {
|
session, err := a.GetSession(token)
|
||||||
|
csrfCheckPassed := true
|
||||||
|
|
||||||
|
if err == nil && cookieAuth && r.Method != "GET" && r.Header.Get(model.HEADER_REQUESTED_WITH) != model.HEADER_REQUESTED_WITH_XML {
|
||||||
|
bodyBytes, _ := ioutil.ReadAll(r.Body)
|
||||||
|
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||||
|
r.ParseForm()
|
||||||
|
sentToken := r.FormValue("csrf")
|
||||||
|
expectedToken := session.GetCSRF()
|
||||||
|
|
||||||
|
if sentToken != expectedToken {
|
||||||
|
csrfCheckPassed = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Request Body again, since otherwise form values aren't accessible in plugin handler
|
||||||
|
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
if session != nil && err == nil && csrfCheckPassed {
|
||||||
r.Header.Set("Mattermost-User-Id", session.UserId)
|
r.Header.Set("Mattermost-User-Id", session.UserId)
|
||||||
|
context.SessionId = session.Id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,5 +100,5 @@ func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler
|
|||||||
r.URL.RawQuery = newQuery.Encode()
|
r.URL.RawQuery = newQuery.Encode()
|
||||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, path.Join(subpath, "plugins", params["plugin_id"]))
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, path.Join(subpath, "plugins", params["plugin_id"]))
|
||||||
|
|
||||||
handler(&plugin.Context{}, w, r)
|
handler(context, w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,20 @@ func (me *Session) GetUserRoles() []string {
|
|||||||
return strings.Fields(me.Roles)
|
return strings.Fields(me.Roles)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (me *Session) GenerateCSRF() string {
|
||||||
|
token := NewId()
|
||||||
|
me.AddProp("csrf", token)
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *Session) GetCSRF() string {
|
||||||
|
if me.Props == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return me.Props["csrf"]
|
||||||
|
}
|
||||||
|
|
||||||
func SessionsToJson(o []*Session) string {
|
func SessionsToJson(o []*Session) string {
|
||||||
if b, err := json.Marshal(o); err != nil {
|
if b, err := json.Marshal(o); err != nil {
|
||||||
return "[]"
|
return "[]"
|
||||||
|
|||||||
@@ -63,3 +63,18 @@ func TestSessionJson(t *testing.T) {
|
|||||||
|
|
||||||
session.SetExpireInDays(10)
|
session.SetExpireInDays(10)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSessionCSRF(t *testing.T) {
|
||||||
|
s := Session{}
|
||||||
|
token := s.GetCSRF()
|
||||||
|
assert.Empty(t, token)
|
||||||
|
|
||||||
|
token = s.GenerateCSRF()
|
||||||
|
assert.NotEmpty(t, token)
|
||||||
|
|
||||||
|
token2 := s.GetCSRF()
|
||||||
|
assert.NotEmpty(t, token2)
|
||||||
|
assert.Equal(t, token, token2)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ type API interface {
|
|||||||
// UnregisterCommand unregisters a command previously registered via RegisterCommand.
|
// UnregisterCommand unregisters a command previously registered via RegisterCommand.
|
||||||
UnregisterCommand(teamId, trigger string) error
|
UnregisterCommand(teamId, trigger string) error
|
||||||
|
|
||||||
|
// GetSession returns the session object for the Session ID
|
||||||
|
GetSession(sessionId string) (*model.Session, *model.AppError)
|
||||||
|
|
||||||
// GetConfig fetches the currently persisted config
|
// GetConfig fetches the currently persisted config
|
||||||
GetConfig() *model.Config
|
GetConfig() *model.Config
|
||||||
|
|
||||||
|
|||||||
@@ -558,6 +558,35 @@ func (s *apiRPCServer) UnregisterCommand(args *Z_UnregisterCommandArgs, returns
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Z_GetSessionArgs struct {
|
||||||
|
A string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Z_GetSessionReturns struct {
|
||||||
|
A *model.Session
|
||||||
|
B *model.AppError
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *apiRPCClient) GetSession(sessionId string) (*model.Session, *model.AppError) {
|
||||||
|
_args := &Z_GetSessionArgs{sessionId}
|
||||||
|
_returns := &Z_GetSessionReturns{}
|
||||||
|
if err := g.client.Call("Plugin.GetSession", _args, _returns); err != nil {
|
||||||
|
log.Printf("RPC call to GetSession API failed: %s", err.Error())
|
||||||
|
}
|
||||||
|
return _returns.A, _returns.B
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *apiRPCServer) GetSession(args *Z_GetSessionArgs, returns *Z_GetSessionReturns) error {
|
||||||
|
if hook, ok := s.impl.(interface {
|
||||||
|
GetSession(sessionId string) (*model.Session, *model.AppError)
|
||||||
|
}); ok {
|
||||||
|
returns.A, returns.B = hook.GetSession(args.A)
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("API GetSession called but not implemented.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type Z_GetConfigArgs struct {
|
type Z_GetConfigArgs struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ package plugin
|
|||||||
//
|
//
|
||||||
// It is currently a placeholder while the implementation details are sorted out.
|
// It is currently a placeholder while the implementation details are sorted out.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
|
SessionId string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -499,6 +499,31 @@ func (_m *API) GetPublicChannelsForTeam(teamId string, offset int, limit int) (*
|
|||||||
return r0, r1
|
return r0, r1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSession provides a mock function with given fields: sessionId
|
||||||
|
func (_m *API) GetSession(sessionId string) (*model.Session, *model.AppError) {
|
||||||
|
ret := _m.Called(sessionId)
|
||||||
|
|
||||||
|
var r0 *model.Session
|
||||||
|
if rf, ok := ret.Get(0).(func(string) *model.Session); ok {
|
||||||
|
r0 = rf(sessionId)
|
||||||
|
} else {
|
||||||
|
if ret.Get(0) != nil {
|
||||||
|
r0 = ret.Get(0).(*model.Session)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var r1 *model.AppError
|
||||||
|
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
|
||||||
|
r1 = rf(sessionId)
|
||||||
|
} else {
|
||||||
|
if ret.Get(1) != nil {
|
||||||
|
r1 = ret.Get(1).(*model.AppError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r0, r1
|
||||||
|
}
|
||||||
|
|
||||||
// GetTeam provides a mock function with given fields: teamId
|
// GetTeam provides a mock function with given fields: teamId
|
||||||
func (_m *API) GetTeam(teamId string) (*model.Team, *model.AppError) {
|
func (_m *API) GetTeam(teamId string) (*model.Team, *model.AppError) {
|
||||||
ret := _m.Called(teamId)
|
ret := _m.Called(teamId)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user