“shubhamdhamal” commited on
Commit
7362a57
·
1 Parent(s): cd7924e

Initialize CSRFProtect and ProxyFix for HF Spaces

Browse files
Files changed (1) hide show
  1. web_app/__init__.py +7 -2
web_app/__init__.py CHANGED
@@ -6,6 +6,7 @@ from flask_sqlalchemy import SQLAlchemy
6
  from flask_login import LoginManager
7
  from flask_migrate import Migrate
8
  from flask_cors import CORS
 
9
  from config import Config
10
  from werkzeug.middleware.proxy_fix import ProxyFix
11
 
@@ -14,17 +15,21 @@ login_manager = LoginManager()
14
  login_manager.login_view = 'auth.login' # Route for @login_required
15
  login_manager.login_message_category = 'info'
16
  migrate = Migrate()
 
17
 
18
 
19
  def create_app(config_class=Config):
20
  app = Flask(__name__)
21
  app.config.from_object(config_class)
22
 
23
- # If the app is running behind a proxy (like on Render), fix the WSGI environment
24
- if os.environ.get('RENDER'):
25
  app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1,
26
  x_proto=1, x_host=1, x_prefix=1)
27
 
 
 
 
28
  # Enable CORS for all routes
29
  # This allows requests from Codespace frontend and mobile app
30
  allowed_origins = [
 
6
  from flask_login import LoginManager
7
  from flask_migrate import Migrate
8
  from flask_cors import CORS
9
+ from flask_wtf.csrf import CSRFProtect
10
  from config import Config
11
  from werkzeug.middleware.proxy_fix import ProxyFix
12
 
 
15
  login_manager.login_view = 'auth.login' # Route for @login_required
16
  login_manager.login_message_category = 'info'
17
  migrate = Migrate()
18
+ csrf = CSRFProtect()
19
 
20
 
21
  def create_app(config_class=Config):
22
  app = Flask(__name__)
23
  app.config.from_object(config_class)
24
 
25
+ # If the app is running behind a proxy (like on Render or HF Spaces), fix the WSGI environment
26
+ if os.environ.get('RENDER') or os.environ.get('SPACE_ID'):
27
  app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1,
28
  x_proto=1, x_host=1, x_prefix=1)
29
 
30
+ # Initialize CSRF protection
31
+ csrf.init_app(app)
32
+
33
  # Enable CORS for all routes
34
  # This allows requests from Codespace frontend and mobile app
35
  allowed_origins = [