“shubhamdhamal” commited on
Commit
9a6b6cc
·
1 Parent(s): 6e409ef

Fix CSRF: initialize session and disable secure cookie for HF internal HTTP

Browse files
Files changed (2) hide show
  1. config.py +4 -3
  2. web_app/auth_routes.py +9 -1
config.py CHANGED
@@ -31,9 +31,10 @@ class Config:
31
  PERMANENT_SESSION_LIFETIME = 7200 # 2 hours
32
  SESSION_COOKIE_NAME = 'learning_path_session'
33
 
34
- # Production settings (HF Spaces uses HTTPS)
35
- SESSION_COOKIE_SECURE = IS_PRODUCTION # True for HTTPS, False for HTTP
36
- REMEMBER_COOKIE_SECURE = IS_PRODUCTION
 
37
  REMEMBER_COOKIE_SAMESITE = 'Lax'
38
 
39
  LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT')
 
31
  PERMANENT_SESSION_LIFETIME = 7200 # 2 hours
32
  SESSION_COOKIE_NAME = 'learning_path_session'
33
 
34
+ # HF Spaces internal traffic is HTTP even though external is HTTPS
35
+ # Setting SECURE=False allows cookies to be set over internal HTTP
36
+ SESSION_COOKIE_SECURE = False # Must be False for HF Spaces
37
+ REMEMBER_COOKIE_SECURE = False
38
  REMEMBER_COOKIE_SAMESITE = 'Lax'
39
 
40
  LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT')
web_app/auth_routes.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify
2
  from flask_login import login_user, logout_user, login_required, current_user
3
  # Assuming db and login_manager are initialized in __init__.py
4
  from web_app import db, login_manager
@@ -15,6 +15,10 @@ bp = Blueprint('auth', __name__, template_folder='templates/auth')
15
 
16
  @bp.route('/register', methods=['GET', 'POST'])
17
  def register():
 
 
 
 
18
  if current_user.is_authenticated:
19
  return redirect('/') # Redirect to React homepage
20
 
@@ -50,6 +54,10 @@ def register():
50
 
51
  @bp.route('/login', methods=['GET', 'POST'])
52
  def login():
 
 
 
 
53
  if current_user.is_authenticated:
54
  return redirect('/')
55
 
 
1
+ from flask import Blueprint, render_template, redirect, url_for, flash, request, jsonify, session
2
  from flask_login import login_user, logout_user, login_required, current_user
3
  # Assuming db and login_manager are initialized in __init__.py
4
  from web_app import db, login_manager
 
15
 
16
  @bp.route('/register', methods=['GET', 'POST'])
17
  def register():
18
+ # Ensure session is initialized for CSRF token
19
+ session.setdefault('_csrf_init', True)
20
+ session.modified = True
21
+
22
  if current_user.is_authenticated:
23
  return redirect('/') # Redirect to React homepage
24
 
 
54
 
55
  @bp.route('/login', methods=['GET', 'POST'])
56
  def login():
57
+ # Ensure session is initialized for CSRF token
58
+ session.setdefault('_csrf_init', True)
59
+ session.modified = True
60
+
61
  if current_user.is_authenticated:
62
  return redirect('/')
63