handle csrf, fix json unmarshall
This commit is contained in:
1 parent
afabeb4c17
commit
ca0966bc33
3 files changed
+115
-52
No files matched your search
+71
-50
@@ -80,6 +80,11 @@ type PullConfigResult struct {
|
|||||||
Revision int
|
Revision int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type extraHeader struct {
|
||||||
|
Name string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
type slbRequest struct {
|
type slbRequest struct {
|
||||||
UserToken string `json:"userToken"`
|
UserToken string `json:"userToken"`
|
||||||
IsOneToOne bool `json:"isOneToOne"`
|
IsOneToOne bool `json:"isOneToOne"`
|
||||||
@@ -218,7 +223,7 @@ func secFetchSiteFor(endpoint, origin string) string {
|
|||||||
return "cross-site"
|
return "cross-site"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) do(method, endpoint, contentType string, body io.Reader) ([]byte, int, error) {
|
func (c *Client) do(method, endpoint, contentType string, body io.Reader, extraHeaders ...extraHeader) ([]byte, int, error) {
|
||||||
req, err := http.NewRequest(method, endpoint, body)
|
req, err := http.NewRequest(method, endpoint, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
@@ -236,6 +241,9 @@ func (c *Client) do(method, endpoint, contentType string, body io.Reader) ([]byt
|
|||||||
if contentType != "" {
|
if contentType != "" {
|
||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
|
for _, h := range extraHeaders {
|
||||||
|
req.Header.Set(h.Name, h.Value)
|
||||||
|
}
|
||||||
resp, err := c.HTTP.Do(req)
|
resp, err := c.HTTP.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
@@ -252,20 +260,24 @@ func (c *Client) restForm(action string, form url.Values) ([]byte, int, error) {
|
|||||||
|
|
||||||
func (c *Client) restFormAuthed(action string, form url.Values) ([]byte, int, error) {
|
func (c *Client) restFormAuthed(action string, form url.Values) ([]byte, int, error) {
|
||||||
endpoint := c.portal + "/rest/" + action + ".json"
|
endpoint := c.portal + "/rest/" + action + ".json"
|
||||||
|
var extraHeaders []extraHeader
|
||||||
if c.sessid != "" {
|
if c.sessid != "" {
|
||||||
form.Set("sessid", c.sessid)
|
form.Set("sessid", c.sessid)
|
||||||
endpoint += "?sessid=" + url.QueryEscape(c.sessid)
|
endpoint += "?sessid=" + url.QueryEscape(c.sessid)
|
||||||
|
extraHeaders = append(extraHeaders, extraHeader{Name: "X-Bitrix-Csrf-Token", Value: c.sessid})
|
||||||
}
|
}
|
||||||
return c.do("POST", endpoint, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()))
|
return c.do("POST", endpoint, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()), extraHeaders...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ajaxAction(action string, form url.Values) ([]byte, int, error) {
|
func (c *Client) ajaxAction(action string, form url.Values) ([]byte, int, error) {
|
||||||
endpoint := c.portal + "/bitrix/services/main/ajax.php?action=" + action
|
endpoint := c.portal + "/bitrix/services/main/ajax.php?action=" + action
|
||||||
|
var extraHeaders []extraHeader
|
||||||
if c.sessid != "" {
|
if c.sessid != "" {
|
||||||
endpoint += "&sessid=" + url.QueryEscape(c.sessid)
|
endpoint += "&sessid=" + url.QueryEscape(c.sessid)
|
||||||
form.Set("sessid", c.sessid)
|
form.Set("sessid", c.sessid)
|
||||||
|
extraHeaders = append(extraHeaders, extraHeader{Name: "X-Bitrix-Csrf-Token", Value: c.sessid})
|
||||||
}
|
}
|
||||||
return c.do("POST", endpoint, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()))
|
return c.do("POST", endpoint, "application/x-www-form-urlencoded", strings.NewReader(form.Encode()), extraHeaders...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) fetchConference(alias string) (conferenceParams, error) {
|
func (c *Client) fetchConference(alias string) (conferenceParams, error) {
|
||||||
@@ -342,32 +354,36 @@ func (c *Client) tryJoinCall(chatID string) (callInfo, error) {
|
|||||||
return info, err
|
return info, err
|
||||||
}
|
}
|
||||||
var out struct {
|
var out struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Data struct {
|
Data json.RawMessage `json:"data"`
|
||||||
Success bool `json:"success"`
|
|
||||||
Call struct {
|
|
||||||
UUID string `json:"UUID"`
|
|
||||||
ChatID json.Number `json:"CHAT_ID"`
|
|
||||||
} `json:"call"`
|
|
||||||
CallToken string `json:"callToken"`
|
|
||||||
} `json:"data"`
|
|
||||||
Errors []struct {
|
Errors []struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Code string `json:"code"`
|
Code any `json:"code"`
|
||||||
} `json:"errors"`
|
} `json:"errors"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &out); err != nil {
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
return info, fmt.Errorf("tryJoinCall: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
return info, fmt.Errorf("tryJoinCall: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
if out.Status == "success" && !out.Data.Success {
|
var data struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Call struct {
|
||||||
|
UUID string `json:"UUID"`
|
||||||
|
ChatID json.Number `json:"CHAT_ID"`
|
||||||
|
} `json:"call"`
|
||||||
|
CallToken string `json:"callToken"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(out.Data, &data); err != nil && out.Status == "success" {
|
||||||
|
return info, fmt.Errorf("tryJoinCall: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
|
}
|
||||||
|
if out.Status == "success" && !data.Success {
|
||||||
return info, fmt.Errorf("tryJoinCall: no active call at chat%s, organizer not in the room", chatID)
|
return info, fmt.Errorf("tryJoinCall: no active call at chat%s, organizer not in the room", chatID)
|
||||||
}
|
}
|
||||||
if out.Status != "success" || out.Data.Call.UUID == "" {
|
if out.Status != "success" || data.Call.UUID == "" {
|
||||||
return info, fmt.Errorf("tryJoinCall failed (status %d, %s, errors %+v, body %s)", status, out.Status, out.Errors, common.BodySnippet(body))
|
return info, fmt.Errorf("tryJoinCall failed (status %d, %s, errors %+v, body %s)", status, out.Status, out.Errors, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
info.UUID = out.Data.Call.UUID
|
info.UUID = data.Call.UUID
|
||||||
info.ChatID = out.Data.Call.ChatID.String()
|
info.ChatID = data.Call.ChatID.String()
|
||||||
info.CallToken = out.Data.CallToken
|
info.CallToken = data.CallToken
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,14 +409,14 @@ func (c *Client) slbJoin(info callInfo, mustCreate bool) (JoinResult, error) {
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
var out struct {
|
var out struct {
|
||||||
Result JoinResult `json:"result"`
|
Result JoinResult `json:"result"`
|
||||||
Error string `json:"error"`
|
Error json.RawMessage `json:"error"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &out); err != nil {
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
return res, fmt.Errorf("slb join: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
return res, fmt.Errorf("slb join: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
if out.Result.MediaServerURL == "" || out.Result.RoomData == "" {
|
if out.Result.MediaServerURL == "" || out.Result.RoomData == "" {
|
||||||
return res, fmt.Errorf("slb join: missing roomData/mediaServerUrl (status %d, err %s)", status, out.Error)
|
return res, fmt.Errorf("slb join: missing roomData/mediaServerUrl (status %d, err %s)", status, string(out.Error))
|
||||||
}
|
}
|
||||||
return out.Result, nil
|
return out.Result, nil
|
||||||
}
|
}
|
||||||
@@ -441,27 +457,30 @@ func (c *Client) createRoom() (callInfo, error) {
|
|||||||
return info, err
|
return info, err
|
||||||
}
|
}
|
||||||
var out struct {
|
var out struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Data struct {
|
Data json.RawMessage `json:"data"`
|
||||||
ChatID json.Number `json:"chatId"`
|
|
||||||
Alias string `json:"alias"`
|
|
||||||
Link string `json:"link"`
|
|
||||||
} `json:"data"`
|
|
||||||
Errors []struct {
|
Errors []struct {
|
||||||
Code json.Number `json:"code"`
|
Code any `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
} `json:"errors"`
|
} `json:"errors"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &out); err != nil {
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
return info, fmt.Errorf("createRoom: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
return info, fmt.Errorf("createRoom: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
info.ChatID = out.Data.ChatID.String()
|
var data struct {
|
||||||
info.Alias = out.Data.Alias
|
ChatID json.Number `json:"chatId"`
|
||||||
info.GuestLink = out.Data.Link
|
Alias string `json:"alias"`
|
||||||
|
Link string `json:"link"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(out.Data, &data); err == nil {
|
||||||
|
info.ChatID = data.ChatID.String()
|
||||||
|
info.Alias = data.Alias
|
||||||
|
info.GuestLink = data.Link
|
||||||
|
}
|
||||||
if info.ChatID == "" {
|
if info.ChatID == "" {
|
||||||
errMsg := ""
|
errMsg := ""
|
||||||
if len(out.Errors) > 0 {
|
if len(out.Errors) > 0 {
|
||||||
errMsg = out.Errors[0].Code.String() + " " + out.Errors[0].Message
|
errMsg = fmt.Sprintf("%v %s", out.Errors[0].Code, out.Errors[0].Message)
|
||||||
}
|
}
|
||||||
return info, fmt.Errorf("createRoom: no chatId (status %d, status=%q err=%q, body %s)", status, out.Status, errMsg, common.BodySnippet(body))
|
return info, fmt.Errorf("createRoom: no chatId (status %d, status=%q err=%q, body %s)", status, out.Status, errMsg, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
@@ -488,15 +507,15 @@ func (c *Client) KickUser(userID string) error {
|
|||||||
var out struct {
|
var out struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Errors []struct {
|
Errors []struct {
|
||||||
Code json.Number `json:"code"`
|
Code any `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
} `json:"errors"`
|
} `json:"errors"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &out); err != nil {
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
return fmt.Errorf("kick: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
return fmt.Errorf("kick: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
if len(out.Errors) > 0 {
|
if len(out.Errors) > 0 {
|
||||||
return fmt.Errorf("kick: chatId=%s userId=%s rejected (status %d, %s %s)", c.chatID, userID, status, out.Errors[0].Code.String(), out.Errors[0].Message)
|
return fmt.Errorf("kick: chatId=%s userId=%s rejected (status %d, %v %s)", c.chatID, userID, status, out.Errors[0].Code, out.Errors[0].Message)
|
||||||
}
|
}
|
||||||
c.LogFn("[kick] removed userId=%s from chatId=%s", userID, c.chatID)
|
c.LogFn("[kick] removed userId=%s from chatId=%s", userID, c.chatID)
|
||||||
return nil
|
return nil
|
||||||
@@ -598,18 +617,19 @@ func (c *Client) getGuestLink(chatID string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
var out struct {
|
var out struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Data struct {
|
Data json.RawMessage `json:"data"`
|
||||||
GuestLink string `json:"guestLink"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &out); err != nil {
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
return "", fmt.Errorf("getGuestLink: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
return "", fmt.Errorf("getGuestLink: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
if out.Data.GuestLink == "" {
|
var data struct {
|
||||||
return "", fmt.Errorf("getGuestLink: empty (status %d)", status)
|
GuestLink string `json:"guestLink"`
|
||||||
}
|
}
|
||||||
return out.Data.GuestLink, nil
|
if err := json.Unmarshal(out.Data, &data); err != nil || data.GuestLink == "" {
|
||||||
|
return "", fmt.Errorf("getGuestLink: empty (status %d, body %s)", status, common.BodySnippet(body))
|
||||||
|
}
|
||||||
|
return data.GuestLink, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getCallToken(chatID string) (callToken, userToken string, err error) {
|
func (c *Client) getCallToken(chatID string) (callToken, userToken string, err error) {
|
||||||
@@ -622,17 +642,18 @@ func (c *Client) getCallToken(chatID string) (callToken, userToken string, err e
|
|||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
var out struct {
|
var out struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Data struct {
|
Data json.RawMessage `json:"data"`
|
||||||
CallToken string `json:"callToken"`
|
|
||||||
UserToken string `json:"userToken"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body, &out); err != nil {
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
return "", "", fmt.Errorf("getCallToken: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
return "", "", fmt.Errorf("getCallToken: %w (status %d, body %s)", err, status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
if out.Data.CallToken == "" {
|
var data struct {
|
||||||
|
CallToken string `json:"callToken"`
|
||||||
|
UserToken string `json:"userToken"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(out.Data, &data); err != nil || data.CallToken == "" {
|
||||||
return "", "", fmt.Errorf("getCallToken: empty (status %d, body %s)", status, common.BodySnippet(body))
|
return "", "", fmt.Errorf("getCallToken: empty (status %d, body %s)", status, common.BodySnippet(body))
|
||||||
}
|
}
|
||||||
return out.Data.CallToken, out.Data.UserToken, nil
|
return data.CallToken, data.UserToken, nil
|
||||||
}
|
}
|
||||||
@@ -243,13 +243,19 @@ func (c *Client) completeOAuth() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
target = c.portal + "/"
|
target = c.portal + "/"
|
||||||
body, _, err = c.getPage(target)
|
body, final, err = c.getPage(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if !strings.HasPrefix(final, c.portal) {
|
||||||
|
return fmt.Errorf("oauth handoff landed outside the portal, final=%s", final)
|
||||||
|
}
|
||||||
if m := reSessid.FindStringSubmatch(body); m != nil {
|
if m := reSessid.FindStringSubmatch(body); m != nil {
|
||||||
c.sessid = m[1]
|
c.sessid = m[1]
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
c.sessid = ""
|
||||||
|
c.LogFn("[auth] no csrf token on the portal page, it will be taken from the first response")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("oauth handoff did not converge")
|
return fmt.Errorf("oauth handoff did not converge")
|
||||||
|
|||||||
+37
-1
@@ -132,7 +132,18 @@ func (c *Client) cookieHosts() []string {
|
|||||||
|
|
||||||
func (c *Client) withRelogin(do func() ([]byte, int, error)) ([]byte, int, error) {
|
func (c *Client) withRelogin(do func() ([]byte, int, error)) ([]byte, int, error) {
|
||||||
body, status, err := do()
|
body, status, err := do()
|
||||||
if err != nil || !looksUnauth(body, status) {
|
if err != nil {
|
||||||
|
return body, status, err
|
||||||
|
}
|
||||||
|
if token := csrfTokenFromBody(body); token != "" && token != c.sessid {
|
||||||
|
c.LogFn("[auth] stale csrf token, retrying with the one from the response")
|
||||||
|
c.sessid = token
|
||||||
|
body, status, err = do()
|
||||||
|
if err != nil {
|
||||||
|
return body, status, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !looksUnauth(body, status) {
|
||||||
return body, status, err
|
return body, status, err
|
||||||
}
|
}
|
||||||
if c.email == "" || c.password == "" {
|
if c.email == "" || c.password == "" {
|
||||||
@@ -145,6 +156,31 @@ func (c *Client) withRelogin(do func() ([]byte, int, error)) ([]byte, int, error
|
|||||||
return do()
|
return do()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func csrfTokenFromBody(body []byte) string {
|
||||||
|
var out struct {
|
||||||
|
Errors []struct {
|
||||||
|
Code any `json:"code"`
|
||||||
|
CustomData json.RawMessage `json:"customData"`
|
||||||
|
} `json:"errors"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(body, &out); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, e := range out.Errors {
|
||||||
|
if e.Code != "invalid_csrf" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var customData struct {
|
||||||
|
CSRF string `json:"csrf"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(e.CustomData, &customData); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return customData.CSRF
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func looksUnauth(body []byte, status int) bool {
|
func looksUnauth(body []byte, status int) bool {
|
||||||
if status == 401 || status == 403 {
|
if status == 401 || status == 403 {
|
||||||
return true
|
return true
|
||||||
|
|||||||
Reference in new issue
Block a user