1

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. I am using Flask 2.2.2 but it has already been tested under all v2 and the traffic goes through treafik. I have tested almost all browsers and none of them work although the cookies are stored under /.

from datetime import timedelta
import logging
from flask import Flask, request, redirect, url_for, session, make_response

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="host", value="host", domain=f'request.headers.get("Host")')
        resp.set_cookie(key="proto", value="proto", domain=f'request.headers.get("Host")')
        resp.set_cookie(key="token", value="TestCookie", domain=f'request.headers.get("Host")')
        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} 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")
    host = ""
    bol=False
    if request.headers.get("X-Forwarded-Host") is not None:
        host = str(request.headers.get("X-Forwarded-Host"))
        proto = str(request.headers.get("X-Forwarded-Proto"))
        bol=True
    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

 * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
 * Serving Flask app 'test'
 * Debug mode: on
 * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No'}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1'}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11'}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No111'}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1111'}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost
***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE***
Redirect to Callback
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11111'}> None
Redirect to Entrypoint
Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No111111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE
leaving page to http://domain.localhost

0 Answers0