
    ܖiY                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZmZ dd
lmZmZmZmZmZ ddlmZ ddl m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z0 ddl.m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z:  ejv                           ejx                  e=      Z> ejx                  d      Z?e?j                  ej                  j                  dd      j                                ed'd       ZDej                   G d deFej                               ZHej                   G d deFej                               ZIej                   G d deFej                               ZJej                   G d deFej                               ZKej                   G d d eFej                               ZLd(d!ZMd)d"ZNd*d#ZOd+d$ZP	 	 	 	 	 	 	 	 	 	 	 	 d,d%ZQd-d&ZRy).z+
Command-line entrypoints for `pip-audit`.
    )annotationsN)Iterator)	ExitStackcontextmanager)Path)IONoReturncast)__version__)AuditOptionsAuditor)DependencySourceDependencySourceError	PipSourcePyProjectSourceRequirementSource)PyLockSource)ResolvedFixVersionSkippedFixVersionresolve_fix_versions)ColumnsFormatCycloneDxFormat
JsonFormatMarkdownFormatVulnerabilityFormat)EcosystemsService
OsvServicePyPIService)ConnectionError)
DependencyResolvedDependencySkippedDependencyVulnerabilityResultVulnerabilityService)AuditSpinner
AuditState)assert_never	pip_auditPIP_AUDIT_LOGLEVELINFOc              #     K   t        |       dv rt        j                   y| j                  d      5 }| ddd       y# 1 sw Y   yxY ww)z
    A context managing wrapper for pip-audit's `--output` flag. This allows us
    to avoid `argparse.FileType`'s "eager" file creation, which is generally
    the wrong/unexpected behavior when dealing with fallible processes.
    >   stdout-wN)strsysr,   open)nameios     R/var/www/html/content-pipeline/venv/lib/python3.12/site-packages/pip_audit/_cli.py
_output_ior5   8   sD      4yO#jjYYs^ 	rH	 	 	s   2AA	AAAc                  4    e Zd ZdZdZdZdZdZdZd
dZ	ddZ
y	)OutputFormatChoicez:
    Output formats supported by the `pip-audit` CLI.
    columnsjsonzcyclonedx-jsonzcyclonedx-xmlmarkdownc                   | t         j                  u rt        ||      S | t         j                  u rt	        ||      S | t         j
                  u r$t        t        j                  j                        S | t         j                  u r$t        t        j                  j                        S | t         j                  u rt        ||      S t        |        y )N)inner_format)r7   Columnsr   Jsonr   CycloneDxJsonr   InnerFormatCycloneDxXmlXmlMarkdownr   r'   )selfoutput_descoutput_aliasess      r4   	to_formatzOutputFormatChoice.to_formatR   s    %--- n==',,,k>::'555"0K0K0P0PQQ'444"0K0K0O0OPP'000!+~>>    c                    | j                   S NvaluerD   s    r4   __str__zOutputFormatChoice.__str__`       zzrH   N)rE   boolrF   rP   returnr   rQ   r/   )__name__
__module____qualname____doc__r=   r>   r?   rA   rC   rG   rN    rH   r4   r7   r7   F   s,     GD$M"LHrH   r7   c                  $    e Zd ZdZdZdZdZddZy)VulnerabilityServiceChoicezA
    Python vulnerability services supported by `pip-audit`.
    osvpypiesmsc                    | j                   S rJ   rK   rM   s    r4   rN   z"VulnerabilityServiceChoice.__str__n   rO   rH   NrR   )rS   rT   rU   rV   OsvPypiEsmsrN   rW   rH   r4   rY   rY   d   s     CDDrH   rY   c                  ,    e Zd ZdZdZdZdZddZd	dZy)
VulnerabilityDescriptionChoicez^
    Whether or not vulnerability descriptions should be added to the `pip-audit` output.
    onoffautoc                    | t         j                  u ry| t         j                  u ry| t         j                  u rt	        |t
        j                  u       S t        |        y NTF)rb   OnOffAutorP   r7   r>   r'   rD   format_s     r4   to_boolz&VulnerabilityDescriptionChoice.to_bool|   sT    144437773888#5#:#::;;rH   c                    | j                   S rJ   rK   rM   s    r4   rN   z&VulnerabilityDescriptionChoice.__str__   rO   rH   Nrl   r7   rQ   rP   rR   	rS   rT   rU   rV   rh   ri   rj   rm   rN   rW   rH   r4   rb   rb   r   "     
