Spaces:
Paused
Paused
Alessandro Piana
commited on
Commit
·
39f2499
1
Parent(s):
270a2b4
dockerfile con logging 23
Browse files- app.py +46 -1
- auth.py +3 -1
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from flask import Flask, render_template, redirect, url_for, flash
|
|
| 11 |
from flask_login import LoginManager, current_user
|
| 12 |
import logging
|
| 13 |
from werkzeug.middleware.proxy_fix import ProxyFix # NUOVA RIGA
|
|
|
|
| 14 |
|
| 15 |
# Configure logging
|
| 16 |
logging.basicConfig(
|
|
@@ -40,7 +41,19 @@ app.config['SESSION_COOKIE_SECURE'] = True
|
|
| 40 |
app.config['REMEMBER_COOKIE_SECURE'] = True # Per "remember me"
|
| 41 |
app.config['SESSION_COOKIE_HTTPONLY'] = True
|
| 42 |
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # Bilancia sicurezza e redirect
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Disable caching for static files in debug mode
|
| 46 |
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
|
@@ -179,7 +192,39 @@ def add_header(response):
|
|
| 179 |
response.headers['Pragma'] = 'no-cache'
|
| 180 |
response.headers['Expires'] = '-1'
|
| 181 |
return response
|
|
|
|
|
|
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
@app.route('/')
|
| 185 |
def index():
|
|
|
|
| 11 |
from flask_login import LoginManager, current_user
|
| 12 |
import logging
|
| 13 |
from werkzeug.middleware.proxy_fix import ProxyFix # NUOVA RIGA
|
| 14 |
+
from flask_cors import CORS
|
| 15 |
|
| 16 |
# Configure logging
|
| 17 |
logging.basicConfig(
|
|
|
|
| 41 |
app.config['REMEMBER_COOKIE_SECURE'] = True # Per "remember me"
|
| 42 |
app.config['SESSION_COOKIE_HTTPONLY'] = True
|
| 43 |
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # Bilancia sicurezza e redirect
|
| 44 |
+
# --- INIZIO BLOCCO CORS (AGGIUNGI QUI) ---
|
| 45 |
+
|
| 46 |
+
# Inizializza CORS con supporto ai cookie
|
| 47 |
+
CORS(
|
| 48 |
+
app,
|
| 49 |
+
supports_credentials=True,
|
| 50 |
+
origins=["*"], # In produzione, sostituisci con il tuo dominio HF: "https://tuo-nome.hf.space"
|
| 51 |
+
allow_headers=["Content-Type", "Authorization", "X-Requested-With"],
|
| 52 |
+
expose_headers=["Set-Cookie"]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
logger.info("--- CORS: Configurato con supports_credentials=True")
|
| 56 |
+
# --- FINE BLOCCO CORS ---
|
| 57 |
|
| 58 |
# Disable caching for static files in debug mode
|
| 59 |
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
|
|
|
| 192 |
response.headers['Pragma'] = 'no-cache'
|
| 193 |
response.headers['Expires'] = '-1'
|
| 194 |
return response
|
| 195 |
+
# --- INIZIO BLOCCO FORZA SET-COOKIE (AGGIUNGI QUI) ---
|
| 196 |
+
from flask.sessions import SecureCookieSessionInterface
|
| 197 |
|
| 198 |
+
@app.after_request
|
| 199 |
+
def force_session_cookie(response):
|
| 200 |
+
"""
|
| 201 |
+
Forza l'invio del cookie di sessione con attributi compatibili con HTTPS e proxy HF.
|
| 202 |
+
Necessario perché Flask non imposta automaticamente SameSite=None su HTTPS.
|
| 203 |
+
"""
|
| 204 |
+
if request.path.startswith('/auth/login') and response.status_code == 302:
|
| 205 |
+
# Solo dopo un login riuscito (redirect 302)
|
| 206 |
+
serializer = SecureCookieSessionInterface().get_signing_serializer(app)
|
| 207 |
+
if serializer is None:
|
| 208 |
+
logger.error("--- FORCE_COOKIE: Impossibile serializzare la sessione (SECRET_KEY mancante?)")
|
| 209 |
+
return response
|
| 210 |
+
|
| 211 |
+
session_data = dict(session)
|
| 212 |
+
if '_user_id' in session_data:
|
| 213 |
+
cookie_value = serializer.dumps(session_data)
|
| 214 |
+
response.set_cookie(
|
| 215 |
+
'session',
|
| 216 |
+
value=cookie_value,
|
| 217 |
+
secure=True,
|
| 218 |
+
httponly=True,
|
| 219 |
+
samesite='None', # CRUCIALE per HF Spaces (proxy cross-origin)
|
| 220 |
+
path='/',
|
| 221 |
+
max_age=60*60*24*7 # 1 settimana
|
| 222 |
+
)
|
| 223 |
+
logger.info("--- FORCE_COOKIE: Cookie sessione forzato con SameSite=None; Secure")
|
| 224 |
+
else:
|
| 225 |
+
logger.warning("--- FORCE_COOKIE: Nessun _user_id nella sessione, cookie non impostato")
|
| 226 |
+
return response
|
| 227 |
+
# --- FINE BLOCCO FORZA SET-COOKIE ---
|
| 228 |
|
| 229 |
@app.route('/')
|
| 230 |
def index():
|
auth.py
CHANGED
|
@@ -121,7 +121,9 @@ def login():
|
|
| 121 |
|
| 122 |
# Chiamata a Flask-Login
|
| 123 |
login_user(user, remember=remember)
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
logger.info(f"--- AUTH/LOGIN: Funzione login_user() completata. Reindirizzamento alla chat...")
|
| 126 |
|
| 127 |
# Redirect alla chat (o alla pagina 'next')
|
|
|
|
| 121 |
|
| 122 |
# Chiamata a Flask-Login
|
| 123 |
login_user(user, remember=remember)
|
| 124 |
+
# AGGIUNGI QUESTA RIGA QUI
|
| 125 |
+
from flask import session
|
| 126 |
+
logger.info(f"--- AUTH/LOGIN: login_user() completata. _user_id nella sessione: {session.get('_user_id')}")
|
| 127 |
logger.info(f"--- AUTH/LOGIN: Funzione login_user() completata. Reindirizzamento alla chat...")
|
| 128 |
|
| 129 |
# Redirect alla chat (o alla pagina 'next')
|
requirements.txt
CHANGED
|
@@ -11,4 +11,5 @@ bitsandbytes
|
|
| 11 |
sentencepiece
|
| 12 |
Jinja2
|
| 13 |
gunicorn
|
| 14 |
-
Flask-Session
|
|
|
|
|
|
| 11 |
sentencepiece
|
| 12 |
Jinja2
|
| 13 |
gunicorn
|
| 14 |
+
Flask-Session
|
| 15 |
+
Flask-CORS
|