-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api: sanity check the "to" address of outgoing messages (#12135)
* feat: api: sanity check the "to" address of outgoing messages If the "to" address of an outgoing message is a _delegated_ address, verify that it maps to a valid Ethereum address. This isn't a consensus critical change, but it'll help prevent client-side address conversion libraries from directing messages into oblivion (e.g., by mis-translating `0xff0000....` addresses into `f410f...` addresses instead of `f0...` addresses. * tests for invalid delegated addresses * fix lint --------- Co-authored-by: aarshkshah1992 <[email protected]>
- Loading branch information
1 parent
85abc61
commit 58c029a
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package full | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/crypto" | ||
|
||
"github.com/filecoin-project/lotus/chain/types" | ||
) | ||
|
||
func TestSanityCheckOutgoingMessage(t *testing.T) { | ||
// fails for invalid delegated address | ||
badTo, err := address.NewFromString("f410f74aaaaaaaaaaaaaaaaaaaaaaaaac5sh2bf3lgta") | ||
require.NoError(t, err) | ||
msg := &types.Message{ | ||
To: badTo, | ||
} | ||
|
||
err = sanityCheckOutgoingMessage(msg) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "is a delegated address but not a valid Eth Address") | ||
|
||
// works for valid delegated address | ||
goodTo, err := address.NewFromString("f410faxfebiima2gp4lduo2k3vt2iuqapuk3logeftky") | ||
require.NoError(t, err) | ||
msg = &types.Message{ | ||
To: goodTo, | ||
} | ||
err = sanityCheckOutgoingMessage(msg) | ||
require.NoError(t, err) | ||
|
||
// works for valid non-delegated address | ||
goodTo, err = address.NewFromString("f1z762skeib2v6zlkvhywmjxbv3dxoiv4hmb6gs4y") | ||
require.NoError(t, err) | ||
msg = &types.Message{ | ||
To: goodTo, | ||
} | ||
err = sanityCheckOutgoingMessage(msg) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestMpoolPushInvalidDelegatedAddressFails(t *testing.T) { | ||
badTo, err := address.NewFromString("f410f74aaaaaaaaaaaaaaaaaaaaaaaaac5sh2bf3lgta") | ||
require.NoError(t, err) | ||
module := &MpoolModule{} | ||
m := &MpoolAPI{ | ||
MpoolModuleAPI: module, | ||
} | ||
smsg := &types.SignedMessage{ | ||
Message: types.Message{ | ||
From: badTo, | ||
To: badTo, | ||
}, | ||
Signature: crypto.Signature{ | ||
Type: crypto.SigTypeSecp256k1, | ||
Data: []byte("signature"), | ||
}, | ||
} | ||
_, err = m.MpoolPush(context.Background(), smsg) | ||
require.Error(t, err) | ||
|
||
require.Contains(t, err.Error(), "is a delegated address but not a valid Eth Address") | ||
} |