Skip to content

Commit 151725c

Browse files
committed
chore: refactor changes to accomodate changes in main
Signed-off-by: Carl Flottmann <carl.flottmann@oracle.com>
1 parent 7dfdd51 commit 151725c

File tree

7 files changed

+23
-31
lines changed

7 files changed

+23
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicRes
106106
logger.debug(error_msg)
107107
raise HeuristicAnalyzerValueError(error_msg)
108108

109-
if len(releases) != 1: # We only analyze packages with a single release, this heuristic does not apply.
110-
return HeuristicResult.SKIP, {}
111-
112109
# Since there is only one release, the latest version should be that release
113110
release = pypi_package_json.get_latest_version()
114111
if release is None:
@@ -128,6 +125,9 @@ def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicRes
128125
logger.debug(error_msg)
129126
raise HeuristicAnalyzerValueError(error_msg)
130127

128+
if len(releases) != 1: # We only analyze packages with a single release, this heuristic does not apply.
129+
return HeuristicResult.SKIP, {}
130+
131131
years = []
132132
months = []
133133
publish_days = []

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ class PackageDescriptionIntentAnalyzer(BaseHeuristicAnalyzer):
2424
)
2525

2626
def __init__(self) -> None:
27-
super().__init__(
28-
name="package_description_intent", heuristic=Heuristics.PACKAGE_DESCRIPTION_INTENT, depends_on=None
29-
)
27+
super().__init__(name="package_description_intent", heuristic=Heuristics.PACKAGE_DESCRIPTION_INTENT)
3028

3129
def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicResult, dict[str, JsonType]]:
3230
"""Analyze the package.

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,13 @@ class SimilarProjectAnalyzer(BaseHeuristicAnalyzer):
2121
"""Check whether the package has a similar structure to other packages maintained by the same user."""
2222

2323
def __init__(self) -> None:
24+
# TODO: not touched by depends_on refactoring yet
2425
super().__init__(
2526
name="similar_project_analyzer",
2627
heuristic=Heuristics.SIMILAR_PROJECTS,
2728
# TODO: these dependencies are used as this heuristic currently downloads many package sourcecode
2829
# tarballs. Refactoring this heuristic to run more efficiently means this should have depends_on=None.
29-
depends_on=[
30-
(Heuristics.EMPTY_PROJECT_LINK, HeuristicResult.FAIL),
31-
(Heuristics.ONE_RELEASE, HeuristicResult.FAIL),
32-
(Heuristics.HIGH_RELEASE_FREQUENCY, HeuristicResult.FAIL),
33-
(Heuristics.UNCHANGED_RELEASE, HeuristicResult.FAIL),
34-
(Heuristics.CLOSER_RELEASE_JOIN_DATE, HeuristicResult.FAIL),
35-
(Heuristics.SUSPICIOUS_SETUP, HeuristicResult.FAIL),
36-
(Heuristics.WHEEL_ABSENCE, HeuristicResult.FAIL),
37-
(Heuristics.ANOMALOUS_VERSION, HeuristicResult.FAIL),
38-
(Heuristics.TYPOSQUATTING_PRESENCE, HeuristicResult.FAIL),
39-
(Heuristics.FAKE_EMAIL, HeuristicResult.FAIL),
40-
],
30+
# TODO: depends_on doesn't exist anymore, try fix this...
4131
)
4232

4333
def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicResult, dict[str, JsonType]]:

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ class StubNameAnalyzer(BaseHeuristicAnalyzer):
1717
"""Check whether the package name contains 'stub'."""
1818

1919
def __init__(self) -> None:
20-
super().__init__(
21-
name="stub_name_analyzer",
22-
heuristic=Heuristics.STUB_NAME,
23-
depends_on=None,
24-
)
20+
# TODO: not touched by depends_on refactoring yet
21+
super().__init__(name="stub_name_analyzer", heuristic=Heuristics.STUB_NAME)
2522

2623
def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicResult, dict[str, JsonType]]:
2724
"""Analyze the package.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ class TypeStubFileAnalyzer(BaseHeuristicAnalyzer):
2020
FILES_THRESHOLD = 10
2121

2222
def __init__(self) -> None:
23-
super().__init__(
24-
name="type_stub_file",
25-
heuristic=Heuristics.TYPE_STUB_FILE,
26-
depends_on=None,
27-
)
23+
# TODO: not touched by depends_on refactoring yet
24+
super().__init__(name="type_stub_file", heuristic=Heuristics.TYPE_STUB_FILE)
2825

2926
def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicResult, dict[str, JsonType]]:
3027
"""Analyze the package.

tests/malware_analyzer/pypi/test_anomalous_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def test_multiple_releases(pypi_package_json: MagicMock) -> None:
416416
"0.2": release_content,
417417
}
418418
pypi_package_json.get_releases.return_value = releases
419+
pypi_package_json.get_latest_version.return_value = "0.2"
419420
expected_result: tuple[HeuristicResult, dict] = (HeuristicResult.SKIP, {})
420421

421422
actual_result = analyzer.analyze(pypi_package_json)

tests/malware_analyzer/pypi/test_pypi_sourcecode_analyzer.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,31 @@ def test_nonexistent_rule_path(tmp_path: Path) -> None:
120120
_ = PyPISourcecodeAnalyzer(resources_path=RESOURCES_PATH)
121121

122122

123-
def test_invalid_custom_rules(tmp_path: Path) -> None:
123+
def test_invalid_custom_rules(tmp_path: Path, pypi_package_json: MagicMock) -> None:
124124
"""Test for when the provided file is not a valid semgrep rule (should error).
125125
126126
Parameters
127127
----------
128128
tmp_path: Path
129129
Pytest temporary path fixture.
130+
pypi_package_json: MagicMock
131+
The PyPIPackageJsonAsset MagicMock fixture.
130132
"""
131133
# Use this file as an invalid semgrep rule as it is most definitely not a semgrep rule, and does exist.
132134
defaults_content = f"""
133135
[heuristic.pypi]
134136
custom_semgrep_rules_path = {os.path.abspath(__file__)}
135137
"""
136138
config_defaults(tmp_path, defaults_content)
137-
with pytest.raises(ConfigurationError):
138-
_ = PyPISourcecodeAnalyzer(resources_path=RESOURCES_PATH)
139+
140+
analyzer = PyPISourcecodeAnalyzer(resources_path=RESOURCES_PATH)
141+
pypi_package_json.package_sourcecode_path = os.path.join(
142+
os.path.dirname(os.path.abspath(__file__)), "resources", "sourcecode_samples"
143+
)
144+
145+
# Semgrep should fail to run when we launch analysis
146+
with pytest.raises(HeuristicAnalyzerValueError):
147+
_ = analyzer.analyze(pypi_package_json)
139148

140149

141150
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)