From 174aa0730f7fef1cb3dced5fa033c539208ac870 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Wed, 13 Dec 2017 19:07:57 +0500 Subject: [PATCH] :recycle: Use fasthttp.Args more safe --- delete_chat_photo.go | 5 +++-- delete_chat_sticker_set.go | 5 +++-- delete_message.go | 5 +++-- delete_sticker_from_set.go | 5 +++-- export_chat_invite_link.go | 5 +++-- get_chat.go | 5 +++-- get_chat_administrators.go | 5 +++-- get_chat_member.go | 5 +++-- get_chat_members_count.go | 5 +++-- get_file.go | 5 +++-- get_sticker_set.go | 5 +++-- kick_chat_member.go | 5 +++-- leave_chat.go | 5 +++-- send_chat_action.go | 5 +++-- send_sticker.go | 5 +++-- set_chat_photo.go | 5 +++-- set_chat_sticker_set.go | 5 +++-- set_chat_title.go | 5 +++-- set_sticker_position_in_set.go | 5 +++-- set_webhook.go | 7 ++++--- unban_chat_member.go | 5 +++-- unpin_chat_message.go | 5 +++-- 22 files changed, 67 insertions(+), 45 deletions(-) diff --git a/delete_chat_photo.go b/delete_chat_photo.go index 18fbb96..4233350 100644 --- a/delete_chat_photo.go +++ b/delete_chat_photo.go @@ -14,10 +14,11 @@ import ( // Note: In regular groups (non-supergroups), this method will only work if the // 'All Members Are Admins' setting is off in the target group. func (bot *Bot) DeleteChatPhoto(chatID int64) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "deleteChatPhoto", &args) + resp, err := bot.request(nil, "deleteChatPhoto", args) if err != nil { return false, err } diff --git a/delete_chat_sticker_set.go b/delete_chat_sticker_set.go index 7b32bcb..1194cb7 100644 --- a/delete_chat_sticker_set.go +++ b/delete_chat_sticker_set.go @@ -12,10 +12,11 @@ import ( // can_set_sticker_set optionally returned in getChat requests to check if the bot can use this // method. Returns True on success. func (bot *Bot) DeleteChatStickerSet(chatID int64) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "deleteChatStickerSet", &args) + resp, err := bot.request(nil, "deleteChatStickerSet", args) if err != nil { return false, err } diff --git a/delete_message.go b/delete_message.go index d41042e..9a47e59 100644 --- a/delete_message.go +++ b/delete_message.go @@ -15,11 +15,12 @@ import ( // bot has can_delete_messages permission in a supergroup or a channel, it can // delete any message there. Returns True on success. func (bot *Bot) DeleteMessage(chatID int64, message int) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("message_id", strconv.Itoa(message)) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "deleteMessage", &args) + resp, err := bot.request(nil, "deleteMessage", args) if err != nil { return false, err } diff --git a/delete_sticker_from_set.go b/delete_sticker_from_set.go index 2fff0fa..6823d66 100644 --- a/delete_sticker_from_set.go +++ b/delete_sticker_from_set.go @@ -8,10 +8,11 @@ import ( // DeleteStickerFromSet delete a sticker from a set created by the bot. Returns // True on success. func (bot *Bot) DeleteStickerFromSet(sticker string) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("sticker", sticker) - resp, err := bot.request(nil, "deleteStickerFromSet", &args) + resp, err := bot.request(nil, "deleteStickerFromSet", args) if err != nil { return false, err } diff --git a/export_chat_invite_link.go b/export_chat_invite_link.go index 133f669..5f188de 100644 --- a/export_chat_invite_link.go +++ b/export_chat_invite_link.go @@ -11,10 +11,11 @@ import ( // bot must be an administrator in the chat for this to work and must have the // appropriate admin rights. Returns exported invite link as String on success. func (bot *Bot) ExportChatInviteLink(chatID int64) (string, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "exportChatInviteLink", &args) + resp, err := bot.request(nil, "exportChatInviteLink", args) if err != nil { return "", err } diff --git a/get_chat.go b/get_chat.go index 8e61c91..0cbbc1a 100644 --- a/get_chat.go +++ b/get_chat.go @@ -11,10 +11,11 @@ import ( // for one-on-one conversations, current username of a user, group or channel, // etc.). Returns a Chat object on success. func (bot *Bot) GetChat(chatID int64) (*Chat, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "getChat", &args) + resp, err := bot.request(nil, "getChat", args) if err != nil { return nil, err } diff --git a/get_chat_administrators.go b/get_chat_administrators.go index 969dd5a..6777d2b 100644 --- a/get_chat_administrators.go +++ b/get_chat_administrators.go @@ -12,10 +12,11 @@ import ( // chat administrators except other bots. If the chat is a group or a supergroup // and no administrators were appointed, only the creator will be returned. func (bot *Bot) GetChatAdministrators(chatID int64) (*[]ChatMember, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "getChatAdministrators", &args) + resp, err := bot.request(nil, "getChatAdministrators", args) if err != nil { return nil, err } diff --git a/get_chat_member.go b/get_chat_member.go index 1069b32..b62a0d1 100644 --- a/get_chat_member.go +++ b/get_chat_member.go @@ -10,11 +10,12 @@ import ( // GetChatMember get information about a member of a chat. Returns a ChatMember // object on success. func (bot *Bot) GetChatMember(chatID int64, user int) (*ChatMember, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("user_id", strconv.Itoa(user)) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "getChatMember", &args) + resp, err := bot.request(nil, "getChatMember", args) if err != nil { return nil, err } diff --git a/get_chat_members_count.go b/get_chat_members_count.go index bace678..053dfc0 100644 --- a/get_chat_members_count.go +++ b/get_chat_members_count.go @@ -10,10 +10,11 @@ import ( // GetChatMembersCount get the number of members in a chat. Returns Int on // success. func (bot *Bot) GetChatMembersCount(chatID int64) (int, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "getChatMembersCount", &args) + resp, err := bot.request(nil, "getChatMembersCount", args) if err != nil { return 0, err } diff --git a/get_file.go b/get_file.go index 457699f..ebbddfa 100644 --- a/get_file.go +++ b/get_file.go @@ -17,10 +17,11 @@ import ( // should save the file's MIME type and name (if available) when the File object // is received. func (bot *Bot) GetFile(file string) (*File, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("file_id", file) - resp, err := bot.request(nil, "getFile", &args) + resp, err := bot.request(nil, "getFile", args) if err != nil { return nil, err } diff --git a/get_sticker_set.go b/get_sticker_set.go index 88b805f..1992124 100644 --- a/get_sticker_set.go +++ b/get_sticker_set.go @@ -7,10 +7,11 @@ import ( // GetStickerSet get a sticker set. On success, a StickerSet object is returned. func (bot *Bot) GetStickerSet(name string) (*StickerSet, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("name", name) - resp, err := bot.request(nil, "getStickerSet", &args) + resp, err := bot.request(nil, "getStickerSet", args) if err != nil { return nil, err } diff --git a/kick_chat_member.go b/kick_chat_member.go index 5917c15..81ba097 100644 --- a/kick_chat_member.go +++ b/kick_chat_member.go @@ -16,12 +16,13 @@ import ( // Admins' setting is off in the target group. Otherwise members may only be removed by the group's // creator or by the member that added them. func (bot *Bot) KickChatMember(chatID int64, userID int, untilDate int64) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) args.Add("user_id", strconv.Itoa(userID)) args.Add("until_date", strconv.FormatInt(untilDate, 10)) - resp, err := bot.request(nil, "kickChatMember", &args) + resp, err := bot.request(nil, "kickChatMember", args) if err != nil { return false, err } diff --git a/leave_chat.go b/leave_chat.go index 4be3578..61d59c8 100644 --- a/leave_chat.go +++ b/leave_chat.go @@ -9,10 +9,11 @@ import ( // LeaveChat leave a group, supergroup or channel. Returns True on success. func (bot *Bot) LeaveChat(chat int64) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chat, 10)) - resp, err := bot.request(nil, "leaveChat", &args) + resp, err := bot.request(nil, "leaveChat", args) if err != nil { return false, err } diff --git a/send_chat_action.go b/send_chat_action.go index cbfa525..bc6422d 100644 --- a/send_chat_action.go +++ b/send_chat_action.go @@ -14,11 +14,12 @@ import ( // We only recommend using this method when a response from the bot will take a // noticeable amount of time to arrive. func (bot *Bot) SendChatAction(chatID int64, action string) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("action", action) // Type of action to broadcast args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "sendChatAction", &args) + resp, err := bot.request(nil, "sendChatAction", args) if err != nil { return false, err } diff --git a/send_sticker.go b/send_sticker.go index 09f99ca..c706c67 100644 --- a/send_sticker.go +++ b/send_sticker.go @@ -51,7 +51,8 @@ func NewSticker(chatID int64, sticker interface{}) *SendStickerParameters { // SendSticker send .webp stickers. On success, the sent Message is returned. func (bot *Bot) SendSticker(params *SendStickerParameters) (*Message, error) { - var args http.Args + args := http.AcquireArgs() +defer http.ReleaseArgs(args) args.Add("chat_id", params.ChatID) args.Add("disable_notification", strconv.FormatBool(params.DisableNotification)) @@ -86,7 +87,7 @@ func (bot *Bot) SendSticker(params *SendStickerParameters) (*Message, error) { return false, errors.New("use string only (for current version of go-telegram)") } - resp, err := bot.upload(buffer.Bytes(), multi.Boundary(), "setWebhook", &args) + resp, err := bot.upload(buffer.Bytes(), multi.Boundary(), "setWebhook", args) if err != nil { return false, err } diff --git a/set_chat_photo.go b/set_chat_photo.go index 94941c0..6185a49 100644 --- a/set_chat_photo.go +++ b/set_chat_photo.go @@ -14,10 +14,11 @@ import ( // Note: In regular groups (non-supergroups), this method will only work if the 'All Members Are // Admins' setting is off in the target group. func (bot *Bot) SetChatPhoto(chatID int64, photo InputFile) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.upload(photo, TypePhoto, "chat_photo", "setChatPhoto", &args) + resp, err := bot.upload(photo, TypePhoto, "chat_photo", "setChatPhoto", args) if err != nil { return false, err } diff --git a/set_chat_sticker_set.go b/set_chat_sticker_set.go index 8c8413e..6f18318 100644 --- a/set_chat_sticker_set.go +++ b/set_chat_sticker_set.go @@ -12,11 +12,12 @@ import ( // can_set_sticker_set optionally returned in getChat requests to check if the bot can use this // method. Returns True on success. func (bot *Bot) SetChatStickerSet(chatID int64, stickerSetName string) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) args.Add("sticker_set_name", stickerSetName) - resp, err := bot.request(nil, "setChatStickerSet", &args) + resp, err := bot.request(nil, "setChatStickerSet", args) if err != nil { return false, err } diff --git a/set_chat_title.go b/set_chat_title.go index 86e676e..a29596b 100644 --- a/set_chat_title.go +++ b/set_chat_title.go @@ -14,11 +14,12 @@ import ( // Note: In regular groups (non-supergroups), this method will only work if the // 'All Members Are Admins' setting is off in the target group. func (bot *Bot) SetChatTitle(chatID int64, title string) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("title", title) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "setChatTitle", &args) + resp, err := bot.request(nil, "setChatTitle", args) if err != nil { return false, err } diff --git a/set_sticker_position_in_set.go b/set_sticker_position_in_set.go index c8778d7..1486100 100644 --- a/set_sticker_position_in_set.go +++ b/set_sticker_position_in_set.go @@ -10,11 +10,12 @@ import ( // SetStickerPositionInSet move a sticker in a set created by the bot to a // specific position. Returns True on success. func (bot *Bot) SetStickerPositionInSet(sticker string, position int) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("sticker", sticker) args.Add("position", strconv.Itoa(position)) - resp, err := bot.request(nil, "setStickerPositionInSet", &args) + resp, err := bot.request(nil, "setStickerPositionInSet", args) if err != nil { return false, err } diff --git a/set_webhook.go b/set_webhook.go index c577f4c..32e3d56 100644 --- a/set_webhook.go +++ b/set_webhook.go @@ -55,7 +55,8 @@ func NewWebhook(url string, file interface{}) *SetWebhookParameters { // recommend using a secret path in the URL, e.g. https://www.example.com/. // Since nobody else knows your bot‘s token, you can be pretty sure it’s us. func (bot *Bot) SetWebhook(params *SetWebhookParameters) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("url", params.URL) if len(params.AllowedUpdates) > 0 { @@ -70,10 +71,10 @@ func (bot *Bot) SetWebhook(params *SetWebhookParameters) (bool, error) { resp := &Response{} if params.Certificate != nil { resp, err = bot.upload( - params.Certificate, "certificate", "cert.pem", setWebhook, &args, + params.Certificate, "certificate", "cert.pem", setWebhook, args, ) } else { - resp, err = bot.request(nil, setWebhook, &args) + resp, err = bot.request(nil, setWebhook, args) } if err != nil { return false, err diff --git a/unban_chat_member.go b/unban_chat_member.go index 8829c06..ed768de 100644 --- a/unban_chat_member.go +++ b/unban_chat_member.go @@ -12,11 +12,12 @@ import ( // to join via link, etc. The bot must be an administrator for this to work. // Returns True on success. func (bot *Bot) UnbanChatMember(chatID int64, user int) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("user_id", strconv.Itoa(user)) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "unbanChatMember", &args) + resp, err := bot.request(nil, "unbanChatMember", args) if err != nil { return false, err } diff --git a/unpin_chat_message.go b/unpin_chat_message.go index d5355ac..36569cc 100644 --- a/unpin_chat_message.go +++ b/unpin_chat_message.go @@ -11,10 +11,11 @@ import ( // administrator in the chat for this to work and must have the appropriate admin // rights. Returns True on success. func (bot *Bot) UnpinChatMessage(chatID int64) (bool, error) { - var args http.Args + args := http.AcquireArgs() + defer http.ReleaseArgs(args) args.Add("chat_id", strconv.FormatInt(chatID, 10)) - resp, err := bot.request(nil, "unpinChatMessage", &args) + resp, err := bot.request(nil, "unpinChatMessage", args) if err != nil { return false, err }