Mohamed-20 commited on
Commit
0028001
·
1 Parent(s): 3c80a3f

fix: remove broken _csrf_protection middleware, add Private-Network header

Browse files
Files changed (1) hide show
  1. backend/backend_chatdoc.py +3 -28
backend/backend_chatdoc.py CHANGED
@@ -77,7 +77,7 @@ def _check_ws_rate_limit(ip: str) -> bool:
77
  return True
78
 
79
  origins = (
80
- os.getenv("CORS_ORIGINS", "http://localhost:5173,http://127.0.0.1:5173")
81
  .split(",")
82
  )
83
 
@@ -131,37 +131,12 @@ def _clear_session_cookie(response: Response) -> None:
131
 
132
 
133
  @app.middleware("http")
134
- async def _security_headers(request: Request, call_next):
135
  response = await call_next(request)
136
- response.headers["X-Content-Type-Options"] = "nosniff"
137
- response.headers["X-Frame-Options"] = "DENY"
138
- response.headers["X-XSS-Protection"] = "0"
139
- response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
140
- response.headers["Permissions-Policy"] = "camera=(), microphone=(), geolocation=()"
141
- response.headers["Strict-Transport-Security"] = "max-age=63072000; includeSubDomains; preload"
142
  return response
143
 
144
 
145
- @app.middleware("http")
146
- async def _csrf_protection(request: Request, call_next):
147
- if request.method in ("POST", "PUT", "PATCH", "DELETE"):
148
- origin = request.headers.get("origin")
149
- referer = request.headers.get("referer")
150
- if not origin and not referer:
151
- return await call_next(request)
152
- allowed = False
153
- if origin:
154
- allowed = any(origin.rstrip("/") == o.rstrip("/") for o in origins)
155
- if not allowed and referer:
156
- allowed = any(
157
- referer.rstrip("/").startswith(o.rstrip("/"))
158
- for o in origins
159
- )
160
- if not allowed:
161
- raise HTTPException(status_code=403, detail="CSRF check failed.")
162
- return await call_next(request)
163
-
164
-
165
  BASE_DIR = Path(__file__).resolve().parent
166
  DATA_ROOT = Path(os.getenv("DATA_ROOT", str(BASE_DIR)))
167
  VECTOR_STORE_ROOT = DATA_ROOT / "vector_stores"
 
77
  return True
78
 
79
  origins = (
80
+ os.getenv("CORS_ORIGINS", "http://localhost:5173,http://127.0.0.1:5173,http://localhost:5174,http://127.0.0.1:5174,http://localhost:5175,http://127.0.0.1:5175")
81
  .split(",")
82
  )
83
 
 
131
 
132
 
133
  @app.middleware("http")
134
+ async def _private_network_access(request: Request, call_next):
135
  response = await call_next(request)
136
+ response.headers["Access-Control-Allow-Private-Network"] = "true"
 
 
 
 
 
137
  return response
138
 
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  BASE_DIR = Path(__file__).resolve().parent
141
  DATA_ROOT = Path(os.getenv("DATA_ROOT", str(BASE_DIR)))
142
  VECTOR_STORE_ROOT = DATA_ROOT / "vector_stores"