Skip to content

Commit d0a0d65

Browse files
committed
refactor: move threshold configuration to defaults.ini
Signed-off-by: Amine <amine.raouane@enim.ac.ma>
1 parent 6da5458 commit d0a0d65

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/macaron/config/defaults.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ popular_packages_path =
609609
# A boolean value that determines whether to check the deliverability of the email address.
610610
check_deliverability = True
611611

612+
# The threshold for a package's description score to be considered secure.
613+
score_threshold = 70
614+
612615
# ==== The following sections are for source code analysis using Semgrep ====
613616
# rulesets: a reference to a 'ruleset' in this section refers to a Semgrep .yaml file containing one or more rules.
614617
# rules: a reference to a 'rule' in this section refers to an individual rule ID, specified by the '- id:' field in

src/macaron/malware_analyzer/pypi_heuristics/metadata/inconsistent_description.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77

88
from macaron.ai.clients.ai_factory import AIClientFactory
9+
from macaron.config.defaults import defaults
910
from macaron.errors import HeuristicAnalyzerValueError
1011
from macaron.json_tools import JsonType, json_extract
1112
from macaron.malware_analyzer.pypi_heuristics.base_analyzer import BaseHeuristicAnalyzer
@@ -35,8 +36,6 @@ class InconsistentDescriptionAnalyzer(BaseHeuristicAnalyzer):
3536
}
3637
"""
3738

38-
THRESHOLD = 60
39-
4039
RESPONSE_FORMAT = {
4140
"type": "json_schema",
4241
"json_schema": {
@@ -65,9 +64,18 @@ def __init__(self) -> None:
6564
super().__init__(
6665
name="inconsistent_description_analyzer", heuristic=Heuristics.INCONSISTENT_DESCRIPTION, depends_on=None
6766
)
67+
self.threshold = self._load_defaults()
6868
factory = AIClientFactory()
6969
self.client = factory.create_client(self.SYSTEM_PROMPT.strip())
7070

71+
def _load_defaults(self) -> int:
72+
"""Load the default values from defaults.ini."""
73+
section_name = "heuristic.pypi"
74+
if defaults.has_section(section_name):
75+
section = defaults[section_name]
76+
return section.getint("score_threshold", 70)
77+
return 70
78+
7179
def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicResult, dict[str, JsonType]]:
7280
"""Analyze the package.
7381
@@ -100,7 +108,7 @@ def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicRes
100108
response_format=self.RESPONSE_FORMAT,
101109
)
102110

103-
if analysis_result["score"] < self.THRESHOLD:
111+
if analysis_result["score"] < self.threshold:
104112
return HeuristicResult.FAIL, {
105113
"message": f"inconsistent description with score {analysis_result['score']}. because {analysis_result['reason']}"
106114
}

0 commit comments

Comments
 (0)