Check cookies are present before trying to log in

This commit is contained in:
Tulir Asokan 2024-02-26 16:48:06 +02:00
parent b3457907fa
commit eccbb9910e
2 changed files with 27 additions and 0 deletions

View file

@ -174,6 +174,9 @@ func fnLoginGoogleCookies(ce *WrappedCommandEvent) {
if err != nil {
ce.Reply("Failed to parse cookies: %v", err)
return
} else if missingCookie := findMissingCookies(cookies); missingCookie != "" {
ce.Reply("Missing %s cookie", missingCookie)
return
}
ce.Redact()
err = ce.User.LoginGoogle(ce.Ctx, cookies, func(emoji string) {

View file

@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"strings"
@ -321,6 +322,15 @@ type RespGoogleLoginStart struct {
Emoji string `json:"emoji"`
}
func findMissingCookies(cookies map[string]string) string {
for _, requiredCookie := range []string{"SID", "SSID", "HSID", "OSID", "APISID", "SAPISID"} {
if _, ok := cookies[requiredCookie]; !ok {
return requiredCookie
}
}
return ""
}
func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Request) {
userID := r.URL.Query().Get("user_id")
user := prov.bridge.GetUserByMXID(id.UserID(userID))
@ -339,6 +349,20 @@ func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Req
ErrCode: "bad json",
})
return
} else if len(req.Cookies) == 0 {
log.Warn().Msg("No cookies in request")
jsonResponse(w, http.StatusBadRequest, Error{
Error: "No cookies in request",
ErrCode: "missing cookies",
})
return
} else if missingCookie := findMissingCookies(req.Cookies); missingCookie != "" {
log.Warn().Msg("Missing cookies in request")
jsonResponse(w, http.StatusBadRequest, Error{
Error: fmt.Sprintf("Missing %s cookie", missingCookie),
ErrCode: "missing cookies",
})
return
}
emoji, err := user.AsyncLoginGoogleStart(req.Cookies)
if err != nil {