Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

validate the address when APIService.GetAccount() is called. #13

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion rpc/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package rpc

import (
"encoding/hex"
"strings"

"github.com/medibloc/go-medibloc/consensus/dpos"

Expand Down Expand Up @@ -49,14 +50,18 @@ func newAPIService(bm *core.BlockManager, tm *core.TransactionManager, ee *core.
}
}

func isValidAccount(account string) bool {
return len(account) == 66 && (strings.HasPrefix(account, "02") || strings.HasPrefix(account, "03"))
}

// GetAccount handles GetAccount rpc.
func (s *APIService) GetAccount(ctx context.Context, req *rpcpb.GetAccountRequest) (*rpcpb.Account,
error) {
var block *core.Block
var err error

// XOR
if !(math.XOR(len(req.Address) != 66, req.Alias == "") && math.XOR(req.Type == "", req.Height == 0)) {
if !(math.XOR(isValidAccount(req.Address), req.Alias != "") && math.XOR(req.Type != "", req.Height != 0)) {
return nil, status.Error(codes.InvalidArgument, ErrMsgInvalidRequest)
}

Expand Down
12 changes: 12 additions & 0 deletions rpc/api_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func TestAPIService_GetAccount(t *testing.T) {

e := httpexpect.New(t, testutil.IP2Local(seed.Config.Config.Rpc.HttpListen[0]))

e.GET("/v1/account").
WithQuery("type", "tail").
WithQuery("address", "0000").
Expect().
Status(http.StatusBadRequest)

e.GET("/v1/account").
WithQuery("type", "tail").
WithQuery("address", "000000000000000000000000000000000000000000000000000000000000000000").
Expect().
Status(http.StatusBadRequest)

e.GET("/v1/account").
WithQuery("type", "tail").
WithQuery("address", payer.Addr).
Expand Down