a
    Õ…õjão  ã                   @  s<  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Zddl	Z	ddl
mZmZ ddlmZ ddlmZ ddd	œd
d„Zdddœdd„Zddœdd„Zd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*d+d,d-d.d/œZddd0œd1d2„Zdddœd3d4„Zdd5d6dd7œd8d9„Zddd:œd;d<„Zdd6d6d5d6d6dd=œd>d?„Zd@ZdddAœdBdC„Z dddAœdDdE„Z!ddFddGœdHdI„Z"ddddJœdKdL„Z#d6dMddNœdOdP„Z$dFdœdQdR„Z%e&dSk�r8ze'e%ƒ ƒ‚W nJ e(e)e*e+f�y6 Z, z(e-dTe,› �e	j.dU� e'dVƒ‚W Y dZ,[,n
dZ,[,0 0 dS )Wz]Build V2.2 isolated UN R155 traceability test copies of existing T-xxx Threat Register pages.é    )ÚannotationsN)ÚCounterÚdefaultdict)ÚPath)ÚAnyr   Ústr)ÚvalueÚreturnc                 C  s   t jt| d ur| ndƒdd�S )NÚ T)Zquote)ÚhtmlÚescaper   )r   © r   úbuild_r155_test_pages_v2_3.pyÚe   s    r   r   zdict[str, Any])Úpathr	   c                 C  s˜   |   ¡ std| › �ƒ‚zt | jdd�¡}W nN tjyz } z4td| › d|j› d|j› d|j	› �ƒ|‚W Y d }~n
d }~0 0 t
|tƒs”td| › �ƒ‚|S )	NzInput file not found: úutf-8©ÚencodingzInvalid JSON in z: line z	, column z: zExpected JSON object in )Úis_fileÚFileNotFoundErrorÚjsonÚloadsÚ	read_textZJSONDecodeErrorÚ
ValueErrorÚlinenoÚcolnoÚmsgÚ
isinstanceÚdict)r   ÚdataÚexcr   r   r   Ú	load_json   s     ÿþ
r!   zargparse.Namespace)r	   c                  C  s°   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tdƒd� | jdttdƒd� | jdttdƒd� | jdttdƒd� | jdddd� |  ¡ S )Nz1Build isolated UN R155 test copies of T-xxx pages)Údescriptionz--project-rootÚ.)ÚtypeÚdefaultz--regulatory-rootzregulatory/r155z--mapping-filez.regulatory/r155/r155_threat_mappings_v3_2.jsonú--threat-registerzdata/threats_register.jsonz--source-pageszsite/threatsz--outputz	site/r155z--validatorz regulatory/r155/validate_r155.pyz--skip-validationÚ
store_truez'Skip validate_r155.py (not recommended))ÚactionÚhelp)ÚargparseÚArgumentParserÚadd_argumentr   Ú
parse_args)Úparserr   r   r   Úargs#   s(    ÿýýr/   )Úrootr   r	   c                 C  s   |  ¡ r|S | | S ©N)Zis_absolute)r0   r   r   r   r   Úresolve:   s    r2   )Únoder	   c                 C  s&   t |  d¡p"|  d¡p"|  d¡p"dƒS )NÚlabelÚtitler"   r
   ©r   Úget©r3   r   r   r   Ú
node_title>   s    r9   c                 C  s(   t |  d¡p|  d¡pdƒ}| dd¡S )Nr$   Z	node_typer3   Ú_ú )r   r7   Úreplace)r3   r   r   r   r   Ú	node_kindB   s    r=   )Úsourcer	   c                 C  sd   |   d¡p|   d¡pd}t|   d¡p&dƒ ¡ }| d¡sB| d¡r\dt|ƒ› d	t|ƒ› d
�S t|ƒS )NZdocument_referenceZ	source_idzOfficial sourceÚurlr
   zhttps://zhttp://z	<a href="z," target="_blank" rel="noopener noreferrer">u    â†—</a>)r7   r   ÚstripÚ
