guarddog 2.0.4__py3-none-any.whl → 2.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
- from guarddog.analyzer.metadata.detector import Detector
2
1
  from abc import abstractmethod
3
- from typing import Optional
4
- import os
5
- from functools import reduce
2
+ import hashlib
6
3
  import logging
4
+ import os
5
+ from typing import Optional
6
+
7
+ from guarddog.analyzer.metadata.detector import Detector
7
8
 
8
9
  log = logging.getLogger("guarddog")
9
10
 
@@ -28,6 +29,15 @@ class BundledBinary(Detector):
28
29
  name: Optional[str] = None,
29
30
  version: Optional[str] = None,
30
31
  ) -> tuple[bool, str]:
32
+ def format_file(file: str, kind: str) -> str:
33
+ return f"{file} ({kind})"
34
+
35
+ def sha256(file: str) -> str:
36
+ with open(file, "rb") as f:
37
+ hasher = hashlib.sha256()
38
+ while (chunk := f.read(4096)):
39
+ hasher.update(chunk)
40
+ return hasher.hexdigest()
31
41
 
32
42
  log.debug(
33
43
  f"Running bundled binary heuristic on package {name} version {version}"
@@ -35,15 +45,25 @@ class BundledBinary(Detector):
35
45
  if not path:
36
46
  raise ValueError("path is needed to run heuristic " + self.get_name())
37
47
 
38
- bin_files = []
48
+ bin_files = {}
39
49
  for root, _, files in os.walk(path):
40
50
  for f in files:
41
- kind = self.is_binary(os.path.join(root, f))
51
+ path = os.path.join(root, f)
52
+ kind = self.is_binary(path)
42
53
  if kind:
43
- bin_files.append(f"{f} type {kind}")
44
- if bin_files:
45
- return True, "Binary file/s detected in package: " + reduce(lambda x, y: f"{x}, {y}", bin_files)
46
- return False, ""
54
+ digest = sha256(path)
55
+ if digest not in bin_files:
56
+ bin_files[digest] = [format_file(f, kind)]
57
+ else:
58
+ bin_files[digest].append(format_file(f, kind))
59
+
60
+ if not bin_files:
61
+ return False, ""
62
+
63
+ output_lines = '\n'.join(
64
+ f"{digest}: {', '.join(files)}" for digest, files in bin_files.items()
65
+ )
66
+ return True, f"Binary file/s detected in package:\n{output_lines}"
47
67
 
48
68
  def is_binary(self, path: str) -> Optional[str]:
49
69
  max_head = len(max(self.magic_bytes.values()))