Question

Secure Cookies not working, despite successful HTTPS connection

Posted May 14, 2021 198 views
NginxNode.jsDockerLet's Encrypt

Hi :)

I am trying to debug my express server. I have a reverse proxy set up with NGINX and Let’s Encrypt. I am able to successfully connect using HTTPS. I get the green lock and it says ‘Connection secure’.

I am using session-based auth with express-sessions. My cookies work perfectly when the secure flag is not set. The moment I set secure to true my cookies gets rejected. Not sure why this is, I have trust-proxy set up and my connection is secure. Any ideas?

Here is a sample of the logs I am getting:
“Some cookies are misusing the recommended “SameSite“ attribute”
“Cookie “sid” has been rejected because it is already expired.”

And here is my cookie setup:

if (process.env.NODE_ENV === "production") {
  app.set('trust proxy', 1)
}

app.use(
  session({
    name: "sid",
    resave: false,
    saveUninitialized: false,
    secret: process.env.SESSION_SECRET,
    cookie: {
      domain: process.env.BASE_URL,
      path: "/",
      sameSite: "strict",
      secure: process.env.NODE_ENV === "production" ? true : false,
      maxAge: 1000 * 60 * 60 * 2, // 2 hours
    },
    store: MongoStore.create({
      client: db.getClient(),
      ttl: 60 * 60 * 2, // 2 hours, in seconds
      touchAfter: 24 * 3600, // time period in seconds
      crypto: {
        secret: process.env.STORE_SECRET,
      },
    }),
  })
);

Like I mentioned, no logs or errors once I take out secure (i.e. if I don’t see my NODE_ENV to “production”)

Thank you in advance.

edited by bobbyiliev
2 comments
  • I realized that the logs are the result of my middleware rejecting the cookies and clearing them. The issue appears to be express-sessions not setting the req.session or the session cookie (named sid). I have another cookie set with express’ native res.cookie method and it works just fine, even though it also has secure: true and the same domain, path, etc. as my express-sessions cookie. Is there some additional configuration I am missing with setting secure to true with express sessions?

  • Here are my logs from express-sessions:
    app1 | Fri, 14 May 2021 20:30:29 GMT express-session no SID sent, generating session
    app
    1 | Fri, 14 May 2021 20:30:29 GMT express-session saving orpPtmM2ASOpDt3Cf7nuNdLowtwAUNib
    app1 | Fri, 14 May 2021 20:30:29 GMT express-session split response
    app
    1 | Fri, 14 May 2021 20:30:29 GMT express-session not secured
    app_1 | Fri, 14 May 2021 20:30:29 GMT express-session no SID sent, generating session

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

Could you also share the Nginx server block that you are using here?

I could suggest adding the following to your reverse proxy configuration:

proxy_set_header X-Forwarded-Proto $scheme;

Regards,
Bobby