tokencard / contracts

The Consumer Contract Wallet
GNU General Public License v3.0
94 stars 40 forks source link

Base64 decoding does not validate its input #395

Open i-stam opened 5 years ago

i-stam commented 5 years ago

Description The _base64decode function that implements decoding of base64 strings does not properly validate its input.

The function _base64decode function shown in Figure 1 is used to decode a base64 string into a list of bytes.

However, this function does not fail in any way if an invalid base64 input is provided. The only check provided will verify the length of the input. This check is insufficient to properly validate all possible inputs

Exploit Scenario Alice's code interacts with the TokenCard contracts by sending a base64 string to decode. Her code has a bug and produces an invalid base64 string. This string is incorrectly decoded into a list of bytes by the TokenCard contract, causing unexpected behaviour for Alice.

Recommendation Short term, properly document this behaviour and make sure users are aware that they should provide only valid base64 strings.

Long term, implement a thorough validation check in the _base64decode function. The function should revert if an invalid input is provided. Use Echidna and Manticore to verify that the validation is working as expected.

mischat commented 5 years ago

Wow, i have used this thing :

https://github.com/gnidan/solregex

To generate this regex : ([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)? taken from : https://stackoverflow.com/questions/8571501/how-to-check-whether-a-string-is-base64-encoded-or-not

https://gist.github.com/mischat/0c5ebf51ad35d9190bdb5557d2863bad