startswithr   )r>   r4   r?   r   r   r   Úsource_linkG   s
    rB   )Úmappingr	   c                 C  s   t |  d¡p|  d¡pdƒS )NÚmapping_strengthZrelationshipÚ	candidater6   )rC   r   r   r   rD   O   s    rD   )z4.3.1zVehicle-related backend serversÚdirectzzVehicle-related backend services are directly covered by the Annex 5 high-level area for vehicle-related back-end servers.)z4.3.2zVehicle communication channelsrF   zaVehicle communication channels are directly covered by the corresponding Annex 5 high-level area.)z4.3.3z%Vehicle update procedures and UN R156rF   z£Vehicle software-update processes align directly with the Annex 5 high-level area for vehicle update procedures; UN R156 is retained as related regulatory context.)z4.3.4z2Unintended human actions and staff privilege abuserF   z’Human error, social engineering and misuse of authorized access align directly with the Annex 5 high-level area covering unintended human actions.)z4.3.5z%External connectivity and connectionsrF   zŽVehicle external interfaces and connected equipment align directly with the Annex 5 high-level area for external connectivity and connections.)z4.3.6z'Vehicle data and code as attack targetsrF   z¯Unauthorized access to or manipulation of vehicle data, code and credentials aligns directly with the Annex 5 high-level area covering vehicle data and code as attack targets.)z4.3.7z7Exploitable vulnerabilities and insufficient protectionrF   z¤Insufficiently protected vehicle systems and components align directly with the Annex 5 high-level area for exploitable vulnerabilities and insufficient protection.)úCross-cuttingz/Identity, cryptographic keys and trust servicesúcross-cuttingzÉCovers weaknesses across the full identity, access and cryptographic trust lifecycle that could enable unauthorized access, impersonation or trusted malicious actions across many vehicles and services.)rG   z5Perception, localization and automated-driving inputsrH   z°Covers deliberate manipulation of sensors, positioning, maps and AI inputs that could cause automated-driving systems to misunderstand the road environment and behave unsafely.)rG   z=Fleet, dispatch, teleoperation and mobility-control platformsrH   z­Covers centralized operational platforms whose compromise could allow one attacker to reroute, immobilize or manipulate multiple vehicles or essential fleets simultaneously.)rG   z4Connected road and traffic-management infrastructurerH   z©Covers road and traffic-management systems outside the vehicle that can create unsafe traffic conditions or disrupt mobility even when individual vehicles remain secure.)rG   z+Shared dependency / single point of failurerH   z®Covers concentration risk where the failure or compromise of one shared supplier, platform, component or service simultaneously affects multiple fleets or mobility providers.)zT-058zT-059zT-060zT-061zT-062zT-063zT-064zT-065zT-066zT-067zT-068zT-069)Ú	threat_idr	   c              	   C  sL   t  | ¡}|sdS |\}}}}dt|ƒ› dt|ƒ› dt|ƒ› dt|ƒ› d�	S )Nr
   z”
  <div class="r155-baseline">
    <div class="r155-baseline-head"><span class="r155-baseline-badge">RDW BASELINE</span><span class="r155-strength">zC</span></div>
    <p class="small"><strong>R155 coverage:</strong> õ    Â· z></p>
    <p class="small"><strong>Mapping rationale:</strong> z…</p>
    <p class="muted small">Protected high-level baseline. Analytical Annex 5 candidates below do not overwrite it.</p>
  </div>
)ÚRDW_BASELINEr7   r   )rI   ÚitemZcoverager5   ÚrelationÚ	rationaler   r   r   Úrender_rdw_baselinec   s    
þýýürO   c                 C  sX   dt |  dd¡ƒ› dt |  dd¡ƒ› dt |  dd¡ƒ› dt t| ƒƒ› dt t| ƒƒ› d	�S )
NzP<div class="r155-mitigation"><div class="r155-node-line"><span class="r155-ref">Úpartú-z / ÚtableÚ	referenceú</span><span class="r155-kind">z*</span></div><div class="r155-node-title">z</div></div>)r   r7   r=   r9   r8   r   r   r   Úrender_mitigationq   s    þþþ
ý
ûÿrU   zdict[str, dict[str, Any]]zlist[dict[str, Any]])rC   ÚnodesÚversionsr	   c              )     s(  t |  d¡pdƒ‰ˆˆ }g ‰ t|  d¡p,g ƒt| d¡p<g ƒ D ]}|ˆv rB|ˆ vrBˆ  |¡ qB‡ ‡fdd„|D ƒ}d dd„ |D ƒ¡pŒd	}d ‡fd
d„ˆ D ƒ¡}|s®d}t |  d¡p¼dƒ ¡ }|  d|  d¡¡}	t|	ttfƒrðt|	ƒd›nd}
d dd„ |  dg ¡D ƒ¡}dt	|ƒ› dt	|ƒ› dt	|ƒ› dt	t
| ƒƒ› dt	|
ƒ› dt	| dd¡ƒ› dt	| dd¡ƒ› dt	| dd¡ƒ› d t	| d!d¡ƒ› d"t	t|ƒƒ› d#t	t|ƒƒ› d$t	| d%¡�p¼dƒ› d&t	| d'¡�pÒdƒ› d(t	|  d)¡�pèdƒ› d*|› d+tˆ ƒ› d,|› d-|› d.t	|  d/¡ƒ› d0t	ˆƒ› d1�)S )2NZregulatory_node_idr
   Úmitigation_nodesZrelated_mitigationsc                   s8   g | ]0‰ ˆˆ   d ¡pg v s0t‡ fdd„ˆD ƒƒrˆ ‘qS )Úaffected_nodesc                 3  s    | ]}|ˆ   d ¡pg v V  qdS )rY   N©r7   ©Ú.0Zmid©Úversionr   r   Ú	<genexpr>Œ   ó    z,render_mapping.<locals>.<listcomp>.<genexpr>)r7   Úany)r\   )Úmitigation_idsÚnode_idr]   r   Ú
