a
    h‘õj’†  ã                   @  s  U 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mZ ddl	m
Z
 ddlmZ h d£Zh d£Zd	h d
£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœdh d£ddœd h d!£ddœd"h d#£ddœd$h d%£ddœd&h d'£ddœd(h d)£ddœd*h d+£ddœd,h d-£ddœd.h d/£ddœd0h d1£ddœd2h d3£ddœd4h d5£ddœd6h d7£ddœd8œZd9ed:< d;d<d=d;d>d?œd=d<d@dAdBdCœdDdEd@dAdFdGœd@d@dBdAdAdHœdId>dAdJdKœd=d@d<dAdLœd;d@dAdAdMœdNdJdFdOœdPœZdQedR< h dS£ZdTdU„ eddVƒD ƒZdWdXdYdZgd[d\h d]£dd^d_œd`dadbgdcddh de£ddfd_œdgdag dh¢didjh dk£ddld_œdmdXg dn¢dodph dq£ddrœdsdtdugdvdwh dx£ddrœdydXg dz¢d{d|h d}£dd~d_œddXg d€¢d�d‚h dƒ£dd„d_œfZd…ed†< d‡dˆœd‰dŠ„Zd‹dŒd�œdŽd�„Zd�d‘d’œd“d”„Zd�d•d’œd–d—„Zd�d‘d’œd˜d™„Zdšd›dœdšd�d�dždŸœZd ed¡< d¢d£dBdBd¤œZd ed¥< d¦d¦d§d§d¨d©œZd edª< d«ZdŒd¬d­œd®d¯„Zd°d‘d±œd²d³„Z dŒd‘d´œdµd¶„Z!d‘dŒd·d¸œd¹dº„Z"d‘dŒd»d¸œd¼d½„Z#d‘d•d¾œd¿dÀ„Z$d°d dÁdÂœdÃdÄ„Z%d°d d•dÅdÆœdÇdÈ„Z&dŒdŒdÉdÊœdËdÌ„Z'dŒdÍd­œdÎdÏ„Z(d•d•dÐdÑœdÒdÓ„Z)d•d•d»dÑœdÔdÕ„Z*dŒdŒdÍdÖd×œdØdÙ„Z+dŒdÚd­œdÛdÜ„Z,dçdÝdÞdÞdÝdßœdàdá„Z-dÞdˆœdâdã„Z.e/däk�rze0e.ƒ ƒ‚W nF e1e2f�y Z3 z(e4dåe3› �ej5dæ� e0dƒ‚W Y dZ3[3n
dZ3[30 0 dS )èaf  Generate conservative UN R155 V3.2.1 review mappings with post-production support relevance.

V3 preserves the V2-compatible ``mappings`` array for Annex 5 Part A candidates,
but adds:

* phrase-aware technical concepts;
* attack-path bridges between a threat consequence and Annex 5 mechanisms;
* negative compatibility gates to reduce obvious false positives;
* R155 CSMS/type-approval relations outside Annex 5;
* explicit review-only output. No mapping is automatically accepted.

