Exploring a critical Net::Protocol issue in Ruby 2.6.0p0 and how it can lead to a security problem
If you do any HTTP communication (HTTP requests, Elasticsearch, etc), do not upgrade to 2.6.0p0 or apply the patch below as soon as possible.
Ruby is eating up characters when pushed over HTTP
Ruby 2.6.0 has been released not long ago. Not many are unfortunately aware of a major bug that was introduced with these release.
This bug can affect you in many ways, some of which you may not even be aware. All may run well up until you decide to send a particular type of payload and then, things will get interesting.
What am I talking about?
This. What does it even mean? Well in the best scenario it means, that you will end up having a critical error like so:
However, there’s a much more interesting case that you can encounter. You can end up sending data that will be trimmed in a way that will make your server receive incomplete yet valid information.
That is not a security issue per se but can be a massive problem if you use your format as a protocol between some internal services.
Sidenote: bjeanes reported on Github, that this bug can also corrupt JSON in a way that will make it parsable but incorrect regarding data it consists.
Set HTTP API as a POC of this bug
To illustrate how this bug can become problematic and hard to debug, let’s build an HTTP based API that implements basic set operations via the web.
Some assumptions for the sake of simplicity:
- we always send data in the following format: DATA,COMMAND;
- we have three commands: GET, ADD and DEL;
- to save a couple of bytes, when no command provided as a second argument, we run an ADD command;
This is how our abstract API could work:
A set API server implementation
The implementation of such an API server will just take us a couple of lines in Ruby:
You can start it by running:
A set API client implementation
The client is not much more complicated:
When executed, you end up with exactly what we’ve wanted to achieve:
Risk of an uncompleted payload
So far so good. We have an excellent API that we can use for storing anything we want. And here magic starts.
We decide to store some analytics results, that are used by other APIs to grant access to some super essential and expensive business information™.
It doesn’t matter what the results are. All we need to know from our perspective, is the fact, that it will fit into memory. So, we hand out our API client code to other developers; we run our server and… in the middle of the night the phone rings:
Data that is supposed to be deleted is still available. We constantly run the DEL command but nothing disappears! We need to revoke all the access ASAP!
How can it be!? This service has been running for months now, and everything was good. There was a recent update in Ruby, but even after that specs were passing and the service has been running for at least two weeks.
And this is the moment when this bug presents itself in all the glory. For big enough payload, Ruby is trimming data that is being sent, and unfortunately for us, it trims last three letters, that is the full DEL command. When we run an ADD and DEL on a given string, we expect it not to be in the results anymore, however…
Note: the dots from the payload below aren’t usual dots but Unicode middle dots — that is important.
The data is still there! Because the data consists multibyte characters, the payload got trimmed, and we’ve ended up with a non-direct GET operation (DATA,) instead of a DEL. We had three multibyte characters in the data, and because of that, Ruby removed three last characters from the string before sending it to the server.
Patching things up
As a temporary patch you can use the body_stream instead of using body combined with Ruby StringIO:
or if you use Faraday, you can just apply following patch:
Here’s the proper fix, however Ruby 2.6.1 has not yet been released.
It’s a rather unpleasant bug, and I’m quite surprised that despite being fixed, new Ruby version hasn’t had been released yet. For now, if my patches work for you, that’s great but anyhow I would advise you to downgrade to Ruby 2.5.3 just to be sure, that there aren’t other scenarios in which this bug may become even more problematic.
Cover photo by theilr on Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0) license.
Originally published at Running with Ruby.