Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid cookie value - cookie has spaces #2996

Closed
danielb2 opened this issue Dec 31, 2015 · 12 comments · Fixed by #3113
Closed

Invalid cookie value - cookie has spaces #2996

danielb2 opened this issue Dec 31, 2015 · 12 comments · Fixed by #3113
Assignees
Labels
non issue Issue is not a problem or requires changes

Comments

@danielb2
Copy link
Contributor

ref #2513

I came across the issue above with a website I'm migrating that has improperly escaped cookies with spaces in them. When trying to access a hapi server with those cookies, hapi fails with the error

{"statusCode":400,"error":"Bad Request","message":"Invalid cookie value"}

Because it's using legacy code, the cookies are stored on the user client.

Thoughts on how to handle this?

@hueniverse
Copy link
Contributor

Did you try turning strict cookie parsing off?

@danielb2
Copy link
Contributor Author

danielb2 commented Jan 1, 2016

Are you talking about disabling parsing for cookies altogether, or is there one specifically to parse it still, just not in a "strict" way ? The latter I can't find

@hueniverse
Copy link
Contributor

@hueniverse hueniverse added the non issue Issue is not a problem or requires changes label Jan 4, 2016
@hueniverse hueniverse self-assigned this Jan 4, 2016
@danielb2
Copy link
Contributor Author

danielb2 commented Jan 5, 2016

I tried this before asking, but looks like I had missed one cookie name. 😓

@danielb2
Copy link
Contributor Author

danielb2 commented Jan 5, 2016

Oh, and thanks

@danielb2
Copy link
Contributor Author

danielb2 commented Apr 6, 2016

This came up again in a different context.

I tried using server.state('SESSIONID', { strictHeader: false }); however, that fails.

The only thing that worked is setting the config to option to state: { parse: false }

I looked at https://github.com/hapijs/hapi/blob/master/lib/connection.js#L78 but it looks like the server.state has an impact here per cookie.

What am I missing this time? Is it just not possible to configure per cookie?

@hueniverse
Copy link
Contributor

How is the cookie invalid?

@danielb2
Copy link
Contributor Author

danielb2 commented Apr 7, 2016

 const headers = {
     Cookie: 'SESSIONID=4/7/2016 18:31:45 PM'
 };
 server.inject({ method: 'GET', url: url, headers: headers }, (res) => {

this will cause the error. removing the spaces causes it to work

@hueniverse
Copy link
Contributor

Can you PR a test case showing how setting a single cookie config fails?

@danielb2
Copy link
Contributor Author

danielb2 commented Apr 8, 2016

going to close this either way

@carraher
Copy link

rather than setting parse:false I have worked around this by setting

      "routes":
            "state" : {
              "parse": true,
              "failAction": "ignore"
            }
      }

@tombh
Copy link

tombh commented Jul 27, 2017

I just want to explain why @carraher's solution above worked for me. I'm using the hapi-cookie-auth plugin, which doesn't support the ignoreErrors: true option. I was in the situation of upgrading a site that had set, what that plugin would consider, invalid cookies. So I didn't want to create a single server.state() line just to ignore the errors on that one bad cookie name. That's why @carraher's more generic approach worked for me. Thanks.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
non issue Issue is not a problem or requires changes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants