Spaces:
Sleeping
Sleeping
Sign-in works inside the Space page (partitioned cross-site cookie)
Browse files- Dockerfile +1 -1
- REVISION +1 -1
- app/server.py +19 -2
Dockerfile
CHANGED
|
@@ -27,7 +27,7 @@ COPY --chown=1000:1000 --from=web /web/dist app/web/dist
|
|
| 27 |
RUN chmod +x start.sh
|
| 28 |
USER 1000
|
| 29 |
ENV HOME=/home/ubuntu AEMON_SEED=/home/ubuntu/aemon/seed/history.json LEXSI_DATA=/tmp/aemon-data \
|
| 30 |
-
MAX_TURNS=45 PYTHONDONTWRITEBYTECODE=1 LD_LIBRARY_PATH=/opt/llama
|
| 31 |
# Sign-in is by username alone (a new name opens an account, seeded from AEMON_SEED); the optional secrets
|
| 32 |
# AEMON_TEST_USER and AEMON_TEST_PASSWORD still create a shared account at start
|
| 33 |
EXPOSE 7860
|
|
|
|
| 27 |
RUN chmod +x start.sh
|
| 28 |
USER 1000
|
| 29 |
ENV HOME=/home/ubuntu AEMON_SEED=/home/ubuntu/aemon/seed/history.json LEXSI_DATA=/tmp/aemon-data \
|
| 30 |
+
MAX_TURNS=45 PYTHONDONTWRITEBYTECODE=1 LD_LIBRARY_PATH=/opt/llama CROSS_SITE_COOKIE=1
|
| 31 |
# Sign-in is by username alone (a new name opens an account, seeded from AEMON_SEED); the optional secrets
|
| 32 |
# AEMON_TEST_USER and AEMON_TEST_PASSWORD still create a shared account at start
|
| 33 |
EXPOSE 7860
|
REVISION
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
|
|
|
|
| 1 |
+
165a43f
|
app/server.py
CHANGED
|
@@ -107,6 +107,21 @@ def login_page() -> Response:
|
|
| 107 |
return FileResponse(PAGE, headers=NO_CACHE) if PAGE else HTMLResponse(NO_BUILD, status_code=503)
|
| 108 |
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
@app.post('/api/login')
|
| 111 |
def login(body: SignIn) -> JSONResponse:
|
| 112 |
name = body.name.strip().lower()
|
|
@@ -127,7 +142,8 @@ def login(body: SignIn) -> JSONResponse:
|
|
| 127 |
token = secrets.token_urlsafe(32)
|
| 128 |
store.start_session(token, name, now + SESSION_SECONDS)
|
| 129 |
response = JSONResponse({'user': name})
|
| 130 |
-
response.set_cookie(COOKIE, token, max_age=SESSION_SECONDS,
|
|
|
|
| 131 |
return response
|
| 132 |
|
| 133 |
|
|
@@ -135,7 +151,8 @@ def login(body: SignIn) -> JSONResponse:
|
|
| 135 |
def logout(request: Request) -> JSONResponse:
|
| 136 |
store.end_session(request.cookies.get(COOKIE))
|
| 137 |
response = JSONResponse({'signed_out': True})
|
| 138 |
-
response.delete_cookie(COOKIE, path='/')
|
|
|
|
| 139 |
return response
|
| 140 |
|
| 141 |
|
|
|
|
| 107 |
return FileResponse(PAGE, headers=NO_CACHE) if PAGE else HTMLResponse(NO_BUILD, status_code=503)
|
| 108 |
|
| 109 |
|
| 110 |
+
def _cookie_policy() -> dict[str, Any]:
|
| 111 |
+
"""Strict and same-site by default. A hosted copy shown inside another site's page (a Hugging Face Space is framed by
|
| 112 |
+
huggingface.co) sets CROSS_SITE_COOKIE=1: a strict cookie is never sent from inside that frame, so the session is
|
| 113 |
+
a partitioned, secure cookie that only works over HTTPS and only inside that frame's site."""
|
| 114 |
+
if os.environ.get('CROSS_SITE_COOKIE') == '1':
|
| 115 |
+
return {'httponly': True, 'samesite': 'none', 'secure': True}
|
| 116 |
+
return {'httponly': True, 'samesite': 'strict', 'secure': False}
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def _partition(response: Response) -> None:
|
| 120 |
+
"""Chrome drops a third-party cookie unless it is Partitioned, which Starlette sets only on Python 3.14 and later."""
|
| 121 |
+
if os.environ.get('CROSS_SITE_COOKIE') == '1':
|
| 122 |
+
response.raw_headers = [(k, v + b'; Partitioned') if k == b'set-cookie' else (k, v) for k, v in response.raw_headers]
|
| 123 |
+
|
| 124 |
+
|
| 125 |
@app.post('/api/login')
|
| 126 |
def login(body: SignIn) -> JSONResponse:
|
| 127 |
name = body.name.strip().lower()
|
|
|
|
| 142 |
token = secrets.token_urlsafe(32)
|
| 143 |
store.start_session(token, name, now + SESSION_SECONDS)
|
| 144 |
response = JSONResponse({'user': name})
|
| 145 |
+
response.set_cookie(COOKIE, token, max_age=SESSION_SECONDS, path='/', **_cookie_policy())
|
| 146 |
+
_partition(response)
|
| 147 |
return response
|
| 148 |
|
| 149 |
|
|
|
|
| 151 |
def logout(request: Request) -> JSONResponse:
|
| 152 |
store.end_session(request.cookies.get(COOKIE))
|
| 153 |
response = JSONResponse({'signed_out': True})
|
| 154 |
+
response.delete_cookie(COOKIE, path='/', **_cookie_policy())
|
| 155 |
+
_partition(response)
|
| 156 |
return response
|
| 157 |
|
| 158 |
|