tecuts commited on
Commit
4d462b5
·
verified ·
1 Parent(s): b36825f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -12
app.py CHANGED
@@ -12,7 +12,7 @@ import logging
12
 
13
  # --- Security Helper Functions ---
14
  def verify_origin(request: Request):
15
- """Verify that the request comes from an allowed origin for /chat endpoint"""
16
  origin = request.headers.get("origin")
17
  referer = request.headers.get("referer")
18
 
@@ -21,22 +21,17 @@ def verify_origin(request: Request):
21
  "https://www.chrunos.com"
22
  ]
23
 
24
- # Allow localhost for development (you can remove this in production)
25
- #if origin and any(origin.startswith(local) for local in ["http://localhost:", "http://127.0.0.1:"]):
26
- # return True
27
-
28
- # Check origin header
29
- if origin in allowed_origins:
30
  return True
31
 
32
- # Check referer header as fallback
33
  if referer and any(referer.startswith(allowed) for allowed in allowed_origins):
34
  return True
35
 
36
- raise HTTPException(
37
- status_code=403,
38
- detail="Access denied: This endpoint is only accessible from chrunos.com"
39
- )
40
 
41
  # --- Configure Logging ---
42
  logging.basicConfig(level=logging.INFO)
 
12
 
13
  # --- Security Helper Functions ---
14
  def verify_origin(request: Request):
15
+ """Simplified check since CORS middleware handles most cases"""
16
  origin = request.headers.get("origin")
17
  referer = request.headers.get("referer")
18
 
 
21
  "https://www.chrunos.com"
22
  ]
23
 
24
+ # If origin header exists, trust CORS middleware validation
25
+ if origin:
 
 
 
 
26
  return True
27
 
28
+ # Fallback to referer check for non-CORS requests
29
  if referer and any(referer.startswith(allowed) for allowed in allowed_origins):
30
  return True
31
 
32
+ # If neither header present, log and deny
33
+ logger.info(f"No valid origin/referer - Origin: {origin}, Referer: {referer}")
34
+ raise HTTPException(status_code=403, detail="Access denied")
 
35
 
36
  # --- Configure Logging ---
37
  logging.basicConfig(level=logging.INFO)