B
CDrH   rb   c                  ,    e Zd ZdZdZdZdZddZd	dZy)
VulnerabilityAliasChoicezY
    Whether or not vulnerability aliases should be added to the `pip-audit` output.
    rc   rd   re   c                    | t         j                  u ry| t         j                  u ry| t         j                  u rt	        |t
        j                  u       S t        |        y rg   )rs   rh   ri   rj   rP   r7   r>   r'   rk   s     r4   rm   z VulnerabilityAliasChoice.to_bool   sT    +...-111-222#5#:#::;;rH   c                    | j                   S rJ   rK   rM   s    r4   rN   z VulnerabilityAliasChoice.__str__   rO   rH   Nro   rR   rp   rW   rH   r4   rs   rs      rq   rH   rs   c                  (    e Zd ZdZdZdZddZddZy)	ProgressSpinnerChoicezG
    Whether or not `pip-audit` should display a progress spinner.
    rc   rd   c                &    | t         j                  u S rJ   )rw   rh   rM   s    r4   __bool__zProgressSpinnerChoice.__bool__   s    ,////rH   c                    | j                   S rJ   rK   rM   s    r4   rN   zProgressSpinnerChoice.__str__   rO   rH   N)rQ   rP   rR   )rS   rT   rU   rV   rh   ri   ry   rN   rW   rH   r4   rw   rw      s     
B
C0rH   rw   c                >    |  ddj                  d |D               dS )zC
    Render a `--help`-style string for the given enumeration.
    z (choices: , c              3  2   K   | ]  }t        |        y wrJ   )r/   ).0vs     r4   	<genexpr>z_enum_help.<locals>.<genexpr>   s     ':1A':s   ))join)msges     r4   
_enum_helpr      s'     U+dii':'::;1==rH   c                X    t         j                  |        t        j                  d       y)zB
    Log a fatal error to the standard error stream and exit.
       N)loggererrorr0   exit)r   s    r4   _fatalr      s     LLHHQKrH   c                    t        j                  ddt         j                        } | j                         }| j	                  ddddt
                | j	                  d	d
