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

Throw error if marshaling invalid Uint256 #11347

Merged
merged 4 commits into from
Sep 6, 2022
Merged

Throw error if marshaling invalid Uint256 #11347

merged 4 commits into from
Sep 6, 2022

Conversation

jtraglia
Copy link
Contributor

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

Noticed that Uint256 didn't check that the value is a valid unsigned 256-bit integer. It could have been any big.Int value. We should return an error during marshaling/unmarshaling if the value is invalid.

@jtraglia jtraglia requested a review from a team as a code owner August 29, 2022 23:04
@jtraglia
Copy link
Contributor Author

This isn't actually an issue. It will be checked when encoded into a protobuf. For example:

Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty" ssz-size:"32"`

If the value is greater than 32 bytes, it is an invalid uint256 value. This does still allow negative numbers though.

@jtraglia jtraglia closed this Aug 30, 2022
@jtraglia
Copy link
Contributor Author

Hmm nevermind. The protobuf check doesn't work the way I expected. I expected ToProto to return an error if the value is greater than 32 bytes, as specified by ssz-size:"32" in the structure. See this test, executed on develop:

// data.message.header.value is greater than uint256.max
var testBuilderBidInvalidValue = `{
    "version":"bellatrix",
	"data":{
		"message":{
			"header":{
				"parent_hash":"0xa0513a503d5bd6e89a144c3268e5b7e9da9dbf63df125a360e3950a7d0d67131",
				"fee_recipient":"0xdfb434922631787e43725c6b926e989875125751",
				"state_root":"0xca3149fa9e37db08d1cd49c9061db1002ef1cd58db2210f2115c8c989b2bdf45",
				"receipts_root":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
				"logs_bloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
				"prev_randao":"0xc2fa210081542a87f334b7b14a2da3275e4b281dd77b007bcfcb10e34c42052e",
				"block_number":"1",
				"gas_limit":"10000000",
				"gas_used":"0",
				"timestamp":"4660",
				"extra_data":"0x",
				"base_fee_per_gas":"7",
				"block_hash":"0x10746fa06c248e7eacd4ff8ad8b48a826c227387ee31a6aa5eb4d83ddad34f07",
				"transactions_root":"0x7ffe241ea60187fdb0187bfa22de35d1f9bed7ab061d9401fd47e34a54fbede1"
			},
			"value":"115792089237316195423570985008687907853269984665640564039457584007913129639936",
			"pubkey":"0x8645866c95cbc2e08bc77ccad473540eddf4a1f51a2a8edc8d7a673824218f7f68fe565f1ab38dadd5c855b45bbcec95"
		},
		"signature":"0x9183ebc1edf9c3ab2bbd7abdc3b59c6b249d6647b5289a97eea36d9d61c47f12e283f64d928b1e7f5b8a5182b714fa921954678ea28ca574f5f232b2f78cf8900915a2993b396e3471e0655291fec143a300d41408f66478c8208e0f9be851dc"
	}
}`

func TestBuilderBidUnmarshalInvalidValue(t *testing.T) {
	r := &ExecHeaderResponse{}
	require.NoError(t, json.Unmarshal([]byte(testBuilderBidInvalidValue), r))
	_, err := r.ToProto()
	require.NotEqual(t, nil, err)
}

ToProto will return a nil error: Values are equal, both values are equal: <nil> (<nil>).

And r.Data.Message.Value.BitLen() is 257 (one more bit than what is allowed).

@jtraglia jtraglia reopened this Aug 30, 2022
@jtraglia
Copy link
Contributor Author

There's a similar problem for all of the hexutil.Bytes fields too, right?

Copy link
Contributor

@rkapka rkapka 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, thanks!

@rkapka rkapka merged commit 0cee01a into prysmaticlabs:develop Sep 6, 2022
@jtraglia jtraglia deleted the check-uint256-bounds branch September 6, 2022 18:33
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.

3 participants