The output remains suitable for build_r155_test_pages_v2_2.py because the
existing top-level ``mappings`` structure is retained.
é    )ÚannotationsN)Údefaultdict)ÚPath)ÚAny>   ÚtoZcyberZwithinZacrossÚaÚfromZattacksZthroughZtheZvehicleZofÚasZsystemsZattackZvehiclesÚinÚwithÚrelatedZ	regardingÚcybersecurityÚforÚandZanÚorZonZintoÚsystemZby>!   ZsecurityZapplicationZlostZhostedZthirdZcriticalZimpactZpartyZ	componentZ
componentsÚserviceÚdataZrisksÚservicesÚexternalÚendZinformationZriskZusedZfunctionr   ZsoftwareZinsufficientZoemZapplicationsÚmanufacturersZ	functionsÚlossZdigitalÚmanufacturerZusingÚsupportZ
management)úend of supportúend-of-supportzend of lifezend-of-life>   ZlegacyZobsoleteZobsolescenceÚeosZunsupportedÚeolé   )ÚphrasesÚaliasesÚmin_aliases)z loss of information in the cloudzcloud data losszcloud information loss>   ZbackupÚavailabilityÚcloudr   é   )zback-end serverzbackend serverzback end serverzserver compromise>   ZinternetÚbackendÚremoter$   Úserver)zbackend outagezback-end outagezserver disruptionzservices being disrupted>   r&   r(   ÚoutageÚ
disruptionr#   )úsupply chainzthird-partyzthird partyzservice providerztier 1ztier 2>   Z
contractedÚproviderÚsupplierÚ
dependencyÚdependenciesÚ	suppliers)zvehicle takeoverzloss of controlzgain controlzremote controlzunsafe vehicle>   Z
immobilizeZtakeoverZsteeringÚdisableÚcontrolZacceleratingZbraking)zremotely operatezremote operationzremote command>   Zcommandr'   r2   ZoperateÚ
telematics)zautonomous drivingzautomated drivingzself drivingzdriving functions>   ZadasÚlidarÚ
perceptionZ	automatedÚcameraZ
autonomous)zsensor inputzsensor inputszsensor spoofingúphysical manipulation>   ÚsensorsÚspoofingr4   Zmanipulationr5   Úsensorr6   Zradar)zinfotainment systemzinfotainment unitz	head unit>   Ú	bluetoothÚwifiZmediazwi-fiÚusbZinfotainment)zexternal connectivityzexternal interfacezdiagnostic portzobd port>   r;   r<   Z
diagnosticr=   Z	interfaceZportZobdr3   )zcommunication channelzcommunications channelzsession hijackingzman in the middle>   ZnetworkZmessagesÚmessageZreplayr9   ZcommunicationZchannel)zcode injectionzdata injectionzmanipulation of vehicle datazmanipulation of vehicle code>   Z	injectionZfirmwareZmodifyZerasureZ	overwriteÚcodeZmodified)znetwork separationznetwork segmentationz#circumvention of network separation>   ZgatewayZpivotZ	isolationZcircumventionZsegmentationZ
separation)zunauthorized accesszunauthorised accesszabuse of privilegeszprivilege abuse>   ÚaccessZ	privilegeZunauthorizedZunauthorisedZcredentialsZbackdoor)zdata exfiltrationzextract datazinformation disclosureÚeavesdropping>   Z
disclosureZexfiltrationÚextractZ	espionagerA   )zpersonal datazprivacy informationzlocation trackingzvehicle occupantszbehavioral tracking>   Z	occupantsZoccupantÚlocationZsurveillanceÚprivacyZtrackingÚ
monitoring)zuse in botnetszvehicle botnetzdistributed attackzdistributed denial of service>   ZscalableZdistributedZbotnetZddosZbotnetsZ	endpoints)zservice outagezservices outagezmobility serviceszstop functioning>   ZinterruptionZ	interruptr)   r*   r   r#   )zshort encryption keyszcryptographic compromisezlong validityzweak cryptography>   ÚkeysZcryptographicZcertificateZ
encryptionZcryptographyZcertificates)zelectronic hardwarez"replacement of electronic hardwarer7   >   Zphysicalr8   Zhardwarer:   Zreplacement)zsensitive dataz$loss or compromise of sensitive datazdata breach>   r   rD   ZtheftZconfidentialityZ	sensitive)Zend_of_supportZcloud_information_lossÚbackend_compromiseÚbackend_disruptionÚsupplier_dependencyÚvehicle_controlÚremote_operationÚautonomous_drivingÚsensor_manipulationÚinfotainment_entryÚexternal_connectivityÚcommunication_channelÚcode_data_manipulationÚnetwork_separationÚunauthorized_accessÚdata_exfiltrationÚprivacy_surveillanceÚbotnet_distributed_attackÚservice_availabilityÚcryptographic_weaknessZphysical_sensor_hardwareÚsensitive_data_losszdict[str, dict[str, Any]]ÚCONCEPTSg333333Ó?g¤p=
×£Ð?gìQ¸…ëÑ?g)\�Âõ(Ì?)rO   rP   rQ   rR   rS   ç¸…ëQ¸Î?ç
×£p=
Ç?çš™™™™™É?)rK   rR   rQ   rP   rM   g{®GázÔ?gq=
×£pÍ?ç¸…ëQ¸¾?)rM   rR   rQ   rP   rX   )rS   rO   rG   rQ   rP   gÃõ(\�ÂÕ?gìQ¸…ëÁ?)rH   rG   rP   rO   )rG   rP   rS   rO   )rT   rS   rP   rG   g{®GázÄ?)rG   rS   rQ   )rN   rJ   rL   rV   rW   rT   rU   rI   zdict[str, dict[str, float]]ÚATTACK_PATHS>   )rL   rY   )rV   rY   )rJ   rY   )rW   rY   c                 C  s   i | ]}d |› �d |› �“qS )z4.3.© )Ú.0Znumberr`   r`   ú#build_r155_review_mapping_v3_2_1.pyÚ
<dictcomp>Ü   ó    rc   é   zR155-CSMS-GOVERNANCEZcsmsz2.3z7.2.2.2(a-d)zHCybersecurity governance, responsibilities and risk management processes)zinsufficient priorityzregulation and oversightzsafety/securityÚ
governance>   Ú	oversightZ
compliancer   rf   Ú
regulationÚpriority))ri   rf   rh   rg   )Úrelation_idÚlayerÚ
referencesÚtitler    r!   r"   Úrequired_groupszR155-AUTHORITY-COMPETENCEZtype_approvalz5.3.1zYCompetent personnel and uniform evaluation by Approval Authorities and Technical Services)zinsufficient oversightzvehicle authorityztechnical servicezmarket surveillance>   Ú	technicalr   Ú
competencerg   Ú	authorityÚ	knowledgeÚ
complexity))rg   rq   ro   rp   rr   rs   zR155-PRE-APPROVAL-TESTING)z5.1.2z5.1.3(d)z7.3.6z7Appropriate and sufficient testing before type approval)z+inadequate testing prior to market approvalztesting prior to approvalzinsufficient testing>   ÚtestÚmarketÚtestingÚapprovalÚverification))rv   rt   rx   )rw   ru   zR155-SUPPLIER-DEPENDENCIES)z5.1.1(a)z7.2.2.5z7.3.2z8Management of supplier and service-provider dependencies)zvehicle ecosystemr+   zthrough supplierszsupplier dependencies>   Z	ecosystemr,   r-   r.   r/   r0   )rj   rk   rl   rm   r    r!   r"   z"R155-RISK-ASSESSMENT-OTHER-THREATSZvehicle_typez7.3.3z[Exhaustive vehicle-type risk assessment including external systems and other relevant risks)zsocietal disruptionzsystemic riskzshared dependencyzcascading effects>   ZsystemicZ	cascadingZinteractionsZfleetZsocietalr   zR155-POST-PRODUCTION-SUPPORT)z2.7z
7.2.2.1(c)z
7.2.2.2(g)z7.2.2.3ú7.2.2.4z:Cybersecurity support throughout the post-production phase)zreduced cybersecurity supportr   r   úpost-productionúpost production>   rz   r   Zpostr   r   Ú	lifecycler   r   r   r   Z
production))r   rz   r{   r   r   r   r   zR155-MONITORING-RESPONSE)z7.2.2.2(g-h)ry   z7.4zGContinual monitoring, response and reporting over the vehicle lifecycle)rE   zincident responserz   znew cyber threats>   Ú	reportingÚdetectZincidentÚresponser|   rE   ))rE   r~   r   r}   rz   ztuple[dict[str, Any], ...]ÚREQUIREMENT_RULESzargparse.Namespace)Úreturnc                  C  sp   t jdd�} | jdttdƒd� | jdttdƒd� | jdttd	ƒd� | jd
tdd� | jdtdd� |  ¡ S )Nz Build UN R155 V3 review mappings)Údescriptionz
--registerzdata/threats_register.json)ÚtypeÚdefaultz--annex5zregulatory/r155/annex5.jsonz--outputz)regulatory/r155/r155_threat_mappings.jsonz--min-scorer]   z--max-candidatesé   )ÚargparseÚArgumentParserÚadd_argumentr   ÚfloatÚintÚ
parse_args)Úparserr`   r`   rb   r‹   0  s    r‹   r   zdict[str, Any])Úpathr�   c              
   C  s¢   zt  | jdd�¡}W nn tyJ } ztd| › �ƒ|‚W Y d }~nDd }~0  t jy„ } z"td| › d|› �ƒ|‚W Y d }~n