dd       |j	                  ddt        dddd       |j	                  dt        dd       | j	                  ddd       | j	                  ddt        t        t        j                  j                  dt        j                        dt        dt                      | j	                  d!d"t        t        t        j                  j                  d#t        j                        d$t        d%t                      | j	                  d&t        d'd(t        j                  j                  d)t         j"                        d*+       | j	                  d,d-dd.       | j	                  d/d0dd1       | j	                  d2t$        t$        dt$        j&                  t        j                  j                  d3t$        j(                        d45       | j	                  d6t*        t*        dt*        j&                  t*        j(                  d75       | j	                  d8t        d9:       | j	                  d;t,        t,        t        j                  j                  d<t,        j&                        d=>       | j	                  d?t.        d@dAB       |j	                  dCt        dDddEg dFG       | j	                  dHdIdJdKdLM       | j	                  dNddO       | j	                  dPddQ       | j	                  dRt        dS:       | j	                  dTt        dUddVg dWG       | j	                  dXddY       | j	                  dZdd[       | j	                  d\d]t        d^d_t        j                  j                  d`da      b       | j	                  dct        ddddeg dfG       | j	                  dgddh       | S )iNz	pip-auditzHaudit the Python environment for dependencies with known vulnerabilities)progdescriptionformatter_classz-Vz	--versionversionz	%(prog)s )actionr   z-lz--local
store_truez;show only results for dependencies in the local environment)r   helpz-rz--requirementREQUIREMENTappendrequirementszIaudit the given requirements file; this option can be used multiple times)typemetavarr   destr   project_path?z.audit a local Python project at the given path)r   nargsr   z--lockedzeaudit lock files from the local Python project. This flag only applies to auditing from project pathsz-fz--formatPIP_AUDIT_FORMATFORMATz#the format to emit audit results in)r   choicesdefaultr   r   z-sz--vulnerability-servicePIP_AUDIT_VULNERABILITY_SERVICESERVICEz7the vulnerability service to audit dependencies againstz	--osv-urlOSV_URLosv_urlPIP_AUDIT_OSV_URLz1URL to use for the OSV API instead of the default)r   r   r   r   r   z-dz	--dry-runzwithout `--fix`: collect all dependencies but do not perform the auditing step; with `--fix`: perform the auditing step but do not perform any fixesz-Sz--strictzFfail the entire audit if dependency collection fails on any dependencyz--descPIP_AUDIT_DESCzinclude a description for each vulnerability; `auto` defaults to `on` for the `json` format. This flag has no effect on the `cyclonedx-json` or `cyclonedx-xml` formats.)r   r   r   constr   r   z	--aliaseszincludes alias IDs for each vulnerability; `auto` defaults to `on` for the `json` format. This flag has no effect on the `cyclonedx-json` or `cyclonedx-xml` formats.z--cache-dirzTthe directory to use as an HTTP cache for PyPI; uses the `pip` HTTP cache by default)r   r   z--progress-spinnerPIP_AUDIT_PROGRESS_SPINNERzdisplay a progress spinner)r   r   r   r   z	--timeout   zset the socket timeout)r   r   r   z--pathPATHpathszirestrict to the specified installation path for auditing packages; this option can be used multiple times)r   r   r   r   r   r   z-vz	--verbosecountr   zNrun with additional debug logging; supply multiple times to increase verbosity)r   r   r   z--fixz=automatically upgrade dependencies with known vulnerabilitiesz--require-hasheszrequire a hash to check each requirement against, for repeatable audits; this option is implied when any package in a requirements file has a `--hash` option.z--index-urlzbase URL of the Python Package Index; this should point to a repository compliant with PEP 503 (the simple repository API); this will be resolved by pip if not specifiedz--extra-index-urlURLextra_index_urlszpextra URLs of package indexes to use in addition to `--index-url`; should follow the same rules as `--index-url`z--skip-editablez0don't audit packages that are marked as editablez	--no-depszadon't perform any dependency resolution; requires all requirements are pinned to an exact versionz-oz--outputFILEz output results to the given filePIP_AUDIT_OUTPUTr,   )r   r   r   r   z--ignore-vulnIDignore_vulnsz_ignore a specific vulnerability by its vulnerability ID; this option can be used multiple timesz--disable-pipzdon't use `pip` for dependency resolution; this can only be used with hashed requirements files or if the `--no-deps` flag has been provided)argparseArgumentParserArgumentDefaultsHelpFormatteradd_mutually_exclusive_groupadd_argumentr   r   r7   osenvirongetr=   r   rY   r_   r/   r   DEFAULT_OSV_URLrb   rh   rj   rs   rw   int)parserdep_source_argss     r4   _parserr      s   $$^ >>F
 99;O
k)yQ\P]E^_
J	     X !    =	 !  ;   "

13E3M3MN=?QR   !'*

@B\BaBabE&
   

2J4N4NO@   O	   U	   +.,//

/1O1T1TUE  
 %(&))(--E  
 c  
 "%

;=R=U=UV)   %	     1 ! 	 ]   L  
 T   b   &  	 ?  
    /

18<   5      MrH   c                   | j                         }|j                  dk\  rt        j                  d       |j                  dk\  r#t	        j
                         j                  d       t        j                  d|        |S )Nr   DEBUG   zparsed arguments: )
parse_argsverbosepackage_loggersetLevellogging	getLoggerr   debug)r   argss     r4   _parse_argsr     sh    D ||q(||q$$W-
LL%dV,-KrH   c                "   |r[t        | j                  d            }| dz  }|j                         r|j                  |       |st	        d|         t        |      S | dz  }|j                         rt        ||||      S t	        d|         y )Nzpylock.*.tomlzpylock.tomlzno lockfiles found in zpyproject.toml)	index_urlr   statez*couldn't find a supported project file in )listglobis_filer   r   r   r   )r   r   r   lockedr   all_pylocksgeneric_pylockpyproject_paths           r4   _dep_source_from_project_pathr     s     <,,_=>%5!!#~.+L>:;K(( "$44N-	
 	
 7~FGrH   c                 d   t               } t        |       }|j                  t        j                  u r-t        |j                  |j                  |j                        }n|j                  t        j                  u r"t        |j                  |j                        }nS|j                  t        j                  u r"t        |j                  |j                        }nt        |j                         |j                  j                  |j                         }|j"                  j                  |j                         }|j                   j%                  ||      }|j&                  |j(                  r| j+                  d       |j,                  |j.                  r| j+                  d       nw|j0                  r| j+                  d       nY|j2                  r| j+                  d       n;|j4                  r| j+                  d       n|j6                  r| j+                  d	       |j.                  r!|j4                  rt8        j;                  d
       |j.                  r%t=        t
              rt8        j;                  d       |j4                  r*t8        j;                  d       t8        j;                  d       t?               5 }g }|j@                  r|jC                  tE        d             |jG                  tI        |            }|j,                  |j,                  D ]!  }	|	jK                         rtM        d|	        # tO        |j,                  |j.                  |j4                  |j6                  |jP                  |j0                  |j2                  |      }
nq|j&                  8tS        |j&                  |j0                  |j2                  |j(                  |      }
n-tU        |jV                  |jX                  |jP                  |      }
t[        t]        |j^                  xr |j`                               }i }d}d}d}d}tc        |jd                        }	 |jg                  |
      D ][  \  }}|ji                         rrtk        tl        |      }|jn                  r%tM        |jp                   d|jr                          n+|ju                  d|jp                   d|jr                          |dz  }nltk        tv        |      }t8        jy                  d|jp                   d|jz                   d       |ju                  d|jp                   d|jz                   d       |r=|D cg c]  }|j}                  |      r| }}|t        |      t        |      z
  z  }|}|||<   t        |      dkD  sI|dz  }|t        |      z  }^ 	 t               }d}d}|j`                  r't        |||      D ]  }|j^                  r|ji                         rJtk        t        |      }t8        j                  d|j                  jp                   d|jr                          nItk        t        |      }t8        j                  d|j                  jp                   d |jz                          |ji                         sBtk        t        |      }	 |
ja                  |       |dz  }|t        ||j                           z  }|jC                  |        ddd       dkD  rrd!| }nd"}d#| d$|dk(  rd%nd& | d' d(|dk(  rd)nd* 	}|j`                  r|d+ d(|dk(  rd%nd& d' d(|dk(  rd)nd* z  }t        |t        j                  ,       t        |j                        5 }t        |j!                        |,       ddd       |k7  rt        j                  d       yyd-}r	|d.| d/z  }t        |t        j                  ,       dkD  s|j                  r<t        |j                        5 }t        |j!                        |,       ddd       yyc c}w # t        $ r}tM        t        |             Y d}~d}~wt        $ r4}t8        j+                  t        |             tM        d       Y d}~d}~ww xY w# t        $ rA}t        |      }t8        jy                  |       t        |j                  |      }Y d}~d}~ww xY w# 1 sw Y   xY w# 1 sw Y   axY w# 1 sw Y   yxY w)0z1
    The primary entrypoint for `pip-audit`.
    )	cache_dirtimeoutr   )r   r   Nz6The --locked flag can only be used with a project pathzBThe --require-hashes flag can only be used with --requirement (-r)z=The --index-url flag can only be used with --requirement (-r)zCThe --extra-index-url flag can only be used with --requirement (-r)z;The --no-deps flag can only be used with --requirement (-r)z?The --disable-pip flag can only be used with --requirement (-r)z?The --no-deps flag is redundant when used with --require-hasheszThe --require-hashes flag with --service osv only enforces hash presence NOT hash validity. Use --service pypi to enforce hash validity.zX--no-deps is supported, but users are encouraged to fully hash their pinned dependencieszbConsider using a tool like `pip-compile`: https://pip-tools.readthedocs.io/en/latest/#using-hasheszCollecting inputs)memberszinvalid requirements input: )require_hashesno_depsdisable_pipskip_editabler   r   r   )localr   r   r   )dry_run)optionsr   z: z	Skipping r   z	Auditing z (r   zUTip: your network may be blocking this service. Try another service with `-s SERVICE`zDry run: would have skipped z upgrade because zDry run: would have upgraded z to z
