flask-session icon indicating copy to clipboard operation
flask-session copied to clipboard

The session is unavailable because no secret key was set.

Open raucci2000 opened this issue 5 years ago • 6 comments

Hi guys! I'm trying to use flask-session, but something happening.

ERROR MESSAGE: RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

MY CODE: from flask import request, url_for, Flask, Response, session from flask_session import Session import os

app = Flask(name) SESSION_TYPE='redis' PERMANENT_SESSION_LIFETIME=1800

app.config.update( SECRET_KEY=os.urandom(24) )

app.config.from_object(name) Session(app) app.run()

TESTING SESSION: session["key"]="test 123"

raucci2000 avatar Oct 14 '19 12:10 raucci2000

Try this snippet

from flask import Flask, session
from flask_session import Session
import os

app = Flask(__name__)
SESSION_TYPE = "redis"
PERMANENT_SESSION_LIFETIME = 1800

app.config.update(SECRET_KEY=os.urandom(24))

app.config.from_object(__name__)
Session(app)

if __name__ == "__main__":
    with app.test_request_context("/"):
        session["key"] = "value"

you can run the script directly, but I can't reproduce the issue as you described.

frostming avatar Oct 19 '19 13:10 frostming

Have the same problem. init.py of app

app = Flask(name, static_folder='./static/') app.config.from_pyfile('../allium.cfg') app.config.update(SECRET_KEY=app.secret_key) Session(app)

allium.cfg

import random,string SESSTION_TYPE="memcached" WTF_CSRF_ENABLED=True SECRET_KEY = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(52))

And

session['masterkey']='masterkey' in Blueprint

gives:

FLASK_APP = run.py FLASK_ENV = development FLASK_DEBUG = 0 In folder /home/sginne/dev/src/Allium /home/sginne/dev/src/Allium/venv/bin/python3.7 -m flask run

  • Serving Flask app "run.py"
  • Environment: development
  • Debug mode: off ESNIOK2NVCW9BKDJ9MVXKL8DAXCA03GRAHRA517KVMKIZP6PZN86
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) no master key [2020-02-12 20:32:59,990] ERROR in app: Exception on /admin [GET] Traceback (most recent call last): File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functionsrule.endpoint File "/home/sginne/dev/src/Allium/app/routes/admin.py", line 9, in admin session['masterkey']='masterkey' File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/werkzeug/local.py", line 350, in setitem self._get_current_object()[key] = value File "/home/sginne/dev/src/Allium/venv/lib64/python3.7/site-packages/flask/sessions.py", line 103, in _fail "The session is unavailable because no secret " RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. 127.0.0.1 - - [12/Feb/2020 20:32:59] "GET /admin HTTP/1.1" 500 -

?

sginne avatar Feb 12 '20 18:02 sginne

Got the same error even SECRET_KEY was set

psdon avatar Jul 16 '20 10:07 psdon

For me it works when you add:

app.config['SESSION_TYPE'] = 'filesystem'

Anyways this project seems dead , and the forks of this project are also dead. Any good alternatives to this library?

Armster15 avatar Feb 21 '21 18:02 Armster15

For me it works when you add:

app.config['SESSION_TYPE'] = 'filesystem'

Anyways this project seems dead , and the forks of this project are also dead. Any good alternatives to this library?

If it is complete and works as expected, you can still use it?

tf42src avatar Jul 22 '22 23:07 tf42src

I am not using even a session in my Flask application but I need to define the code below to get the flash message on my HTML homepage. app.secret_key="anystringhere"

otherwise, I get the message below: The session is unavailable because no secret key was set.

Do I really need to define a secret key under this situation?

cgironda avatar Aug 09 '22 07:08 cgironda

I have the same problem, many times ago I wrote a program that used session and I didn't need to set SECRET_KEY and everything worked fine, now even if I set SECRET_KEY it does not work and throw exception for every request. I tried all different ways to configure, even I used debugger to trace SECRET_KEY and everything seemed OK, it was there! but it keep saying that SECRET_KEY is not set.

b-simjoo avatar Dec 04 '22 09:12 b-simjoo

This should be fixed in release 0.5.0, coming soon.

christopherpickering avatar May 11 '23 16:05 christopherpickering

This should be fixed in release 0.5.0, coming soon.

The change in release 0.5.0 does not seem to be relevant to this issue. That being said, it is fine to close it. People can always reopen it if necessary.

rayluo avatar May 22 '23 05:05 rayluo

from flask_session import Session from flask import Flask, render_template, request, jsonify,session,redirect

app = Flask(name) app.secret_key = '_5#y2L"F4Q8z\n\xec]/' app.config["SESSION_PERMANENT"] = True app.config["PERMANENT_SESSION_LIFETIME"] = 300 app.config['SESSION_TYPE'] = 'filesystem' Session(app)

@app.route("/") def index_get(): return render_template("base.html")

after adding the session_type it worked

AMAN1620 avatar Mar 08 '24 11:03 AMAN1620

@AMAN1620 Which version are you using? In 0.6.0 and up you shouldn't need to set a secret key unless you are using SESSION_USE_SIGNER

Lxstr avatar Mar 08 '24 11:03 Lxstr