Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message representation for signing #658

Merged
merged 7 commits into from
Oct 25, 2021
Merged

Fix message representation for signing #658

merged 7 commits into from
Oct 25, 2021

Conversation

alpe
Copy link
Contributor

@alpe alpe commented Oct 21, 2021

Resolves #642

This PR introduces the RawContractMessage type for incoming and outgoing data to a contract.
I have also revisited some other places where we were using json.RawMessage currently or before the change:

The protobuf diffs describe the changes best

@ethanfrey
Copy link
Member

Happy if @webmaster128 takes a look here - he understands the issue better than I

@codecov
Copy link

codecov bot commented Oct 21, 2021

Codecov Report

Merging #658 (481be4d) into master (57517b0) will increase coverage by 0.16%.
The diff coverage is 75.60%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #658      +/-   ##
==========================================
+ Coverage   59.95%   60.11%   +0.16%     
==========================================
  Files          48       48              
  Lines        5324     5346      +22     
==========================================
+ Hits         3192     3214      +22     
+ Misses       1906     1904       -2     
- Partials      226      228       +2     
Impacted Files Coverage Δ
x/wasm/keeper/query_plugins.go 79.47% <50.00%> (-0.47%) ⬇️
x/wasm/types/tx.go 44.09% <71.42%> (+5.74%) ⬆️
x/wasm/keeper/legacy_querier.go 69.89% <80.00%> (+0.66%) ⬆️
x/wasm/keeper/querier.go 62.12% <100.00%> (+0.38%) ⬆️
x/wasm/types/proposal.go 82.62% <100.00%> (ø)
x/wasm/keeper/keeper.go 87.20% <0.00%> (+0.34%) ⬆️

if a == nil {
return errors.New("unmarshalJSON on nil pointer")
}
*a = append((*a)[0:0], b...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this type rather than json.RawMessage everywhere gives flexibility.

What about doing ValidateBasic() in UnmarshalJSON()? Then we guarantee it is always valid

Copy link
Contributor Author

@alpe alpe Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this as well but then ended with not adding it here. My main driver was not mixing concerns.
In the sdk message ValidateBasic functions we would need to call RawContractMessage.ValidateBasic anyway for the cases when we initialize the objects not from json sources like protobuf or in the handler_plugin_encoders.
We are in good company here as the stdlib json type has separated this as well.

@alpe alpe marked this pull request as ready for review October 22, 2021 11:38
Copy link
Member

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

x/wasm/types/tx_test.go Show resolved Hide resolved
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Some safety measures and we're good to go.

x/wasm/types/tx.go Show resolved Hide resolved
if r == nil {
return []byte("null"), nil
}
return r, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is what Ethan actually meant, but I think we need JSON validation here. An RawContractMessage instance should hold valid JSON but may or may not have corrupted data. Even if the current flow calls ValidateBasic first, the type itself should not make assumtions how it is used. The use cases change and I can imagine use cases such as client signing where ValidateBasic is not called before the JSON representation is created.

Now in order to avoid the Cosmos logic in here, I'd use two funtions:

  1. RawContractMessage#IsValidJSON that is called here
  2. RawContractMessage#ValidateBasic that calls 1. but could also implement additional checks such as length limits.

This should address the separation of concerns mentioned in the other comment.

Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

@alpe alpe merged commit dfba139 into master Oct 25, 2021
@alpe alpe deleted the 642-inner_json branch October 25, 2021 13:23
faddat added a commit to notional-labs/wasmd that referenced this pull request Nov 25, 2021
* Introduce RawContractMessage type

* Add json signbytes test for proposals

* No assumptions on MsgIBCSend.data content

* Smart query uses RawContractMessage

* Revert method signature change to be consistent

* Review comment

* Update after discussions

(cherry picked from commit dfba139)
faddat pushed a commit to notional-labs/wasmd that referenced this pull request Nov 27, 2021
* Introduce RawContractMessage type

* Add json signbytes test for proposals

* No assumptions on MsgIBCSend.data content

* Smart query uses RawContractMessage

* Revert method signature change to be consistent

* Review comment

* Update after discussions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Amino JSON representation of inner message in Msg{Instantiate,Migrate,Execute}Contract
3 participants