Skip to content

Commit 76ec530

Browse files
authored
fix: added check for missing release information for anomalous version (#1235)
anomalous version now checks for when there is a release entry but no actual information there. Unit tests reflect this.
1 parent b823cab commit 76ec530

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicRes
132132
logger.debug(error_msg)
133133
raise HeuristicAnalyzerValueError(error_msg) from release_error
134134

135-
try:
136-
version = parse(release)
137-
except InvalidVersion:
138-
return HeuristicResult.SKIP, {self.DETAIL_INFO_KEY: Versioning.INVALID.value}
135+
if not release_metadata:
136+
error_msg = "Release is missing metadata information"
137+
logger.debug(error_msg)
138+
raise HeuristicAnalyzerValueError(error_msg)
139139

140140
years = []
141141
months = []
@@ -161,6 +161,11 @@ def analyze(self, pypi_package_json: PyPIPackageJsonAsset) -> tuple[HeuristicRes
161161

162162
days = list(range(min(publish_days) - self.day_publish_error, max(publish_days) + self.day_publish_error + 1))
163163

164+
try:
165+
version = parse(release)
166+
except InvalidVersion:
167+
return HeuristicResult.SKIP, {self.DETAIL_INFO_KEY: Versioning.INVALID.value}
168+
164169
calendar = False
165170
calendar_semantic = False
166171

tests/malware_analyzer/pypi/test_anomalous_version.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,65 @@ def test_analyze_no_information(pypi_package_json: MagicMock) -> None:
2121
analyzer.analyze(pypi_package_json)
2222

2323

24+
def test_analyze_no_releases(pypi_package_json: MagicMock) -> None:
25+
"""Test for when there are no release entries, so error."""
26+
analyzer = AnomalousVersionAnalyzer()
27+
28+
pypi_package_json.get_releases.return_value = {}
29+
pypi_package_json.get_latest_version.return_value = None
30+
31+
with pytest.raises(HeuristicAnalyzerValueError):
32+
analyzer.analyze(pypi_package_json)
33+
34+
35+
def test_analyze_missing_release(pypi_package_json: MagicMock) -> None:
36+
"""Test for when there is a release entry, but no actual release information, so error."""
37+
analyzer = AnomalousVersionAnalyzer()
38+
version = "1.1"
39+
40+
pypi_package_json.get_releases.return_value = {version: []}
41+
pypi_package_json.get_latest_version.return_value = version
42+
43+
with pytest.raises(HeuristicAnalyzerValueError):
44+
analyzer.analyze(pypi_package_json)
45+
46+
47+
def test_analyze_missing_time(pypi_package_json: MagicMock) -> None:
48+
"""Test for when the supplied upload time does not conform with PEP 440, so error."""
49+
analyzer = AnomalousVersionAnalyzer()
50+
version = "1.1"
51+
release = {
52+
version: [
53+
{
54+
"comment_text": "",
55+
"digests": {
56+
"blake2b_256": "defa2fbcebaeeb909511139ce28dac4a77ab2452ba72b49a22b12981b2f375b3",
57+
"md5": "9203bbb130f8ddb38269f4861c170d04",
58+
"sha256": "168bcccbf5106132e90b85659297700194369b8f6b3e5a03769614f0d200e370",
59+
},
60+
"downloads": -1,
61+
"filename": "ttttttttest_nester.py-0.1.0.tar.gz",
62+
"has_sig": False,
63+
"md5_digest": "9203bbb130f8ddb38269f4861c170d04",
64+
"packagetype": "sdist",
65+
"python_version": "source",
66+
"requires_python": None,
67+
"size": 546,
68+
"url": "https://files.pythonhosted.org/packages/de/fa/"
69+
+ "2fbcebaeeb909511139ce28dac4a77ab2452ba72b49a22b12981b2f375b3/ttttttttest_nester.py-0.1.0.tar.gz",
70+
"yanked": False,
71+
"yanked_reason": None,
72+
}
73+
]
74+
}
75+
76+
pypi_package_json.get_releases.return_value = release
77+
pypi_package_json.get_latest_version.return_value = version
78+
79+
with pytest.raises(HeuristicAnalyzerValueError):
80+
analyzer.analyze(pypi_package_json)
81+
82+
2483
def test_analyze_invalid_time(pypi_package_json: MagicMock) -> None:
2584
"""Test for when the supplied upload time does not conform with PEP 440, so error."""
2685
analyzer = AnomalousVersionAnalyzer()

0 commit comments

Comments
 (0)