anucssa/cssavote2 — pull request #1 by daxtens (2014-08-12 10:25:14+00:00)
Title: Remove (theoretical) timing attack against the comparison with AUTH_TOKEN

In the handler for '/admin', there's a raw string comparison:

```
if params["token"] != ENV["AUTH_TOKEN"]
```

This is, in theory, vulnerable to a timing attack: an attacker can work out the secret by finding out what takes longer to compare, allowing the secret to be discovered one character at a time.

I've spent (an embarrassingly long amount of) time trying to attack it in practise, and I don't think it's actually feasible to attack. Having said that, this patch fixes the vulnerability by including a `secure_compare` function that takes constant time, even with different length inputs.[0]

There's also some sample code at https://gist.github.com/daxtens/a3a59f163f08f9b447bb - quick demo that will demonstrate both the theoretical vulnerability and the fact that the secure comparison function isn't vulnerable to it.

[0] Different length strings are kind of not perfect, although it's unlikely to be an issue. It's still the case that `secure_compare(very short string, very long string)` is distinguishable from `secure_compare(very long string, very long string)`, for example. The function is also asymmetric in it's arguments: unpacking one of the arguments to bytes is super slow because you get a huge array of objects. It's a downside of using such a high-level, everything-is-an-object interpreted language. At any rate it leaks less information, and the information is less exploitable.