Skip to content

Commit

Permalink
fix: unescape query id (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
gazenw authored Oct 6, 2024
1 parent 107836a commit e1f8eaa
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
15 changes: 12 additions & 3 deletions modules/runes/api/httphandler/get_balances_by_address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package httphandler

import (
"net/url"

"github.com/cockroachdb/errors"
"github.com/gaze-network/indexer-network/common/errs"
"github.com/gaze-network/indexer-network/modules/runes/internal/entity"
Expand All @@ -22,13 +24,20 @@ const (
getBalancesDefaultLimit = 100
)

func (r getBalancesRequest) Validate() error {
func (r *getBalancesRequest) Validate() error {
var errList []error
if r.Wallet == "" {
errList = append(errList, errors.New("'wallet' is required"))
}
if r.Id != "" && !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.New("'id' is not valid rune id or rune name"))
if r.Id != "" {
id, err := url.QueryUnescape(r.Id)
if err != nil {
return errors.WithStack(err)
}
r.Id = id
if !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.Errorf("id '%s' is not valid rune id or rune name", r.Id))
}
}
if r.Limit < 0 {
errList = append(errList, errors.New("'limit' must be non-negative"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r getBalancesBatchRequest) Validate() error {
errList = append(errList, errors.Errorf("queries[%d]: 'wallet' is required", i))
}
if query.Id != "" && !isRuneIdOrRuneName(query.Id) {
errList = append(errList, errors.Errorf("queries[%d]: 'id' is not valid rune id or rune name", i))
errList = append(errList, errors.Errorf("queries[%d]: id '%s' is not valid rune id or rune name", i, query.Id))
}
if query.Limit < 0 {
errList = append(errList, errors.Errorf("queries[%d]: 'limit' must be non-negative", i))
Expand Down
10 changes: 8 additions & 2 deletions modules/runes/api/httphandler/get_holders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httphandler
import (
"bytes"
"encoding/hex"
"net/url"
"slices"

"github.com/cockroachdb/errors"
Expand All @@ -24,10 +25,15 @@ const (
getHoldersMaxLimit = 1000
)

func (r getHoldersRequest) Validate() error {
func (r *getHoldersRequest) Validate() error {
var errList []error
id, err := url.QueryUnescape(r.Id)
if err != nil {
return errors.WithStack(err)
}
r.Id = id
if !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.New("'id' is not valid rune id or rune name"))
errList = append(errList, errors.Errorf("id '%s' is not valid rune id or rune name", r.Id))
}
if r.Limit < 0 {
errList = append(errList, errors.New("'limit' must be non-negative"))
Expand Down
10 changes: 8 additions & 2 deletions modules/runes/api/httphandler/get_token_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httphandler

import (
"net/url"
"slices"

"github.com/cockroachdb/errors"
Expand All @@ -17,10 +18,15 @@ type getTokenInfoRequest struct {
BlockHeight uint64 `query:"blockHeight"`
}

func (r getTokenInfoRequest) Validate() error {
func (r *getTokenInfoRequest) Validate() error {
var errList []error
id, err := url.QueryUnescape(r.Id)
if err != nil {
return errors.WithStack(err)
}
r.Id = id
if !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.New("'id' is not valid rune id or rune name"))
errList = append(errList, errors.Errorf("id '%s' is not valid rune id or rune name", r.Id))
}
return errs.WithPublicMessage(errors.Join(errList...), "validation error")
}
Expand Down
14 changes: 11 additions & 3 deletions modules/runes/api/httphandler/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cmp"
"encoding/hex"
"fmt"
"net/url"
"slices"

"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand All @@ -27,10 +28,17 @@ const (
getTransactionsMaxLimit = 3000
)

func (r getTransactionsRequest) Validate() error {
func (r *getTransactionsRequest) Validate() error {
var errList []error
if r.Id != "" && !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.New("'id' is not valid rune id or rune name"))
if r.Id != "" {
id, err := url.QueryUnescape(r.Id)
if err != nil {
return errors.WithStack(err)
}
r.Id = id
if !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.Errorf("id '%s' is not valid rune id or rune name", r.Id))
}
}
if r.FromBlock < -1 {
errList = append(errList, errors.Errorf("invalid fromBlock range"))
Expand Down
15 changes: 12 additions & 3 deletions modules/runes/api/httphandler/get_utxos_by_address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package httphandler

import (
"net/url"

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/cockroachdb/errors"
"github.com/gaze-network/indexer-network/common/errs"
Expand All @@ -22,13 +24,20 @@ const (
getUTXOsMaxLimit = 3000
)

func (r getUTXOsRequest) Validate() error {
func (r *getUTXOsRequest) Validate() error {
var errList []error
if r.Wallet == "" {
errList = append(errList, errors.New("'wallet' is required"))
}
if r.Id != "" && !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.New("'id' is not valid rune id or rune name"))
if r.Id != "" {
id, err := url.QueryUnescape(r.Id)
if err != nil {
return errors.WithStack(err)
}
r.Id = id
if !isRuneIdOrRuneName(r.Id) {
errList = append(errList, errors.Errorf("id '%s' is not valid rune id or rune name", r.Id))
}
}
if r.Limit < 0 {
errList = append(errList, errors.New("'limit' must be non-negative"))
Expand Down

0 comments on commit e1f8eaa

Please sign in to comment.