github-vet / github-vet/rangeloop-pointer-findings

TicketsBot/worker: bot/listeners/messagequeue/ticketclose.go; 84 LoC

Open
#10,937 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [TicketsBot/worker](https://www.github.com/TicketsBot/worker) at [bot/listeners/messagequeue/ticketclose.go](https://github.com/TicketsBot/worker/blob/d32cf6ccef3a3e3ab13ca509091738ae7f3ee3f4/bot/listeners/messagequeue/ticketclose.go#L24-L107)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable payload used in defer or goroutine at line 26

[Click here to see the code in its original context.](https://github.com/TicketsBot/worker/blob/d32cf6ccef3a3e3ab13ca509091738ae7f3ee3f4/bot/listeners/messagequeue/ticketclose.go#L24-L107)

Click here to show the 84 line(s) of Go which triggered the analyzer.

```go
for payload := range ch {
go func() {
if payload.Reason == "" {
payload.Reason = "No reason specified"
}
// Get the ticket struct
ticket, err := dbclient.Client.Tickets.Get(payload.TicketId, payload.GuildId)
if err != nil {
sentry.Error(err)
return
}

// Check that this is a valid ticket
if ticket.GuildId == 0 {
return
}

// Create error context for later
errorContext := errorcontext.WorkerErrorContext{
Guild: ticket.GuildId,
User: payload.UserId,
}

// Get bot token for guild
var token string
var botId uint64
{
whiteLabelBotId, isWhitelabel, err := dbclient.Client.WhitelabelGuilds.GetBotByGuild(payload.GuildId)
if err != nil {
sentry.ErrorWithContext(err, errorContext)
}

if isWhitelabel {
bot, err := dbclient.Client.Whitelabel.GetByBotId(whiteLabelBotId); if err != nil {
sentry.ErrorWithContext(err, errorContext)
return
}

if bot.Token == "" {
token = os.Getenv("WORKER_PUBLIC_TOKEN")
} else {
token = bot.Token
botId = whiteLabelBotId
}
} else {
token = os.Getenv("WORKER_PUBLIC_TOKEN")
}
}

// Create ratelimiter
var keyPrefix string
if botId != 0 { // If is whitelabel
keyPrefix = fmt.Sprintf("ratelimiter:%d", botId)
} else {
keyPrefix = "ratelimiter:public"
}

// TODO: Handle large sharding buckets - envvar?
rateLimiter := ratelimit.NewRateLimiter(ratelimit.NewRedisStore(redis.Client, keyPrefix), 1)

// Get whether the guild is premium for log archiver
isPremium := utils.PremiumClient.GetTierByGuildId(payload.GuildId, true, token, rateLimiter) > premium.None

// Create worker context
ctx := &worker.Context{
Token: token,
IsWhitelabel: botId != 0,
Cache: cache.Client, // TODO: Less hacky
RateLimiter: rateLimiter,
}

// Get the member object
member, err := ctx.GetGuildMember(ticket.GuildId, payload.UserId)
if err != nil {
sentry.LogWithContext(err, errorContext)
return
}

if ticket.ChannelId == nil {
return
}
logic.CloseTicket(ctx, ticket.GuildId, *ticket.ChannelId, 0, member, &payload.Reason, false, isPremium)
}()
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: d32cf6ccef3a3e3ab13ca509091738ae7f3ee3f4

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in bot/listeners/messagequeue/ticketclose.go at the range loop and goroutine around lines 24-107. Read how payload is used inside the goroutine, then verify that each queued message is handled with its own payload and that ticket closing still completes correctly after the change.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.