1- from fastapi import FastAPI , Depends , HTTPException , Security
1+ from fastapi import FastAPI , Depends , HTTPException , Security , WebSocket
22from fastapi .security import OAuth2PasswordBearer
33from fastapi_limiter import FastAPILimiter
44from fastapi_limiter .depends import RateLimiter
1111from tools .wallet import WalletTool
1212from lib .security import SecurityHandler
1313from lib .mcp_transport import MCPTransport
14+ from lib .monitoring import MonitoringHandler
15+ from lib .data_privacy import DataPrivacyHandler
1416from lib .logger import logger
1517from lib .errors import ValidationError
1618from neondatabase import AsyncClient
2729wallet_tool = WalletTool (db_client )
2830security_handler = SecurityHandler (db_client )
2931mcp_transport = MCPTransport ()
32+ monitoring_handler = MonitoringHandler (db_client )
33+ data_privacy_handler = DataPrivacyHandler (db_client )
3034
3135# CORS configuration
3236app .add_middleware (
3337 CORSMiddleware ,
3438 allow_origins = ["https://webxos.netlify.app" ],
3539 allow_credentials = True ,
36- allow_methods = ["GET" , "POST" ],
40+ allow_methods = ["GET" , "POST" , "DELETE" ],
3741 allow_headers = ["Authorization" , "X-Session-ID" , "Content-Type" ]
3842)
3943
@@ -46,7 +50,6 @@ class JSONRPCRequest(BaseModel):
4650def sanitize_input (value : Any ) -> Any :
4751 """Sanitize input to prevent injection attacks."""
4852 if isinstance (value , str ):
49- # Remove potentially malicious characters and escape HTML
5053 value = re .sub (r'[<>;{}]' , '' , value )
5154 return escape (value )
5255 elif isinstance (value , dict ):
@@ -84,13 +87,11 @@ async def shutdown_event():
8487@app .post ("/mcp/execute" , response_model = JSONRPCRequest , dependencies = [Depends (RateLimiter (times = 100 , seconds = 900 ))])
8588async def execute (request : JSONRPCRequest , user : Dict [str , Any ] = Depends (get_current_user )):
8689 try :
87- # Sanitize request parameters
8890 sanitized_params = sanitize_input (request .params )
8991 method = request .method
9092 params = sanitized_params
9193 params ["user_id" ] = user ["user_id" ]
9294
93- # Apply stricter rate limiting for cash-out
9495 if method == "wallet.cashOut" :
9596 await RateLimiter (times = 5 , seconds = 900 )(request )
9697
@@ -122,6 +123,18 @@ async def execute(request: JSONRPCRequest, user: Dict[str, Any] = Depends(get_cu
122123 id = request .id
123124 )
124125
126+ @app .get ("/monitoring/kpis" , dependencies = [Depends (RateLimiter (times = 10 , seconds = 60 ))])
127+ async def get_kpis (time_window_hours : int = 24 , handler : MonitoringHandler = Depends (lambda : MonitoringHandler (DatabaseConfig ()))):
128+ return await handler .get_security_kpis (time_window_hours )
129+
130+ @app .websocket ("/monitoring/kpis/stream" )
131+ async def stream_kpis (websocket : WebSocket , handler : MonitoringHandler = Depends (lambda : MonitoringHandler (DatabaseConfig ()))):
132+ await handler .stream_kpis (websocket )
133+
134+ @app .post ("/privacy/erase" , dependencies = [Depends (RateLimiter (times = 3 , seconds = 3600 ))])
135+ async def erase_data (input : DataErasureInput , handler : DataPrivacyHandler = Depends (lambda : DataPrivacyHandler (DatabaseConfig ()))):
136+ return await handler .erase_user_data (input )
137+
125138@app .get ("/openapi.json" )
126139async def get_openapi ():
127140 return app .openapi ()
0 commit comments