matias-cataife commited on
Commit
9687f99
·
1 Parent(s): 62f41ca

fix: cross-site cookie + auth-status open path for HF iframe

Browse files
Files changed (2) hide show
  1. auth.py +4 -2
  2. static/app.js +1 -1
auth.py CHANGED
@@ -21,7 +21,8 @@ COOKIE_NAME = 'auth'
21
  COOKIE_MAX_AGE = 60 * 60 * 24 * 30 # 30 days
22
 
23
  # paths that bypass auth
24
- OPEN_PATHS = {'/api/login', '/healthz', '/api/docs', '/openapi.json'}
 
25
  OPEN_PREFIXES = ('/static/',)
26
 
27
  _serializer = URLSafeTimedSerializer(SESSION_SECRET, salt='auth-cookie')
@@ -68,8 +69,9 @@ def register_auth_routes(app: FastAPI):
68
  if not secrets.compare_digest(password, APP_PASSWORD):
69
  return JSONResponse(status_code=401, content={'detail': 'wrong password'})
70
  response = JSONResponse({'ok': True})
 
71
  response.set_cookie(COOKIE_NAME, _sign(), max_age=COOKIE_MAX_AGE,
72
- httponly=True, samesite='lax', secure=False)
73
  return response
74
 
75
  @app.post('/api/logout')
 
21
  COOKIE_MAX_AGE = 60 * 60 * 24 * 30 # 30 days
22
 
23
  # paths that bypass auth
24
+ OPEN_PATHS = {'/api/login', '/api/logout', '/api/auth-status',
25
+ '/healthz', '/api/docs', '/openapi.json'}
26
  OPEN_PREFIXES = ('/static/',)
27
 
28
  _serializer = URLSafeTimedSerializer(SESSION_SECRET, salt='auth-cookie')
 
69
  if not secrets.compare_digest(password, APP_PASSWORD):
70
  return JSONResponse(status_code=401, content={'detail': 'wrong password'})
71
  response = JSONResponse({'ok': True})
72
+ # SameSite=None + Secure required so the cookie survives the HF iframe (cross-site).
73
  response.set_cookie(COOKIE_NAME, _sign(), max_age=COOKIE_MAX_AGE,
74
+ httponly=True, samesite='none', secure=True)
75
  return response
76
 
77
  @app.post('/api/logout')
static/app.js CHANGED
@@ -29,7 +29,7 @@ const API = {
29
  };
30
 
31
  async function jfetch(url, opts = {}) {
32
- const o = Object.assign({ headers: { 'Content-Type': 'application/json' }, credentials: 'same-origin' }, opts);
33
  const r = await fetch(url, o);
34
  if (r.status === 401) {
35
  await showLoginModal();
 
29
  };
30
 
31
  async function jfetch(url, opts = {}) {
32
+ const o = Object.assign({ headers: { 'Content-Type': 'application/json' }, credentials: 'include' }, opts);
33
  const r = await fetch(url, o);
34
  if (r.status === 401) {
35
  await showLoginModal();