Hapi: Invalid cookie value

Created on 24 Apr 2015  路  18Comments  路  Source: hapijs/hapi

I have a blank server running with just , and it's working fine. But when i forward the port trough NAT because i have server listening inside VM and when i try to access the API on the host machine i get this:
{"statusCode":400,"error":"Bad Request","message":"Invalid cookie value"}

Any idea why is this happening, and how to fix it?

Most helpful comment

@MathieuLoutre Yep, i just added

config: {
      state: {
        parse: false, // parse and store in request.state
        failAction: 'ignore' // may also be 'ignore' or 'log'
      }
    }

to the route and it's working.
Thanks man!

All 18 comments

Looks like a validation error, at least I get similar responses when validation fails. Do you have any kind of request validation in there?

nope, just this:
server.route({
method: 'GET',
path: '/',
handler: function(req, res) {
res('Hello World!');
}
});

Just tried your code, works perfectly here. Can you post the rest of your app.js?

I've seen that before and I'm not sure what's exactly happening (although nothing as complex as port forwarding was needed, just a simple server). The server wasn't even using any cookie related thing explicitly (no cookie auth etc.). Can you try in an incognito window?
I suspect a malformed cookie has been set somehow and hapi can't deal with it.

@edimoldovan
var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 3000 });
server.route({
method: 'GET',
path: '/',
handler: function(req, res) {
res('Hello World!');
}
});

server.start(function () {
console.log('Server running at:', server.info.uri);
});

But there is no issue on the host machine, but where the port is forwarded.

@MathieuLoutre Yes, it works in incognito, do you know witch cookie is the issue?

@seemsindie no clue. But if you find out, I'm interested!

@MathieuLoutre i have another project on same domain 'localhost', and that project have two cookies, tr and tr_tkn. Can i somehow turn off cookie parsing, or if i can parse what i just want?

Hum, I'm not 100% sure as I haven't done it myself, but I'd look here: https://github.com/hapijs/hapi/blob/master/API.md#route.config.state

@MathieuLoutre Yep, i just added

config: {
      state: {
        parse: false, // parse and store in request.state
        failAction: 'ignore' // may also be 'ignore' or 'log'
      }
    }

to the route and it's working.
Thanks man!

Nice! Happy that we found a solution :)

This happens at least when there's a space in the cookie. When dealing with legacy code and migration, this can be an issue. Should hapi really fail completely here?

I just got the same error on a freshly installed Hapi with the "hello" example from the homepage. After adding the config to the route by Mathieu it worked. Not a good experience.

It still worked after deleting all my cookies for the domain (which was localhost).

thank u very much.

Wait, you have to ignore a "failAction" to make cookies work? Is there another way?

@george-norris-salesforce you can also disable the server.options.state.strictHeader option to allow malformed cookies:

const server = new Hapi.Server({
    host: '0.0.0.0',
    port: 3000,
    state: {
      strictHeader: false
    }
});

I had the same problem with a cookie value from a third party which included spaces. This fixed it.

I solved like this

server.connection({
  host: '0.0.0.0',
  port: port,
  state: {
    ignoreErrors: true
  },
  }
});

This just happened to me. It happens due to a malformed cookie. The boom error data contains the name of the cookie which is breaking the request parsing. If you delete it, the problem is fixed

@svallory In case the cookie is generated in a third-party service which you have no control, I recommend the configuration above.

Was this page helpful?
0 / 5 - 0 ratings