o
    i                     @   s   U d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z
 eeZe jddG dd	 d	eZzd dlZeje
jd
dd ZW n eyR   ed Y nw g Zee ed< dddZdd ZdS )    N)IAST)Vulnerability)
get_logger)configT)unsafe_hashc                   @   s   e Zd ZU eed< dS )VulnerabilityFoundInTesttestN)__name__
__module____qualname__str__annotations__ r   r   W/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/appsec/_iast/_pytest_plugin.pyr      s   
 r   )autousec              	   c   s    dV  |du r
dS | tj}|du r%|tj}|du r dS t|}|d r_|d D ]}tt	| j
j|d |d |d d q-| jdradd	d
 |d D }td|  dS dS dS )z
        Extract the vulnerabilities discovered in tests.
        Optionally output the test as failed if vulnerabilities are found.
        Nvulnerabilitiestypeevidencelocation)r   r   r   r   zddtrace-iast-fail-testsz, c                 S   s   g | ]}|d  qS )r   r   ).0vulnr   r   r   
<listcomp>5   s    z ddtrace_iast.<locals>.<listcomp>z'There are vulnerabilities in the code: )_get_struct_tagr   STRUCTget_tagJSONjsonloads	vuln_dataappendr   nodenodeidr   	getoptionjoinpytestfail)requestddspan	dict_datadatar   vulnsr   r   r   ddtrace_iast   s2   
	r+   zpytest not importedr      c                 C   s   z9t | d)}| }td|| d }tt||| }||| }||fW  d   W S 1 s2w   Y  W dS  tyK   tjd| dd Y dS w )	z3Extracts code snippet around the given line number.rr      NzError reading file %sT)exc_info) r   )open	readlinesmaxminlen	Exceptionlogdebug)filepathline_numbercontextfilelinesstartendcoder   r   r   extract_code_snippet?   s   (rA   c                 C   sX  t jsd S ts| jddddd | d d S | jddddd tD ]}| jd|j dd |jd}| jd	|j d|| d
 | d|jd  d|jd   t	|jd |jd \}}|r| d |d urt
||d dD ]%\}}||jd kr| j|dd| ddd qt| |dd|  qtn	| j|d dd | d q"d S )N=zDatadog Code Security ReportT)purpleboldzNo vulnerabilities found.zTest: )rD   	INJECTIONzVulnerability: )rD   redyellowz
Location: path:linezCode:r.   )r>   4dz: )rD   rC   r   )
asm_config_iast_enabledr   	write_sep
write_liner   r   endswithr   rA   	enumeratewrite)terminalreporterentryhigh_severitycode_snippet
start_lineirJ   r   r   r   print_iast_reportM   s:   

"
rY   )r,   )dataclassesr   ddtrace.appsec._constantsr   ddtrace.appsec._iast.reporterr   ddtrace.internal.loggerr   ddtrace.internal.settings.asmr   rL   r	   r7   	dataclassr   r$   fixturerM   r+   ImportErrorr8   r   listr   rA   rY   r   r   r   r   <module>   s&   

!
