Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Improve bounds checking in rest converters #6567

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions engine/access/rest/routes/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ func TestCreateTransaction(t *testing.T) {
{"reference_block_id", "-1", `{"code":400, "message":"invalid reference block ID: invalid ID format"}`},
{"reference_block_id", "", `{"code":400, "message":"reference block not provided"}`},
{"gas_limit", "-1", `{"code":400, "message":"invalid gas limit: value must be an unsigned 64 bit integer"}`},
{"gas_limit", "18446744073709551616", `{"code":400, "message":"invalid gas limit: value overflows uint64 range"}`},
{"payer", "yo", `{"code":400, "message":"invalid payer: invalid address"}`},
{"proposal_key", "yo", `{"code":400, "message":"request body contains an invalid value for the \"proposal_key\" field (at position 461)"}`},
{"authorizers", "", `{"code":400, "message":"request body contains an invalid value for the \"authorizers\" field (at position 32)"}`},
Expand Down
7 changes: 7 additions & 0 deletions engine/access/rest/util/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"encoding/base64"
"errors"
"fmt"
"strconv"
)
Expand All @@ -15,6 +16,9 @@ func FromUint[U uint | uint64 | uint32](number U) string {
func ToUint64(uint64Str string) (uint64, error) {
val, err := strconv.ParseUint(uint64Str, 10, 64)
if err != nil {
if errors.Is(err, strconv.ErrRange) {
return 0, fmt.Errorf("value overflows uint64 range")
}
return 0, fmt.Errorf("value must be an unsigned 64 bit integer") // hide error from user
}
return val, nil
Expand All @@ -24,6 +28,9 @@ func ToUint64(uint64Str string) (uint64, error) {
func ToUint32(uint32Str string) (uint32, error) {
val, err := strconv.ParseUint(uint32Str, 10, 32)
if err != nil {
if errors.Is(err, strconv.ErrRange) {
return 0, fmt.Errorf("value overflows uint32 range")
}
return 0, fmt.Errorf("value must be an unsigned 32 bit integer") // hide error from user
}
return uint32(val), nil
Expand Down
Loading