7
7
from blint .binary import parse , parse_dex
8
8
from blint .cyclonedx .spec import (
9
9
Component ,
10
- ComponentEvidence ,
11
- FieldModel ,
12
- Identity ,
13
- Method ,
14
10
Property ,
15
11
RefType ,
16
12
Scope ,
17
- Technique ,
18
13
Type ,
19
14
)
20
15
from blint .logger import LOG
21
- from blint .utils import check_command , find_files , unzip_unsafe
16
+ from blint .utils import check_command , create_component_evidence , find_files , unzip_unsafe
22
17
23
18
ANDROID_HOME = os .getenv ("ANDROID_HOME" )
24
19
APKANALYZER_CMD = os .getenv ("APKANALYZER_CMD" )
25
- if not APKANALYZER_CMD and ANDROID_HOME and (
20
+ if (
21
+ not APKANALYZER_CMD
22
+ and ANDROID_HOME
23
+ and (
26
24
os .path .exists (os .path .join (ANDROID_HOME , "cmdline-tools" , "latest" , "bin" , "apkanalyzer" ))
25
+ )
27
26
):
28
- APKANALYZER_CMD = os .path .join (
29
- ANDROID_HOME , "cmdline-tools" , "latest" , "bin" , "apkanalyzer"
30
- )
27
+ APKANALYZER_CMD = os .path .join (ANDROID_HOME , "cmdline-tools" , "latest" , "bin" , "apkanalyzer" )
31
28
elif check_command ("apkanalyzer" ):
32
29
APKANALYZER_CMD = "apkanalyzer"
33
30
@@ -50,7 +47,8 @@ def exec_tool(args, cwd=None, stdout=subprocess.PIPE):
50
47
env = os .environ .copy (),
51
48
shell = sys .platform == "win32" ,
52
49
encoding = "utf-8" ,
53
- check = False , )
50
+ check = False ,
51
+ )
54
52
except subprocess .SubprocessError as e :
55
53
LOG .exception (e )
56
54
return None
@@ -145,9 +143,7 @@ def collect_version_files_metadata(app_file, app_temp_dir):
145
143
with open (vf , encoding = "utf-8" ) as fp :
146
144
version_data = fp .read ().strip ()
147
145
if name and version_data :
148
- component = create_version_component (
149
- app_file , group , name , rel_path , version_data
150
- )
146
+ component = create_version_component (app_file , group , name , rel_path , version_data )
151
147
file_components .append (component )
152
148
return file_components
153
149
@@ -177,14 +173,11 @@ def create_version_component(app_file, group, name, rel_path, version_data):
177
173
version = version_data ,
178
174
purl = purl ,
179
175
scope = Scope .required ,
180
- evidence = ComponentEvidence (
181
- identity = Identity (
182
- field = FieldModel .purl , confidence = 1 , methods = [Method (
183
- technique = Technique .manifest_analysis , value = rel_path , confidence = 1 , )], )
184
- ),
176
+ evidence = create_component_evidence (rel_path , 1.0 ),
185
177
properties = [
186
178
Property (name = "internal:srcFile" , value = rel_path ),
187
- Property (name = "internal:appFile" , value = app_file ), ],
179
+ Property (name = "internal:appFile" , value = app_file ),
180
+ ],
188
181
)
189
182
component .bom_ref = RefType (purl )
190
183
return component
@@ -260,7 +253,8 @@ def parse_so_file(app_file, app_temp_dir, sof):
260
253
# Retrieve the version number from notes
261
254
version = get_so_version (so_metadata .get ("notes" , []))
262
255
functions = [
263
- f .get ("name" ) for f in so_metadata .get ("functions" , [])
256
+ f .get ("name" )
257
+ for f in so_metadata .get ("functions" , [])
264
258
if f .get ("name" ) and not f .get ("name" ).startswith ("_" )
265
259
]
266
260
purl = f"pkg:generic/{ name } @{ version } "
@@ -273,19 +267,11 @@ def parse_so_file(app_file, app_temp_dir, sof):
273
267
version = version ,
274
268
purl = purl ,
275
269
scope = Scope .required ,
276
- evidence = ComponentEvidence (
277
- identity = Identity (
278
- field = FieldModel .purl ,
279
- confidence = 0.5 ,
280
- methods = [
281
- Method (technique = Technique .binary_analysis , value = rel_path , confidence = 0.5 , )
282
- ],
283
- )
284
- ),
270
+ evidence = create_component_evidence (str (rel_path ), 0.5 ),
285
271
properties = [
286
272
Property (name = "internal:srcFile" , value = rel_path ),
287
273
Property (name = "internal:appFile" , value = app_file ),
288
- Property (name = "internal:functions" , value = ", " .join (set (functions )), ),
274
+ Property (name = "internal:functions" , value = ", " .join (set (functions ))),
289
275
],
290
276
)
291
277
component .bom_ref = RefType (purl )
@@ -331,13 +317,11 @@ def collect_dex_files_metadata(app_file, parent_component, app_temp_dir):
331
317
dex_metadata = parse_dex (adex )
332
318
name = os .path .basename (adex ).removesuffix (".dex" )
333
319
rel_path = os .path .relpath (adex , app_temp_dir )
334
- group = ( parent_component .group if parent_component and parent_component .group else "" )
320
+ group = parent_component .group if parent_component and parent_component .group else ""
335
321
version = (
336
- parent_component .version if parent_component and parent_component .version else
337
- "latest" )
338
- component = create_dex_component (
339
- app_file , dex_metadata , group , name , rel_path , version
322
+ parent_component .version if parent_component and parent_component .version else "latest"
340
323
)
324
+ component = create_dex_component (app_file , dex_metadata , group , name , rel_path , version )
341
325
file_components .append (component )
342
326
return file_components
343
327
@@ -365,19 +349,7 @@ def create_dex_component(app_file, dex_metadata, group, name, rel_path, version)
365
349
version = version ,
366
350
purl = purl ,
367
351
scope = Scope .required ,
368
- evidence = ComponentEvidence (
369
- identity = Identity (
370
- field = FieldModel .purl ,
371
- confidence = 0.2 ,
372
- methods = [
373
- Method (
374
- technique = Technique .binary_analysis ,
375
- value = rel_path ,
376
- confidence = 0.2 ,
377
- )
378
- ],
379
- )
380
- ),
352
+ evidence = create_component_evidence (rel_path , 0.2 ),
381
353
properties = [
382
354
Property (name = "internal:srcFile" , value = rel_path ),
383
355
Property (name = "internal:appFile" , value = app_file ),
@@ -393,11 +365,7 @@ def create_dex_component(app_file, dex_metadata, group, name, rel_path, version)
393
365
Property (
394
366
name = "internal:classes" ,
395
367
value = ", " .join (
396
- set (
397
- sorted (
398
- [_clean_type (c .fullname ) for c in dex_metadata .get ("classes" )]
399
- )
400
- )
368
+ set (sorted ([_clean_type (c .fullname ) for c in dex_metadata .get ("classes" )]))
401
369
),
402
370
),
403
371
],
@@ -430,9 +398,7 @@ def collect_files_metadata(app_file, parent_component, deep_mode):
430
398
file_components += collect_version_files_metadata (app_file , app_temp_dir )
431
399
file_components += collect_so_files_metadata (app_file , app_temp_dir )
432
400
if deep_mode :
433
- file_components += collect_dex_files_metadata (
434
- app_file , parent_component , app_temp_dir
435
- )
401
+ file_components += collect_dex_files_metadata (app_file , parent_component , app_temp_dir )
436
402
shutil .rmtree (app_temp_dir , ignore_errors = True )
437
403
return file_components
438
404
@@ -445,9 +411,7 @@ def parse_apk_summary(data):
445
411
name = parts [0 ]
446
412
version = parts [- 1 ]
447
413
purl = f"pkg:apk/{ name } @{ version } "
448
- component = Component (
449
- type = Type .application , name = name , version = version , purl = purl
450
- )
414
+ component = Component (type = Type .application , name = name , version = version , purl = purl )
451
415
component .bom_ref = RefType (purl )
452
416
return component
453
417
return None
0 commit comments