Skip to content

Commit

Permalink
fix(x/auth): allow multiple = signs in GetTxsEvent (backport #12474) (
Browse files Browse the repository at this point in the history
#13598)

* fix(x/auth): allow multiple = signs in `GetTxsEvent` (#12474)

(cherry picked from commit 18da0e9)

# Conflicts:
#	CHANGELOG.md

* fix changelog

* changelog

* fix: flakey test

Co-authored-by: Tyler <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent f538e09 commit afdf06a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (x/auth) [#13612](https://github.com/cosmos/cosmos-sdk/pull/13612) Add `Query/ModuleAccountByName` endpoint for accessing the module account info by module name.

## Bug Fixes

* (x/auth/tx) [#12474](https://github.com/cosmos/cosmos-sdk/pull/12474) Remove condition in GetTxsEvent that disallowed multiple equal signs, which would break event queries with base64 strings (i.e. query by signature).

## [v0.46.3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.3) - 2022-10-20

ATTENTION:
Expand All @@ -52,7 +56,7 @@ All users should upgrade immediately.
Users *must* add a replace directive in their go.mod for the new `ics23` package in the SDK:

```go
replace github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v8.0.0
replace github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
```

### Features
Expand Down
12 changes: 9 additions & 3 deletions x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tx
import (
"context"
"fmt"
"regexp"
"strings"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
Expand Down Expand Up @@ -40,13 +41,18 @@ func NewTxServer(clientCtx client.Context, simulate baseAppSimulateFn, interface
}
}

var _ txtypes.ServiceServer = txServer{}
var (
_ txtypes.ServiceServer = txServer{}

// EventRegex checks that an event string is formatted with {alphabetic}.{alphabetic}={value}
EventRegex = regexp.MustCompile(`^[a-zA-Z]+\.[a-zA-Z]+=\S+$`)
)

const (
eventFormat = "{eventType}.{eventAttribute}={value}"
)

// TxsByEvents implements the ServiceServer.TxsByEvents RPC method.
// GetTxsEvent implements the ServiceServer.TxsByEvents RPC method.
func (s txServer) GetTxsEvent(ctx context.Context, req *txtypes.GetTxsEventRequest) (*txtypes.GetTxsEventResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "request cannot be nil")
Expand All @@ -70,7 +76,7 @@ func (s txServer) GetTxsEvent(ctx context.Context, req *txtypes.GetTxsEventReque
}

for _, event := range req.Events {
if !strings.Contains(event, "=") || strings.Count(event, "=") > 1 {
if !EventRegex.Match([]byte(event)) {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid event; event %s should be of the format: %s", event, eventFormat))
}
}
Expand Down
78 changes: 78 additions & 0 deletions x/auth/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package tx_test

import (
"context"
"encoding/base64"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -28,6 +30,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtest "github.com/cosmos/cosmos-sdk/x/auth/client/testutil"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
bankcli "github.com/cosmos/cosmos-sdk/x/bank/client/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
Expand Down Expand Up @@ -111,6 +114,81 @@ func (s *IntegrationTestSuite) TearDownSuite() {
s.network.Cleanup()
}

func (s *IntegrationTestSuite) TestQueryBySig() {
// broadcast tx
txb := s.mkTxBuilder()
txbz, err := s.cfg.TxConfig.TxEncoder()(txb.GetTx())
s.Require().NoError(err)
resp, err := s.queryClient.BroadcastTx(context.Background(), &tx.BroadcastTxRequest{TxBytes: txbz, Mode: tx.BroadcastMode_BROADCAST_MODE_SYNC})
s.Require().NoError(err)
s.Require().NotEmpty(resp.TxResponse.TxHash)

s.Require().NoError(s.network.WaitForNextBlock())

// get the signature out of the builder
sigs, err := txb.GetTx().GetSignaturesV2()
s.Require().NoError(err)
s.Require().Len(sigs, 1)
sig, ok := sigs[0].Data.(*signing.SingleSignatureData)
s.Require().True(ok)

// encode, format, query
b64Sig := base64.StdEncoding.EncodeToString(sig.Signature)
sigFormatted := fmt.Sprintf("%s.%s='%s'", sdk.EventTypeTx, sdk.AttributeKeySignature, b64Sig)
res, err := s.queryClient.GetTxsEvent(context.Background(), &tx.GetTxsEventRequest{
Events: []string{sigFormatted},
OrderBy: 0,
Page: 0,
Limit: 10,
})
s.Require().NoError(err)
s.Require().Len(res.Txs, 1)
s.Require().Len(res.Txs[0].Signatures, 1)
s.Require().Equal(res.Txs[0].Signatures[0], sig.Signature)

// bad format should error
_, err = s.queryClient.GetTxsEvent(context.Background(), &tx.GetTxsEventRequest{Events: []string{"tx.foo.bar='baz'"}})
s.Require().ErrorContains(err, "invalid event;")
}

func TestEventRegex(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
event string
match bool
}{
{
name: "valid: with quotes",
event: "tx.message='something'",
match: true,
},
{
name: "valid: no quotes",
event: "tx.message=something",
match: true,
},
{
name: "invalid: too many separators",
event: "tx.message.foo='bar'",
match: false,
},
{
name: "valid: symbols ok",
event: "tx.signature='foobar/baz123=='",
match: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
match := authtx.EventRegex.Match([]byte(tc.event))
require.Equal(t, tc.match, match)
})
}
}

func (s IntegrationTestSuite) TestSimulateTx_GRPC() {
val := s.network.Validators[0]
txBuilder := s.mkTxBuilder()
Expand Down

0 comments on commit afdf06a

Please sign in to comment.