@@ -57,29 +57,37 @@ def get_oss_lic_in_jar(data):
5757 return license
5858
5959
60+ def merge_oss_and_vul_items (bin , key , oss_list , vulnerability_items ):
61+ bin .set_oss_items (oss_list )
62+ if vulnerability_items and vulnerability_items .get (key ):
63+ bin .vulnerability_items .extend (vulnerability_items .get (key , []))
64+
65+
6066def merge_binary_list (owasp_items , vulnerability_items , bin_list ):
6167 not_found_bin = []
6268
63- # key : file_path / value : oss_list for one binary
69+ # key : file_path / value : {" oss_list": [oss], "sha1": sha1} for one binary
6470 for key , value in owasp_items .items ():
6571 found = False
72+ oss_list = value ["oss_list" ]
73+ sha1 = value .get ("sha1" , "" )
6674 for bin in bin_list :
6775 if bin .source_name_or_path == key :
68- for oss in value :
76+ found = True
77+ for oss in oss_list :
6978 if oss .name and oss .license :
7079 bin .found_in_owasp = True
7180 break
72- bin .set_oss_items (value )
73- if vulnerability_items and vulnerability_items .get (key ):
74- bin .vulnerability_items .extend (vulnerability_items .get (key ))
75- found = True
76- break
81+ merge_oss_and_vul_items (bin , key , oss_list , vulnerability_items )
82+ else :
83+ if bin .checksum == sha1 :
84+ merge_oss_and_vul_items (bin , key , oss_list , vulnerability_items )
7785
7886 if not found :
7987 bin_item = BinaryItem (os .path .abspath (key ))
8088 bin_item .binary_name_without_path = os .path .basename (key )
8189 bin_item .source_name_or_path = key
82- bin_item .set_oss_items (value )
90+ bin_item .set_oss_items (oss_list )
8391 not_found_bin .append (bin_item )
8492
8593 bin_list += not_found_bin
@@ -192,7 +200,8 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
192200 success = False
193201 return owasp_items , vulnerability_items , success
194202
195- dependencies = jar_contents .get ("dependencies" )
203+ dependencies = jar_contents .get ("dependencies" , [])
204+
196205 try :
197206 for val in dependencies :
198207 bin_with_path = ""
@@ -204,6 +213,8 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
204213 oss_license = get_oss_lic_in_jar (val )
205214 oss_name_found = False
206215
216+ sha1 = val .get ("sha1" , "" )
217+
207218 all_evidence = val .get ("evidenceCollected" , {})
208219 vulnerability = val .get ("vulnerabilityIds" , [])
209220 all_pkg_info = val .get ("packages" , [])
@@ -260,30 +271,25 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
260271 vulnerability_items = get_vulnerability_info (file_with_path , vulnerability , vulnerability_items , remove_vulnerability_items )
261272
262273 if oss_name != "" or oss_ver != "" or oss_license != "" or oss_dl_url != "" :
263- oss_list_for_file = owasp_items .get (file_with_path , [])
264-
265- existing_oss = None
266- for item in oss_list_for_file :
267- if item .name == oss_name and item .version == oss_ver :
268- existing_oss = item
269- break
270-
271- if not existing_oss :
272- oss = OssItem (oss_name , oss_ver , oss_license , oss_dl_url )
273- oss .comment = "OWASP result"
274-
275- if file_with_path in owasp_items :
276- owasp_items [file_with_path ].append (oss )
277- else :
278- owasp_items [file_with_path ] = [oss ]
274+ oss = OssItem (oss_name , oss_ver , oss_license , oss_dl_url )
275+ oss .comment = "OWASP result"
276+
277+ if file_with_path in owasp_items :
278+ owasp_items [file_with_path ]["oss_list" ].append (oss )
279+ # Update sha1 if not already set or if current sha1 is empty
280+ if not owasp_items [file_with_path ]["sha1" ] and sha1 :
281+ owasp_items [file_with_path ]["sha1" ] = sha1
282+ else :
283+ owasp_items [file_with_path ] = {
284+ "oss_list" : [oss ],
285+ "sha1" : sha1
286+ }
279287 except Exception as ex :
280- logger .debug (f"Error to get depency Info in jar_contets: { ex } " )
281- success = False
288+ logger .debug (f"Error to get dependency Info in jar_contents: { ex } " )
282289
283290 try :
284291 if os .path .isfile (json_file ):
285292 os .remove (json_file )
286293 except Exception as ex :
287294 logger .debug (f"Error - There is no .json file : { ex } " )
288-
289295 return owasp_items , vulnerability_items , success
0 commit comments