Skip to content

Commit

Permalink
reset to release finding one by one, and merge encrypted_desc,desc to…
Browse files Browse the repository at this point in the history
… one accept interface
  • Loading branch information
kevin-yuhh committed Jan 31, 2023
1 parent d8ce8ae commit e6834bf
Show file tree
Hide file tree
Showing 10 changed files with 811 additions and 347 deletions.
43 changes: 27 additions & 16 deletions proto/shentu/bounty/v1/bounty.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,42 @@ message Finding {

uint64 finding_id = 1 [(gogoproto.jsontag) = "id", (gogoproto.moretags) = "yaml:\"id\""];
string title = 2 [(gogoproto.moretags) = "yaml:\"title\""];
google.protobuf.Any encrypted_desc = 3 [(cosmos_proto.accepts_interface) = "EncryptedDesc",(gogoproto.moretags) = "yaml:\"encrypted_desc\""];
string desc = 4 [(gogoproto.moretags) = "yaml:\"desc\""];
uint64 program_id = 5 [(gogoproto.moretags) = "yaml:\"program_id\""];
SeverityLevel severity_level = 6 [(gogoproto.moretags) = "yaml:\"severity_level\""];;
google.protobuf.Any encrypted_poc = 7 [(cosmos_proto.accepts_interface) = "EncryptedPoc",(gogoproto.moretags) = "yaml:\"encrypted_poc\""];
string poc = 8 [(gogoproto.moretags) = "yaml:\"poc\""];
string submitter_address = 9 [(gogoproto.moretags) = "yaml:\"submitter_address\""];
FindingStatus finding_status = 10 [(gogoproto.moretags) = "yaml:\"finding_status\""];
google.protobuf.Any encrypted_comment = 11 [(cosmos_proto.accepts_interface) = "EncryptedComment",(gogoproto.moretags) = "yaml:\"encrypted_comment\""];
string comment = 12 [(cosmos_proto.accepts_interface) = "Comment",(gogoproto.moretags) = "yaml:\"comment\""];
google.protobuf.Any finding_desc = 3 [(cosmos_proto.accepts_interface) = "FindingDesc",(gogoproto.moretags) = "yaml:\"finding_desc\""];
uint64 program_id = 4 [(gogoproto.moretags) = "yaml:\"program_id\""];
SeverityLevel severity_level = 5 [(gogoproto.moretags) = "yaml:\"severity_level\""];;
google.protobuf.Any finding_poc = 6 [(cosmos_proto.accepts_interface) = "FindingPoc",(gogoproto.moretags) = "yaml:\"finding_poc\""];
string submitter_address = 7 [(gogoproto.moretags) = "yaml:\"submitter_address\""];
FindingStatus finding_status = 8 [(gogoproto.moretags) = "yaml:\"finding_status\""];
google.protobuf.Any finding_comment = 9 [(cosmos_proto.accepts_interface) = "FindingComment",(gogoproto.moretags) = "yaml:\"finding_comment\""];
}

message EciesEncryptedDesc {
option (cosmos_proto.implements_interface) = "EncryptedDesc";
bytes encrypted_desc = 1;
option (cosmos_proto.implements_interface) = "FindingDesc";
bytes finding_desc = 1;
}

message EciesEncryptedPoc {
option (cosmos_proto.implements_interface) = "EncryptionPoc";
bytes encrypted_poc = 1;
option (cosmos_proto.implements_interface) = "FindingPoc";
bytes finding_poc = 1;
}

message EciesEncryptedComment {
option (cosmos_proto.implements_interface) = "EncryptionComment";
bytes encrypted_comment = 1;
option (cosmos_proto.implements_interface) = "FindingComment";
bytes finding_comment = 1;
}
message PlainTextDesc {
option (cosmos_proto.implements_interface) = "FindingDesc";
bytes finding_desc = 1;
}

message PlainTextPoc {
option (cosmos_proto.implements_interface) = "FindingPoc";
bytes finding_poc = 1;
}

