Spaces:
Sleeping
Sleeping
File size: 6,425 Bytes
6859bba 6908b1e 6859bba 40b9f21 6859bba 40b9f21 6859bba 40b9f21 6859bba 40b9f21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import os
import json
import logging
from datetime import datetime, timezone
from fastapi import Request, HTTPException, Security
from fastapi.security import APIKeyHeader
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
_api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
# ---------------------------------------------------------------------------
# Security logger — writes to a dedicated security.log file
# ---------------------------------------------------------------------------
_security_log = logging.getLogger("ai-server.security")
_security_log_path = os.getenv("SECURITY_LOG_PATH", "security.log")
try:
_security_handler: logging.Handler = logging.FileHandler(
_security_log_path, encoding="utf-8"
)
except (PermissionError, OSError):
_security_handler = logging.StreamHandler()
_security_handler.setFormatter(
logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
)
_security_log.addHandler(_security_handler)
_security_log.setLevel(logging.WARNING)
# ---------------------------------------------------------------------------
# Forbidden fields — PII / communication / auth data that must never appear
# ---------------------------------------------------------------------------
FORBIDDEN_FIELDS = {
"email", "password", "encrypted_password", "phone",
"avatar_url", "security_question", "security_answer",
"raw_user_meta_data", "raw_app_meta_data",
"recovery_token", "confirmation_token",
"content", "body", "message", "subject",
"storage_path", "public_url", "file_path",
"token", "session", "cookie", "access_token", "refresh_token",
}
async def verify_api_key(
api_key: str | None = Security(_api_key_header),
) -> str:
"""Validate the API key sent by the Next.js backend.
Reads from AI_SERVER_API_KEY env var.
"""
expected = os.getenv("AI_SERVER_API_KEY")
if not expected:
raise HTTPException(status_code=500, detail="AI_SERVER_API_KEY not configured on the server")
if api_key is None:
raise HTTPException(status_code=401, detail="Missing X-API-Key header")
if api_key != expected:
raise HTTPException(status_code=403, detail="Invalid API key")
return api_key
# ---------------------------------------------------------------------------
# Payload inspector
# ---------------------------------------------------------------------------
def inspect_payload(data: dict, path: str = "") -> list[str]:
"""Recursively check a payload for forbidden fields.
Returns violation descriptions (field paths only — never values).
"""
violations: list[str] = []
for key, val in data.items():
full_path = f"{path}.{key}" if path else key
if key.lower() in FORBIDDEN_FIELDS:
violations.append(f"Forbidden field detected: {full_path}")
if isinstance(val, dict):
violations.extend(inspect_payload(val, full_path))
if isinstance(val, list):
for i, item in enumerate(val):
if isinstance(item, dict):
violations.extend(inspect_payload(item, f"{full_path}[{i}]"))
return violations
class PayloadInspectorMiddleware:
"""Pure-ASGI middleware that rejects POST requests containing forbidden PII.
Implemented as ASGI (not BaseHTTPMiddleware) because BaseHTTPMiddleware
buffers response bodies and breaks StreamingResponse / truncates bodies
behind the HF Spaces proxy.
"""
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope.get("type") != "http" or scope.get("method") != "POST":
await self.app(scope, receive, send)
return
# Drain the incoming body
chunks: list[bytes] = []
more_body = True
while more_body:
message = await receive()
if message["type"] != "http.request":
# Disconnect or unexpected message type — bail out
await self.app(scope, receive, send)
return
chunks.append(message.get("body", b"") or b"")
more_body = message.get("more_body", False)
body_bytes = b"".join(chunks)
# Inspect
if body_bytes:
try:
data = json.loads(body_bytes)
if isinstance(data, dict):
violations = inspect_payload(data)
if violations:
timestamp = datetime.now(timezone.utc).isoformat()
client_host = "unknown"
client_info = scope.get("client")
if client_info:
client_host = client_info[0] if len(client_info) else "unknown"
path = scope.get("path", "")
_security_log.warning(
"PAYLOAD VIOLATION | ip=%s | path=%s | time=%s | fields=%s",
client_host, path, timestamp, "; ".join(violations),
)
body = json.dumps({
"error": "payload_violation",
"detail": "Request contains forbidden data fields that violate the AI data privacy boundary.",
"fields": violations,
}).encode("utf-8")
await send({
"type": "http.response.start",
"status": 400,
"headers": [
(b"content-type", b"application/json"),
(b"content-length", str(len(body)).encode()),
],
})
await send({"type": "http.response.body", "body": body})
return
except (json.JSONDecodeError, UnicodeDecodeError):
pass
# Replay the buffered body so the downstream app can read it
replayed = False
async def replay_receive():
nonlocal replayed
if not replayed:
replayed = True
return {"type": "http.request", "body": body_bytes, "more_body": False}
return await receive()
await self.app(scope, replay_receive, send)
|