expressjs / session

Simple session middleware for Express
MIT License
6.26k stars 979 forks source link

Potential solution for Cookie options(secure) in Local HTTP Environments #982

Open darcy521 opened 6 months ago

darcy521 commented 6 months ago

From my understanding, if we are running our application locally without using HTTPS and Nginx or something similar, secure will not work under the logic of express-session.

In the latest version of Chrome and Firefox HTTPS requirements are ignored when the Secure attribute is set by localhost, please see the MDN documents below: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie image Chrome permits setting the secure attribute for cookies under HTTP on localhost. However, express-session does not create cookies under HTTP, which is why cookies might not appear even if the secure and sameSite are correctly set.

I wonder if we can refine logic to align with Chrome rules, so I tried to do some 'naive' optimization by checking if application is running locally.

image image

I am aware that there may be some security concerns related to my changes (Like if there are any other Reverse Proxys running locally under production mode, cookie will still be set even though they haven't set 'x-forwarded-proto' headers). If possible, could you happen to identify these issues so I can learn from them? Thank you! :)