Update start.sh
Browse files
start.sh
CHANGED
|
@@ -19,33 +19,41 @@ nginx
|
|
| 19 |
# 3. Start API (port 5001)
|
| 20 |
echo "🧠 Starting API..."
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
API_PID=$!
|
| 50 |
|
| 51 |
# 4. Start Web (port 3000) with correct API URLs
|
|
|
|
| 19 |
# 3. Start API (port 5001)
|
| 20 |
echo "🧠 Starting API..."
|
| 21 |
|
| 22 |
+
# SAFE MIDDLEWARE PATCH: Create a wrapper that modifies cookies in responses
|
| 23 |
+
cat << 'MIDDLEWARE_EOF' > /app/api/cookie_patch_middleware.py
|
| 24 |
+
"""WSGI Middleware to fix cookie HttpOnly for CSRF tokens on HF Spaces"""
|
| 25 |
+
from app import app as flask_app
|
| 26 |
+
|
| 27 |
+
class CookiePatchMiddleware:
|
| 28 |
+
def __init__(self, app):
|
| 29 |
+
self.app = app
|
| 30 |
+
|
| 31 |
+
def __call__(self, environ, start_response):
|
| 32 |
+
def custom_start_response(status, headers, exc_info=None):
|
| 33 |
+
new_headers = []
|
| 34 |
+
for name, value in headers:
|
| 35 |
+
if name.lower() == 'set-cookie' and 'csrf' in value.lower():
|
| 36 |
+
# Remove HttpOnly from CSRF cookies
|
| 37 |
+
value = value.replace('; HttpOnly', '').replace(';HttpOnly', '')
|
| 38 |
+
# Ensure SameSite=None and Secure
|
| 39 |
+
if 'SameSite' not in value:
|
| 40 |
+
value += '; SameSite=None'
|
| 41 |
+
if 'Secure' not in value:
|
| 42 |
+
value += '; Secure'
|
| 43 |
+
print(f"🔧 MIDDLEWARE PATCHED CSRF COOKIE: {value[:50]}...")
|
| 44 |
+
new_headers.append((name, value))
|
| 45 |
+
return start_response(status, new_headers, exc_info)
|
| 46 |
+
return self.app(environ, custom_start_response)
|
| 47 |
+
|
| 48 |
+
# Wrap the Flask app with our middleware
|
| 49 |
+
app = CookiePatchMiddleware(flask_app)
|
| 50 |
+
print("✅ COOKIE PATCH MIDDLEWARE LOADED!")
|
| 51 |
+
MIDDLEWARE_EOF
|
| 52 |
+
|
| 53 |
+
echo "🔧 Cookie patch middleware created!"
|
| 54 |
+
|
| 55 |
+
# Run Gunicorn with the middleware wrapper
|
| 56 |
+
gunicorn --bind 127.0.0.1:5001 --workers 1 --timeout 360 --forwarded-allow-ips='*' cookie_patch_middleware:app &
|
| 57 |
API_PID=$!
|
| 58 |
|
| 59 |
# 4. Start Web (port 3000) with correct API URLs
|