flask icon indicating copy to clipboard operation
flask copied to clipboard

Flask session cookie secure flag is not getting set.

Open Set4now opened this issue 5 years ago • 8 comments

Expected Behavior

Flask session cookie secure flag is not getting set. One chrome developer tools, i see in the cookie sesction. the cookie name "session" is not have secure colum checked.

app.config.update(
    SESSION_COOKIE_SECURE=True, - -> This one
    DEBUG=True,
    REMEMBER_COOKIE_SECURE=True,
)

Actual Behavior

Paste the full traceback if there was an exception.

Environment

  • Python version: 3.6
  • Flask version: 1.x
  • Werkzeug version:

Set4now avatar Dec 13 '20 17:12 Set4now

As the docs for this configuration variable said:

The application must be served over HTTPS for this to make sense.

You should test this configuration variable on an HTTPS server.

greyli avatar Dec 16 '20 05:12 greyli

Yes, it is serving over HTTPS, Is it possible to add the secure flag to the default flask session cookie, i tried session_cookie_secure to True , but no luck.. however when i add the secure to set cookie for any other cookies, it works verified the same in browser.. but the flask default session (cookie) isn’t getting the flag

Set4now avatar Dec 16 '20 06:12 Set4now

There may be something wrong with your other code. I just deployed a minimal application here with the following code:

from flask import Flask, session

app = Flask(__name__)

app.config.update(
    SECRET_KEY='test',
    SESSION_COOKIE_SECURE=True,
)

@app.route('/')
def hello():
    session['name'] = 'grey'
    return 'Hello, world!'

The configuration variable works as expected.

greyli avatar Dec 16 '20 06:12 greyli

I think i was not clear enough. If i set any manually key into session , it works fine with the flag. what am trying to know is the default session that flask provides ( which is basically a cookied stored in browser) hashed with the secret key. i can see that in the browser console in under cookies section. a cookie names "session" encoded So can we set this cookie flag as secure.

Set4now avatar Dec 16 '20 08:12 Set4now

session eyJjc3JmX3Rva2VuIjoiNzE2MWQ5MjA3N2Q1ZTM5NGY3N2JhYjZhMjFhY2ExNzgwNjU2MzNiNCJ9.X7e9fQ.QbdNNUKo363HMz8BH9_p2Bcyu5o sumdeb-ieewebportal.anka.corpee.example.com / Session 118 ✓     Medium

Set4now avatar Dec 16 '20 08:12 Set4now

Flask won't create the session cookie if you didn't set any value in flask.session. Besides, If you set SESSION_COOKIE_SECURE to True, it will also not create the session cookie when the connection is insecure (http://). You may need to remove your old cookie in browser before test.

Since I can confirm this is not a issue about Flask itself, if you have any further question, please use Stack Overflow or Pallets Discord's #questions channel.

greyli avatar Dec 17 '20 01:12 greyli

Its a https site, even in the app home page, i don't have any manual cookie. Yes am using flask-wtf for csrf, that is getting set in the flask session , Okay i will check in stack overflow.

Set4now avatar Dec 17 '20 05:12 Set4now

I was to fix this issue. thanks for your support.

Set4now avatar Dec 23 '20 07:12 Set4now