Skip to content

Commit

Permalink
fix(senders): remove telegram token from logs (hotfix) (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf authored Jul 27, 2023
1 parent 25982aa commit 6f19376
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func ReplaceSubstring(str, beg, end, rep string) string {
result := str
startIndex := strings.Index(str, beg)
if startIndex != -1 {
startIndex += len(beg)
endIndex := strings.Index(str[startIndex:], end)
if endIndex != -1 {
endIndex += len(str[:startIndex])
Expand Down
10 changes: 5 additions & 5 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,23 @@ func testRounding(baseTimestamp, retention int64) {
func TestReplaceSubstring(t *testing.T) {
Convey("test replace substring", t, func() {
Convey("replacement string in the middle", func() {
So(ReplaceSubstring("https://api.telegram.org/botXXX/getMe", "bot", "/", "[DELETED]"), ShouldResemble, "https://api.telegram.org/[DELETED]/getMe")
So(ReplaceSubstring("telebot: Post https://api.telegram.org/botXXX/getMe", "/bot", "/", "[DELETED]"), ShouldResemble, "telebot: Post https://api.telegram.org/bot[DELETED]/getMe")
})

Convey("replacement string at the beginning", func() {
So(ReplaceSubstring("botXXX/getMe", "bot", "/", "[DELETED]"), ShouldResemble, "[DELETED]/getMe")
So(ReplaceSubstring("/botXXX/getMe", "/bot", "/", "[DELETED]"), ShouldResemble, "/bot[DELETED]/getMe")
})

Convey("replacement string at the end", func() {
So(ReplaceSubstring("https://api.telegram.org/botXXX/", "bot", "/", "[DELETED]"), ShouldResemble, "https://api.telegram.org/[DELETED]/")
So(ReplaceSubstring("telebot: Post https://api.telegram.org/botXXX/", "/bot", "/", "[DELETED]"), ShouldResemble, "telebot: Post https://api.telegram.org/bot[DELETED]/")
})

Convey("no replacement string", func() {
So(ReplaceSubstring("https://api.telegram.org/getMe", "bot", "/", "[DELETED]"), ShouldResemble, "https://api.telegram.org/getMe")
So(ReplaceSubstring("telebot: Post https://api.telegram.org/getMe", "/bot", "/", "[DELETED]"), ShouldResemble, "telebot: Post https://api.telegram.org/getMe")
})

Convey("there is the beginning of replacement string, but no end", func() {
So(ReplaceSubstring("https://api.telegram.org/botXXX error", "bot", "/", "[DELETED]"), ShouldResemble, "https://api.telegram.org/botXXX error")
So(ReplaceSubstring("https://api.telegram.org/botXXX error", "/bot", "/", "[DELETED]"), ShouldResemble, "https://api.telegram.org/botXXX error")
})
})
}
2 changes: 1 addition & 1 deletion senders/telegram/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func removeTokenFromError(err error, bot *telebot.Bot) error {
url = bot.URL
}
if strings.Contains(err.Error(), url) {
return errors.New(moira.ReplaceSubstring(err.Error(), "bot", "/", hidden))
return errors.New(moira.ReplaceSubstring(err.Error(), "/bot", "/", hidden))
}
return err
}
Expand Down

0 comments on commit 6f19376

Please sign in to comment.