@@ -25,13 +25,17 @@ def run_analysis(params, func):
2525 logger .error (f"Run Analysis : { ex } " )
2626
2727
28- def get_oss_ver (version ):
28+ def get_oss_ver (version_info ):
2929 oss_version = ""
30-
31- if version ['source' ] == 'pom' :
32- if version ['name' ] == 'version' :
33- oss_version = version ['value' ]
34-
30+ if version_info .get ('source' ) == 'central' :
31+ if version_info .get ('name' ) == 'version' :
32+ oss_version = version_info .get ('value' )
33+ elif version_info .get ('source' ) == 'pom' :
34+ if version_info .get ('name' ) == 'version' :
35+ oss_version = version_info .get ('value' )
36+ elif version_info .get ('source' , "" ).lower () == 'manifest' :
37+ if version_info .get ('name' ) == 'Implementation-Version' or version_info .get ('name' ) == 'Bundle-Version' :
38+ oss_version = version_info .get ('value' )
3539 return oss_version
3640
3741
@@ -102,6 +106,60 @@ def get_vulnerability_info(file_with_path, vulnerability, vulnerability_items, r
102106 return vulnerability_items
103107
104108
109+ def get_oss_groupid (evidence_info ):
110+ oss_groupid = ""
111+ # First, Get groupid from Central, else get it from pom
112+ if evidence_info .get ('source' ) == 'central' :
113+ if evidence_info .get ('name' ) == 'groupid' :
114+ oss_groupid = evidence_info .get ('value' )
115+ elif evidence_info .get ('source' ) == 'pom' :
116+ if evidence_info .get ('name' ) == 'groupid' :
117+ oss_groupid = evidence_info .get ('value' )
118+ return oss_groupid
119+
120+
121+ def get_oss_artifactid (evidence_info ):
122+ oss_artifactid = ""
123+ # Get OSS Info from POM
124+ if evidence_info .get ('source' ) == 'pom' :
125+ if evidence_info .get ('name' ) == 'artifactid' :
126+ oss_artifactid = evidence_info .get ('value' )
127+ return oss_artifactid
128+
129+
130+ def get_oss_dl_url (evidence_info ):
131+ oss_dl_url = ""
132+ if evidence_info .get ('name' ) == 'url' :
133+ oss_dl_url = evidence_info .get ('value' )
134+ return oss_dl_url
135+
136+
137+ def get_oss_info_from_pkg_info (pkg_info ):
138+ oss_name = ""
139+ oss_version = ""
140+
141+ try :
142+ if pkg_info .get ('id' ) != "" :
143+ # Get OSS Name
144+ if pkg_info .get ('id' ).startswith ('pkg:maven' ):
145+ # ex, pkg:maven/com.hankcs/[email protected] 146+ oss_name = pkg_info .get ('id' ).split ('@' )[0 ]
147+ oss_name = f"{ oss_name .split ('/' )[- 2 ]} :{ oss_name .split ('/' )[- 1 ]} "
148+ elif pkg_info .get ('id' ).startswith ('pkg:npm' ):
149+ 150+ oss_name = pkg_info .get ('id' ).split ('@' )[0 ]
151+ oss_name = oss_name .replace ('pkg:npm' , 'npm' )
152+ oss_name = oss_name .replace ('/' , ':' )
153+ else :
154+ oss_name = pkg_info .get ('id' ).split ('@' )[0 ]
155+ oss_name = oss_name .split ('/' )[- 1 ]
156+ # Get OSS Version
157+ oss_version = pkg_info .get ('id' ).split ('@' )[1 ]
158+ except Exception as ex :
159+ logger .debug (f"Error to get value for oss name and version: { ex } " )
160+ return oss_name , oss_version
161+
162+
105163def analyze_jar_file (path_to_find_bin ):
106164 remove_owasp_item = []
107165 owasp_items = {}
@@ -135,12 +193,13 @@ def analyze_jar_file(path_to_find_bin):
135193 oss_groupid = ""
136194 oss_dl_url = ""
137195 oss_license = get_oss_lic_in_jar (val )
138- get_oss_info = False
196+ oss_name_found = False
139197
140198 all_evidence = val .get ("evidenceCollected" )
141199 vulnerability = val .get ("vulnerabilityIds" )
200+ all_pkg_info = val .get ("packages" )
201+
142202 vendor_evidences = all_evidence .get ('vendorEvidence' )
143- product_evidences = all_evidence .get ('productEvidence' )
144203 version_evidences = all_evidence .get ('versionEvidence' )
145204
146205 # Check if the file is .jar file
@@ -151,35 +210,37 @@ def analyze_jar_file(path_to_find_bin):
151210 bin_with_path = bin_with_path .split ('.jar' )[0 ] + '.jar'
152211
153212 file_with_path = os .path .relpath (bin_with_path , path_to_find_bin )
154- # Get Version info from versionEvidence
155- for version_info in version_evidences :
156- oss_ver = get_oss_ver (version_info )
157-
158- # Get Artifact ID, Group ID, OSS Name from vendorEvidence
159- for vendor_info in vendor_evidences :
160- # Get OSS Info from POM
161- if vendor_info ['source' ] == 'pom' :
162- if vendor_info ['name' ] == 'artifactid' :
163- oss_artifactid = vendor_info ['value' ]
164- if vendor_info ['name' ] == 'groupid' :
165- oss_groupid = vendor_info ['value' ]
166- if vendor_info ['name' ] == 'url' :
167- oss_dl_url = vendor_info ['value' ]
213+
214+ # First, Get OSS Name and Version info from pkg_info
215+ for pkg_info in all_pkg_info :
216+ oss_name , oss_ver = get_oss_info_from_pkg_info (pkg_info )
217+
218+ if oss_name == "" and oss_ver == "" :
219+ # If can't find name and version, Find thoes in vendorEvidence and versionEvidence .
220+ # Get Version info from versionEvidence
221+ for version_info in version_evidences :
222+ oss_ver = get_oss_ver (version_info )
223+
224+ # Get Artifact ID, Group ID, OSS Name from vendorEvidence
225+ for vendor_info in vendor_evidences :
226+ if oss_groupid == "" :
227+ oss_groupid = get_oss_groupid (vendor_info )
228+ if oss_artifactid == "" :
229+ oss_artifactid = get_oss_artifactid (vendor_info )
230+ if oss_dl_url == "" :
231+ oss_dl_url = get_oss_dl_url (vendor_info )
232+ # Combine groupid and artifactid
168233 if oss_artifactid != "" and oss_groupid != "" :
169234 oss_name = f"{ oss_groupid } :{ oss_artifactid } "
170-
171- # Check if get oss_name and version from pom
172- if oss_name != "" and oss_ver != "" :
173- get_oss_info = True
174-
175- # If there is no pom.mxl in .jar file, get oss info from MANIFEST.MF file
176- if get_oss_info is False :
177- for product_info in product_evidences :
178- if product_info ['source' ] == 'Manifest' :
179- if oss_name == "" and (product_info ['name' ] == 'Implementation-Title' or product_info ['name' ] == 'specification-title' ):
180- oss_name = product_info ['value' ]
181- if oss_ver == "" and (product_info ['name' ] == 'Implementation-Version' or product_info ['name' ] == 'Bundle-Version' ):
182- oss_ver = product_info ['value' ]
235+ oss_name_found = True
236+ # If oss_name is found, break
237+ if oss_name_found :
238+ break
239+ else :
240+ # Get only dl_url from vendorEvidence
241+ for vendor_info in vendor_evidences :
242+ if oss_dl_url == "" :
243+ oss_dl_url = get_oss_dl_url (vendor_info )
183244
184245 # Get Vulnerability Info.
185246 vulnerability_items = get_vulnerability_info (file_with_path , vulnerability , vulnerability_items , remove_vulnerability_items )
0 commit comments