<listcomp>ˆ   s   ýz"render_mapping.<locals>.<listcomp>c              
   s  sV   | ]N}d t | d¡p| d¡ƒ› dt | d¡p0dƒ› dt | d¡pDdƒ› d�V  qd	S )
z<li><strong>Zamendment_labelZ
version_idu   </strong> Â· entry into force Zentry_into_forcerQ   u    Â· legal basis Zlegal_basis_referenceú</li>N)r   r7   )r\   r^   r   r   r   r_   Ž   s   ûÿþýz!render_mapping.<locals>.<genexpr>zZ<li>No node-specific amendment recorded in versions.json; base/version basis applies.</li>c                 3  s   | ]}t ˆ | ƒV  qd S r1   )rU   r[   )rV   r   r   r_   –   r`   zE<p class="muted small">No corresponding mitigation node recorded.</p>ÚstatusÚreviewÚscoreÚrelevance_scorez.2frQ   c                 s  s   | ]}d t |ƒ› d�V  qdS )z<span class="r155-term">z</span>N)r   )r\   Ztermr   r   r   r_   œ   r`   Zmatched_termsz,
<article class="r155-mapping" data-status="zN">
  <div class="r155-mapping-head">
    <div>
      <span class="r155-status ú">z*</span>
      <span class="r155-strength">zE</span>
    </div>
    <div class="r155-score">Mapping score <strong>zY</strong></div>
  </div>
  <div class="r155-node-line">
    <span class="r155-ref">Annex ÚannexÚ5u	    Â· Part rP   u
    Â· Table rR   u    Â· Ref rS   z$</span>
    <span class="r155-kind">z</span>
  </div>
  <h3>z4</h3>
  <p class="small"><strong>Category:</strong> Zcategory_referencerJ   Zcategory_titlez9</p>
  <p class="small"><strong>NCRC rationale:</strong> rN   z</p>
  <div class="r155-terms">z;</div>
  <details>
    <summary>Corresponding mitigations (z.)</summary>
    <div class="r155-mitigations">zp</div>
  </details>
  <details>
    <summary>Version and amendment traceability</summary>
    <ul class="small">z9</ul>
  </details>
  <div class="r155-id small">Mapping: Z
mapping_idu
    Â· Node: z</div>
