-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transfer): Don't allow unknown fields during json unmarshalling. (#…
…6428) * fix(transfer): Don't allow unknown fields during json unmarshalling. Use json's DisallowUnknownFields flag on the decoder to control error emission for uknown fields. Implement Unmarshaller interface for both version of the ics20 packet data. * docs: expand on recursion protection.
- Loading branch information
1 parent
45c7eea
commit 80b0c9a
Showing
2 changed files
with
64 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package types | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
// UnmarshalJSON implements the Unmarshaller interface for FungibleTokenPacketDataV2. | ||
func (ftpd *FungibleTokenPacketDataV2) UnmarshalJSON(bz []byte) error { | ||
// Recursion protection. We cannot unmarshal into FungibleTokenPacketData directly | ||
// else UnmarshalJSON is going to get invoked again, ad infinum. Create an alias | ||
// and unmarshal into that, instead. | ||
type ftpdAlias FungibleTokenPacketDataV2 | ||
|
||
d := json.NewDecoder(bytes.NewReader(bz)) | ||
// Raise errors during decoding if unknown fields are encountered. | ||
d.DisallowUnknownFields() | ||
|
||
var alias ftpdAlias | ||
if err := d.Decode(&alias); err != nil { | ||
return err | ||
} | ||
|
||
*ftpd = FungibleTokenPacketDataV2(alias) | ||
return nil | ||
} | ||
|
||
// UnmarshalJSON implements the Unmarshaller interface for FungibleTokenPacketData. | ||
func (ftpd *FungibleTokenPacketData) UnmarshalJSON(bz []byte) error { | ||
// Recursion protection. We cannot unmarshal into FungibleTokenPacketData directly | ||
// else UnmarshalJSON is going to get invoked again, ad infinum. Create an alias | ||
// and unmarshal into that, instead. | ||
type ftpdAlias FungibleTokenPacketData | ||
|
||
d := json.NewDecoder(bytes.NewReader(bz)) | ||
// Raise errors during decoding if unknown fields are encountered. | ||
d.DisallowUnknownFields() | ||
|
||
var alias ftpdAlias | ||
if err := d.Decode(&alias); err != nil { | ||
return err | ||
} | ||
|
||
*ftpd = FungibleTokenPacketData(alias) | ||
return nil | ||
} |