Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 17472fd8a900e0c0eefc0eee073a89b4e89ba110 Author: Wim <wim@42.be> Date: Mon Jul 08 22:19:45 2019 +0000 Fix panic by checking slice bounds in handleEntities (telegram). Fixes #857 (#858) Besides the bound checking, this now also use utf16 as suggested by https://github.com/go-telegram-bot-api/telegram-bot-api/issues/231 diff --git a/bridge/telegram/handlers.go b/bridge/telegram/handlers.go index 98b1020..bbe1243 100644 --- a/bridge/telegram/handlers.go +++ b/bridge/telegram/handlers.go @@ -510 +511 @@ import (   "regexp"   "strconv"   "strings" + "unicode/utf16"     "github.com/42wim/matterbridge/bridge/config"   "github.com/42wim/matterbridge/bridge/helper" - "github.com/go-telegram-bot-api/telegram-bot-api" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"  )    func (b *Btelegram) handleUpdate(rmsg *config.Message, message, posted, edited *tgbotapi.Message) *tgbotapi.Message { @@ -3758 +37613 @@ func (b *Btelegram) handleEntities(rmsg *config.Message, message *tgbotapi.Messa   b.Log.Errorf("entity text_link url parse failed: %s", err)   continue   } - link := rmsg.Text[e.Offset : e.Offset+e.Length] - rmsg.Text = strings.Replace(rmsg.Text, link, url.String(), 1) + utfEncodedString := utf16.Encode([]rune(rmsg.Text)) + if e.Offset+e.Length > len(utfEncodedString) { + b.Log.Errorf("entity length is too long %d > %d", e.Offset+e.Length, len(utfEncodedString)) + continue + } + link := utf16.Decode(utfEncodedString[e.Offset : e.Offset+e.Length]) + rmsg.Text = strings.Replace(rmsg.Text, string(link), url.String(), 1)   }   }  }