</article>
)r   r7   ÚlistÚappendÚjoinÚlowerr   ÚintÚfloatr   rD   r=   r9   Úlen)rC   rV   rW   r3   r   Zaffected_versionsZversion_htmlZmitigationsrf   rh   Z
score_textZtermsr   )rb   rc   rV   r   Úrender_mapping}   s|    (þ
úùÿÿÿ
ûù
ö
ö
ö
ö
õ
óòòñðîíéççrt   )rM   r	   c                 C  sì   d  dd„ |  dg ¡D ƒ¡pd}t|  d¡p.dƒ dd	¡}t|  d
¡pHdƒ ¡ }|  d¡p\g }tdd„ |D ƒƒ}|r~d  |¡nd}dt|ƒ› dt|ƒ› dt|ƒ› dt|ƒ› dt|  d¡pºdƒ› dt|ƒ› dt|ƒ› dt|  d¡pâdƒ› d�S )NrJ   c                 s  s   | ]}|rt |ƒV  qd S r1   )r   ©r\   Úxr   r   r   r_   ½   r`   z.render_requirement_relation.<locals>.<genexpr>Z
referencesrQ   ÚlayerZrequirementr:   r;   rf   rg   Zfield_evidencec                 S  s.   h | ]&}t |tƒr| d ¡rt| d ¡ƒ’qS )Zsource_field©r   r   r7   r   ©r\   rL   r   r   r   Ú	<setcomp>Á   r`   z.render_requirement_relation.<locals>.<setcomp>z, z/<article class="r155-requirement" data-status="z>"><div class="r155-requirement-head"><span class="r155-status rj   rT   z</span></div><h3>r5   zR155 requirement relationz8</h3><p class="small"><strong>R155 references:</strong> z7</p><p class="small"><strong>Evidence fields:</strong> z</p><p class="muted small">rN   z>Analytical requirement-level relation; expert review required.z</p></article>)ro   r7   r   r<   rp   Úsortedr   )rM   Zrefsrw   rf   ZevidenceÚfieldsZevidence_textr   r   r   Úrender_requirement_relation¼   s,     ÿÿþýüûúÿr}   )rI   Úthreat_mappingsÚthreat_relationsrV   rW   Úsourcesr	   c                   sÜ   t dd„ |D ƒƒ}t| ƒ}d dd„ |D ƒ¡}|r@d|› d�}	nd}	d ‡ ‡fdd„|D ƒ¡}
|
sfd	}
d d
d„ |D ƒ¡}dt| ƒ› d|› d|	› dt|ƒ› d| dd¡› d| dd¡› dtdd„ |D ƒƒ› d|
› d|› d�S )Nc                 s  s    | ]}t | d ¡pdƒV  qdS ©rf   rg   Nr6   ry   r   r   r   r_   ×   r`   z&render_r155_section.<locals>.<genexpr>r
   c                 s  s   | ]}t |ƒV  qd S r1   )r}   ry   r   r   r   r_   Ù   r`   a  <div class="r155-subheading"><strong>R155 REQUIREMENT RELEVANCE</strong><p class="muted small">Candidate relations to the main R155 CSMS, vehicle-type and type-approval requirements. These are separate from Annex 5 and require expert review.</p></div><div class="r155-requirements">z</div>z®<div class="r155-subheading"><strong>R155 REQUIREMENT RELEVANCE</strong><p class="muted small">No main-text R155 requirement relation was generated for this threat.</p></div>c                 3  s   | ]}t |ˆ ˆƒV  qd S r1   )rt   ry   ©rV   rW   r   r   r_   å   r`   z°<div class="r155-empty"><strong>No R155 mapping candidates</strong><p>No candidate relationship between this threat and the current Annex 5 mapping set was generated.</p></div>c              
   s  sˆ   | ]€}d t |ƒ› dt| d¡p*| d¡p*dƒ› dt| d¡p>dƒ› �| d¡sX| d¡rvdt| d¡pn| d¡ƒ› �nd	 d
 V  qdS )z<li>rJ   Zsource_statusZsource_typerQ   Zlegal_statusZauthentic_legal_referenceZlegal_binding_referenceu    Â· authentic legal reference: r
   re   N)rB   r   r7   )r\   r>   r   r   r   r_   ë   s    öÿþýÿÿ ù	÷zô
<section class="card r155-panel" id="r155">
  <div class="r155-heading">
    <div>
      <p class="section-kicker">Regulatory traceability test</p>
      <h2>UN Regulation No. 155</h2>
      <p class="muted small">Experimental NCRC mapping of aB   to UN R155 requirements and Annex 5. A mapping is analytical traceability, not a determination of compliance, non-compliance or legal effect.</p>
    </div>
    <span class="r155-test-badge">TEST COPY</span>
  </div>
  <div class="r155-audit-relevance">
    <div class="r155-audit-kicker">Audit relevance</div>
    <p>UN R155 Annex 5 Parts A, B and C are intended to be considered in risk assessment and mitigation. The mappings below identify Annex 5 areas potentially relevant to this Threat Register entry. They do not establish compliance or non-compliance.</p>
    <div class="r155-audit-question">
      <span>Assessment question</span>
      <strong>Is there sufficient evidence that the relevant threat scenarios and corresponding mitigations were considered in the applicable risk assessment?</strong>
    </div>
  </div>
  z
  zù
  <div class="r155-subheading"><strong>ANNEX 5 ANALYTICAL CANDIDATES</strong><p class="muted small">Annex 5 relationships from the analytical mapping set, kept separate from the RDW baseline.</p></div>
  <div class="r155-summary">
    <div><strong>z6</strong><span>Mappings</span></div>
    <div><strong>rg   r   z4</strong><span>Review</span></div>
    <div><strong>Úacceptedz6</strong><span>Accepted</span></div>
    <div><strong>c                 S  s"   h | ]}|  d g ¡D ]}|’qqS )rX   rZ   )r\   rL   Úmr   r   r   rz     r`   z&render_r155_section.<locals>.<setcomp>z7</strong><span>Mitigation refs</span></div>
  </div>
  zs
  <details class="r155-sources">
    <summary>Regulatory sources and legal status</summary>
    <ul class="small">zZ</ul>
  </details>
  <div class="r155-top-return"><a href="#top">TOP</a></div>
