|
| 1 | +from collections import defaultdict |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from twyn.similarity.algorithm import ( |
| 5 | + AbstractSimilarityAlgorithm, |
| 6 | + SimilarityThreshold, |
| 7 | +) |
| 8 | +from twyn.trusted_packages.managers.base import OrderedPackages |
| 9 | +from twyn.trusted_packages.models import TyposquatCheckResultEntry |
| 10 | +from twyn.trusted_packages.selectors import AbstractSelector |
| 11 | + |
| 12 | + |
| 13 | +class TrustedNpmPackageManager: |
| 14 | + """Representation of namespaces that can be trusted.""" |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + names: set[str], |
| 19 | + algorithm: AbstractSimilarityAlgorithm, |
| 20 | + selector: AbstractSelector, |
| 21 | + threshold_class: type[SimilarityThreshold], |
| 22 | + ) -> None: |
| 23 | + self.packages, self.namespaces = self._create_names_dictionary(names) |
| 24 | + |
| 25 | + self.threshold_class = threshold_class |
| 26 | + self.selector = selector |
| 27 | + self.algorithm = algorithm |
| 28 | + |
| 29 | + def __contains__(self, obj: Any) -> bool: |
| 30 | + """Check if an object exists in the trusted namespaces.""" |
| 31 | + if isinstance(obj, str): |
| 32 | + return obj in self.packages[obj[0]] or obj in self.namespaces |
| 33 | + return False |
| 34 | + |
| 35 | + def _create_names_dictionary(self, names: set[str]) -> tuple[OrderedPackages, OrderedPackages]: |
| 36 | + """Create a dictionary which will group all packages that start with the same letter under the same key.""" |
| 37 | + first_letter_names: OrderedPackages = defaultdict(set) |
| 38 | + namespaces: OrderedPackages = defaultdict(set) |
| 39 | + for name in names: |
| 40 | + if name.startswith("@"): |
| 41 | + namespace, dependency = name.split("/") |
| 42 | + namespaces[namespace].add(dependency) |
| 43 | + else: |
| 44 | + first_letter_names[name[0]].add(name) |
| 45 | + return first_letter_names, namespaces |
| 46 | + |
| 47 | + def _get_typosquats_from_namespace_dependency(self, package_name: str) -> TyposquatCheckResultEntry: |
| 48 | + namespace, dependency = package_name.split("/") |
| 49 | + threshold = self.threshold_class.from_name(namespace) |
| 50 | + typosquat_result = TyposquatCheckResultEntry(dependency=package_name) |
| 51 | + for trusted_namespace_name in self.selector.select_similar_names( |
| 52 | + names={"@": self.namespaces.keys()}, name=namespace |
| 53 | + ): |
| 54 | + distance = self.algorithm.get_distance(namespace, trusted_namespace_name) |
| 55 | + if threshold.is_inside_threshold(distance) and dependency in self.namespaces[trusted_namespace_name]: |
| 56 | + typosquat_result.add(f"{trusted_namespace_name}/{dependency}") |
| 57 | + return typosquat_result |
| 58 | + |
| 59 | + def _get_typosquats_from_dependency(self, package_name: str) -> TyposquatCheckResultEntry: |
| 60 | + threshold = self.threshold_class.from_name(package_name) |
| 61 | + typosquat_result = TyposquatCheckResultEntry(dependency=package_name) |
| 62 | + for trusted_package_name in self.selector.select_similar_names(names=self.packages, name=package_name): |
| 63 | + distance = self.algorithm.get_distance(package_name, trusted_package_name) |
| 64 | + if threshold.is_inside_threshold(distance): |
| 65 | + typosquat_result.add(trusted_package_name) |
| 66 | + return typosquat_result |
| 67 | + |
| 68 | + def get_typosquat(self, package_name: str) -> TyposquatCheckResultEntry: |
| 69 | + """Check if a given package name is similar to any trusted package and returns it. |
| 70 | +
|
| 71 | + Only if there is a match on the first letter can a package name be |
| 72 | + considered similar to another one. The algorithm provided and the threshold |
| 73 | + are used to determine if the package name can be considered similar. |
| 74 | + """ |
| 75 | + if package_name.startswith("@"): |
| 76 | + return self._get_typosquats_from_namespace_dependency(package_name) |
| 77 | + return self._get_typosquats_from_dependency(package_name) |
0 commit comments