Skip to content

tgbotapi.NewWebhook ignores error on invalid url #311

@kortov

Description

@kortov

Here's an error is ignored

telegram-bot-api/helpers.go

Lines 394 to 400 in aa124ef

func NewWebhook(link string) WebhookConfig {
u, _ := url.Parse(link)
return WebhookConfig{
URL: u,
}
}

so when you pass an url like "https://example.com" + bot.Token here (and I think it's easy to do such mistake)

wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)

the test (and bot of course) fails with this

=== RUN   TestSetWebhookWithoutCert
2020/02/29 22:51:04 setWebhook resp: {"ok":true,"result":true,"description":"Webhook is already deleted"}
--- FAIL: TestSetWebhookWithoutCert (2.83s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x5d6e4a]

goroutine 6 [running]:
testing.tRunner.func1(0xc0000e4100)
	/snap/go/5364/src/testing/testing.go:874 +0x3a3
panic(0x71b060, 0x9fd7f0)
	/snap/go/5364/src/runtime/panic.go:679 +0x1b2
net/url.(*URL).String(0x0, 0xc000001980, 0x300000002)
	/snap/go/5364/src/net/url/url.go:775 +0x4a
github.com/go-telegram-bot-api/telegram-bot-api.(*BotAPI).SetWebhook(0xc0000ea000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/home/user/GolandProjects/telegram-bot-api/bot.go:454 +0x3c8
github.com/go-telegram-bot-api/telegram-bot-api_test.TestSetWebhookWithoutCert(0xc0000e4100)
	/home/user/GolandProjects/telegram-bot-api/bot_test.go:491 +0x15d
testing.tRunner(0xc0000e4100, 0x799998)
	/snap/go/5364/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
	/snap/go/5364/src/testing/testing.go:960 +0x350

Process finished with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions