rack/rack — pull request #272 by infertux (2011-11-20 12:07:46+00:00)
Title: Drop invalid cookies

Here's a quite common scenario that throws an exception in Rack then crashes Rails:

1. Set up a Google Analytics campaign containing the % character, e.g. `Example (Not 100% Escaped)`.

2. FeedBurner doesn't escape correctly the % character (it should replace it with %25) so we end up with an URL like:
`https://example.com/?utm_source=feedburner&utm_medium=twitter&utm_campaign=Feed%3A+Example+%28Not+100%+Escaped%29`

3. Whenever someone clicks this URL, it sets the GA's `__utmz` cookie to `...utmcsr=feedburner|utmccn=Feed:%20Example%20(Not%20100%%20Escaped)|utmcmd=twitter`.

4. This incorrectly escaped poison cookie is passed to Rack through Rails.

5. Rack can't decode it and raises an exception which is not caught by Rails then crashes the whole application rendering a nice HTTP 500 :/.

So we can patch either Rails or Rack. I don't see any point at throwing an exception in this case since the upper application (e.g. Rails) won't be able to use this cookie value anyway. So this patch simply drops incorrectly escaped cookie pairs (see added specs in `test/spec_utils.rb`).

Maybe a better compromise would be to use Rack's logger and display some kind of warning when Rack is unable to decode.

Related discussions: #225 & https://github.com/rails/rails/issues/2622.