Skip to content

Wrong parsing dynamicBytes and unsafe ABI decoding  #801

Open
@6od9i

Description

@6od9i

What happened?

When parse response with dynamicBytes by method decodeSingleType, from ABIDecoding file, to array of responses set not new Data with bounds [0<..count], but slice for example on screenshot [32<..64]
photo_2023-03-15 10 01 39

And when i try to decode my values from this responses array with slices, ABIDecoder checks only size of data to avoid out of bounds
guard data.count >= pointer + type.memoryUsage else {return (nil, nil)}

and this data has correct size, and this check is succeed, but data is slice with bounds [32<..64], and it is crashed in
let elementItself = data[pointer ..< pointer + type.memoryUsage]
because pointer = 0, and type.memoryUsage = 32

What are the steps to reproduce?

1 request aggregated method for getting balances of some different tokens by methods "balanceOf" and "getEthBalance" , for example for 3 tokens from one wallet in one network with "createReadOperation" from Web3.Contract with aggregate ABI
2 try to decode response with contract's method contract.methods[methodName].decodeReturnData
3 app is crashed

What is the expected behavior?

I expect that work fine and not crashed, i think this fix solved the problem
replace
let elementItself = data[pointer ..< pointer + type.memoryUsage]
to
let elementItself = data[data.indices.startIndex + Int(pointer) ..< data.indices.startIndex + Int(pointer + type.memoryUsage)]

it will more safe, than previous because protect us from any slices of data

What is the error thrown?

EXC_BREAKPOINT (code=1, subcode=0x1aeb9fcb4)

What's the stack trace said?

#2 0x0000000103462ab8 in static ABIDecoder.followTheData(type:data:pointer:) at web3swift/Sources/Web3Core/EthereumABI/ABIDecoding.swift:191

OS version

iOS 16.1.1

Library version

"branch" : "develop",
"revision" : "2357868ce27009c5dec6b7a9f64d0a520e85b9af"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions