Skip to content

Commit 24d224e

Browse files
committed
refactor: adjustments based on recent changes in the main branch
1 parent f1883f1 commit 24d224e

File tree

10 files changed

+41
-49
lines changed

10 files changed

+41
-49
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class TyposquattingPresenceAnalyzer(BaseHeuristicAnalyzer):
6060
}
6161

6262
def __init__(self, popular_packages_path: str | None = None) -> None:
63-
super().__init__(
64-
name="typosquatting_presence_analyzer", heuristic=Heuristics.TYPOSQUATTING_PRESENCE, depends_on=None
65-
)
63+
super().__init__(name="typosquatting_presence_analyzer", heuristic=Heuristics.TYPOSQUATTING_PRESENCE)
6664
self.default_path = os.path.join(MACARON_PATH, "resources/popular_packages.txt")
6765
if popular_packages_path:
6866
self.default_path = popular_packages_path

src/macaron/malware_analyzer/pypi_heuristics/sourcecode/pypi_sourcecode_analyzer.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ def __init__(self, resources_path: str | None = None) -> None:
5656
super().__init__(
5757
name="suspicious_patterns_analyzer",
5858
heuristic=Heuristics.SUSPICIOUS_PATTERNS,
59-
# We include the SKIP condition here as we want to consider the case where EMPTY_PROJECT_LINK fails,
60-
# meaning SOURCE_CODE_REPO is skipped, as this is still a scenario where the source code repository
61-
# is not available, so we want to run source code analysis.
62-
depends_on=[
63-
(Heuristics.SOURCE_CODE_REPO, HeuristicResult.FAIL),
64-
(Heuristics.SOURCE_CODE_REPO, HeuristicResult.SKIP),
65-
],
6659
)
6760
if resources_path is None:
6861
resources_path = global_config.resources_path

src/macaron/malware_analyzer/pypi_heuristics/sourcecode/suspicious_setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ def _get_setup_source_code(self, pypi_package_json: PyPIPackageJsonAsset) -> str
4343
str | None
4444
The source code.
4545
"""
46-
sourcecode_url: str | None = pypi_package_json.get_sourcecode_url()
46+
sourcecode_url: str | None = pypi_package_json.get_sourcecode_url(package_type="sdist")
4747
if sourcecode_url is None:
48-
error_msg = "Package metadata does not supply a tarball"
49-
logger.debug(error_msg)
50-
raise HeuristicAnalyzerValueError(error_msg)
48+
# This isn't an error as some packages may be distributed just as wheels, which typically don't
49+
# include setup.py files, or at least don't run then automatically.
50+
logger.info("Package metadata does not supply a tarball")
51+
return None
5152

5253
# Get name of file.
5354
_, _, file_name = sourcecode_url.rpartition("/")

src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def analyze_source(
135135
logger.debug("Instantiating %s", PyPISourcecodeAnalyzer.__name__)
136136
analyzer = PyPISourcecodeAnalyzer()
137137

138-
if not force and analyzer.depends_on and self._should_skip(results, analyzer.depends_on):
138+
# If SOURCE_CODE_REPO failed, there is no source code repository available for this package. This is when we would want
139+
# to run source code analysis.
140+
if not force and results[Heuristics.SOURCE_CODE_REPO] == HeuristicResult.PASS:
139141
return {analyzer.heuristic: HeuristicResult.SKIP}, {}
140142

141143
try:

tests/malware_analyzer/pypi/test_closer_release_join_date.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from datetime import datetime
66
from unittest.mock import MagicMock
77

8+
import pytest
9+
10+
from macaron.errors import HeuristicAnalyzerValueError
811
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult
912
from macaron.malware_analyzer.pypi_heuristics.metadata.closer_release_join_date import CloserReleaseJoinDateAnalyzer
1013

@@ -47,8 +50,8 @@ def test_analyze_process(pypi_package_json: MagicMock) -> None:
4750
assert "latest_release_date" in detail_info
4851

4952

50-
def test_analyze_skip(pypi_package_json: MagicMock) -> None:
51-
"""Test analyze method when the heuristic should be skipped."""
53+
def test_analyze_no_maintainers(pypi_package_json: MagicMock) -> None:
54+
"""Test analyze method when there are no maintainers, raising an error."""
5255
analyzer = CloserReleaseJoinDateAnalyzer()
5356

5457
# Set up mock return values.
@@ -57,9 +60,5 @@ def test_analyze_skip(pypi_package_json: MagicMock) -> None:
5760
pypi_package_json.component_name = "mock1"
5861

5962
# Call the method.
60-
result, detail_info = analyzer.analyze(pypi_package_json)
61-
62-
# Assert.
63-
assert result == HeuristicResult.SKIP
64-
assert "maintainers_join_date" in detail_info
65-
assert "latest_release_date" in detail_info
63+
with pytest.raises(HeuristicAnalyzerValueError):
64+
_ = analyzer.analyze(pypi_package_json)

tests/malware_analyzer/pypi/test_high_release_frequency.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2024 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""Tests for high release frequency heuristic."""
55

66
from unittest.mock import MagicMock
77

8+
import pytest
9+
10+
from macaron.errors import HeuristicAnalyzerValueError
811
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult
912
from macaron.malware_analyzer.pypi_heuristics.metadata.high_release_frequency import HighReleaseFrequencyAnalyzer
1013

@@ -59,8 +62,8 @@ def test_analyze_low_frequency_fail(pypi_package_json: MagicMock) -> None:
5962
assert detail_info == {"frequency": 1}
6063

