@@ -1088,12 +1088,21 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s
10881088<!-- end-socket-alert-{ pkg_marker } -->
10891089 """
10901090
1091+ @staticmethod
1092+ def _advisory_id_pattern (ids ) -> "re.Pattern[str]" :
1093+ """Matches any of the IDs in full, so CVE-2021-2333 does not match inside CVE-2021-23337."""
1094+ alternatives = "|" .join (re .escape (advisory ) for advisory in sorted (ids , key = len , reverse = True ))
1095+ return re .compile (rf"(?<![\w-])(?:{ alternatives } )(?![\w-])" , re .IGNORECASE )
1096+
10911097 @staticmethod
10921098 def advisory_ids_for_title (alert : Issue ) -> list :
10931099 """The alert's GHSA and CVE IDs, GHSA first, minus any its title already names."""
1094- title = ( getattr (alert , "title" , "" ) or "" ). upper ()
1100+ title = getattr (alert , "title" , "" ) or ""
10951101 ids = alert .advisory_ids () if hasattr (alert , "advisory_ids" ) else []
1096- return [advisory for advisory in ids if advisory .upper () not in title ]
1102+ return [
1103+ advisory for advisory in ids
1104+ if not Messages ._advisory_id_pattern ([advisory ]).search (title )
1105+ ]
10971106
10981107 @staticmethod
10991108 def alert_title_with_advisory (alert : Issue ) -> str :
@@ -1109,34 +1118,53 @@ def alert_title_with_advisory(alert: Issue) -> str:
11091118 return title
11101119
11111120 @staticmethod
1112- def advisory_link_html (advisory : str ) -> str :
1121+ def _advisory_link_base (advisory : str ) -> str | None :
1122+ for pattern , base_url in ADVISORY_LINK_BASES :
1123+ if pattern .match (advisory ):
1124+ return base_url
1125+ return None
1126+
1127+ @staticmethod
1128+ def advisory_link_html (advisory : str , label_html : str | None = None ) -> str :
11131129 """An advisory ID linked to its public advisory page.
11141130
11151131 Only IDs in the canonical GHSA or CVE shape are linked, so an unexpected
11161132 value renders as escaped text instead of an arbitrary URL path.
11171133 """
1118- for pattern , base_url in ADVISORY_LINK_BASES :
1119- if pattern .match (advisory ):
1120- return (
1121- f'<a href="{ Messages .html_attr (base_url + advisory )} ">'
1122- f"{ Messages .html_text (advisory )} </a>"
1123- )
1124- return Messages .html_text (advisory )
1134+ label = Messages .html_text (advisory ) if label_html is None else label_html
1135+ base_url = Messages ._advisory_link_base (advisory )
1136+ if base_url is None :
1137+ return label
1138+ return f'<a href="{ Messages .html_attr (base_url + advisory )} ">{ label } </a>'
11251139
11261140 @staticmethod
11271141 def alert_title_html (alert : Issue ) -> str :
1128- """The PR/MR comment row title: escaped title, then each advisory ID linked .
1142+ """The PR/MR comment row title as HTML .
11291143
1130- Developers who only see the PR comment can open the GHSA on GitHub and the
1131- CVE on NVD without a Socket login. Alerts usually carry both IDs, and
1132- either can be published first, so the row shows every ID the alert has .
1144+ The title is escaped. The alert's GHSA and CVE IDs are linked in place where
1145+ the title names them in full, and the remaining IDs follow once, linked, in
1146+ parentheses. Only canonical GHSA and CVE IDs become links .
11331147 """
1134- title = Messages .html_text (getattr (alert , "title" , "" ) or "" )
1135- ids = Messages .advisory_ids_for_title (alert )
1136- if not ids :
1137- return title
1138- links = ", " .join (Messages .advisory_link_html (advisory ) for advisory in ids )
1139- return f"{ title } ({ links } )"
1148+ title_html = Messages .html_text (getattr (alert , "title" , "" ) or "" )
1149+ ids = alert .advisory_ids () if hasattr (alert , "advisory_ids" ) else []
1150+ remaining = Messages .advisory_ids_for_title (alert )
1151+ # Canonical IDs are letters, digits and hyphens, which escaping leaves alone.
1152+ in_title = {
1153+ advisory .upper (): advisory
1154+ for advisory in ids
1155+ if advisory not in remaining and Messages ._advisory_link_base (advisory )
1156+ }
1157+ if in_title :
1158+ title_html = Messages ._advisory_id_pattern (in_title .values ()).sub (
1159+ lambda match : Messages .advisory_link_html (
1160+ in_title [match .group (0 ).upper ()], match .group (0 )
1161+ ),
1162+ title_html ,
1163+ )
1164+ if not remaining :
1165+ return title_html
1166+ links = ", " .join (Messages .advisory_link_html (advisory ) for advisory in remaining )
1167+ return f"{ title_html } ({ links } )"
11401168
11411169 @staticmethod
11421170 def security_comment_template (diff : Diff , config = None ) -> str :
0 commit comments