Skip to content

Commit fa41f42

Browse files
authored
Check hash lengths before storing tspend/treasury policies.
1 parent 2ce3cd7 commit fa41f42

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

internal/rpc/jsonrpc/methods.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,6 +3820,11 @@ func (s *Server) setTreasuryPolicy(ctx context.Context, icmd interface{}) (inter
38203820

38213821
var ticketHash *chainhash.Hash
38223822
if cmd.Ticket != nil && *cmd.Ticket != "" {
3823+
if len(*cmd.Ticket) != chainhash.MaxHashStringSize {
3824+
err := fmt.Errorf("invalid ticket hash length, expected %d got %d",
3825+
chainhash.MaxHashStringSize, len(*cmd.Ticket))
3826+
return nil, rpcError(dcrjson.ErrRPCDecodeHexString, err)
3827+
}
38233828
var err error
38243829
ticketHash, err = chainhash.NewHashFromStr(*cmd.Ticket)
38253830
if err != nil {
@@ -3931,13 +3936,24 @@ func (s *Server) setTSpendPolicy(ctx context.Context, icmd interface{}) (interfa
39313936
return nil, errUnloadedWallet
39323937
}
39333938

3939+
if len(cmd.Hash) != chainhash.MaxHashStringSize {
3940+
err := fmt.Errorf("invalid tspend hash length, expected %d got %d",
3941+
chainhash.MaxHashStringSize, len(cmd.Hash))
3942+
return nil, rpcError(dcrjson.ErrRPCDecodeHexString, err)
3943+
}
3944+
39343945
hash, err := chainhash.NewHashFromStr(cmd.Hash)
39353946
if err != nil {
39363947
return nil, rpcError(dcrjson.ErrRPCDecodeHexString, err)
39373948
}
39383949

39393950
var ticketHash *chainhash.Hash
39403951
if cmd.Ticket != nil && *cmd.Ticket != "" {
3952+
if len(*cmd.Ticket) != chainhash.MaxHashStringSize {
3953+
err := fmt.Errorf("invalid ticket hash length, expected %d got %d",
3954+
chainhash.MaxHashStringSize, len(*cmd.Ticket))
3955+
return nil, rpcError(dcrjson.ErrRPCDecodeHexString, err)
3956+
}
39413957
var err error
39423958
ticketHash, err = chainhash.NewHashFromStr(*cmd.Ticket)
39433959
if err != nil {

rpc/jsonrpc/types/methods.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,16 @@ type SetTreasuryPolicyCmd struct {
927927
Ticket *string
928928
}
929929

930+
// NewSetTreasuryPolicyCmd returns a new instance which can be used to issue a settreasurypolicy
931+
// JSON-RPC command.
932+
func NewSetTreasuryPolicyCmd(key string, policy string, ticket *string) *SetTreasuryPolicyCmd {
933+
return &SetTreasuryPolicyCmd{
934+
Key: key,
935+
Policy: policy,
936+
Ticket: ticket,
937+
}
938+
}
939+
930940
// TSpendPolicyCmd defines the parameters for the tspendpolicy JSON-RPC
931941
// command.
932942
type TSpendPolicyCmd struct {
@@ -942,6 +952,16 @@ type SetTSpendPolicyCmd struct {
942952
Ticket *string
943953
}
944954

955+
// NewSetTSpendPolicyCmd returns a new instance which can be used to issue a settspendpolicy
956+
// JSON-RPC command.
957+
func NewSetTSpendPolicyCmd(hash string, policy string, ticket *string) *SetTSpendPolicyCmd {
958+
return &SetTSpendPolicyCmd{
959+
Hash: hash,
960+
Policy: policy,
961+
Ticket: ticket,
962+
}
963+
}
964+
945965
// SetTxFeeCmd defines the settxfee JSON-RPC command.
946966
type SetTxFeeCmd struct {
947967
Amount float64 // In DCR

rpc/jsonrpc/types/methods_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,66 @@ func TestWalletSvrCmds(t *testing.T) {
963963
Amount: 0.5,
964964
},
965965
},
966+
{
967+
name: "settspendpolicy",
968+
newCmd: func() (interface{}, error) {
969+
return dcrjson.NewCmd(Method("settspendpolicy"), "hash", "policy")
970+
},
971+
staticCmd: func() interface{} {
972+
return NewSetTSpendPolicyCmd("hash", "policy", nil)
973+
},
974+
marshalled: `{"jsonrpc":"1.0","method":"settspendpolicy","params":["hash","policy"],"id":1}`,
975+
unmarshalled: &SetTSpendPolicyCmd{
976+
Hash: "hash",
977+
Policy: "policy",
978+
Ticket: nil,
979+
},
980+
},
981+
{
982+
name: "settspendpolicy optional",
983+
newCmd: func() (interface{}, error) {
984+
return dcrjson.NewCmd(Method("settspendpolicy"), "hash", "policy", "ticket")
985+
},
986+
staticCmd: func() interface{} {
987+
return NewSetTSpendPolicyCmd("hash", "policy", dcrjson.String("ticket"))
988+
},
989+
marshalled: `{"jsonrpc":"1.0","method":"settspendpolicy","params":["hash","policy","ticket"],"id":1}`,
990+
unmarshalled: &SetTSpendPolicyCmd{
991+
Hash: "hash",
992+
Policy: "policy",
993+
Ticket: dcrjson.String("ticket"),
994+
},
995+
},
996+
{
997+
name: "settreasurypolicy",
998+
newCmd: func() (interface{}, error) {
999+
return dcrjson.NewCmd(Method("settreasurypolicy"), "key", "policy")
1000+
},
1001+
staticCmd: func() interface{} {
1002+
return NewSetTreasuryPolicyCmd("key", "policy", nil)
1003+
},
1004+
marshalled: `{"jsonrpc":"1.0","method":"settreasurypolicy","params":["key","policy"],"id":1}`,
1005+
unmarshalled: &SetTreasuryPolicyCmd{
1006+
Key: "key",
1007+
Policy: "policy",
1008+
Ticket: nil,
1009+
},
1010+
},
1011+
{
1012+
name: "settreasurypolicy optional",
1013+
newCmd: func() (interface{}, error) {
1014+
return dcrjson.NewCmd(Method("settreasurypolicy"), "key", "policy", "ticket")
1015+
},
1016+
staticCmd: func() interface{} {
1017+
return NewSetTreasuryPolicyCmd("key", "policy", dcrjson.String("ticket"))
1018+
},
1019+
marshalled: `{"jsonrpc":"1.0","method":"settreasurypolicy","params":["key","policy","ticket"],"id":1}`,
1020+
unmarshalled: &SetTreasuryPolicyCmd{
1021+
Key: "key",
1022+
Policy: "policy",
1023+
Ticket: dcrjson.String("ticket"),
1024+
},
1025+
},
9661026
{
9671027
name: "settxfee",
9681028
newCmd: func() (interface{}, error) {

0 commit comments

Comments
 (0)