</section>
)r   rO   ro   r   rs   r7   )rI   r~   r   rV   rW   r€   Zstatus_countsZbaseline_htmlZrequirement_bodyZrequirement_htmlÚbodyZsource_itemsr   r‚   r   Úrender_r155_sectionÏ   sH    þÿÿÿ
õúîíê
é
èçåâr†   a³  
<style id="r155-test-style">
.r155-panel{border-top:4px solid #325d88;scroll-margin-top:18px}.r155-audit-relevance{margin:16px 0;padding:15px 16px;background:color-mix(in srgb,var(--accent) 6%,var(--surface-alt));border:1px solid color-mix(in srgb,var(--accent) 22%,var(--line));border-left:4px solid var(--accent);border-radius:6px}.r155-audit-relevance p{margin:6px 0 12px}.r155-audit-kicker{color:var(--accent);font-size:.78rem;font-weight:800;text-transform:uppercase;letter-spacing:.06em}.r155-audit-question{padding:11px 12px;background:var(--bg);border:1px solid var(--line);border-radius:5px}.r155-audit-question span{display:block;color:var(--muted);font-size:.7rem;font-weight:700;text-transform:uppercase;letter-spacing:.05em;margin-bottom:4px}.r155-audit-question strong{font-size:.9rem}.r155-heading{display:flex;justify-content:space-between;gap:20px;align-items:flex-start}.r155-test-badge{padding:5px 9px;border:1px solid #b37a20;border-radius:5px;color:#b37a20;font-size:.72rem;font-weight:800;letter-spacing:.08em}.r155-summary{display:grid;grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:8px;margin:16px 0}.r155-summary>div{padding:11px;background:var(--surface-alt);border:1px solid var(--line);border-radius:5px}.r155-summary strong{display:block;font-size:1.25rem;color:var(--accent)}.r155-summary span{font-size:.72rem;color:var(--muted)}.r155-mapping{border:1px solid var(--line);border-left:4px solid #b37a20;border-radius:6px;padding:14px;margin:12px 0;background:var(--surface-alt)}.r155-mapping[data-status="accepted"]{border-left-color:#2f7d59}.r155-mapping[data-status="rejected"]{border-left-color:#8c4a4a}.r155-mapping-head,.r155-node-line{display:flex;align-items:center;justify-content:space-between;gap:10px;flex-wrap:wrap}.r155-status,.r155-strength,.r155-ref,.r155-kind,.r155-term{display:inline-block;padding:2px 7px;border-radius:10px;font-size:.7rem}.r155-status{border:1px solid currentColor;text-transform:uppercase;font-weight:800;color:#b37a20}.r155-status.accepted{color:#2f7d59}.r155-strength,.r155-kind,.r155-term{background:var(--bg);border:1px solid var(--line);color:var(--muted)}.r155-ref{font-family:monospace;background:color-mix(in srgb,var(--accent) 10%,transparent);color:var(--accent)}.r155-score,.r155-id{color:var(--muted)}.r155-mapping h3{margin:10px 0 6px}.r155-terms{display:flex;gap:5px;flex-wrap:wrap;margin:8px 0}.r155-mitigation{padding:10px 0;border-bottom:1px solid var(--line)}.r155-mitigation:last-child{border-bottom:0}.r155-node-title{margin-top:5px}.r155-empty{padding:18px;background:var(--surface-alt);border:1px solid var(--line);border-radius:6px}.r155-sources{margin-top:16px;padding-top:12px;border-top:1px solid var(--line)}.hero{display:flex;flex-wrap:wrap;align-items:flex-start;gap:6px}.hero .sub,.hero h1{flex:0 0 100%}.hero .badge,.hero .register-evidence{flex:0 0 auto}.hero .register-evidence{margin-right:0}.r155-regulatory-link{flex:0 0 auto;margin-left:auto;align-self:flex-start;display:inline-flex;align-items:center;justify-content:center;min-height:38px;padding:7px 12px;border:1px solid var(--line);border-radius:6px;background:var(--surface-alt);color:var(--accent);font-size:.82rem;font-weight:600;line-height:1.2;text-decoration:none;white-space:nowrap}.r155-regulatory-link:hover{background:var(--surface-hover);border-color:var(--accent);text-decoration:none}@media(max-width:700px){.r155-heading{flex-direction:column}.r155-regulatory-link{margin-left:auto}}
.r155-baseline{margin:16px 0;padding:14px 16px;border:1px solid var(--line);border-left:4px solid #2f7d59;border-radius:6px;background:var(--surface-alt)}.r155-baseline-head{display:flex;justify-content:space-between;gap:10px;align-items:center}.r155-baseline-badge{padding:4px 8px;border-radius:5px;background:#2f7d59;color:#fff;font-size:.7rem;font-weight:800;letter-spacing:.07em}.r155-subheading{margin:18px 0 8px;padding-bottom:8px;border-bottom:1px solid var(--line)}.r155-subheading p{margin:4px 0 0}.r155-top-return{display:flex;justify-content:flex-end;margin-top:14px}.r155-top-return a{display:inline-flex;align-items:center;justify-content:center;min-height:38px;padding:7px 12px;border:1px solid var(--line);border-radius:6px;background:var(--surface-alt);color:var(--accent);font-size:.82rem;font-weight:600;line-height:1.2;text-decoration:none;white-space:nowrap}.r155-top-return a:hover{background:var(--surface-hover);border-color:var(--accent);text-decoration:none}
.r155-requirements{display:grid;gap:10px;margin:10px 0 18px}.r155-requirement{padding:13px 14px;border:1px solid var(--line);border-left:4px solid #4f6f8f;border-radius:6px;background:var(--surface-alt)}.r155-requirement-head{display:flex;gap:8px;align-items:center;flex-wrap:wrap}.r155-requirement h3{margin:8px 0 6px}
</style>
)Úpager	   c                 C  s6   dD ] }|   d|› d�d|› d�¡} q|   dd¡} | S )N)ZgithubZcvesZpapersZsignalsZ	incidentsZaboutZ
statisticsz	href="../z.html"zhref="../../z+<a class="site-brand" href="../index.html">z.<a class="site-brand" href="../../index.html">)r<   )r‡   Únamer   r   r   Úadjust_copy_links&  s    þr‰   c                 C  s,   t jd| t jd�r| S t jdd| dt jd�S )z6Give each copied page a stable in-page target for TOP.z<body\b[^>]*\bid="top"©Úflagsz<body\b([^>]*)>z<body\1 id="top">é   ©Úcountr‹   )ÚreÚsearchÚIÚsub)r‡   r   r   r   Úadd_top_anchor2  s    r“   rq   )r‡   Úmapping_countr	   c                 C  sv   d}t jdd| dt jd�} t jd| t jt jB d�}|s>tdƒ‚| d	| ¡ … | d¡ | | d
¡ | | ¡ d	…  S )zFInsert Regulatory as the final independent control in the threat hero.zk<a class="r155-regulatory-link" href="#r155" title="Jump to UN R155 regulatory traceability">Regulatory</a>z&<a class="r155-regulatory-link".*?</a>r
   rŒ   r�   z(<div class="hero">.*?)(</div>)rŠ   z3Could not locate threat hero for Regulatory controlNé   )	r�   r’   ÚSr�   r‘   r   ÚstartÚgroupÚend)r‡   r”   ÚlinkZheror   r   r   Úadd_regulatory_hero_link9  s    ÿr›   )r‡   Úsectionr	   c                 C  s€   d| v rt jd|| t jd�} nDd}|| v r>|  ||| d¡} n$d| v rZ|  d|d d¡} ntdƒ‚d| vr||  d	td	 d¡} | S )
