HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/uaclient/api/u/pro/security/cves/_common/__pycache__/v1.cpython-38.pyc
U

��Jh�L�	@s�ddlZddlZddlZddlZddlZddlmZddlmZddl	m
Z
mZmZm
Z
mZddlmZddlmZmZmZmZmZddlmZddlmZdd	lmZdd
lmZmZm Z m!Z!ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+dd
l,m-Z-ddl.m/Z/Gdd�de�Z0Gdd�de�Z1ej2Gdd�dej3��Z4Gdd�d�Z5eee6ee6fe4d�dd�Z7e
ddeejfdee6ee6e
fffg�Z8Gdd�d�Z9Gdd �d ej:d!�Z;Gd"d#�d#�Z<e;eee6d$�d%d&�Z=dS)'�N)�defaultdict)�	lru_cache)�Any�Dict�List�
NamedTuple�Optional)�urljoin)�apt�
exceptions�http�system�util)�#query_installed_source_pkg_versions)�_enabled_services)�UAConfig)�
DataObject�Field�FloatDataValue�StringDataValue)�VULNERABILITY_CACHE_PATH�VULNERABILITY_DATA_CACHE�VULNERABILITY_DATA_TMPL�$VULNERABILITY_DPKG_STATUS_DATE_CACHE�VULNERABILITY_ETAG_CACHE�VULNERABILITY_RESULT_CACHE)�FIPSEntitlement�FIPSUpdatesEntitlement)�DataObjectFile)�UAFilec@s eZdZede�gZdd�ZdS)�VulnerabilityCacheETag�etagcCs
||_dS�N�r!)�selfr!�r%�M/usr/lib/python3/dist-packages/uaclient/api/u/pro/security/cves/_common/v1.py�__init__'szVulnerabilityCacheETag.__init__N)�__name__�
__module__�__qualname__rr�fieldsr'r%r%r%r&r $sr c@s&eZdZede�gZed�dd�ZdS)�VulnerabilityDpkgCacheDate�dpkg_status_date�r-cCs
||_dSr"r.)r$r-r%r%r&r'.sz#VulnerabilityDpkgCacheDate.__init__N)r(r)r*rrr+�floatr'r%r%r%r&r,+sr,c@seZdZdZdZdZdZdS)�VulnerabilityStatusz<
    An enum to represent the status of a vulnerability
    Zno�partialZyesN)r(r)r*�__doc__�NO_FIX_AVAILABLE�PARTIAL_FIX_AVAILABLE�FULL_FIX_AVAILABLEr%r%r%r&r02sr0c@s�eZdZdeeed�dd�Zedd��Zdd�Z	d	d
�Z
eeefd�dd
�Z
eed�dd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)�VulnerabilityDataN��cfg�seriescCs&||_|pt��j|_d|_d|_dS)NF)r8r
�get_release_infor9�_etag�
_refreshed)r$r8r9r%r%r&r'?szVulnerabilityData.__init__cCs|jSr")r<�r$r%r%r&�	refreshedIszVulnerabilityData.refreshedcCstj�t|jt�Sr")�os�path�joinrr9rr=r%r%r&�_get_cache_data_pathMs
�z&VulnerabilityData._get_cache_data_pathcCs"tttttj�t|j�dd�d�S�NF)�nameZ	directoryZprivate)Zdata_object_clsZua_file)	rr rrr?r@rArr9r=r%r%r&�_get_etag_cache_fileRs��z&VulnerabilityData._get_etag_cache_file)�	json_datacCst�|��t�|��dSr")r
�
write_filerB�json�dumps)r$rFr%r%r&�_save_cache_data\sz"VulnerabilityData._save_cache_data)�cache_etag_filer!cCs|�t|d��dS)Nr#)�writer )r$rKr!r%r%r&�_save_etag_cache_sz"VulnerabilityData._save_etag_cachecCs(|js"|��}|��}|r"|j|_|jSr")r;rE�readr!)r$Z	etag_fileZ	etag_datar%r%r&�	_get_etagbszVulnerabilityData._get_etagcCst�t�|����Sr")rH�loadsr
�	load_filerBr=r%r%r&�_get_cache_datalsz!VulnerabilityData._get_cache_datacCsd|j}dd�t|j�jD�}tj|kr4d�|j�}ntj|krJd�|j�}tj|d�}t	|jj
|�S)NcSsg|]
}|j�qSr%)rD)�.0�sr%r%r&�
<listcomp>rsz3VulnerabilityData._get_data_url.<locals>.<listcomp>zfips_{}zfips-updates_{})r9)r9rr8Zenabled_servicesrrD�formatrrr	Zvulnerability_data_url_prefix)r$Z	data_nameZenabled_services_namesZ	data_filer%r%r&�
_get_data_urlos
�

