
    i                         d Z ddlZddlmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlZddlZddlZddlmZmZ ddlmZ d	ed
efdZd	ed
efdZd	ed
efdZdeddfdZdefdZy)z1Custom middleware for the DevAll workflow system.    N)Callable	Awaitable)RequestHTTPExceptionFastAPI)JSONResponse)CORSMiddleware)get_server_loggerLogType)SecurityErrorrequest	call_nextc                   K   | j                   j                  d      xs t        t        j                               }|| j
                  _        t        j                         } ||        d{   }t        j                         |z
  }t               }|j                  | j                  t        | j                        || j                  j                  t        | j                        | j                  r| j                  j                   nd| j                   j                  d             |j#                  |j$                  |||j                   j                  d             ||j                   d<   |S 7 w)z+Add correlation ID to requests for tracing.X-Correlation-IDNz
user-agent)correlation_idpathquery_paramsclient_host
user_agentzcontent-length)r   content_length)headersgetstruuiduuid4stater   timer
   log_requestmethodurlr   dictr   clienthostlog_responsestatus_code)r   r   r   
start_timeresponsedurationloggers          =/Users/bowang/.openclaw/workspace/ChatDev/utils/middleware.pycorrelation_id_middlewarer+      s*    __(();<QDJJL@QN#1GMM Jw''Hyy{Z'H  F
GKK%[['../+2>>GNN''t??&&|4   %''++,<=	   ,:H'(O3 (s   A+E6-E3.DE6c                   K   | j                   j                  j                  d      rv| j                  dv rh| j                  j                  dd      j                         }|j                  d      s-| j                  dk7  r|j                  d      st        dd	
      | j                   j                  }d|v sd|v rtt        j                  d|      r^t               }|j                  dd| t        | j                  dt        t        j                                             t        dd
       ||        d{   }|S 7 w)z)Security middleware to validate requests.z/api/)POSTPUTPATCHzcontent-type zapplication/jsonGETzmultipart/form-datai  z7Content-Type must be application/json for API endpoints)r%   detailz..z./z(\.{2}[/\\])|([/\\]\.{2})PATH_TRAVERSAL_ATTEMPTzSuspicious path detected: r   )r   zInvalid pathN)r    r   
startswithr   r   r   lowerr   researchr
   log_security_eventgetattrr   r   r   r   )r   r   content_typer   r)   r'   s         r*   security_middlewarer;   2   s     {{""7+BZ0Z**>2>DDF&&'9:w~~QV?V**+@A# #T  ;;Dt|tt|99148&(F%%(,TF3&w}}6FDJJLHYZ & 
  CGGw''HO (s   D9E;E<Ec                 2   K    ||        d{   }|S 7 w)z0Rate limiting middleware (basic implementation).N )r   r   r'   s      r*   rate_limit_middlewarer>   Q   s     
 w''HO (s   appreturnc           
         ddg}t        j                  d      }|r@|j                  d      D cg c]#  }|j                         s|j                         % }}d}n|}d}| j	                  t
        ||ddgdgd	gd
       yc c}w )z%Configure and attach CORS middleware.zhttp://localhost:5173zhttp://127.0.0.1:5173CORS_ALLOW_ORIGINS,Nz*^https?://(localhost|127\.0\.0\.1)(:\d+)?$T*r   iX  )allow_originsallow_origin_regexallow_credentialsallow_methodsallow_headersexpose_headersmax_age)osgetenvsplitstripadd_middlewarer	   )r?   default_originsenv_originsooriginsorigin_regexs         r*   add_cors_middlewarerV   Z   s     	 O ))01K&1&7&7&<J&<	1779&<J!D'ee*+  	 Ks   BBc                     t        |         | j                  d      t                | j                  d      t               | S )z.Add all middleware to the FastAPI application.http)rV   
middlewarer+   r;   )r?   s    r*   rP   rP   v   s=      CNN645CNN6./ J    )__doc__r   typingr   r   fastapir   r   r   fastapi.responsesr   fastapi.middleware.corsr	   r   r6   rL   utils.structured_loggerr
   r   utils.exceptionsr   r+   r;   r>   rV   rP   r=   rZ   r*   <module>rb      s    7  & 3 3 * 2  	 	 > *W  Dw 8 > X W  8
 
rZ   