@@ -84,9 +84,22 @@ def is_heading_line(line) -> bool:
8484
8585 @staticmethod
8686 def process_security_comment (comment : Comment , comments ) -> str :
87- lines = []
88- start = False
8987 ignore_all , ignore_commands = Comments .get_ignore_options (comments )
88+ if "start-socket-alerts-table" in "" .join (comment .body_list ):
89+ new_body = Comments .process_original_security_comment (comment , ignore_all , ignore_commands )
90+ else :
91+ new_body = Comments .process_updated_security_comment (comment , ignore_all , ignore_commands )
92+
93+ return new_body
94+
95+ @staticmethod
96+ def process_original_security_comment (
97+ comment : Comment ,
98+ ignore_all : bool ,
99+ ignore_commands : list [tuple [str , str ]]
100+ ) -> str :
101+ start = False
102+ lines = []
90103 for line in comment .body_list :
91104 line = line .strip ()
92105 if "start-socket-alerts-table" in line :
@@ -110,8 +123,97 @@ def process_security_comment(comment: Comment, comments) -> str:
110123 lines .append (line )
111124 else :
112125 lines .append (line )
113- new_body = "\n " .join (lines )
114- return new_body
126+ return "\n " .join (lines )
127+
128+ @staticmethod
129+ def process_updated_security_comment (
130+ comment : Comment ,
131+ ignore_all : bool ,
132+ ignore_commands : list [tuple [str , str ]]
133+ ) -> str :
134+ """
135+ Processes an updated security comment containing an HTML table with alert sections.
136+ Removes entire sections marked by start and end hidden comments if the alert matches
137+ ignore conditions.
138+
139+ :param comment: Comment - The raw comment object containing the existing information.
140+ :param ignore_all: bool - Flag to ignore all alerts.
141+ :param ignore_commands: list of tuples - Specific ignore commands representing (pkg_name, pkg_version).
142+ :return: str - The updated comment as a single string.
143+ """
144+ lines = []
145+ ignore_section = False
146+ pkg_name = pkg_version = "" # Track current package and version
147+
148+ # Loop through the comment lines
149+ for line in comment .body_list :
150+ line = line .strip ()
151+
152+ # Detect the start of an alert section
153+ if line .startswith ("<!-- start-socket-alert-" ):
154+ # Extract package name and version from the comment
155+ try :
156+ start_marker = line [len ("<!-- start-socket-alert-" ):- 4 ] # Strip the comment markers
157+ pkg_name , pkg_version = start_marker .split ("@" ) # Extract pkg_name and pkg_version
158+ except ValueError :
159+ pkg_name , pkg_version = "" , ""
160+
161+ # Determine if we should ignore this alert
162+ ignore_section = ignore_all or any (
163+ Comments .is_ignore (pkg_name , pkg_version , name , version )
164+ for name , version in ignore_commands
165+ )
166+
167+ # If not ignored, include this start marker
168+ if not ignore_section :
169+ lines .append (line )
170+
171+ # Detect the end of an alert section
172+ elif line .startswith ("<!-- end-socket-alert-" ):
173+ # Only include if we are not ignoring this section
174+ if not ignore_section :
175+ lines .append (line )
176+ ignore_section = False # Reset ignore flag
177+
178+ # Include lines inside an alert section only if not ignored
179+ elif not ignore_section :
180+ lines .append (line )
181+
182+ return "\n " .join (lines )
183+
184+ @staticmethod
185+ def extract_alert_details_from_row (row : str , ignore_all : bool , ignore_commands : list [tuple [str , str ]]) -> tuple :
186+ """
187+ Parses an HTML table row (<tr>) to extract alert details and determine if it should be ignored.
188+
189+ :param row: str - The HTML table row as a string.
190+ :param ignore_all: bool - Flag to ignore all alerts.
191+ :param ignore_commands: list of tuples - List of (pkg_name, pkg_version) to ignore.
192+ :return: tuple - (pkg_name, pkg_version, ignore)
193+ """
194+ # Extract package details (pkg_name and pkg_version) from the HTML table row
195+ try :
196+ # Find the relevant <summary> element to extract package information
197+ start_index = row .index ("<summary>" )
198+ end_index = row .index ("</summary>" )
199+ summary_content = row [start_index + 9 :end_index ] # Extract content between <summary> tags
200+
201+ # Example: "npm/malicious-package@1.0.0 - Known Malware Alert"
202+ pkg_info , _ = summary_content .split (" - " , 1 )
203+ pkg_name , pkg_version = pkg_info .split ("@" )
204+ except ValueError :
205+ # If parsing fails, skip this row
206+ return "" , "" , False
207+
208+ # Check ignore logic
209+ ignore = False
210+ for name , version in ignore_commands :
211+ if ignore_all or Comments .is_ignore (pkg_name , pkg_version , name , version ):
212+ ignore = True
213+ break
214+
215+ return pkg_name , pkg_version , ignore
216+
115217
116218 @staticmethod
117219 def check_for_socket_comments (comments : dict ):
0 commit comments