nearcore
nearcore copied to clipboard
jsonrpc: check base58 length limit before the whole buffer is decoded
Currently, when RpcRequest::parse handles legacy path+data query format, it first decodes the data and only later checks its size. This means that the server will dutifully decode any oversized buffer only to discard the result later reporting an error. Problem with that is that base58 is an atrocious format and decoding is O(n²) operation. Because of that, it’s desired to stop decoding as soon as its known that the data is too long. Change the implementation so that this happens.
Note that for simplicity this commit doesn’t implement a short-circuit taking advantage of the fact that the length of the decoded string can be estimated based on the length of the source. The path+data query format is legacy and should be deleted at some point so I essentially implemented the minimum required for the code to be somewhat sane.