Validate amounts in every GetAmount branch; cap faucet slots
The k and fiat branches returned the float conversion unchecked, so non-positive, NaN or Inf inputs reached callers that have no guard of their own (faucet total, ticket price, shop price, satdress amount). Every branch now rejects non-positive and non-finite amounts and anything above 10,000,000 sat. A faucet can have at most 1000 slots. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
0bcb821b82
commit
bb1037bcb7
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -45,6 +46,26 @@ func decodeAmountFromCommand(input string) (amount int64, err error) {
|
||||
return amount, err
|
||||
}
|
||||
|
||||
// maxAmountSat is the largest amount a single command may name. The bot is a
|
||||
// tip bot; anything above this is a typo or an attack on a conversion path.
|
||||
const maxAmountSat int64 = 10_000_000
|
||||
|
||||
// checkAmount rejects amounts that must never reach a money path: results of
|
||||
// a float conversion that are not a number, non-positive amounts and amounts
|
||||
// above maxAmountSat.
|
||||
func checkAmount(fmount float64, amount int64) (int64, error) {
|
||||
if math.IsNaN(fmount) || math.IsInf(fmount, 0) {
|
||||
return 0, fmt.Errorf("amount is not a number")
|
||||
}
|
||||
if amount <= 0 {
|
||||
return 0, fmt.Errorf("amount must be greater than 0")
|
||||
}
|
||||
if amount > maxAmountSat {
|
||||
return 0, fmt.Errorf("amount must not be greater than %d sat", maxAmountSat)
|
||||
}
|
||||
return amount, nil
|
||||
}
|
||||
|
||||
// GetAmount parses an amount from a string like 1.2k or 3.50€
|
||||
// and returns the value in satoshis
|
||||
func GetAmount(input string) (amount int64, err error) {
|
||||
@@ -62,8 +83,7 @@ func GetAmount(input string) (amount int64, err error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
amount = int64(fmount * 1000)
|
||||
return amount, err
|
||||
return checkAmount(fmount, int64(fmount*1000))
|
||||
}
|
||||
|
||||
// convert fiat currencies to satoshis
|
||||
@@ -82,8 +102,8 @@ func GetAmount(input string) (amount int64, err error) {
|
||||
if !(price.Price[currency] > 0) {
|
||||
return 0, fmt.Errorf("price is zero")
|
||||
}
|
||||
amount = int64(fmount / price.Price[currency] * float64(100_000_000))
|
||||
return amount, nil
|
||||
sats := fmount / price.Price[currency] * float64(100_000_000)
|
||||
return checkAmount(sats, int64(sats))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,10 +112,7 @@ func GetAmount(input string) (amount int64, err error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if amount <= 0 {
|
||||
return 0, fmt.Errorf("amount must be greater than 0")
|
||||
}
|
||||
return amount, err
|
||||
return checkAmount(float64(amount), amount)
|
||||
}
|
||||
|
||||
func SatoshisToFiat(amount int64, currency string) (fiat float64, err error) {
|
||||
|
||||
@@ -53,6 +53,10 @@ func (bot TipBot) mapFaucetLanguage(ctx context.Context, command string) context
|
||||
return ctx
|
||||
}
|
||||
|
||||
// maxFaucetSlots bounds how many users one faucet can pay out to. Every slot
|
||||
// is a message edit and an LNbits transfer.
|
||||
const maxFaucetSlots = 1000
|
||||
|
||||
func (bot TipBot) createFaucet(ctx context.Context, text string, sender *tb.User) (*InlineFaucet, error) {
|
||||
amount, err := decodeAmountFromCommand(text)
|
||||
if err != nil {
|
||||
@@ -70,6 +74,9 @@ func (bot TipBot) createFaucet(ctx context.Context, text string, sender *tb.User
|
||||
return nil, errors.New(errors.InvalidAmountPerUserError, fmt.Errorf("invalid amount per user"))
|
||||
}
|
||||
nTotal := int(amount / perUserAmount)
|
||||
if nTotal > maxFaucetSlots {
|
||||
return nil, errors.New(errors.InvalidAmountPerUserError, fmt.Errorf("faucet has too many slots (%d > %d)", nTotal, maxFaucetSlots))
|
||||
}
|
||||
fromUser := LoadUser(ctx)
|
||||
fromUserStr := GetUserStr(sender)
|
||||
balance, err := bot.GetUserBalanceCached(fromUser)
|
||||
|
||||
Reference in New Issue
Block a user