d }~0 0 t|tƒsžtd| › �ƒ‚|S )Núutf-8©ÚencodingzInput file not found: zInvalid JSON in z: zExpected JSON object in )ÚjsonÚloadsÚ	read_textÚFileNotFoundErrorÚ
ValueErrorZJSONDecodeErrorÚ
isinstanceÚdict)r�   r   Úexcr`   r`   rb   Úload:  s    $,
r™   r   Ústr)Úvaluer�   c                 C  s@   t | pdƒ ¡  dd¡ dd¡}t dd|¡}t dd|¡ ¡ S )NÚ u   â€“ú-u   â€”z[-_/]+ú z\s+)rš   ÚlowerÚreplaceÚreÚsubÚstrip)r›   Útextr`   r`   rb   Ú	normalizeF  s     r¥   zset[str]c                 C  s   dd„ t  dt| ƒ¡D ƒS )Nc                 S  s$   h | ]}t |ƒd kr|tvr|’qS )r%   )ÚlenÚSTOP)ra   Zwordr`   r`   rb   Ú	<setcomp>M  s   ÿztokens.<locals>.<setcomp>z	[a-z0-9]+)r¡   Úfindallr¥   ©r›   r`   r`   rb   ÚtokensL  s    ÿr«   c                 C  s0   t | tttfƒr$d dd„ | D ƒ¡S t| p,dƒS )Nrž   c                 s  s   | ]}|rt |ƒV  qd S ©N©rš   )ra   Úitemr`   r`   rb   Ú	<genexpr>U  rd   zjoin_field.<locals>.<genexpr>rœ   )r–   ÚlistÚtupleÚsetÚjoinrš   rª   r`   r`   rb   Ú
join_fieldS  s    r´   g      ð?g333333ë?gffffffî?gÍÌÌÌÌÌì?g      è?)rm   Útitle_nlr‚   Úannex5_areaZtermsZsurfacesZtagszdict[str, float]ÚCORE_FIELD_WEIGHTSgffffffÖ?g      Ð?)ÚcategoryZprimary_domainZsecondary_domainsZaffectsÚCATEGORY_FIELD_WEIGHTSç{®Gáz´?g¸…ëQ¸®?ç{®Gáz¤?)Zdomain_rationaleZimpact_rationaleZlikelihood_rationaleZrisk_rationaleri   ÚCONTEXT_FIELD_WEIGHTS©rm   rµ   r‚   zdict[str, dict[str, str]])Úthreatr�   c                   s6   ‡ fdd„t D ƒ‡ fdd„tD ƒ‡ fdd„tD ƒdœS )Nc                   s2   i | ]*}t ˆ  |d ¡ƒ ¡ r|t ˆ  |d ¡ƒ“qS ©rœ   ©r´   Úgetr£   ©ra   Úfield©r¾   r`   rb   rc   |  s   þz!threat_fields.<locals>.<dictcomp>c                   s2   i | ]*}t ˆ  |d ¡ƒ ¡ r|t ˆ  |d ¡ƒ“qS r¿   rÀ   rÂ   rÄ   r`   rb   rc   �  s   þc                   s2   i | ]*}t ˆ  |d ¡ƒ ¡ r|t ˆ  |d ¡ƒ“qS r¿   rÀ   rÂ   rÄ   r`   rb   rc   †  s   þ)Úcorer¸   Úcontext)r·   r¹   r¼   rÄ   r`   rÄ   rb   Úthreat_fieldsz  s    
þ
þ
þõrÇ   zdict[str, str])Úfieldsr�   c                 C  s   d  dd„ |  ¡ D ƒ¡S )Nrž   c                 s  s   | ]}|r|V  qd S r¬   r`   ©ra   r›   r`   r`   rb   r¯   �  rd   z!combine_fields.<locals>.<genexpr>)r³   Úvalues©rÈ   r`   r`   rb   Úcombine_fieldsŽ  s    rÌ   )Únoder�   c              
   C  s<   d  dd„ |  dd¡|  dd¡|  dd¡|  dd¡fD ƒ¡S )	Nrž   c                 s  s   | ]}|rt |ƒV  qd S r¬   r­   rÉ   r`   r`   rb   r¯   “  s   ýznode_text.<locals>.<genexpr>Zlabelrœ   Zcategory_titler‚   Zexample)r³   rÁ   )rÍ   r`   r`   rb   Ú	node_text’  s
    
þÿrÎ   z"tuple[bool, str | None, list[str]])r¤   Úruler�   c                 C  st   t | ƒ}| dd¡D ]"}t |ƒ|v rdt|ƒg f  S qtt|ƒt| dtƒ ¡ƒ@ ƒ}t|ƒt| dd¡ƒkd |fS )Nr    r`   Tr!   r"   r%   )r¥   rÁ   rš   Úsortedr«   r²   r¦   rŠ   )r¤   rÏ   Z
normalizedÚphraseZ
alias_hitsr`   r`   rb   Úrule_match_details›  s    rÒ   Úboolc                 C  s   t | |ƒd S )Nr   )rÒ   )r¤   rÏ   r`   r`   rb   Ú	rule_hits¤  s    rÔ   )r¤   r�   c                   s   ‡ fdd„t  ¡ D ƒS )Nc                   s   h | ]\}}t ˆ |ƒr|’qS r`   )rÔ   )ra   ÚnamerÏ   ©r¤   r`   rb   r¨   ©  rd   zconcept_hits.<locals>.<setcomp>)rZ   ÚitemsrÖ   r`   rÖ   rb   Úconcept_hits¨  s    rØ   zdict[str, list[dict[str, Any]]])rÈ   Úweightsr�   c           
   	   C  sf   t tƒ}|  ¡ D ]L\}}t ¡ D ]:\}}t||ƒ\}}}	|s>q ||  ||| ||	dœ¡ q qt|ƒS )N)Úsource_fieldÚfield_weightÚmatched_phraseÚmatched_aliases)r   r°   r×   rZ   rÒ   Úappendr—   )
rÈ   rÙ   ÚevidencerÃ   r¤   rÕ   rÏ   ÚmatchedrÑ   r!   r`   r`   rb   Úfield_concept_evidence¬  s    ürá   z,tuple[float, set[str], list[dict[str, Any]]])Úthreat_field_maprÙ   Únode_tokensr�   c           
      C  sv   d}t ƒ }g }|  ¡ D ]T\}}t|ƒ|@ t }|s4q|| }	||	t|ƒ 7 }||O }| ||	t|ƒdœ¡ q|||fS )Nç        )rÚ   rÛ   Úmatched_terms)r²   r×   r«   ÚWEAK_TOKENSr¦   rÞ   rÐ   )
râ   rÙ   rã   Útotalrà   Ú
provenancerÃ   r¤   Z
field_hitsZweightr`   r`   rb   Úweighted_overlap¿  s     ý
ré   z!tuple[bool, list[dict[str, Any]]])r¾   rÏ   r�   c                   sÂ  t | ƒ‰ d}‡ fdd„|D ƒ}ˆ d }tt|ƒƒ‰| dd¡D ]Z}dd„ |D ƒ}tˆƒ}t‡fd	d
„|D ƒƒ}t|dd„ |D ƒ@ ƒ}	|s>|	s>dg f  S q>g }
| ¡ D ].\}}t||ƒ\}}}|r¦|
 	|||dœ¡ q¦|
râd|
fS t
ƒ }| ¡ D ]"}|t|ƒt
| dt
ƒ ¡ƒ@ O }qð|�s"dg fS t
ƒ }g }| ¡ D ]H\}}t|ƒt
| dt
ƒ ¡ƒ@ }|�r4||O }| 	|dt|ƒdœ¡ �q4t||B ƒt| dd¡ƒk}|�s¨dg fS dddt|ƒdœg| fS )zJRequire primary scenario evidence; category can support but not originate.r½   c                   s&   i | ]}|ˆ d  v r|ˆ d  | “qS ©rÅ   r`   )ra   rÕ   rË   r`   rb   rc   Ú  rd   z*requirement_rule_match.<locals>.<dictcomp>r¸   rn   r`   c                 S  s   h | ]}t |ƒ’qS r`   )r¥   ©ra   Ztermr`   r`   rb   r¨   á  rd   z)requirement_rule_match.<locals>.<setcomp>c                 3  s   | ]}d |v r|ˆ v V  qdS )rž   Nr`   rë   )Úprimary_textr`   rb   r¯   ã  rd   z)requirement_rule_match.<locals>.<genexpr>c                 S  s   h | ]}d |vr|’qS )rž   r`   rë   r`   r`   rb   r¨   ä  rd   F)rÚ   rÜ   rÝ   Tr!   Nr"   r%   Ztitle_or_description)rÇ   r¥   rÌ   rÁ   r«   ÚanyrÓ   r×   rÒ   rÞ   r²   rÊ   rÐ   r¦   rŠ   )r¾   rÏ   Zprimary_namesZprimaryZ	secondaryÚgroupZnormalized_groupZprimary_tokensZ
phrase_hitZ	token_hitrß   rÃ   r¤   rà   rÑ   r!   Zprimary_aliasesZcategory_aliasesZsupport_evidenceÚhitsZenoughr`   )rÈ   rì   rb   Úrequirement_rule_matchÖ  sR     ýürð   z
str | Nonec                   s.   t |  d¡pdƒ‰ t‡ fdd„t ¡ D ƒd ƒS )Nr¶   rœ   c                 3  s   | ]\}}|ˆ v r|V  qd S r¬   r`   )ra   Zarear¸   ©Úrawr`   rb   r¯   	  rd   z explicit_area.<locals>.<genexpr>)rš   rÁ   ÚnextÚAREA_TO_CATEGORYr×   rÄ   r`   rñ   rb   Úexplicit_area  s    rõ   ztuple[float, list[str]])Úthreat_conceptsÚnode_conceptsr�   c                 C  s~   d}g }t | ƒD ]d}t |ƒD ]V}t |i ¡ |d¡}||krR|}|› d|› �g}q|r||kr| |› d|› �¡ qq||fS )Nrä   z->)rÐ   r_   rÁ   rÞ   )rö   r÷   ÚscoreÚpathsÚsourceÚtargetZboostr`   r`   rb   Úbest_attack_path  s    rü   c                   s   t ‡ fdd„| D ƒƒS )Nc                 3  s$   | ]}ˆ D ]}||ft v V  q
qd S r¬   )ÚINCOMPATIBLE_CONCEPTS)ra   ÚleftÚright©r÷   r`   rb   r¯     rd   zincompatible.<locals>.<genexpr>)rí   )rö   r÷   r`   r   rb   Úincompatible  s    r  zItuple[float, list[str], list[str], list[str], str, dict[str, Any]] | None)r¾   rÍ   Úexplicitr�   c           "        sî  t | ƒ‰ tˆ d ƒ}tˆ d ƒ}tˆ d ƒ}t|ƒ}t|ƒ}tˆ d tƒ}tˆ d tƒ}	‡ fdd„tD ƒ}
t|
dd„ |
D ƒƒ}t|ƒ}t|ƒ}t|	ƒ}t	|ƒ}||@ }t
ˆ d t|ƒ\}}}t
ˆ d t|ƒ\}}}t
ˆ d t|ƒ\}}}||B |B }|||	|| | t|| | dƒdœ}|�rH| d	¡|k�rHd
t|ƒt|ƒg d|fS t||ƒ\}}t|�p`||ƒ�r‚|�s‚|dk �r‚|�s‚d S |�ròdtddt|ƒ ƒ }|tdd| ƒ7 }|tdd| ƒ7 }|tdd| ƒ7 }t|dƒt|ƒt|ƒ|d|fS |�rPd| }|tdd| ƒ7 }|tdd| ƒ7 }|tdd| ƒ7 }t|dƒt|ƒg |d|fS t|ƒ}t|ƒdk �sp|dk �rtd S t|ƒ|B } | �r–t|ƒt| ƒ nd }!d!|! td"d| ƒ }|tdd| ƒ7 }|tdd| ƒ7 }t|d#ƒt|ƒg g d$|fS )%NrÅ   r¸   rÆ   c                   s&   i | ]}|ˆ d  v r|ˆ d  | “qS rê   r`   rÂ   rË   r`   rb   rc   *  s   þz#candidate_score.<locals>.<dictcomp>c                 S  s   i | ]}|t | “qS r`   )r·   rÂ   r`   r`   rb   rc   1  rd   é   )Úattack_source_conceptsÚcore_conceptsZsupporting_category_conceptsZterm_evidenceZweighted_term_scoreÚcategory_referencegffffffæ?Úexplicit_annex5_areag      ø?g¸…ëQ¸Þ?r\   gìQ¸…ë±?gš™™™™™¹?gš™™™™™™?r»   g¸…ëQ¸Ž?g{®Gáz”?g{®Gáz„?gq=
×£pí?Zshared_core_conceptgR¸…ëQØ?rº   g¸…ëQ¸ž?g{®Gázt?g)\�Âõ(ì?Zcore_attack_path_bridger%   r   rä   r^   r[   g
×£p=
ç?Zweighted_core_lexical_seed)rÇ   rÌ   rÎ   r«   rá   r·   r¹   ÚATTACK_SOURCE_FIELDSr²   rØ   ré   r¼   ÚroundrÁ   rÐ   rü   r  Úminr¦   )"r¾   rÍ   r  Zcore_rawZcategory_rawZcontext_rawZnode_rawrã   Zcore_concept_evidenceZcategory_concept_evidenceZattack_source_fieldsZattack_source_evidencer  r  Zcategory_conceptsr÷   Zshared_core_conceptsZcore_weightZ
core_termsZcore_term_evidenceZcategory_weightZcategory_termsZcategory_term_evidenceZcontext_weightZcontext_termsZcontext_term_evidenceÚoverlaprè   Z
path_scorerù   rø   Zcore_fields_with_termsÚunionZjaccardr`   rË   rb   Úcandidate_score  s|    
þþ
ÿ

ÿ

ÿ

û(r  zlist[dict[str, Any]]c                 C  s‚   g }t D ]t}t| |ƒ\}}|s q| |d › d| d › �| d |  dd¡d|d t|d ƒ|d d	d
|dd d d d dœdœ¡ q|S )Nrj   r�   Úidrm   rœ   úUN-R155rk   rl   Úreviewr   zuCandidate generated from title/description evidence, optionally supported by category fields; expert review required.©ZdecisionZreviewed_byZreviewed_atZnote)rj   Ú	threat_idÚthreat_titleÚregulation_idrk   rl   rm   ÚstatusÚrelationshipÚfield_evidenceÚ	rationaler  )r€   rð   rÞ   rÁ   r°   )r¾   Ú	relationsrÏ   rà   rß   r`   r`   rb   Úrequirement_relationsq  s(    

ô
r  zlist[tuple[Any, ...]]rŠ   )Ú
candidatesÚlimitÚmax_per_attack_pathr�   c           	      C  sª   g }g }t tƒ}| D ]l}|d }|r,|d nd|d › �}|| |krR| |¡ q| |¡ ||  d7  < t|ƒ|kr|  S q|D ]}| |¡ t|ƒ|kr† q¦q†|S )zKSelect high scoring candidates without one attack path monopolising output.é   r   zbasis:r…   r   )r   rŠ   rÞ   r¦   )	r  r  r  ÚselectedZdeferredZpath_countsÚ	candidaterù   Zfamilyr`   r`   rb   Úselect_diverse_candidatesˆ  s$    



r!  c                    sZ  t ƒ } t| jƒ}t| jƒ}| dg ¡}dd„ | dg ¡D ƒ}t|tƒrJ|sRtdƒ‚g }g }d}i ‰ i ‰|D �]‚}t|tƒrj| d¡s†qjt	|d ƒ}	t
|ƒ}
g }|D ]V}t|||
ƒ}|d u rÄ|d7 }q¢|\}}}}}}|| jk ràq¢| |||||||f¡ q¢|jd	d
„ dd� t|| jdd�}t|ƒˆ |	< |D ]¢\}}}}}}}| d|	› dt	|d ƒ dd¡› �|	| dd¡|d |d | d¡ddt|dƒ|||||| dg ¡|dk�r¶d|
› d�ndd d d d dœdœ¡ �q,t|ƒ}t|ƒˆ|	< | |¡ qjd d„ |D ƒ}g }|D ]P}	ˆ  |	d¡}ˆ |	d¡}|�r.d!}n|�r:d"}nd#}| |	|||dd$œ¡ �qd%d&d'd(dd)t	| jƒt|ƒd*œt	| jƒt|ƒd+œd,d,ddttƒdttƒttƒttƒttƒdd-œt|ƒt|ƒtd.d/„ ˆ  ¡ D ƒƒtd0d/„ ˆ ¡ D ƒƒ‡ ‡fd1d„|D ƒ|d2œ|||d3œ}| jjj ddd4� | jj!t"j#|dd,d5�d6 d7d8� t$t"j#|d9 dd:�ƒ dS );NÚthreatsc                 S  s(   g | ] }t |tƒr| d ¡dkr|‘qS )ÚpartÚA)r–   r—   rÁ   )ra   rÍ   r`   r`   rb   Ú
<listcomp>©  rd   zmain.<locals>.<listcomp>Únodesz1Threat register or Annex 5 Part A nodes are emptyr   r  r   c                 S  s(   | d t | d ƒt | d ƒt | d ƒfS )Nr   r  r  r%   )r¦   )r®   r`   r`   rb   Ú<lambda>Ä  rd   zmain.<locals>.<lambda>T)ÚkeyÚreverser%   )r  zR155-r�   Z	referenceÚ.Ú_rm   rœ   r  r   r  r  Zrelated_mitigationsr  z3Threat register explicitly identifies Annex 5 area z�Candidate generated from shared technical concepts, an attack-path bridge, or multiple non-generic terms; expert review required.r  )Z
mapping_idr  r  Zregulatory_node_idZregulatory_referenceZregulatory_categoryr  r  rø   Úbasisrå   Zmatched_conceptsZattack_pathsr  Zmitigation_nodesr  r  c                 S  s,   g | ]$}t |tƒr| d ¡rt|d  ƒ‘qS )r  )r–   r—   rÁ   rš   )ra   Útr`   r`   rb   r%  ç  rd   Zannex5_candidates_generatedZ2r155_requirement_relation_without_annex5_candidateZno_direct_mapping_generated)r  ÚclassificationZannex5_candidate_countÚr155_requirement_relation_countÚexpert_review_requiredz2.2.1zNCRC-R155-REVIEW-3.2.1r  zUN-R155-ANNEX-5z¤field-weighted scenario matching with title/description attack origins, attack-path diversity limits, compatibility gates, and hard-gated R155 requirement relations)ÚfileZthreat_count)r1  Zpart_a_reference_countF)Zaccepted_createdZlegal_compliance_inferencer0  Z&recommended_independent_expert_reviewsZ#weak_tokens_cannot_create_candidateZminimum_non_generic_overlapZ!core_fields_can_create_candidatesZcategory_fields_support_onlyZcontext_fields_rank_onlyZattack_path_source_fieldsZ*max_candidates_per_attack_path_before_fillc                 s  s   | ]}|d kV  qdS ©r   Nr`   rÉ   r`   r`   rb   r¯     rd   zmain.<locals>.<genexpr>c                 s  s   | ]}|d kV  qdS r2  r`   rÉ   r`   r`   rb   r¯     rd   c                   s(   g | ] }ˆ   |d ¡sˆ  |d ¡s|‘qS )r   )rÁ   )ra   Útid©ZcountsZrelation_countsr`   rb   r%    s   )Zannex5_mapping_countr/  Zthreats_with_annex5_candidatesZ"threats_with_requirement_relationsZthreats_without_any_candidateZcandidate_pairs_rejected)Zschema_versionZmapping_set_idr  Zannex_idr  ÚmethodZsource_threat_registerZregulatory_sourceZrulesÚsummaryZmappingsZregulatory_relationsZthreat_assessments)ÚparentsÚexist_ok)ÚindentZensure_asciiÚ
rŽ   r�   r6  )r9  )%r‹   r™   ÚregisterZannex5rÁ   r–   r°   r•   r—   rš   rõ   r  Z	min_scorerÞ   Úsortr!  Zmax_candidatesr¦   r    r	  r  ÚextendrÐ   ræ   r·   r¹   r¼   r  ÚsumrÊ   ÚoutputÚparentÚmkdirZ
write_textr‘   ÚdumpsÚprint)Zconfigr;  Zannexr"  r&  Zrowsr  Zrejected_pairsr¾   r3  r  r  rÍ   Úresultrø   r  Zconceptsrù   r,  rè   r  Zcurrent_relationsZ
threat_idsZassessmentsZannex_countZrequirement_countr.  r?  r`   r4  rb   Úmain¤  sà    



þ

ÿëû	õÿø
Þ% rE  Ú__main__zERROR: )r1  )r%   )6Ú__doc__Z
__future__r   r†   r‘   r¡   ÚsysÚcollectionsr   Úpathlibr   Útypingr   r§   ræ   rZ   Ú__annotations__r_   rý   Úrangerô   r€   r‹   r™   r¥   r«   r´   r·   r¹   r¼   r  rÇ   rÌ   rÎ   rÒ   rÔ   rØ   rá   ré   rð   rõ   rü   r  r  r  r!  rE  Ú__name__Ú
SystemExitr•   ÚOSErrorr˜   rC  Ústderrr`   r`   r`   rb   Ú<module>   s   ýýýýýýýýýýýýýýýýýýýýýý–uûûûûüüüýÑ7øøøù
ù
ïø¼Q
ù
üû
		1S ý 