zVulnerabilityData._get_data_urlcCs|��}|dS)N�published_at)�get)r$Zvulnerability_json_datar%r%r&�get_published_date}sz$VulnerabilityData.get_published_datecCs�|��}z$tj|j|��|d�\}}d|_Wntjk
rJ|��YSXt	�
|�d��}t�
�r�|�|�|r�|�|��|�|S)N)r8Zurlr!Tzutf-8)rOrZdownload_xz_file_from_urlr8rWr<rZ
ETagUnchangedrRrHrP�decoder�we_are_currently_rootrJrMrE)r$Z	last_etag�datar!rFr%r%r&rY�s �


zVulnerabilityData.get)N)r(r)r*rr�strr'�propertyr>rBrErrrJrrMrOrRrWrZrYr%r%r%r&r6=s ��



r6)�affected_packages�returncCsPtj}d}|D]}|�d�dk	r|d7}q|t|�kr>tj}n|dkrLtj}|S)Nr�fix_version�)r0r3rY�lenr5r4)r`Zvulnerability_statusZ	num_fixes�pkgr%r%r&�_get_vulnerability_fix_status�s
rf�VulnerabilityParserResult�vulnerability_data_published_at�vulnerabilities_infoc@s4eZdZdd�Zd	eeeed�dd�Zdd�ZdS)
�VulnerabilitiesAlreadyFixedcCstt�|_tdd��|_dS)NcSstt�Sr")r�intr%r%r%r&�<lambda>��z6VulnerabilitiesAlreadyFixed.__init__.<locals>.<lambda>)r�set�_vulns�priority_counterr=r%r%r&r'�s
�z$VulnerabilitiesAlreadyFixed.__init__N)�	vuln_name�vuln_pocket�
vuln_prioritycCs<||j|kr8|j|�|�|r8|j||d7<dS)Nrc)ro�addrp)r$rqrrrsr%r%r&�add_vulnerability�sz-VulnerabilitiesAlreadyFixed.add_vulnerabilitycCsHiid�}|j��D].\}}t|�|d|<t|j|�|d|<q|S)N)�count�inforvrw)ro�itemsrd�dictrp)r$Z	dict_repr�pocketZvulnsr%r%r&�to_dict�s�z#VulnerabilitiesAlreadyFixed.to_dict)N)r(r)r*r'r^rrur{r%r%r%r&rj�s
��rjc@szeZdZdZejeeefeeefd�dd��Z	ejeeefeeefeeefd�dd��Z
eeefeeed�d	d
�Zeeefeeeed�dd
�Zeeefeeeeeed�dd�Z
eeeefeeefeeefd�dd�Zeed�dd�Zedd�ed�dd��Zeeeed�dd�Zeed�dd �Zeeefd!�d"d#�Zeeefeeeeeffd$�d%d&�ZdS)'�VulnerabilityParserN)�affected_pkgracCsdSr"r%)r$r}r%r%r&�get_package_vulnerabilities�sz/VulnerabilityParser.get_package_vulnerabilities)�vulnerability_info�vulnerabilities_dataracCsdSr"r%)r$rr�r%r%r&� _post_process_vulnerability_info�sz4VulnerabilityParser._post_process_vulnerability_info��packages�bin_pkg_name�bin_pkg_versionrqcCsd||jgi||<dS)NZcurrent_version)�vulnerability_type)r$r�r�r�rqr%r%r&�_add_new_vulnerability�s
�z*VulnerabilityParser._add_new_vulnerability�r�r�r�rq�vuln_pkg_statuscCs<||kr|j||||d�|||j�|d|dd��dS�Nr�)rDrbZ
fix_statusZ
fix_origin�r�r��append)r$r�r�r�rqr�r%r%r&�_add_unfixable_vulnerability�s���z0VulnerabilityParser._add_unfixable_vulnerability�r�r�r�rqr��vuln_bin_fix_versionrrcCs<||kr|j||||d�|||j�||||d��dSr�r�)r$r�r�r�rqr�r�rrr%r%r&�_add_fixable_vulnerabilitys
���z.VulnerabilityParser._add_fixable_vulnerability�rq�vulnerabilities�	vuln_info�
vulns_datacCs||kr|j||d�||<dS)N)rr�)r�)r$rqr�r�r�r%r%r&�_add_vulnerability_info s
�z+VulnerabilityParser._add_vulnerability_info)�vuln_source_fixed_versionr�cCs|dkr|dkrdSdS)Nznot-vulnerableTFr%)r$r�r�r%r%r&�is_vulnerability_not_fixable-sz0VulnerabilityParser.is_vulnerability_not_fixable)�maxsize)�binary_pkg_namecCst�ddd|g�\}}|S)Nz
dpkg-queryz-Wz-f=${source:Version})r
Zsubp)r$r��out�_r%r%r&�!_get_installed_source_pkg_version;s��	z5VulnerabilityParser._get_installed_source_pkg_version)r�r�r�cCs.|dkr*|�|�}t�||�dkr&dSdSdS)aZ
        This method checks if we can detect that a vulnerability
        affects a binary package but can't be fixed. This
        situation can happen during a package transition.

        For example, suppose we have this entry for pkg1:

        "pkg1": {
          "source_version": {
            "1.0": {
              "bin-pkg1": "1.0",
              "bin-pkg2": "1.1",
            },
            "1.1": {
              "bin-pkg1": "1.2"
            }
          }
        }

        Notice that version 1.1 doesn't produce bin-pkg2 anymore.
        Therefore, if we detect that a vulnerability is fixable
        by version 1.1, we won't find the binary fixable bersion for
        the bin-pkg2 package.

        If we detect that, we will:

        1. Check if versions of the source package associated with the
           binary package is higher than the vulnerability source fix
           version. If it is, we can say that the system is not vulnerable.
        2. If it is not, then the binary package is affected by the issue, but
           we can't say what the user needs to do to fix it.
        NrFT)r�r
�version_compare)r$r�r�r�Zinstalled_source_pkg_versionr%r%r&�&is_vulnerability_valid_but_not_fixableHs'����z:VulnerabilityParser.is_vulnerability_valid_but_not_fixable)�bin_versionr�cCst�||�dkS�Nr)r
r�)r$r�r�r%r%r&�vulnerability_affects_system�sz0VulnerabilityParser.vulnerability_affects_system)�installed_pkgs_by_sourceccs8|��D]*\}}t|���D]\}}|||fVqqdSr")rx�sorted)r$r��
source_pkgZbinary_pkgsr�Zbinary_installed_versionr%r%r&�_list_binary_packages�s
�z)VulnerabilityParser._list_binary_packages�r�r�cCs�i}i}|�di�}|�di��|ji�}|�|�D�]Z\}}}	|�|i�}
|
�di�}t|�|
���dd�d�D�]\}}
|�|d�}|
�d�}|
�d	�}|j||d
�r�|j|||	||d�|j||||d�qrz(||�d
�}||�di��|�}Wnt	k
�rYqrYnX|�
|||��rL|j|||	|dd�|j||||d�|dk�rXqr|�|	|�rr|j|||	||||d�|j||||d�qrq4t
|�d�||d�d�S)Nr�Zsecurity_issuesZsource_versionscSs|dSr�r%)�xr%r%r&rl�rmzLVulnerabilityParser.get_vulnerabilities_for_installed_pkgs.<locals>.<lambda>)�key�Zsource_fixed_versionZstatus)r�r�r�r�rzZbinary_packages�unknownr�rX)r�r��rhri)rYr�r�r�r~rxr�r�r��KeyErrorr�r�r�rg)r$r�r�r�r�Z
affected_pkgsZ
vulns_infor�r�r�r}Zvuln_source_versionsrqZvulnr�r�r�rzr�r%r%r&�&get_vulnerabilities_for_installed_pkgs�s����

��������
���
��	�
���z:VulnerabilityParser.get_vulnerabilities_for_installed_pkgs)r(r)r*r��abc�abstractmethodrr^rr~r�r�r�r�r�r�rr�rr�r�r�r�r%r%r%r&r|�sd

�


�	
�
�
�


���:�

�r|)�	metaclassc@sheZdZdeeed�dd�Zdd�Zeeefd�dd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�ZdS)�VulnerabilityResultCacheN)r�r9cCs2|pt��j|_||_tttttdd�d�|_	dSrC)
r
r:r9r�rr,rrr�dpkg_status_cache)r$r�r9r%r%r&r's��z!VulnerabilityResultCache.__init__cCstj�t|j|jt�Sr")r?r@rArr9r�rr=r%r%r&�_get_result_cache_paths�z/VulnerabilityResultCache._get_result_cache_path)�vulnerability_datacCs@t��r<t��pd}|j�t|d��t�|�	�t
�|��dS)Nrr.)rr\r
�get_dpkg_status_timer�rLr,r
rGr�rHrI)r$r��latest_dpkg_status_timer%r%r&�save_result_caches���z*VulnerabilityResultCache.save_result_cachecCs(t��p
d}|j��}|sdS||jkS)NrT)r
r�r�rNr-)r$r�Zdpkg_status_cache_objr%r%r&�_has_apt_state_changed&s

z/VulnerabilityResultCache._has_apt_state_changedcCstj�|���Sr")r?r@�existsr�r=r%r%r&�_cache_result_exists.sz-VulnerabilityResultCache._cache_result_existscCs|��sdS|��rdSdS)NFT)r�r�r=r%r%r&�_is_cache_result_valid1s
z/VulnerabilityResultCache._is_cache_result_validcCs|��Sr")r�r=r%r%r&�is_cache_valid:sz'VulnerabilityResultCache.is_cache_validcCst�t�|����Sr")rHrPr
rQr�r=r%r%r&�get_result_cache=sz)VulnerabilityResultCache.get_result_cache)N)r(r)r*r^rr'r�rrr�r�r�r�r�r�r%r%r%r&r�s
	r�)�parserr8r9cCsht||d�}t||jd�}|��}|jsD|��rDt|��|��d�St	�}|j
||d�}|�|j�|S)Nr7)r9r�r�r�)
r6r�r�rYr>r�rgrZr�rr�r�ri)r�r8r9r�Zvulnerabilities_resultZvulnerabilities_json_datar�Zvulnerabilities_parser_resultr%r%r&�get_vulnerabilitiesAs2������r�)>r�Zdatetime�enumrHr?�collectionsr�	functoolsr�typingrrrrrZurllib.parser	Zuaclientr
rrr
rZ'uaclient.api.u.pro.security.fix._commonrZ-uaclient.api.u.pro.status.enabled_services.v1rZuaclient.configrZuaclient.data_typesrrrrZuaclient.defaultsrrrrrrZuaclient.entitlements.fipsrrZuaclient.files.data_typesrZuaclient.files.filesrr r,�unique�Enumr0r6r^rfrgrj�ABCMetar|r�r�r%r%r%r&�<module>sP 
Z���	6?