Skip to content

Commit

Permalink
fix: prevent panic on invalid package number (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jan 9, 2025
1 parent 00d1a33 commit 03a8a20
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 5 additions & 5 deletions internal/engine/command/crowdfund/crowdfund.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ func (c *CrowdfundCmd) GetCommand() *command.Command {
c.subCmdDisable.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator}

c.subCmdReport.AppIDs = entity.AllAppIDs()
c.subCmdReport.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet
c.subCmdReport.TargetFlag = command.TargetMaskMainnet

c.subCmdInfo.AppIDs = entity.AllAppIDs()
c.subCmdInfo.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet
c.subCmdInfo.TargetFlag = command.TargetMaskMainnet

c.subCmdPurchase.AppIDs = entity.AllAppIDs()
c.subCmdPurchase.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet
c.subCmdPurchase.TargetFlag = command.TargetMaskMainnet

c.subCmdClaim.AppIDs = entity.AllAppIDs()
c.subCmdClaim.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet
c.subCmdClaim.TargetFlag = command.TargetMaskMainnet

activeCampaign := c.activeCampaign()
if activeCampaign != nil {
purchaseChoices := []command.Choice{}
for index, pkg := range activeCampaign.Packages {
choice := command.Choice{
Name: pkg.Name,
Value: index,
Value: index + 1,
}

purchaseChoices = append(purchaseChoices, choice)
Expand Down
6 changes: 5 additions & 1 deletion internal/engine/command/crowdfund/purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func (c *CrowdfundCmd) purchaseHandler(
return cmd.RenderFailedTemplate("No active campaign")
}

pkgIndex, _ := strconv.Atoi(args[argNamePurchasePackage])
pkgNumber, _ := strconv.Atoi(args[argNamePurchasePackage])
pkgIndex := pkgNumber - 1
if pkgIndex == -1 || pkgIndex >= len(activeCampaign.Packages) {
return cmd.RenderFailedTemplateF("Invalid package number: %d", pkgNumber)
}
pkg := activeCampaign.Packages[pkgIndex]

purchase := &entity.CrowdfundPurchase{
Expand Down
10 changes: 10 additions & 0 deletions internal/engine/command/crowdfund/purchase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ func TestPurchase(t *testing.T) {
assert.True(t, result.Successful)
assert.Contains(t, result.Message, "payment-link")
})

t.Run("Invalid Package Number", func(t *testing.T) {
args := map[string]string{
"package": "0",
}
result := td.crowdfundCmd.purchaseHandler(caller, td.crowdfundCmd.subCmdPurchase, args)

assert.False(t, result.Successful)
assert.Contains(t, result.Message, "Invalid package number: 0")
})
}

0 comments on commit 03a8a20

Please sign in to comment.