From 172a4510c74cf026b79e142684e75b6d676cfde1 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Tue, 29 Jan 2019 14:23:25 -0800 Subject: [PATCH] Add length caps for governance proposal titles and descriptions (#3434) --- PENDING.md | 1 + x/gov/errors.go | 8 ++++---- x/gov/msgs.go | 13 +++++++++++-- x/gov/msgs_test.go | 3 +++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/PENDING.md b/PENDING.md index 09daf7658..7fc54004b 100644 --- a/PENDING.md +++ b/PENDING.md @@ -45,6 +45,7 @@ IMPROVEMENTS * Gaia * [\#3418](https://github.com/cosmos/cosmos-sdk/issues/3418) Add vesting account genesis validation checks to `GaiaValidateGenesisState`. + * [\#3420](https://github.com/cosmos/cosmos-sdk/issues/3420) Added maximum length to governance proposal descriptions and titles * SDK * \#3435 Test that store implementations do not allow nil values diff --git a/x/gov/errors.go b/x/gov/errors.go index 9b9aa25db..0bf904d33 100644 --- a/x/gov/errors.go +++ b/x/gov/errors.go @@ -46,12 +46,12 @@ func ErrAddressNotStaked(codespace sdk.CodespaceType, address sdk.AccAddress) sd return sdk.NewError(codespace, CodeAddressNotStaked, fmt.Sprintf("Address %s is not staked and is thus ineligible to vote", address)) } -func ErrInvalidTitle(codespace sdk.CodespaceType, title string) sdk.Error { - return sdk.NewError(codespace, CodeInvalidTitle, fmt.Sprintf("Proposal Title '%s' is not valid", title)) +func ErrInvalidTitle(codespace sdk.CodespaceType, errorMsg string) sdk.Error { + return sdk.NewError(codespace, CodeInvalidTitle, errorMsg) } -func ErrInvalidDescription(codespace sdk.CodespaceType, description string) sdk.Error { - return sdk.NewError(codespace, CodeInvalidDescription, fmt.Sprintf("Proposal Desciption '%s' is not valid", description)) +func ErrInvalidDescription(codespace sdk.CodespaceType, errorMsg string) sdk.Error { + return sdk.NewError(codespace, CodeInvalidDescription, errorMsg) } func ErrInvalidProposalType(codespace sdk.CodespaceType, proposalType ProposalKind) sdk.Error { diff --git a/x/gov/msgs.go b/x/gov/msgs.go index 3be7ab583..534316e4f 100644 --- a/x/gov/msgs.go +++ b/x/gov/msgs.go @@ -11,6 +11,9 @@ const ( TypeMsgDeposit = "deposit" TypeMsgVote = "vote" TypeMsgSubmitProposal = "submit_proposal" + + MaxDescriptionLength int = 5000 + MaxTitleLength int = 140 ) var _, _, _ sdk.Msg = MsgSubmitProposal{}, MsgDeposit{}, MsgVote{} @@ -42,10 +45,16 @@ func (msg MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal } // Implements Msg. func (msg MsgSubmitProposal) ValidateBasic() sdk.Error { if len(msg.Title) == 0 { - return ErrInvalidTitle(DefaultCodespace, msg.Title) // TODO: Proper Error + return ErrInvalidTitle(DefaultCodespace, "No title present in proposal") + } + if len(msg.Title) > MaxTitleLength { + return ErrInvalidTitle(DefaultCodespace, fmt.Sprintf("Proposal title is longer than max length of %d", MaxTitleLength)) } if len(msg.Description) == 0 { - return ErrInvalidDescription(DefaultCodespace, msg.Description) // TODO: Proper Error + return ErrInvalidDescription(DefaultCodespace, "No description present in proposal") + } + if len(msg.Description) > MaxDescriptionLength { + return ErrInvalidDescription(DefaultCodespace, fmt.Sprintf("Proposal description is longer than max length of %d", MaxDescriptionLength)) } if !validProposalType(msg.ProposalType) { return ErrInvalidProposalType(DefaultCodespace, msg.ProposalType) diff --git a/x/gov/msgs_test.go b/x/gov/msgs_test.go index 83881834f..3c4472137 100644 --- a/x/gov/msgs_test.go +++ b/x/gov/msgs_test.go @@ -1,6 +1,7 @@ package gov import ( + "strings" "testing" "github.com/stretchr/testify/require" @@ -40,6 +41,8 @@ func TestMsgSubmitProposal(t *testing.T) { {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, sdk.AccAddress{}, coinsPos, false}, {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsZero, true}, {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true}, + {strings.Repeat("#", MaxTitleLength*2), "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, false}, + {"Test Proposal", strings.Repeat("#", MaxDescriptionLength*2), ProposalTypeText, addrs[0], coinsMulti, false}, } for i, tc := range tests {