Skip to content

Commit

Permalink
Merge pull request #10440 from filecoin-project/steb/reject-masked-id
Browse files Browse the repository at this point in the history
feat: eth API: reject masked ID addresses embedded in f410f payloads
  • Loading branch information
arajasek authored Mar 10, 2023
2 parents 816ca59 + f7603f6 commit 9d994da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
23 changes: 17 additions & 6 deletions chain/types/ethtypes/eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,21 @@ func EthAddressFromPubKey(pubk []byte) ([]byte, error) {
return ethAddr, nil
}

var maskedIDPrefix = [20 - 8]byte{0xff}

func IsEthAddress(addr address.Address) bool {
if addr.Protocol() != address.Delegated {
return false
}
payload := addr.Payload()
namespace, _, err := varint.FromUvarint(payload)
namespace, offset, err := varint.FromUvarint(payload)
if err != nil {
return false
}

return namespace == builtintypes.EthereumAddressManagerActorID
payload = payload[offset:]

return namespace == builtintypes.EthereumAddressManagerActorID && len(payload) == 20 && !bytes.HasPrefix(payload, maskedIDPrefix[:])
}

func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
Expand All @@ -326,9 +330,17 @@ func EthAddressFromFilecoinAddress(addr address.Address) (EthAddress, error) {
return EthAddress{}, xerrors.Errorf("invalid delegated address namespace in: %s", addr)
}
payload = payload[n:]
if namespace == builtintypes.EthereumAddressManagerActorID {
return CastEthAddress(payload)
if namespace != builtintypes.EthereumAddressManagerActorID {
return EthAddress{}, ErrInvalidAddress
}
ethAddr, err := CastEthAddress(payload)
if err != nil {
return EthAddress{}, err
}
if ethAddr.IsMaskedID() {
return EthAddress{}, xerrors.Errorf("f410f addresses cannot embed masked-ID payloads: %s", ethAddr)
}
return ethAddr, nil
}
return EthAddress{}, ErrInvalidAddress
}
Expand Down Expand Up @@ -376,8 +388,7 @@ func (ea *EthAddress) UnmarshalJSON(b []byte) error {
}

func (ea EthAddress) IsMaskedID() bool {
idmask := [12]byte{0xff}
return bytes.Equal(ea[:12], idmask[:])
return bytes.HasPrefix(ea[:], maskedIDPrefix[:])
}

func (ea EthAddress) ToFilecoinAddress() (address.Address, error) {
Expand Down
15 changes: 15 additions & 0 deletions chain/types/ethtypes/eth_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin"
)

type TestCase struct {
Expand Down Expand Up @@ -178,6 +179,20 @@ func TestParseEthAddr(t *testing.T) {
}
}

func TestMaskedIDInF4(t *testing.T) {
addr, err := address.NewIDAddress(100)
require.NoError(t, err)

eaddr, err := EthAddressFromFilecoinAddress(addr)
require.NoError(t, err)

badaddr, err := address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, eaddr[:])
require.NoError(t, err)

_, err = EthAddressFromFilecoinAddress(badaddr)
require.Error(t, err)
}

func TestUnmarshalEthCall(t *testing.T) {
data := `{"from":"0x4D6D86b31a112a05A473c4aE84afaF873f632325","to":"0xFe01CC39f5Ae8553D6914DBb9dC27D219fa22D7f","gas":"0x5","gasPrice":"0x6","value":"0x123","data":""}`

Expand Down

0 comments on commit 9d994da

Please sign in to comment.