Problems with restful_authentication Plugin and Internet Explorer Cookies

This post was written by John Trupiano. Read other posts by John Trupiano.

I just ran into a fairly obscure bug. Bit me pretty good and stole an hour from me on an otherwise quiet Friday afternoon.

How the Problem Manifested Itself: Using restful_authentication, I could log in fine using Firefox and Opera, but not Internet Explorer or Safari. I figured, it’s just an HTML POST, nothing special, so what could be going wrong? I started to tail my logfile, and the session#create action was working properly. It was redirecting to a protected page, signifying that the login was successful. However, there was a second redirect occurring immediately after, sending me back to the login page. Here’s a tail of the logfile:

Processing SessionsController#create (for xxx.xxx.xxx.xxxat 2008-07-11 16:09:32) [POST]
  Session ID: 8375beba8418d2f58363b1a05ea93c79
  Parameters: {"commit"=>"Log in", "action"=>"create", "controller"=>"sessions", "password"=>"xxx.xxx.xxx", "login"=>"xxx.xxx.xxx"}
  User Load (0.000701)   SELECT * FROM `users` WHERE (`users`.`login` = 'xxx.xxx.xxx') LIMIT 1
Redirected to http://xxx.xxx.xxx/dashboard
Completed in 0.00163 (611 reqs/sec) | DB: 0.00070 (42%) | 302 Found [http://xxx.xxx.xxx/session]

Processing DashboardController#index (for xxx.xxx.xxx.xxx at 2008-07-11 16:09:32) [GET]
  Session ID: 2a01076513064cce771c062a01da5e54
  Parameters: {"action"=>"index", "controller"=>"dashboard"}
Redirected to http://xxx.xxx.xxx/session/new
Filter chain halted as [#] rendered_or_redirected.
Completed in 0.00043 (2309 reqs/sec) | DB: 0.00000 (0%) | 302 Found [http://xxx.xxx.xxx/dashboard]

Processing SessionsController#new (for xxx.xxx.xxx.xxx at 2008-07-11 16:09:32) [GET]
  Session ID: c8c497cb737dbb7a5977b76ef2a38a04
  Parameters: {"action"=>"new", "controller"=>"sessions"}
Rendering template within layouts/login
Rendering sessions/new
Completed in 0.00108 (928 reqs/sec) | Rendering: 0.00099 (92%) | DB: 0.00000 (0%) | 200 OK [http://xxx.xxx.xxx/session/new]

The first thing that caught my eye was “Filter chain halted…..”. The login_required before_filter (used by restful_authentication) was failing despite the fact that I was able to successfully authenticate in the previous POST to session#create. The next thing that caught my eye was the fact that each of the three requests above has a distinct session id. This suggested to me that there was a problem with the session cookie being set. A little bit of head scratching and several google searches later, I came upon….

The Real Problem: I was accessing the site through a domain name that contained an underscore (e.g. client_app.stagingdomain.com). Why is this a problem? Well, Internet Explorer (and I suppose Safari) rejects cookies for domains that contain an underscore. Oddly enough, I found the solution on a Passenger Forum Post.

As soon as I changed the domain name through which I was accessing my staging server, the problem disappeared.

About the author: John co-founded SmartLogic Solutions with Yair in May 2005. He is actively involved with the Ruby and Rails communities. Check out John's GitHub Projects or follow @jtrupiano on twitter

2 Responses to “Problems with restful_authentication Plugin and Internet Explorer Cookies”

  1. George Anderson Says:

    I ran into the same issue. Frustrating, to say the least. After suitably cursing IE, I came to discover underscores are verboten in domain names.

    From: http://tools.ietf.org/html/rfc1035

    [Domain names (including subdomains] must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen.

    Hyphens, yes. Underscores, no.

    After working with Ruby for a bit, you start to prefer the underscore to the hyphen. Unfortunately, that doesn’t work with domain names.

    Interestingly, using different forms on netsol.com to attempt to register a domain name with an underscore, you either break the page, or get a useful error message: “Please use only letters, numbers or dashes [-]. Do not enter spaces, periods [.] or other punctuation.”

  2. John Trupiano Says:

    Thanks for the extra link George– When you think about it, this is all really my fault. A registrar wouldn’t sell me a domain name with an _. But there’s nothing stopping me from setting up DNS for a subdomain with one…

Leave a Reply