6164

62-
def test_analyze_no_releases_skip(pypi_package_json: MagicMock) -> None:
63-
"""Test HighReleaseFrequencyAnalyzer when no releases are available (should skip).
65+
def test_analyze_no_releases(pypi_package_json: MagicMock) -> None:
66+
"""Test HighReleaseFrequencyAnalyzer when no releases are available (should error for a malformed package).
6467
6568
Parameters
6669
----------
@@ -73,11 +76,8 @@ def test_analyze_no_releases_skip(pypi_package_json: MagicMock) -> None:
7376
pypi_package_json.get_releases.return_value = None
7477

7578
# Call the method.
76-
result, detail_info = analyzer.analyze(pypi_package_json)
77-
78-
# Assert.
79-
assert result == HeuristicResult.SKIP
80-
assert not detail_info
79+
with pytest.raises(HeuristicAnalyzerValueError):
80+
_ = analyzer.analyze(pypi_package_json)
8181

8282

8383
def test_analyze_single_release_skip(pypi_package_json: MagicMock) -> None:

tests/malware_analyzer/pypi/test_one_release_analyzer.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2024 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""Tests for heuristic detecting malicious metadata from PyPI"""
55
from unittest.mock import MagicMock
66

77
import pytest
88

9+
from macaron.errors import HeuristicAnalyzerValueError
910
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult
1011
from macaron.malware_analyzer.pypi_heuristics.metadata.one_release import OneReleaseAnalyzer
1112
from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIPackageJsonAsset
@@ -32,14 +33,12 @@ def setup_one_release_analyzer() -> dict:
3233

3334

3435
def test_analyze_no_releases(one_release_analyzer: dict) -> None:
35-
"""Test for result skipped."""
36+
"""No release information available, should error for a malformed package."""
3637
mock_pypi_package_pass = one_release_analyzer["mock_pypi_package_pass"]
3738
mock_pypi_package_pass.get_releases.return_value = None
38-
expected_result: tuple[HeuristicResult, dict] = (HeuristicResult.SKIP, {"releases": {}})
3939

40-
result = one_release_analyzer["analyzer"].analyze(mock_pypi_package_pass)
41-
42-
assert result == expected_result
40+
with pytest.raises(HeuristicAnalyzerValueError):
41+
_ = one_release_analyzer["analyzer"].analyze(mock_pypi_package_pass)
4342

4443

4544
def test_analyze_one_release(one_release_analyzer: dict) -> None:

tests/malware_analyzer/pypi/test_suspicious_setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2024 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""Tests for suspicious setup.py heuristic."""
@@ -14,7 +14,7 @@
1414
def test_analyze_skip() -> None:
1515
"""Test to ensure the URL of the source distribution is missing.
1616
17-
The heuristic analyzer should return SKIP if the URL is not present.
17+
The heuristic analyzer should return SKIP if the PyPI URL is not present.
1818
"""
1919
mock_pypi_package = MagicMock(spec=PyPIPackageJsonAsset)
2020
mock_pypi_package.get_sourcecode_url.return_value = None

tests/malware_analyzer/pypi/test_unchanged_release.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2024 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""Tests for heuristic detecting malicious metadata from PyPI"""
55
from unittest.mock import MagicMock
66

7+
import pytest
8+
9+
from macaron.errors import HeuristicAnalyzerValueError
710
from macaron.malware_analyzer.pypi_heuristics.heuristics import HeuristicResult
811
from macaron.malware_analyzer.pypi_heuristics.metadata.unchanged_release import UnchangedReleaseAnalyzer
912

@@ -58,8 +61,8 @@ def test_analyze_fail(pypi_package_json: MagicMock) -> None:
5861
assert not detail_info
5962

6063

61-
def test_analyze_skip(pypi_package_json: MagicMock) -> None:
62-
"""Test the analyze method returning SKIP.
64+
def test_analyze_error(pypi_package_json: MagicMock) -> None:
65+
"""Test the digest information being unavailable, resulting in an error.
6366
6467
Parameters
6568
----------
@@ -72,8 +75,5 @@ def test_analyze_skip(pypi_package_json: MagicMock) -> None:
7275
pypi_package_json.get_releases.return_value = None
7376

7477
# Call the method.
75-
result, detail_info = analyzer.analyze(pypi_package_json)
76-
77-
# Assert.
78-
assert result == HeuristicResult.SKIP
79-
assert not detail_info
78+
with pytest.raises(HeuristicAnalyzerValueError):
79+
_ = analyzer.analyze(pypi_package_json)

tests/slsa_analyzer/checks/test_detect_malicious_metadata_check.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def test_detect_malicious_metadata(
106106
load_defaults(user_config_path)
107107
pypi_registry.load_defaults()
108108

109-
httpserver.expect_request("/project/zlibxjson").respond_with_data(p_page_content)
110-
httpserver.expect_request("/user/tser111111").respond_with_data(u_page_content)
109+
httpserver.expect_request("/project/zlibxjson/").respond_with_data(p_page_content)
110+
httpserver.expect_request("/user/tser111111/").respond_with_data(u_page_content)
111111
httpserver.expect_request("/pypi/zlibxjson/json").respond_with_json(package_json)
112112
httpserver.expect_request(
113113
"/packages/3e/1e/b1ecb05e7ca1eb74ca6257a7f43d052b90d2ac01feb28eb28ce677a871ab/zlibxjson-8.2.tar.gz"

0 commit comments

Comments
 (0)