message PlainTextComment {
option (cosmos_proto.implements_interface) = "FindingComment";
bytes finding_comment = 1;
}

enum SeverityLevel {
Expand Down
49 changes: 31 additions & 18 deletions x/bounty/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func NewSubmitFindingCmd() *cobra.Command {

poc, _ := cmd.Flags().GetString(FlagFindingPoc)

//func EncryptMsg(cmd *cobra.Command, programID uint64, desc, poc string) (descAny, pocAny *codectypes.Any, err error) {
descAny, pocAny, err := EncryptMsg(cmd, pid, desc, poc)
if err != nil {
return err
Expand Down Expand Up @@ -209,7 +208,7 @@ func EncryptMsg(cmd *cobra.Command, programID uint64, desc, poc string) (descAny
return nil, nil, err
}
encDesc := types.EciesEncryptedDesc{
EncryptedDesc: encryptedDescBytes,
FindingDesc: encryptedDescBytes,
}
descAny, err = codectypes.NewAnyWithValue(&encDesc)
if err != nil {
Expand All @@ -221,7 +220,7 @@ func EncryptMsg(cmd *cobra.Command, programID uint64, desc, poc string) (descAny
return nil, nil, err
}
encPoc := types.EciesEncryptedPoc{
EncryptedPoc: encryptedPocBytes,
FindingPoc: encryptedPocBytes,
}
pocAny, err = codectypes.NewAnyWithValue(&encPoc)
if err != nil {
Expand Down Expand Up @@ -351,7 +350,7 @@ func HostProcessFinding(cmd *cobra.Command, args []string) (fid uint64,
return fid, commentAny, hostAddr, err
}
encComment := types.EciesEncryptedComment{
EncryptedComment: encryptedComment,
FindingComment: encryptedComment,
}
commentAny, err = codectypes.NewAnyWithValue(&encComment)
if err != nil {
Expand Down Expand Up @@ -420,23 +419,37 @@ func GetFindingPlainText(cmd *cobra.Command, fid uint64, encKeyFile string) (

prvKey := LoadPrvKey(encKeyFile)

encryptedDescBytes := finding.EncryptedDesc.GetValue()
descBytes, err := prvKey.Decrypt(encryptedDescBytes[2:], nil, nil)
if err != nil {
return "", "", "", err
if finding.FindingDesc == nil {
desc = ""
} else {
encryptedDescBytes := finding.FindingDesc.GetValue()
descBytes, err := prvKey.Decrypt(encryptedDescBytes[2:], nil, nil)
if err != nil {
return "", "", "", err
}
desc = string(descBytes)
}

encryptedPocBytes := finding.EncryptedPoc.GetValue()
pocBytes, err := prvKey.Decrypt(encryptedPocBytes[2:], nil, nil)
if err != nil {
return "", "", "", err
if finding.FindingPoc == nil {
poc = ""
} else {
encryptedPocBytes := finding.FindingPoc.GetValue()
pocBytes, err := prvKey.Decrypt(encryptedPocBytes[2:], nil, nil)
if err != nil {
return "", "", "", err
}
poc = string(pocBytes)
}

encryptedCommentBytes := finding.EncryptedComment.GetValue()
commentBytes, err := prvKey.Decrypt(encryptedCommentBytes[2:], nil, nil)
if err != nil {
return "", "", "", err
if finding.FindingComment == nil {
comment = ""
} else {
encryptedCommentBytes := finding.FindingComment.GetValue()
commentBytes, err := prvKey.Decrypt(encryptedCommentBytes[2:], nil, nil)
if err != nil {
return "", "", "", err
}
comment = string(commentBytes)
}

return string(descBytes), string(pocBytes), string(commentBytes), nil
return
}
2 changes: 1 addition & 1 deletion x/bounty/client/cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAnyToBytes(t *testing.T) {

var descAny *codectypes.Any
encDesc := types.EciesEncryptedDesc{
EncryptedDesc: encryptedDesc,
FindingDesc: encryptedDesc,
}
if descAny, err = codectypes.NewAnyWithValue(&encDesc); err != nil {
t.Fatal(err)
Expand Down
10 changes: 6 additions & 4 deletions x/bounty/keeper/finding.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ func (k Keeper) GetPidFindingIDList(ctx sdk.Context, pid uint64) ([]uint64, erro

func (k Keeper) AppendFidToFidList(ctx sdk.Context, pid, fid uint64) error {
fids, err := k.GetPidFindingIDList(ctx, pid)
if err.Error() == types.ErrorEmptyProgramIDFindingList {
fids = []uint64{}
} else if err != nil {
return err
if err != nil {
if err.Error() == types.ErrorEmptyProgramIDFindingList {
fids = []uint64{}
} else {
return err
}
}

fids = append(fids, fid)
Expand Down
37 changes: 31 additions & 6 deletions x/bounty/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ func (k msgServer) SubmitFinding(goCtx context.Context, msg *types.MsgSubmitFind
finding := types.Finding{
FindingId: findingID,
Title: msg.Title,
EncryptedDesc: msg.EncryptedDesc,
FindingDesc: msg.EncryptedDesc,
ProgramId: msg.ProgramId,
SeverityLevel: msg.SeverityLevel,
EncryptedPoc: msg.EncryptedPoc,
FindingPoc: msg.EncryptedPoc,
SubmitterAddress: msg.SubmitterAddress,
FindingStatus: types.FindingStatusUnConfirmed,
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func (k msgServer) hostProcess(ctx sdk.Context, fid uint64, hostAddr string, enc
return nil, fmt.Errorf("%s not the program creator, expect %s", hostAddr, program.CreatorAddress)
}

finding.EncryptedComment = encryptedCommentAny
finding.FindingComment = encryptedCommentAny
return &finding, nil
}

Expand All @@ -226,16 +226,41 @@ func (k msgServer) ReleaseFinding(goCtx context.Context, msg *types.MsgReleaseFi
return nil, fmt.Errorf("%s not the program creator, expect %s", msg.HostAddress, program.CreatorAddress)
}

finding.Desc = msg.Desc
finding.Poc = msg.Poc
finding.Comment = msg.Comment
plainTextDesc := types.PlainTextDesc{
FindingDesc: []byte(msg.Desc),
}
descAny, err := codectypes.NewAnyWithValue(&plainTextDesc)
if err != nil {
return nil, err
}

plainTextPoc := types.PlainTextPoc{
FindingPoc: []byte(msg.Poc),
}
pocAny, err := codectypes.NewAnyWithValue(&plainTextPoc)
if err != nil {
return nil, err
}

plainTextComment := types.PlainTextComment{
FindingComment: []byte(msg.Comment),
}
commentAny, err := codectypes.NewAnyWithValue(&plainTextComment)
if err != nil {
return nil, err
}

finding.FindingDesc = descAny
finding.FindingPoc = pocAny
finding.FindingComment = commentAny

k.SetFinding(ctx, finding)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeReleaseFinding,
sdk.NewAttribute(types.AttributeKeyFindingID, strconv.FormatUint(finding.FindingId, 10)),
sdk.NewAttribute(types.AttributeKeyProgramID, strconv.FormatUint(program.ProgramId, 10)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
Expand Down
22 changes: 15 additions & 7 deletions x/bounty/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func GetDescPocAny(desc, poc string, pubKey *ecies.PublicKey) (descAny, pocAny *
return nil, nil, err
}
encDesc := types.EciesEncryptedDesc{
EncryptedDesc: encryptedDescBytes,
FindingDesc: encryptedDescBytes,
}
descAny, err = codectypes.NewAnyWithValue(&encDesc)
if err != nil {
Expand All @@ -244,7 +244,7 @@ func GetDescPocAny(desc, poc string, pubKey *ecies.PublicKey) (descAny, pocAny *
return nil, nil, err
}
encPoc := types.EciesEncryptedPoc{
EncryptedPoc: encryptedPocBytes,
FindingPoc: encryptedPocBytes,
}
pocAny, err = codectypes.NewAnyWithValue(&encPoc)
if err != nil {
Expand Down Expand Up @@ -412,8 +412,8 @@ func (suite *KeeperTestSuite) TestHostRejectFinding() {
}

func (suite *KeeperTestSuite) TestReleaseFinding() {
programId := suite.InitCreateProgram()
findingId := suite.InitSubmitFinding(programId)
programId, pubKey := suite.InitCreateProgram()
findingId := suite.InitSubmitFinding(programId, pubKey)

testCases := []struct {
name string
Expand Down Expand Up @@ -458,9 +458,17 @@ func (suite *KeeperTestSuite) TestReleaseFinding() {

if testCase.expPass {
suite.Require().NoError(err)
suite.Require().Equal(finding.Desc, testCase.req.Desc)
suite.Require().Equal(finding.Poc, testCase.req.Poc)
suite.Require().Equal(finding.Comment, testCase.req.Comment)
desc, ok := finding.FindingDesc.GetCachedValue().(types.FindingDesc)
suite.Require().True(ok)
suite.Require().Equal(string(desc.GetFindingDesc()), testCase.req.Desc)

poc, ok := finding.FindingPoc.GetCachedValue().(types.FindingPoc)
suite.Require().True(ok)
suite.Require().Equal(string(poc.GetFindingPoc()), testCase.req.Poc)

comment, ok := finding.FindingComment.GetCachedValue().(types.FindingComment)
suite.Require().True(ok)
suite.Require().Equal(string(comment.GetFindingComment()), testCase.req.Comment)
} else {
suite.Require().Error(err)
}
Expand Down
38 changes: 21 additions & 17 deletions x/bounty/types/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ type EncryptionKey interface {
GetEncryptionKey() []byte
}

type EncryptedDesc interface {
type FindingDesc interface {
proto.Message

GetEncryptedDesc() []byte
GetFindingDesc() []byte
}

type EncryptedPoc interface {
type FindingPoc interface {
proto.Message

GetEncryptedPoc() []byte
GetFindingPoc() []byte
}

type EncryptedCommnet interface {
type FindingComment interface {
proto.Message

GetEncryptedComment() []byte
GetFindingComment() []byte
}

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces.
Expand All @@ -47,36 +47,40 @@ func (p Program) GetEncryptionKey() EncryptionKey {
}

func (f Finding) UnpackInterfaces(unpacker codecTypes.AnyUnpacker) error {
var desc EncryptedDesc
var poc EncryptedPoc
err := unpacker.UnpackAny(f.EncryptedDesc, &desc)
var desc FindingDesc
var poc FindingPoc
var comment FindingComment
err := unpacker.UnpackAny(f.FindingDesc, &desc)
if err != nil {
return err
}
if err = unpacker.UnpackAny(f.FindingComment, &comment); err != nil {
return err
}

return unpacker.UnpackAny(f.EncryptedPoc, &poc)
return unpacker.UnpackAny(f.FindingPoc, &poc)
}

func (f Finding) GetEncryptedDesc() EncryptedDesc {
desc, ok := f.EncryptedDesc.GetCachedValue().(EncryptedDesc)
func (f Finding) GetFindingDesc() FindingDesc {
desc, ok := f.FindingDesc.GetCachedValue().(FindingDesc)
if !ok {
return nil
}
return desc
}

func (f Finding) GetEncryptedPoc() EncryptedPoc {
poc, ok := f.EncryptedPoc.GetCachedValue().(EncryptedPoc)
func (f Finding) GetFindingPoc() FindingPoc {
poc, ok := f.FindingPoc.GetCachedValue().(FindingPoc)
if !ok {
return nil
}
return poc
}

func (f Finding) GetEncryptedComment() EncryptedCommnet {
poc, ok := f.EncryptedComment.GetCachedValue().(EncryptedCommnet)
func (f Finding) GetFindingComment() FindingComment {
comment, ok := f.FindingComment.GetCachedValue().(FindingComment)
if !ok {
return nil
}
return poc
return comment
}
Loading

0 comments on commit e6834bf

Please sign in to comment.