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 { if err != nil {
ce.Reply("Failed to parse cookies: %v", err) ce.Reply("Failed to parse cookies: %v", err)
return return
} else if missingCookie := findMissingCookies(cookies); missingCookie != "" {
ce.Reply("Missing %s cookie", missingCookie)
return
} }
ce.Redact() ce.Redact()
err = ce.User.LoginGoogle(ce.Ctx, cookies, func(emoji string) { err = ce.User.LoginGoogle(ce.Ctx, cookies, func(emoji string) {

View file

@ -20,6 +20,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"strings" "strings"
@ -321,6 +322,15 @@ type RespGoogleLoginStart struct {
Emoji string `json:"emoji"` 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) { func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Request) {
userID := r.URL.Query().Get("user_id") userID := r.URL.Query().Get("user_id")
user := prov.bridge.GetUserByMXID(id.UserID(userID)) user := prov.bridge.GetUserByMXID(id.UserID(userID))
@ -339,6 +349,20 @@ func (prov *ProvisioningAPI) GoogleLoginStart(w http.ResponseWriter, r *http.Req
ErrCode: "bad json", ErrCode: "bad json",
}) })
return 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) emoji, err := user.AsyncLoginGoogleStart(req.Cookies)
if err != nil { if err != nil {