Verify only one field is filled out in "enums"
- Dominant language
- Go
- Stars
- 206
- Forks
- 140
- Avg merge
- 2h 44m
- Merged PRs (30d)
- 1
Description
In Rust, we use enums or "union types" to set *exactly one field* of many possibilities. This is enforced by internal data structures as well as the JSON parser.
In Go, we use a struct with many fields to represent this, like [CosmosMsg](https://github.com/CosmWasm/wasmvm/blob/main/types/msg.go#L99-L108) or [QueryRequest](https://github.com/CosmWasm/wasmvm/blob/main/types/queries.go#L83-L90) (and their sub-types). If No fields or multiple fields are filled out, this may introduce some logical errors later on in the consumer, such as this reported error https://github.com/CosmWasm/wasmd/issues/931 (which never happens when coming from the valid Rust type).
To eliminate this class of error and possible attack surface, we should enforce that these Go structs are actually enums (exactly one field is set). IMO, we should add some "Validate" method to do so, but more importantly, *auto-execute the validate method in JSON unmarshalling*. JSON unmarshalling catches all the cases where this unvalidated data is imported from an untrusted contract and we should make it safe by default. Exposing that same logic via a "Validate" method is mainly to allow some assertions in unit tests than manually construct some objects.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.