Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
"""
|
| 2 |
-
app.py β LibBee v3.
|
| 3 |
FastAPI entry point: CORS, middleware, lifespan, router mounting.
|
| 4 |
|
| 5 |
-
Changes over v3.
|
| 6 |
-
-
|
| 7 |
-
|
| 8 |
-
-
|
| 9 |
-
- Section headers added throughout.
|
| 10 |
"""
|
| 11 |
|
| 12 |
# ββ Imports ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -124,7 +123,7 @@ async def lifespan(app: FastAPI):
|
|
| 124 |
app = FastAPI(
|
| 125 |
title="LibBee - KU Library AI",
|
| 126 |
description="Khalifa University Library AI Assistant",
|
| 127 |
-
version="3.
|
| 128 |
lifespan=lifespan,
|
| 129 |
)
|
| 130 |
|
|
@@ -147,21 +146,26 @@ app.add_middleware(
|
|
| 147 |
)
|
| 148 |
|
| 149 |
|
| 150 |
-
# ββ
|
| 151 |
|
| 152 |
@app.middleware("http")
|
| 153 |
async def security_headers(request: Request, call_next):
|
| 154 |
"""
|
| 155 |
Add security headers to every response.
|
| 156 |
-
|
| 157 |
-
X-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
"""
|
| 162 |
response = await call_next(request)
|
| 163 |
response.headers["X-Content-Type-Options"] = "nosniff"
|
| 164 |
-
|
| 165 |
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
|
| 166 |
response.headers["X-XSS-Protection"] = "1; mode=block"
|
| 167 |
response.headers["Permissions-Policy"] = (
|
|
@@ -170,6 +174,8 @@ async def security_headers(request: Request, call_next):
|
|
| 170 |
return response
|
| 171 |
|
| 172 |
|
|
|
|
|
|
|
| 173 |
@app.middleware("http")
|
| 174 |
async def operational_guardrails(request: Request, call_next):
|
| 175 |
settings = get_settings()
|
|
@@ -250,7 +256,7 @@ def health_check():
|
|
| 250 |
settings = get_settings()
|
| 251 |
return {
|
| 252 |
"status": "ok",
|
| 253 |
-
"version": "3.
|
| 254 |
"service": "LibBee KU Library AI",
|
| 255 |
"rag_ready": rag_service.is_ready(),
|
| 256 |
"maintenance_mode": settings.maintenance_mode,
|
|
|
|
| 1 |
"""
|
| 2 |
+
app.py β LibBee v3.6
|
| 3 |
FastAPI entry point: CORS, middleware, lifespan, router mounting.
|
| 4 |
|
| 5 |
+
Changes over v3.5:
|
| 6 |
+
- X-Frame-Options header REMOVED β SAMEORIGIN was blocking the HuggingFace
|
| 7 |
+
Space iframe preview. HF Spaces are sandboxed by HF itself.
|
| 8 |
+
- All other security headers retained unchanged.
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
# ββ Imports ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 123 |
app = FastAPI(
|
| 124 |
title="LibBee - KU Library AI",
|
| 125 |
description="Khalifa University Library AI Assistant",
|
| 126 |
+
version="3.6.0",
|
| 127 |
lifespan=lifespan,
|
| 128 |
)
|
| 129 |
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
|
| 149 |
+
# ββ Security Headers Middleware ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 150 |
|
| 151 |
@app.middleware("http")
|
| 152 |
async def security_headers(request: Request, call_next):
|
| 153 |
"""
|
| 154 |
Add security headers to every response.
|
| 155 |
+
|
| 156 |
+
X-Content-Type-Options β prevents MIME sniffing
|
| 157 |
+
X-Frame-Options β INTENTIONALLY OMITTED (v3.6):
|
| 158 |
+
Setting SAMEORIGIN or DENY blocks the HuggingFace
|
| 159 |
+
Space iframe preview. HF Spaces are sandboxed by
|
| 160 |
+
HuggingFace's own iframe security model, so this
|
| 161 |
+
header is not needed here.
|
| 162 |
+
Referrer-Policy β limits referrer leakage
|
| 163 |
+
X-XSS-Protection β legacy XSS filter (belt-and-braces)
|
| 164 |
+
Permissions-Policy β disables unused browser features
|
| 165 |
"""
|
| 166 |
response = await call_next(request)
|
| 167 |
response.headers["X-Content-Type-Options"] = "nosniff"
|
| 168 |
+
# X-Frame-Options deliberately omitted β SAMEORIGIN breaks HF Space iframe
|
| 169 |
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
|
| 170 |
response.headers["X-XSS-Protection"] = "1; mode=block"
|
| 171 |
response.headers["Permissions-Policy"] = (
|
|
|
|
| 174 |
return response
|
| 175 |
|
| 176 |
|
| 177 |
+
# ββ Operational Guardrails Middleware ββββββββββββββββββββββββββββββββββββββββββ
|
| 178 |
+
|
| 179 |
@app.middleware("http")
|
| 180 |
async def operational_guardrails(request: Request, call_next):
|
| 181 |
settings = get_settings()
|
|
|
|
| 256 |
settings = get_settings()
|
| 257 |
return {
|
| 258 |
"status": "ok",
|
| 259 |
+
"version": "3.6.0",
|
| 260 |
"service": "LibBee KU Library AI",
|
| 261 |
"rag_ready": rag_service.is_ready(),
|
| 262 |
"maintenance_mode": settings.maintenance_mode,
|