Nz	id="r155"z8<section class="card r155-panel" id="r155">.*?</section>rŠ   z<div class="nav">rŒ   z</main>z4Could not find insertion point in copied threat pagezid="r155-test-style"z</head>)r�   r’   r–   r<   r   ÚR155_CSS)r‡   rœ   Zmarkerr   r   r   ÚinjectE  s    rž   zdict[str, list[dict[str, Any]]])ÚthreatsÚ	by_threatr	   c                 C  sª   g }| D ]Ž}t | d¡pdƒ}| |g ¡}tdd„ |D ƒƒ}| dt|ƒ› dt|ƒ› dt| d¡pddƒ› d	t|ƒ› d	| d
d¡› d	| dd¡› d�¡ qdd |¡› d�S )NÚidr
   c                 s  s    | ]}t | d ¡pdƒV  qdS r�   r6   ru   r   r   r   r_   Z  r`   zbuild_index.<locals>.<genexpr>z<tr><td><a href="threats/z.html">z</a></td><td>r5   z	</td><td>rg   r   rƒ   z
</td></tr>u[  <!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>UN R155 Threat Register Test V2.2</title><style>
:root{--bg:#eef1f5;--surface:#fff;--ink:#1b2430;--muted:#5d6b7c;--line:#d5dce5;--accent:#1f4e79}@media(prefers-color-scheme:dark){:root{--bg:#12171e;--surface:#1a212b;--ink:#e6ebf1;--muted:#93a1b3;--line:#2c3644;--accent:#7db4e6}}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:15px/1.5 "Segoe UI",system-ui,sans-serif}main{max-width:1200px;margin:auto;padding:24px}.card{background:var(--surface);border:1px solid var(--line);border-radius:7px;padding:20px;margin-bottom:18px}a{color:var(--accent);text-decoration:none}.muted{color:var(--muted)}table{width:100%;border-collapse:collapse}th,td{text-align:left;padding:9px;border-bottom:1px solid var(--line)}.notice{border-left:4px solid #b37a20}input{width:100%;padding:9px;border:1px solid var(--line);border-radius:5px;background:var(--bg);color:var(--ink);margin-bottom:12px}
</style></head><body><main><p><a href="../index.html">â†� Production Threat Register</a></p><section class="card notice"><h1>UN R155 Threat Register Test V2.2</h1><p class="muted">Isolated test copies of all T-xxx pages. Production pages are not modified. Regulatory mappings are analytical review material and do not constitute compliance determinations.</p></section><section class="card"><input id="q" type="search" placeholder="Filter threat ID or title"><table><thead><tr><th>ID</th><th>Threat</th><th>Mappings</th><th>Review</th><th>Accepted</th></tr></thead><tbody id="rows">a(  </tbody></table></section><script>const q=document.getElementById('q'),rows=[...document.querySelectorAll('#rows tr')];q.addEventListener('input',()=>{const v=q.value.toLowerCase();rows.forEach(r=>r.style.display=r.textContent.toLowerCase().includes(v)?'':'none')});</script></main></body></html>)r   r7   r   rn   r   rs   ro   )rŸ   r    ZrowsÚthreatÚtidÚmappingsZcountsr   r   r   Úbuild_indexU  s.    ÿÿþý
ý
ýÿþr¥   c                   C  s–  dd„ t ddƒD ƒ} ttƒ| kr(tdƒ‚tƒ }|j ¡ }t||jƒ}t||jƒ}t||j	ƒ}t||j
ƒ}t||jƒ}|jsÈ| ¡ s�td|› �ƒ‚tjtjt|ƒt|ƒdt|ƒg|d�}|jd	krÈtd
ƒ‚t|ƒ}	t|d ƒ}
tt||jƒƒ}t|d ƒ}t|d ƒ}dd„ |	 dg ¡D ƒ}dd„ |
 dg ¡D ƒ}dd„ | dg ¡D ƒ}dd„ | dg ¡D ƒ}ttƒ}| dg ¡D ]0}t|tƒ�rp|t| d¡�p’dƒ  |¡ �qp| ¡ D ]}|jdd„ dd� �qªttƒ}| dg ¡D ]0}t|tƒ�rØ|t| d¡�púdƒ  |¡ �qØ| |j d  ¡}| !¡ �r.t" #|¡ |d j$dd!� d	}|D ]¶}t|d" ƒ}||› d#� }| ¡ �s|td$|› �ƒ‚t%|j&d%d&�ƒ}t'|ƒ}| |g ¡}| |g ¡}t(|t)|ƒƒ}t*||||||ƒ}t+||ƒ}|d |› d#� j,|d%d&� |d'7 }�qF|d( j,t-||ƒd%d&� | !¡ �r*t" #|¡ | .|¡ t/d)|› d*|› d+�ƒ t/d,t0d-d.„ | ¡ D ƒƒ› �ƒ t/d/t0d0d.„ | ¡ D ƒƒ› �ƒ t/d1ƒ d	S )2Nc                 S  s   h | ]}d |d›�’qS )zT-Z03dr   )r\   Úir   r   r   rz   h  r`   zmain.<locals>.<setcomp>é:   éF   z.RDW_BASELINE must contain exactly T-058..T-069zValidator not found: r&   )Úcwdr   z5R155 validation failed; test pages were not generatedzannex5.jsonzversions.jsonzsources.jsonc                 S  s$   g | ]}t |tƒr| d ¡r|‘qS ©r¡   )r   r   r7   ru   r   r   r   rd   ƒ  r`   zmain.<locals>.<listcomp>rŸ   c                 S  s.   i | ]&}t |tƒr| d ¡rt|d  ƒ|“qS rª   rx   ru   r   r   r   Ú
<dictcomp>„  r`   zmain.<locals>.<dictcomp>rV   c                 S  s   g | ]}t |tƒr|‘qS r   ©r   r   ru   r   r   r   rd   …  r`   rW   c                 S  s   g | ]}t |tƒr|‘qS r   r¬   ru   r   r   r   rd   †  r`   r€   r¤   rI   r
   c                 S  s   t |  d|  dd¡¡pdƒS )Nrh   ri   r   )rr   r7   )rv   r   r   r   Ú<lambda>Œ  r`   zmain.<locals>.<lambda>T)ÚkeyÚreverseZregulatory_relationsz.tmp)Úparentsr¡   z.htmlz"Production threat page not found: r   r   rŒ   z
index.htmlzWrote z/index.html and z R155 test threat pageszAnnex 5 mappings rendered: c                 s  s   | ]}t |ƒV  qd S r1   ©rs   ©r\   Úvr   r   r   r_   ¬  r`   zmain.<locals>.<genexpr>z%R155 requirement relations rendered: c                 s  s   | ]}t |ƒV  qd S r1   r±   r²   r   r   r   r_   ­  r`   z/Production site/threats pages were not modified)1ÚrangeÚsetrK   ÚRuntimeErrorr/   Zproject_rootr2   Zregulatory_rootÚthreat_registerÚsource_pagesÚoutputÚ	validatorZskip_validationr   r   Ú
subprocessÚrunÚsysÚ
executabler   Ú
returncoder!   Zmapping_filer7   r   rm   r   r   rn   ÚvaluesÚsortZ	with_namerˆ   ÚexistsÚshutilZrmtreeÚmkdirr‰   r   r“   r›   rs   r†   rž   Z
write_textr¥   ÚrenameÚprintÚsum) ZexpectedZcfgr0   Zreg_rootr·   r¸   r¹   rº   ÚresultÚregisterrk   Zmapping_dataZversion_dataZsource_datarŸ   rV   rW   r€   r    rC   rÀ   Zrelations_by_threatrM   ZstagingZwrittenr¢   r£   r>   r‡   Zthreat_r155_mappingsZthreat_r155_relationsrœ   r   r   r   Úmaing  s„    
þ
""






  rÊ   Ú__main__zERROR: )Úfiler•   )/Ú__doc__Z
__future__r   r*   r   r   r�   rÃ   r»   r½   Úcollectionsr   r   Úpathlibr   Útypingr   r   r!   r/   r2   r9   r=   rB   rD   rK   rO   rU   rt   r}   r†   r�   r‰   r“   r›   rž   r¥   rÊ   Ú__name__Ú
SystemExitr   r   r¶   ÚOSErrorr    rÆ   Ústderrr   r   r   r   Ú<module>   sd   ô?N	K
