The Art of Handling Infinite Input in GO Web Handlers
Reading /r/golang topics which post code, there are a few recurring bugs that not just newbies make.
One such bug is in the handling of HTTP handlers. Can you spot it in this toy example that's for demonstration purposes only?
Knowledge of the HTTP protocol when writing HTTP handlers is crucial. The key to the bug is knowing that request bodies can be arbitrarily big, so we must specify the maximum amount of data that we want to handle. The bug is not using MaxBytesReader to do so. An adversary could send us a multi-gigabyte request, and we would buffer it all in-memory, possibly causing a Denial Of Service issue. There are multiple other issues with this code as well, lets go through them:
- Do we really need to buffer the request body in memory?
Apart from helping with debugging, there can also be requirements that mean we do indeed need to buffer it all, but in all other cases we should use the streaming decoder.
Since we are not really interested in the JSON "as a whole", but the data it encodes, we can optimize the decoding this way.
- Decoding JSON into
map[string]anyis an antipattern and leads to ugly code. It rarely happens that the JSON data we want to support is so generic that we can only represent it this way.
Maybe we could partially decode the JSON first, to see what type we should unmarshal it into.
- We didn't customize the HTTP server's timeouts, and we probably should depending on our usecase.
The defaults are:
- no ReadTimeout meaning our handler can take as much time as it needs to process the request before reading the request body
- no ReadHeaderTimeout meaning the client can be infinitely slow in sending us the HTTP headers
- no WriteTimeout so our handler can be infinitely slow in giving a response
- no IdleTimeout so we will keep the TCP connection open indefinitely when keep-alives are enabled
A marginally better version of the code above is:
Sadly the defaults are now impossible to change, as that would be a backward-compatibility break, which is a big no-no.
The compatibility promise is crucial to the health of the Go ecosystem so the only thing to do is to be aware of these issues.
If you would like some additional information with regards to exposing your HTTP server on the net, gopheracademy has some very useful information.