o
    ig                     @   s   U d dl Z d dlmZ d dlmZ d dlmZ eeZ	dZ
eeef Zeeef Ze  aeed< defdd	Zd
edefddZd
edefddZddddZddddZdd ZdS )    N)_get_iast_env)
get_logger)configi   GLOBAL_VULNERABILITIES_LIMITreturnc                 C   s   t | ji  S )z
    Initialize vulnerability maps for a new request.

    Creates a copy of the stored counts from the global map and a new empty map for
    tracking vulnerabilities in the current request.
    )r   getendpoint_keycopycontext r   i/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/appsec/_iast/sampling/vulnerability_detection.pyinit_request_vulnerability_maps   s   r   vulnerability_typec                 C   s   t  }|sdS |jtjkrdS |jrt||_i |_d|_|j| d}|j| d}|d |j| < ||k r8dS | jd7  _dS )z_
    Determine if a vulnerability should be processed based on the optimization algorithm.
    Fr      T)	r   vulnerability_budget
asm_config&_iast_max_vulnerabilities_per_requestsis_first_vulnerabilityr   vulnerability_copy_global_limitvulnerabilities_request_limitr   )r   r   current_countstored_countr   r   r   should_process_vulnerability   s    
r   c                 C   sL   t  }|sdS |j|   d8  < |j|  dkr|j|  | jd8  _dS )NFr   r   T)r   r   popr   )r   r   r   r   r   rollback_quota=   s   r   c                 C   s   | du rt  } | du rtd dS | jtjkr2t| ji }| j	
 D ]\}}|||< q$|t| j< tttkrFtjdd tttks8dS dS )z
    Update the global vulnerability map at the end of a request.

    Args:
        budget_used (bool): Whether the vulnerability detection budget was fully used
    N?No request context found when updating global vulnerability mapF)last)r   logdebugr   r   r   r   r   r   r   itemslenMAX_ENDPOINTSpopitem)r   global_map_entry	vuln_typecountr   r   r   !update_global_vulnerability_limitI   s   


r'   c                 C   s@   | d u rt  } | d u rtd d S i | _i | _d| _d| _d S )Nr   Tr   )r   r   r   r   r   r   r   r
   r   r   r   reset_request_vulnerabilitiesc   s   

r(   c                   C   s   t  ad S N)collectionsOrderedDictr   r   r   r   r   _reset_global_limito   s   r,   r)   )r   N)r*   ddtrace.appsec._iast._iast_envr   ddtrace.internal.loggerr   ddtrace.internal.settings.asmr   r   __name__r   r"   dictstrintRequestDictTypeGlobalDictTyper+   r   __annotations__r   boolr   r   r'   r(   r,   r   r   r   r   <module>   s   
 
