kindohm
kindohm

Reputation: 1600

set secure cookie in sails.js app

What is the way to configure sails.js to set secure cookies? We are using redis to persist session state. The sails.js prescribed way (rather than some Express middleware option) is desired. Ultimately, I want the "secure" column in the Chrome cookies view to be checked for the app's cookie:

enter image description here

In the docs, there is no explicit mention of how to do this:

http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.session.html

There is an ssl config option, but deploying the app with ssl: true did not produce the desired result:

module.exports.session = {
  ...
  ssl: true
  ...
}

The ssl option isn't documented either, but I assume it has something to do with signing cookies instead.

edit: in the screen shot, I'm serving from localhost without HTTPS, but this app is being served from a production server using HTTPS and the same behavior is observed

Upvotes: 3

Views: 4512

Answers (3)

Acheme Paul
Acheme Paul

Reputation: 1275

You can set signed cookies like so

Adding a signed cookie named "chocolatechip" with value "Yummy:

res.cookie('chocolatechip', 'Yummy', {signed:true});

Retrieving the cookie:

req.signedCookies.chocolatechip; //"Yummy"

check out the sails Documentation

Upvotes: 0

Ofer Herman
Ofer Herman

Reputation: 3068

Sails uses express.session to handle session cookies, therefore you can enable secure cookies by setting cookie: { secure: true } in config/session.js

You need to use HTTPS for express to set the cookie

it requires an https-enabled website, i.e., HTTPS is necessary for secure cookies. If secure is set, and you access your site over HTTP, the cookie will not be set.

If you are behind a proxy that does SSL termination on behalf of your web server enable express trust proxy option by adding the following middleware in config/http.js

    module.exports.http = {
      customMiddleware: function(app) {
        app.enable('trust proxy');
      }
    }; 

Upvotes: 6

rdegges
rdegges

Reputation: 33854

It appears that there is not a way to do this currently. If you look at the sails.js session implementation here (https://github.com/balderdashy/sails/blob/98522d0bc5df5e6bc30b4dc35708ae71cf4625e2/lib/hooks/session/index.js) you'll see that there is, in fact, no secure-mode stuff whatsoever :(

Since sails is using their own session store implementation, and not piggybacking off of node-client-sessions or express-sessions, the only way to solve this (I think) would be to submit a PR to the sails people.

Sorry!

Upvotes: 0

Related Questions