Python Flask Missing Sessions and Cookies first time reaching / using reverse Proxy (Treafik)

Viewed 16

If the code comes from somewhere else in the case http://domain.localhost it can't find the cookies and sessions under /, but after the detour via /callback it can find them. The traffic goes through traefik. It has been tested in different browsers. I use Flask 2.2.2 but have also shown other versions. In the browser the cookies are at all times. For 2-3 days I try to find the error

from datetime import timedelta
import logging
from flask import Flask, request, redirect, url_for, session, make_response
import requests
app = Flask(__name__)
app.permanent_session_lifetime = timedelta(minutes=5)
@app.route("/callback/")
def callback():
        cookie = request.cookies.get('token')
        print(f'Entrypoint {request.headers.get("Host")} {session} {cookie}')
        session.permanent = True
        resp=make_response(redirect(url_for("home")))
        resp.set_cookie(key="token", value="TestCookie")
        if "token" not in session:
            session["token"] = "No"
        else:
            session["token"]=f'{session["token"]}1'
        session.modified = True
        print("Redirect to Entrypoint")
        return resp


@app.route("/", methods=['POST','GET'])
def home():
    print(f'Entrypoint {request.headers.get("Host")} {session} {request.cookies.get("token")} WHY IS THIS EMPTY AFTER LEAVING PAGE')
    if request.headers.get("X-Forwarded-Host") is None:
        session.modified = True
        print("leaving page to http://domain.localhost")
        return redirect(f"http://domain.localhost")
    print("Redirect to Callback")
    return redirect(url_for("callback"))


if __name__ == '__main__':
    #logging
    filename = 'logfile.log'
    logging.basicConfig(handlers=[
        logging.FileHandler('logfile.log'),
        logging.StreamHandler()
    ], encoding='utf-8', level=logging.DEBUG, format='%(name)s - %(levelname)s : %(asctime)s - %(message)s')
    logging.getLogger('werkzeug').setLevel(logging.ERROR)
    # logging.getLogger('werkzeug').setLevel(logging.ERROR)
    app.config['SESSION_REFRESH_EACH_REQUEST'] = False
    app.secret_key = "123"
    app.run(port=8888, host='0.0.0.0', debug=True)

Output

Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> None WHY IS THIS EMPTY AFTER LEAVING PAGE
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> TestCookie
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No'}> TestCookie WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
**Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> None WHY IS THIS EMPTY AFTER LEAVING PAG
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No'}> TestCookie
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1'}> TestCookie WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> None WHY IS THIS EMPTY AFTER LEAVING PAGE
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1'}> TestCookie
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11'}> TestCookie WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
...
0 Answers
Related