asnannp commited on
Commit
da2ada3
·
verified ·
1 Parent(s): 826d94e

fix: do not crash auth cookies if is_production is missing

Browse files
Files changed (1) hide show
  1. app/routes/auth.py +19 -4
app/routes/auth.py CHANGED
@@ -266,16 +266,31 @@ def _auth_redirect_for_user(user: User, next_path: str) -> RedirectResponse:
266
  return response
267
 
268
 
269
- def _set_media_auth_cookie(response: Response, token: str, expires_in: int) -> None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  """Allow browser media elements to authenticate without exposing a bearer token."""
271
- settings = get_settings()
272
  response.set_cookie(
273
  key=MEDIA_AUTH_COOKIE,
274
  value=token,
275
  max_age=expires_in,
276
  httponly=True,
277
- secure=settings.is_production,
278
- samesite="none" if settings.is_production else "lax",
279
  path="/generated",
280
  )
281
 
 
266
  return response
267
 
268
 
269
+ def _is_production_environment(settings: object | None = None) -> bool:
270
+ """Resolve production safely even if an older Settings build lacks is_production."""
271
+ active = settings or get_settings()
272
+ flag = getattr(active, "is_production", None)
273
+ if isinstance(flag, bool):
274
+ return flag
275
+ return str(getattr(active, "environment", "") or "").strip().casefold() == "production"
276
+
277
+
278
+ def _set_media_auth_cookie(
279
+ response: Response,
280
+ token: str,
281
+ expires_in: int,
282
+ *,
283
+ settings: object | None = None,
284
+ ) -> None:
285
  """Allow browser media elements to authenticate without exposing a bearer token."""
286
+ production = _is_production_environment(settings)
287
  response.set_cookie(
288
  key=MEDIA_AUTH_COOKIE,
289
  value=token,
290
  max_age=expires_in,
291
  httponly=True,
292
+ secure=production,
293
+ samesite="none" if production else "lax",
294
  path="/generated",
295
  )
296