Skip to content

Commit

Permalink
Fix panic by checking slice bounds in handleEntities (telegram). Fixes
Browse files Browse the repository at this point in the history
…42wim#857 (42wim#858)

Besides the bound checking, this now also use utf16 as suggested by
go-telegram-bot-api/telegram-bot-api#231
  • Loading branch information
42wim authored and zeridon committed Feb 12, 2020
1 parent 0953f7e commit 35b6902
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bridge/telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ 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 {
Expand Down Expand Up @@ -375,8 +376,13 @@ 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)
}
}
}

0 comments on commit 35b6902

Please sign in to comment.