# Rails with Rack 1.4.1 and malformed cookies; advice needed

**URL:** <https://www.ruby-forum.com/t/rails-with-rack-1-4-1-and-malformed-cookies-advice-needed/224465>\
**Category:** Rails\
**Created:** [November 13, 2012, 12:07am UTC](https://www.ruby-forum.com/t/rails-with-rack-1-4-1-and-malformed-cookies-advice-needed/224465 "2012-11-13T00:07:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 13, 2012, 12:07am UTC](https://www.ruby-forum.com/t/rails-with-rack-1-4-1-and-malformed-cookies-advice-needed/224465/1 "2012-11-13T00:07:14Z")

</div>

I’m looking for advice as to the best way to handle a bug in Rack 1.4.1.  
The bug has been fixed upstream, but Rack ~\> 1.4.0 is the requirement  
for  
Rails stable right now, which will mean 1.4.1. Rack HEAD causes a  
warning  
message to be displayed when using the cookie session store, which is  
something being actively worked on.

The rack bug dealing with malformed cookies:

> <https://github.com/rack/rack/issues/386>
>
> When working on multiple rails projects I kept getting the following error:
> 
> NoM…ethodError
> cannot parse Cookie header: undefined method \`size' for nil:NilClass
> 
> I could temporarily fix by deleting my browser cookies. However, not until I pointed to master branch of Rack was I able to make the problem go away for good.
> 
> I posted this solution on StackOverflow, and it seems it's fixing it for others as well.
> http://stackoverflow.com/questions/9054604/rails-3-2-1-cannot-parse-cookie-header-undefined-method-size-for-nilnilcla

Talk about Rack HEAD and the rails cookie store:

> <https://github.com/rails/rails/issues/7372>
>
> Rack::Session::Cookie accepts a :secret option in its initializer. The initializ…er stores that secret in an instance variable and uses it in the \`#set\_session\` method to HMAC sign the data.
> 
> Rack HEAD added security warnings for when the Rack::Session::Cookie middleware is initialized without a secret. 
> 
> This is causing the warning to show up because of the way Rails uses Rack::Session::Cookie:
> 
> Rails uses a subclass of Rack::Session::Cookie called ActionDispatch::Session::CookieStore that overwrites \`#set\_session\` and \`#set\_cookie\` to do its own signed cookie implementation using ActionDispatch::Cookies::SignedCookieJar (using Rack env \`"action\_dispatch.secret\_token"\` instead of the \`@secret\` instance variable). Since Rails doesn't need the \`@secret\` instance variable, it initializes ActionDispatch::Session::CookieStore without providing a :secret option.
> 
> To avoid the warning, we should just not use Rack::Session::Cookie. We're overriding a lot of the functionality for Rack::Session::Cookie anyway, to the point that its most important functionality (cookie signing) is not being used at all.

ActionDispatch::Session::CookieStore not subclassing  
Rack::Session::Cookie  
seems like a great approach. So, both of these issues are being worked  
on  
in great ways, or are already solved.

Our rails apps are mostly installed on sub-domains of a large public  
university domain. We offer an ezproxy service which makes licensed  
campus  
services available to users off-campus. One of those services  
([lynda.com](http://lynda.com))  
sets a malformed cookie (ending in ,😉 which triggers the bug in Rack  
1.4.1  
detailed in the rack issue #386. Because this service can sometimes run  
through a proxy service on our domain, and the vendor specifies this  
cookie  
as a root domain level cookie, the cookie gets set and is then sent for  
all  
apps on the domain, including our rails apps running on sub-domains.  
This  
situation is admittedly not ideal. Neither setting a domain level cookie  
for this purpose, or setting a malformed cookie, are good practices.  
But,  
they are practices I have absolutely no control over. Also, being part  
of  
such a large eco-system means there are numerous cookie practices I have  
no  
control over as an application developer.

The malformed cookie bug essentially means, any site on our domain can  
very  
easily cause my app to throw a 500 application wide, by simply setting a  
domain level malformed cookie. We have sites running on our domain  
written  
in every language and web framework I’ve ever heard of. I’ve been trying  
to  
advocate Rails usage, because I love rails, and am wanting to move some  
fairly important sites (at least to us) to Rails, so I’m trying to  
figure  
out the most reliable way to patch this for production sites using (as  
of  
this writing) rails 3.2.9.

The two options I’ve considered are:

1. Forking Rack, branching from the 1.4.1 tag, and cherry picking the  
commit that got merged in from issue 386, and putting gem ‘rack’, :git  
=\>  
‘my\_repo’, :branch =\> ‘1.4.1-backports’ in my Gemfile.
2. Pointing my Gemfile to rack’s git repo, using some branch or commit  
on  
Master, and either dealing with the rails issue #7372 until it’s fixed,  
or  
passing a :secret into the cookie session store, even though I know  
action  
dispatch wont use it, but will rather use  
“action\_dispatch.secret\_token.”  
This is only a problem for apps using the cookie session store.

I’ve opted for option #1 for now.

The bigger issue for me is that sites created by installing rails as of  
today, with gem install rails, will create rails sites that I know will  
be  
broken for users that simply view [lynda.com](http://lynda.com) videos using our ezproxy  
service, which isn’t all that uncommon. I discovered this by asking 5  
people to test a new application we’re getting ready to launch. One of  
the  
five told me it threw a 500 error, and caused me to start digging into  
all  
of this. As someone who’s trying to advocate rails usage, it may be a  
harder sell now that I feel I need to tell people that gem install rails  
will not create a fully viable environment for us. I navigated that path  
today, got the cookie set, and now 3 of the sites I’ve launched recently  
throw 500 for me.

Sorry for the long message. I’m mostly looking for advice regarding the  
safest, most reliable, and least obtrusive way of getting a patched  
version  
of Rack into my rails 3.2.9 projects. I’ll have to figure out a way to  
communicate this necessity to users interested in getting into Rails.

Thanks,

- Bryan