, ignored  zFound z known vulnerabilityvulnerabilitiesz in  packagepackagesz and fixed )filezNo known vulnerabilities foundr|   z ignored)Pr   r   vulnerability_servicerY   r^   r   r   r   r   r_   r   r`   r   r'   descrm   formataliasesrG   r   r   r   r   r   r   r   r   r   r   warning
isinstancer   progress_spinnerr   r%   enter_contextr&   existsr   r   r   r   r   r   r   r   r   r   fixsetr   audit
is_skippedr
   r"   strictr2   skip_reasonupdate_stater!   r   r   
has_any_idlenr   r/   VulnServiceConnectionErrorr   r   r   infodepr   printr0   stderrr5   outputr   is_manifest) r   r   servicerE   rF   	formatterstackactorsr   reqsourceauditorresult	pkg_count
vuln_count
skip_countvuln_ignore_countvulns_to_ignorespecvulnsr   filtered_vulnsr   fixesfixed_pkg_countfixed_vuln_countr   dser   ignoredsummary_msgr3   s                                    r4   r   r     s    YFvD !!%?%C%CCt~~t||UYUaUab		#	#'A'F'F	FM		#	#'A'F'F	F#dnndllST//0))##DKK0K\\))$++6N%%k>BI  ;;LLQR  LL]^^^LLXY""LL^_\\LLVWLLZ[ t||XYz':>E	

 ||"	
 	G	

 
 r"  MM,':;<##Jv$>? ((( Azz|9#?@A '!!#22 ,,"00..!%!6!6	F *
 3!!%%F jjjj"00	F '<@]UYU]U]Q]+^_>@	

d//0	&}}V4 -e??$ 148D{{$))Bt/?/?.@AB**YtyykDDTDTCU+VW!OJ 2D9DLL9TYYKr$,,q!IJ&&499+R~Q'OP"16%\Aall?>[a%\N%\%Uc.6I)II%*E$tu:>NI#e*,J'-B 88+GVUC "<<~~'"#4c::377<<. I//2.?A
 ##5s;&CCGGLL>QUVYVaVaUb$cd~~'137CF

3'1,(Csww,@@(
 S!/"wr"h A~"#4"56GG ZL",/7HIitI;aY!^	'TV 	
 88./q&6!&;?ARS T%&a /1 49*EGK 	k

+$ 	<)""651;	<'HHQK ( 7R 12(;;K	
 >Y22DKK( @Bi&&vu5B?@ @ 3g &] % 	3q6NN) 	 LLQ 8 		D 1 F&)#h[1/EF]r" r"N	< 	< @ @s   >A/d.D*dDa%a<a 1a3a	C=d1b?8dd,d&a	b<"a<6d<b<)b71d7b<<d?	d	6d>dd		ddd#&d/)r2   r   rQ   zIterator[IO[str]])r   r/   r   ztype[enum.Enum]rQ   r/   )r   r/   rQ   r	   )rQ   argparse.ArgumentParser)r   r  rQ   zargparse.Namespace)r   r   r   r/   r   z	list[str]r   rP   r   r&   rQ   r   )rQ   None)SrV   
__future__r   r   enumr   r   r0   collections.abcr   
contextlibr   r   pathlibr   typingr   r	   r
   r(   r   pip_audit._auditr   r   pip_audit._dependency_sourcer   r   r   r   r   #pip_audit._dependency_source.pylockr   pip_audit._fixr   r   r   pip_audit._formatr   r   r   r   r   pip_audit._servicer   r   r   pip_audit._service.interfacer   r   r    r!   r"   r#   r$   pip_audit._stater%   r&   pip_audit._utilr'   basicConfigr   rS   r   r   r   r   r   upperr5   uniquer/   Enumr7   rY   rb   rs   rw   r   r   r   r   r   r   rW   rH   r4   <module>r-     s   #    	 
 $ 0  % % ! 2  = V V  J I V  6 (    			8	$ #"";/   

';VDJJL M 
 
 dii  : 
dii 
 
 S$))  . sDII  . C  >GTHH#&H:CHMQHZdHH<S@rH   