From b9727a4fbd30a3794fdea3a655fb68274932982a Mon Sep 17 00:00:00 2001 From: Behnaz Hassanshahi Date: Sun, 26 Jul 2026 19:10:53 +1000 Subject: [PATCH 01/11] docs: update docs and Macaron action (#1440) This PR bumps the Macaron GitHub Action to the latest version and also updates the documentation. Signed-off-by: behnazh-w --- .github/workflows/macaron-analysis.yaml | 2 +- README.md | 2 +- docs/source/pages/macaron_action.rst | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/macaron-analysis.yaml b/.github/workflows/macaron-analysis.yaml index 24af5fd55..fd7005c7d 100644 --- a/.github/workflows/macaron-analysis.yaml +++ b/.github/workflows/macaron-analysis.yaml @@ -35,7 +35,7 @@ jobs: # Note: adjust the policy_purl to refer to your repository URL. - name: Run Macaron action id: run_macaron - uses: oracle/macaron@4ddb55e3c9ef2c77b548be55c557078c4476fd9c # v0.24.0 + uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 with: repo_path: ./ policy_file: check-github-actions diff --git a/README.md b/README.md index 70b57a6ec..bc6e7b85d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Use Macaron as a GitHub Action To use the Macaron GitHub Action, add the following step to your workflow (adjust the version as needed). In this example, we use an example policy. For detailed instructions and a comprehensive list of available options, please refer to the [Macaron GitHub Action documentation](https://oracle.github.io/macaron/pages/macaron_action.html). ```yaml -- uses: oracle/macaron@4ddb55e3c9ef2c77b548be55c557078c4476fd9c # v0.24.0 +- uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 with: repo_path: ./ policy_file: check-github-actions diff --git a/docs/source/pages/macaron_action.rst b/docs/source/pages/macaron_action.rst index a16591e8c..1b3f81f4f 100644 --- a/docs/source/pages/macaron_action.rst +++ b/docs/source/pages/macaron_action.rst @@ -24,7 +24,7 @@ When you use this action, you can reference it directly in your workflow. For a steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Run Macaron Security Analysis Action - uses: oracle/macaron@4ddb55e3c9ef2c77b548be55c557078c4476fd9c # v0.24.0 + uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 with: repo_path: ./ policy_file: check-github-actions @@ -45,7 +45,7 @@ verification reads the matching ``macaron.db``. .. code-block:: yaml - name: Verify policy - uses: oracle/macaron@fda4dda04aa7228fcaba162804891806cf5a1375 # v0.22.0 + uses: oracle/macaron@794eec32782979f577efb5ff71d585f683a3550f # v0.25.0 with: policy_file: policy.dl From fce1d32a685c7ae10ebd2d981b88ecea7d20818f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Tr=C3=B6ger?= Date: Sun, 26 Jul 2026 18:07:40 -0700 Subject: [PATCH 02/11] chore(deps-dev): update mypy requirement from >=1.19.1,<1.20 to >=2.0.0,<3.0.0 and fix and adjust configuration and fix code according to the newly uncovered type errors (#1437) Signed-off-by: Jens Troeger --- .pre-commit-config.yaml | 2 +- pyproject.toml | 18 +++++++++++++++--- src/macaron/parsers/actionparser.py | 4 ++-- src/macaron/parsers/yaml/loader.py | 8 ++++---- src/macaron/repo_finder/repo_utils.py | 4 ++-- .../checks/detect_malicious_metadata_check.py | 6 +++--- src/macaron/util.py | 2 +- tests/parsers/yaml/test_yaml_loader.py | 8 ++++---- tests/repo_finder/test_commit_finder.py | 19 +++++++++---------- .../slsa_analyzer/git_service/test_gitlab.py | 7 ++++--- tests/slsa_analyzer/mock_git_utils.py | 4 ++-- 11 files changed, 47 insertions(+), 35 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aca831163..811b0ec10 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -67,7 +67,7 @@ repos: language: python files: ^src/macaron/|^tests/ types: [text, python] - exclude: ^tests/malware_analyzer/pypi/resources/sourcecode_samples.* + pass_filenames: false args: [--show-traceback, --config-file, pyproject.toml] # Enable a whole bunch of useful helper hooks, too. diff --git a/pyproject.toml b/pyproject.toml index 8eabaf6a4..48b742271 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -76,7 +76,7 @@ actions = [ ] dev = [ "flit >=3.2.0,<4.0.0", - "mypy >=1.19.1,<1.20", + "mypy >=2.0.0,<3.0.0", "types-pyyaml >=6.0.4,<7.0.0", "types-requests >=2.25.6,<3.0.0", "types-jsonschema >=4.22.0,<5.0.0", @@ -169,6 +169,18 @@ exclude = [ # https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml [tool.mypy] +files = [ + "src/macaron/", + "tests/", +] +exclude = [ + "tests/malware_analyzer/pypi/resources/sourcecode_samples/", +] +mypy_path = [ + "src/", +] +num_workers = 4 +explicit_package_bases = true show_error_codes = true show_column_numbers = true check_untyped_defs = true @@ -187,13 +199,13 @@ disallow_untyped_decorators = true [[tool.mypy.overrides]] module = [ - "pytest.*", # https://github.com/pytest-dev/pytest/issues/7469 "pydriller.*", "gitdb.*", "yamale.*", "problog.*", ] -ignore_missing_imports = true +# ignore_missing_imports = true +follow_untyped_imports = true # https://pylint.pycqa.org/en/latest/user_guide/configuration/index.html diff --git a/src/macaron/parsers/actionparser.py b/src/macaron/parsers/actionparser.py index 4bed61615..f26f58994 100644 --- a/src/macaron/parsers/actionparser.py +++ b/src/macaron/parsers/actionparser.py @@ -1,4 +1,4 @@ -# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2022 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module contains the parser for GitHub Actions Workflow files.""" @@ -37,7 +37,7 @@ def parse(workflow_path: str) -> Workflow: When parsing fails with errors. """ try: - parse_result = yamale.make_data(workflow_path, parser="ruamel") + parse_result = yamale.make_data(workflow_path, parser="ruamel") # type: ignore[no-untyped-call] except OSError as error: raise ParseError("Cannot parse GitHub Workflow: " + workflow_path) from error diff --git a/src/macaron/parsers/yaml/loader.py b/src/macaron/parsers/yaml/loader.py index e99102bc9..3aff2970a 100644 --- a/src/macaron/parsers/yaml/loader.py +++ b/src/macaron/parsers/yaml/loader.py @@ -1,4 +1,4 @@ -# Copyright (c) 2022 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2022 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module contains the loader for YAML files.""" @@ -38,7 +38,7 @@ def _load_yaml_content(path: os.PathLike | str) -> list: """ try: logger.debug("Loading yaml from file %s", path) - return list(yamale.make_data(path)) + return list(yamale.make_data(path)) # type: ignore[no-untyped-call] except YAMLError as error: abs_path = os.path.abspath(path) @@ -74,7 +74,7 @@ def validate_yaml_data(cls, schema: Schema, data: list) -> bool: """ try: logger.debug("Validate data %s with schema %s.", str(data), str(schema.dict)) - yamale.validate(schema, data) + yamale.validate(schema, data) # type: ignore[no-untyped-call] return True except yamale.YamaleError as error: logger.debug("Yaml data validation failed.") @@ -84,7 +84,7 @@ def validate_yaml_data(cls, schema: Schema, data: list) -> bool: return False @classmethod - def load(cls, path: os.PathLike | str, schema: Schema = None) -> Any: + def load(cls, path: os.PathLike | str, schema: Schema | None = None) -> Any: """Load and return a Python object from a yaml file. If ``schema`` is provided. This method will validate the loaded content against the diff --git a/src/macaron/repo_finder/repo_utils.py b/src/macaron/repo_finder/repo_utils.py index e36a3df36..2023bd4ca 100644 --- a/src/macaron/repo_finder/repo_utils.py +++ b/src/macaron/repo_finder/repo_utils.py @@ -209,8 +209,8 @@ def get_repo_tags(git_obj: Git) -> dict[str, str]: # Retrieve tags using a Git subprocess. repository_path = git_obj.repo.working_tree_dir - if not os.path.isdir(repository_path): - logger.debug("") + if not repository_path or not os.path.isdir(repository_path): + logger.debug("Invalid repository path: %s", repository_path) return {} try: result = subprocess.run( diff --git a/src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py b/src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py index 4d2bfc103..4d9151847 100644 --- a/src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py +++ b/src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py @@ -190,10 +190,10 @@ def evaluate_heuristic_results( problog_code = f"{facts}\n\n{self.malware_rules_problog_model}" logger.debug("Problog model used for evaluation:\n %s", problog_code) - problog_model = PrologString(problog_code) - problog_results: dict[Term, float] = get_evaluatable().create_from(problog_model).evaluate() + problog_model = PrologString(problog_code) # type: ignore[no-untyped-call] + problog_results: dict[Term, float] = get_evaluatable().create_from(problog_model).evaluate() # type: ignore[no-untyped-call] - confidence = problog_results.pop(Term(self.problog_result_access), 0.0) + confidence = problog_results.pop(Term(self.problog_result_access), 0.0) # type: ignore[no-untyped-call] if confidence > 0: # a rule was triggered for term, conf in problog_results.items(): if term.args: diff --git a/src/macaron/util.py b/src/macaron/util.py index d4e6420f0..c3f7f485c 100644 --- a/src/macaron/util.py +++ b/src/macaron/util.py @@ -471,7 +471,7 @@ def check_rate_limit(response: Response) -> None: remains = int(response.headers["X-RateLimit-Remaining"]) if "X-RateLimit-Remaining" in response.headers else 2 if remains <= 1: - rate_limit_reset = response.headers.get("X-RateLimit-Reset", default="") + rate_limit_reset = response.headers.get("X-RateLimit-Reset", "") if not rate_limit_reset: return diff --git a/tests/parsers/yaml/test_yaml_loader.py b/tests/parsers/yaml/test_yaml_loader.py index 56b9f5140..f726dcb25 100644 --- a/tests/parsers/yaml/test_yaml_loader.py +++ b/tests/parsers/yaml/test_yaml_loader.py @@ -1,4 +1,4 @@ -# Copyright (c) 2022 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2022 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """This module test the yaml loader functions.""" @@ -40,7 +40,7 @@ def test_validate_yaml_data(self) -> None: """Test the validate yaml data method.""" # We are not testing the behavior of yamale methods # so the schema and data can be empty. - mock_schema = Schema({}) + mock_schema = Schema({}) # type: ignore[no-untyped-call] mock_data: list = [] # No errors @@ -48,13 +48,13 @@ def test_validate_yaml_data(self) -> None: assert YamlLoader.validate_yaml_data(mock_schema, mock_data) # Errors exist - with patch("yamale.validate", side_effect=YamaleError(results=[])): + with patch("yamale.validate", side_effect=YamaleError(results=[])): # type: ignore[no-untyped-call] assert not YamlLoader.validate_yaml_data(mock_schema, mock_data) def test_load(self) -> None: """Test the load method of YamlLoader.""" schema_file = os.path.join(self.RESOURCES_DIR, "schema.yaml") - schema: Schema = yamale.make_schema(schema_file) + schema: Schema = yamale.make_schema(schema_file) # type: ignore[no-untyped-call] assert YamlLoader.load(os.path.join(self.RESOURCES_DIR, "invalid.yaml")) == {None: None} assert not YamlLoader.load(os.path.join(self.RESOURCES_DIR, "invalid.yaml"), schema) diff --git a/tests/repo_finder/test_commit_finder.py b/tests/repo_finder/test_commit_finder.py index a505b3df2..bf6fb1851 100644 --- a/tests/repo_finder/test_commit_finder.py +++ b/tests/repo_finder/test_commit_finder.py @@ -7,14 +7,13 @@ import os import re import shutil -from typing import Any import hypothesis import pytest from hypothesis import given, settings from hypothesis.strategies import DataObject, data, text from packageurl import PackageURL -from pydriller.git import Git +from pydriller.git import Git, GitCommit from macaron.repo_finder import commit_finder from macaron.repo_finder.commit_finder import AbstractPurlType, determine_optional_suffix_index @@ -117,24 +116,24 @@ def mocked_repo_() -> Git: @pytest.fixture(name="mocked_repo_commit") -def mocked_repo_commit_(mocked_repo: Git) -> Any: +def mocked_repo_commit_(mocked_repo: Git) -> GitCommit: """Add a commit to the mocked repository.""" return mocked_repo.repo.index.commit(message="Commit_0") @pytest.fixture(name="mocked_repo_empty_commit") -def mocked_repo_empty_commit_(mocked_repo: Git) -> Any: +def mocked_repo_empty_commit_(mocked_repo: Git) -> GitCommit: """Add an empty commit to the mocked repository.""" return mocked_repo.repo.index.commit(message="Empty_Commit") @pytest.fixture(name="mocked_repo_expanded") -def mocked_repo_expanded_(mocked_repo: Git, mocked_repo_commit: Any, mocked_repo_empty_commit: Any) -> Any: +def mocked_repo_expanded_(mocked_repo: Git, mocked_repo_commit: GitCommit, mocked_repo_empty_commit: GitCommit) -> Git: """Add tags to the mocked repository.""" mocked_repo.repo.create_tag("4.5", mocked_repo_commit.hexsha) # Create a tag from a tree. - mocked_repo.repo.create_tag("1.0", ref=mocked_repo.repo.heads.master.commit.tree) + mocked_repo.repo.create_tag("1.0", mocked_repo.repo.heads.master.commit.tree.hexsha) # Add a tag with unicode version. mocked_repo.repo.create_tag(UNICODE_VERSION, mocked_repo_commit.hexsha) @@ -200,7 +199,7 @@ def test_commit_finder_tag_failure( ) def test_commit_finder_success_commit( mocked_repo_expanded: Git, - mocked_repo_commit: Any, + mocked_repo_commit: GitCommit, purl_string: str, ) -> None: """Test Commit Finder on mocked repository that should match valid PURLs.""" @@ -223,7 +222,7 @@ def test_commit_finder_success_commit( ], ) def test_commit_finder_success_empty_commit( - mocked_repo_expanded: Git, mocked_repo_empty_commit: Any, purl_string: str + mocked_repo_expanded: Git, mocked_repo_empty_commit: GitCommit, purl_string: str ) -> None: """Test Commit Finder on mocked repository that should match value PURLs.""" match, outcome = commit_finder.find_commit(mocked_repo_expanded, PackageURL.from_string(purl_string)) @@ -231,7 +230,7 @@ def test_commit_finder_success_empty_commit( assert outcome == CommitFinderInfo.MATCHED -def test_commit_finder_repo_purl_success(mocked_repo_expanded: Git, mocked_repo_commit: Any) -> None: +def test_commit_finder_repo_purl_success(mocked_repo_expanded: Git, mocked_repo_commit: GitCommit) -> None: """Test Commit Finder on mocked repository using a repo type PURL.""" match, outcome = commit_finder.find_commit( mocked_repo_expanded, PackageURL.from_string(f"pkg:github/apache/maven@{mocked_repo_commit.hexsha}") @@ -254,7 +253,7 @@ def test_commit_finder_optional_suffixes(version: str, parts: list, expected: in assert determine_optional_suffix_index(version, parts) == expected -def test_get_repo_tags(mocked_repo_empty_commit: Any) -> None: +def test_get_repo_tags(mocked_repo_empty_commit: GitCommit) -> None: """Test the get repo tags utils function.""" # Create the repository object. repo = Git(os.path.join(REPO_DIR)) diff --git a/tests/slsa_analyzer/git_service/test_gitlab.py b/tests/slsa_analyzer/git_service/test_gitlab.py index e0b44fb97..3cb52855a 100644 --- a/tests/slsa_analyzer/git_service/test_gitlab.py +++ b/tests/slsa_analyzer/git_service/test_gitlab.py @@ -1,8 +1,9 @@ -# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2023 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. """Tests for the GitLab git service.""" +import collections.abc import os from pathlib import Path from unittest import mock @@ -117,7 +118,7 @@ def test_self_hosted_gitlab_without_env_set(tmp_path: Path) -> None: @pytest.fixture(name="self_hosted_gitlab") -def self_hosted_gitlab_repo_fixture(request: pytest.FixtureRequest) -> Git: +def self_hosted_gitlab_repo_fixture(request: pytest.FixtureRequest) -> collections.abc.Iterable[Git]: """Create a mock GitLab self_hosted repo. This fixture expects ONE parameter of type STR which will be the origin remote url initialized for the self_hosted @@ -153,7 +154,7 @@ def self_hosted_gitlab_repo_fixture(request: pytest.FixtureRequest) -> Git: yield gitlab_repo - gitlab_repo.clear() + gitlab_repo.clear() # type: ignore[no-untyped-call] # The indirect parameter is used to note that ``self_hosted_gitlab`` should be passed to the ``self_hosted_gitlab`` diff --git a/tests/slsa_analyzer/mock_git_utils.py b/tests/slsa_analyzer/mock_git_utils.py index 5b6011998..2b46e1053 100644 --- a/tests/slsa_analyzer/mock_git_utils.py +++ b/tests/slsa_analyzer/mock_git_utils.py @@ -39,12 +39,12 @@ def initiate_repo(repo_path: str | os.PathLike, git_init_options: dict | None = os.makedirs(repo_path) try: - git_wrapper = Git(repo_path) + git_wrapper = Git(str(repo_path)) return git_wrapper except GitError: # No git repo at repo_path. git.Repo.init(repo_path, **git_init_options) - return Git(repo_path) + return Git(str(repo_path)) def commit_files(git_wrapper: Git, file_names: list) -> bool: From 14843095a5fc8f075f8d48889ff7a241ff5c0b39 Mon Sep 17 00:00:00 2001 From: Ruchit Agrawal Date: Sat, 1 Aug 2026 13:03:46 +0530 Subject: [PATCH 03/11] feat: add package registry and maintainability check (#1400) (#1403) This PR adds a new check mcn_registry_maintainability_1 that validates whether a package exists on its public registry and is actively maintained. Signed-off-by: ruchitagrawal --- docs/source/index.rst | 3 + src/macaron/config/defaults.ini | 17 + .../checks/registry_maintainability_check.py | 707 ++++++++++++++++++ .../maven_central_registry.py | 86 +++ .../policy.dl | 9 +- .../test.yaml | 14 +- tests/integration/cases/npm_request/policy.dl | 10 + .../cases/npm_request/policy_report.json | 16 + tests/integration/cases/npm_request/test.yaml | 29 + .../integration/cases/pypi_aiohttp/policy.dl | 10 + .../cases/pypi_aiohttp/policy_report.json | 16 + .../integration/cases/pypi_aiohttp/test.yaml | 28 + tests/integration/cases/pypi_arrow/policy.dl | 8 +- .../cases/pypi_arrow/policy_report.json | 11 +- tests/integration/cases/pypi_arrow/test.yaml | 2 +- tests/integration/cases/pypi_boto/policy.dl | 10 + .../cases/pypi_boto/policy_report.json | 16 + tests/integration/cases/pypi_boto/test.yaml | 28 + .../test_registry_maintainability_check.py | 573 ++++++++++++++ 19 files changed, 1588 insertions(+), 5 deletions(-) create mode 100644 src/macaron/slsa_analyzer/checks/registry_maintainability_check.py create mode 100644 tests/integration/cases/npm_request/policy.dl create mode 100644 tests/integration/cases/npm_request/policy_report.json create mode 100644 tests/integration/cases/npm_request/test.yaml create mode 100644 tests/integration/cases/pypi_aiohttp/policy.dl create mode 100644 tests/integration/cases/pypi_aiohttp/policy_report.json create mode 100644 tests/integration/cases/pypi_aiohttp/test.yaml create mode 100644 tests/integration/cases/pypi_boto/policy.dl create mode 100644 tests/integration/cases/pypi_boto/policy_report.json create mode 100644 tests/integration/cases/pypi_boto/test.yaml create mode 100644 tests/slsa_analyzer/checks/test_registry_maintainability_check.py diff --git a/docs/source/index.rst b/docs/source/index.rst index 2139f1665..8841c885f 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -100,6 +100,9 @@ the requirements that are currently supported by Macaron. * - ``mcn_license_1`` - **License check** - Check whether the repository license is not in the configured deny-list. - This check detects the repository license via the GitHub API and validates it against a user-defined list of denied SPDX identifiers configured in ``defaults.ini``. If the deny-list is empty, any detected license is accepted. + * - ``mcn_registry_maintainability_1`` + - **Registry maintainability** - Check whether the package exists in its public registry and is actively maintained. + - This check validates registry presence, release recency, and explicit deprecation or yanked status (PyPI, npm). Also checks whether the source repository has been archived and how recently code was pushed, when a GitHub repository is available. If the registry release is stale but the GitHub repository shows recent commit activity within the threshold, the check still passes — this avoids false failures for mono-repos where a sub-module has not had its own registry release recently but the project as a whole is active. For Maven packages, the check always queries Maven Central for the **latest** release date (rather than the pinned version's publish date), so actively maintained packages with old pinned versions are not incorrectly flagged as unmaintained. Maven packages without a pinned version are also evaluated via the latest release date. The inactivity threshold is configurable via ``defaults.ini`` (``[registry_maintainability] inactivity_threshold_days``). Returns ``UNKNOWN`` when the ecosystem is unsupported or a Maven Central lookup fails. ---------------------- How does Macaron work? diff --git a/src/macaron/config/defaults.ini b/src/macaron/config/defaults.ini index 543da38eb..54516beb3 100644 --- a/src/macaron/config/defaults.ini +++ b/src/macaron/config/defaults.ini @@ -774,3 +774,20 @@ denied_licenses = # If True, the check fails when no license is detected. # If False, a missing license results in a low-confidence pass. require_license = False + +# Registry maintainability configuration. +# The mcn_registry_maintainability_1 check validates whether a package exists +# in its public registry and evaluates its maintenance status. +# Supported ecosystems: +# - PyPI: release recency (latest version), yanked status +# - npm: release recency (latest version), deprecated status +# - Maven Central: release recency (latest version); also works for PURLs +# without a pinned version by querying the most recent release directly +[registry_maintainability] + +# Maximum number of days since the last release before a package is considered +# unmaintained. Packages exceeding this threshold will fail the check. +# The same threshold is also applied to the time since the last push to the +# source repository (if available via the GitHub API). +# Default: 365 days (approximately 1 year). +inactivity_threshold_days = 365 diff --git a/src/macaron/slsa_analyzer/checks/registry_maintainability_check.py b/src/macaron/slsa_analyzer/checks/registry_maintainability_check.py new file mode 100644 index 000000000..d40a6ee0c --- /dev/null +++ b/src/macaron/slsa_analyzer/checks/registry_maintainability_check.py @@ -0,0 +1,707 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""This check validates whether a package exists in its public registry and is actively maintained.""" + +import logging +import urllib.parse +from datetime import UTC, datetime + +from packageurl import PackageURL +from sqlalchemy import Boolean, ForeignKey, Integer, String +from sqlalchemy.orm import Mapped, mapped_column + +from macaron.config.defaults import defaults +from macaron.database.table_definitions import CheckFacts +from macaron.errors import InvalidHTTPResponseError +from macaron.json_tools import json_extract +from macaron.slsa_analyzer.analyze_context import AnalyzeContext +from macaron.slsa_analyzer.checks.base_check import BaseCheck +from macaron.slsa_analyzer.checks.check_result import ( + CheckResultData, + CheckResultType, + Confidence, + JustificationType, +) +from macaron.slsa_analyzer.git_service.github import GitHub +from macaron.slsa_analyzer.package_registry.maven_central_registry import MavenCentralRegistry +from macaron.slsa_analyzer.package_registry.npm_registry import NPMRegistry, find_or_create_npm_asset +from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIRegistry, find_or_create_pypi_asset +from macaron.slsa_analyzer.registry import registry +from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo + +logger: logging.Logger = logging.getLogger(__name__) + +_REMEDIATION_GENERIC = "Consider replacing or reviewing this dependency as it may no longer be actively maintained." +_REMEDIATION_DEPRECATED = "This package has been explicitly deprecated or removed. Consider replacing this dependency." +_REMEDIATION_ARCHIVED = ( + "The source repository has been archived and is no longer accepting contributions." + " Consider replacing this dependency." +) + + +class RegistryMaintainabilityFacts(CheckFacts): + """The ORM mapping for justifications in the registry maintainability check.""" + + __tablename__ = "_registry_maintainability_check" + + #: The primary key. + id: Mapped[int] = mapped_column(ForeignKey("_check_facts.id"), primary_key=True) + + #: The name of the matched package registry (e.g. PyPI, npm). + registry_name: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: A human-facing link to the package page on the registry. + registry_url: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.HREF}, + ) + + #: A link to the source repository (GitHub), if available. + repository_url: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.HREF}, + ) + + #: Date string of the most recent release of the package (across all versions). + last_release_date: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Number of days elapsed since the most recent release of the package (across all versions). + days_since_release: Mapped[int | None] = mapped_column( + Integer, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Whether the package version is explicitly deprecated or yanked. + is_deprecated: Mapped[bool | None] = mapped_column( + Boolean, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Human-readable reason provided by the registry for the deprecation or yank. + deprecation_reason: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Whether the source repository is archived (GitHub only). + is_archived: Mapped[bool | None] = mapped_column( + Boolean, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Date string of the most recent push to the source repository (GitHub only). + last_commit_date: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Number of days elapsed since the most recent push to the source repository (GitHub only). + days_since_commit: Mapped[int | None] = mapped_column( + Integer, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + #: Suggested remediation action for the user. + remediation: Mapped[str | None] = mapped_column( + String, + nullable=True, + info={"justification": JustificationType.TEXT}, + ) + + __mapper_args__ = { # noqa: RUF012 + "polymorphic_identity": "_registry_maintainability_check", + } + + +def _build_registry_url( + registry_info: PackageRegistryInfo, name: str, namespace: str | None, version: str +) -> str | None: + """Build a human-facing package page URL for the given registry and package coordinates. + + Parameters + ---------- + registry_info : PackageRegistryInfo + The matched package registry information. + name : str + The package name. + namespace : str | None + The package namespace (used for scoped npm packages, e.g. ``@scope``). + version : str + The package version. + + Returns + ------- + str | None + The human-facing URL, or ``None`` if the registry type is unsupported. + """ + pkg_registry = registry_info.package_registry + + if isinstance(pkg_registry, PyPIRegistry) and pkg_registry.registry_url: + return urllib.parse.urljoin(pkg_registry.registry_url, f"project/{name}/{version}/") + + if isinstance(pkg_registry, NPMRegistry): + package_name = f"{namespace}/{name}" if namespace else name + return f"https://www.npmjs.com/package/{package_name}/v/{version}" + + if isinstance(pkg_registry, MavenCentralRegistry) and namespace: + return f"https://central.sonatype.com/artifact/{namespace}/{name}/{version}" + + return None + + +def _check_deprecated( + registry_info: PackageRegistryInfo, + name: str, + namespace: str | None, + version: str, +) -> tuple[bool | None, str | None]: + """Check whether the package version is explicitly deprecated or yanked. + + The check is ecosystem-specific: + + * **PyPI**: inspects the ``yanked`` flag in the release metadata for the + specific version (``releases[version][i]["yanked"]``). + * **npm**: inspects the top-level ``deprecated`` field in the version + manifest returned by the registry. + * **Other ecosystems**: returns ``(None, None)`` — signal not available. + + Parameters + ---------- + registry_info : PackageRegistryInfo + The matched package registry information. + name : str + The package name. + namespace : str | None + The package namespace (used for scoped npm packages). + version : str + The package version. + + Returns + ------- + tuple[bool | None, str | None] + A tuple ``(is_deprecated, reason)``. When the signal is not available + for the current ecosystem both values are ``None``. + """ + pkg_registry = registry_info.package_registry + + if isinstance(pkg_registry, PyPIRegistry): + pypi_asset = find_or_create_pypi_asset(name, version, registry_info) + if pypi_asset is None: + logger.debug("Could not obtain PyPI package JSON asset for %s@%s.", name, version) + return None, None + + if not (pypi_asset.package_json or pypi_asset.download(dest="")): + logger.debug("Failed to download PyPI package JSON for %s@%s.", name, version) + return None, None + + # The package-level endpoint stores per-version file info under ``releases``. + # A version may have multiple distribution files (.tar.gz, .whl, etc.). + # Per PEP 592, yanking is tracked per file; we treat the version as yanked + # if ANY of its files carries the yanked flag. + version_files = json_extract(pypi_asset.package_json, ["releases", version], list) + if version_files: + yanked_files = [f for f in version_files if f.get("yanked")] + if yanked_files: + yanked_reason: str | None = yanked_files[0].get("yanked_reason") or None + return True, yanked_reason + return False, None + + return False, None + + if isinstance(pkg_registry, NPMRegistry): + npm_asset = find_or_create_npm_asset(name, namespace, version, registry_info) + if npm_asset is None: + logger.debug("Could not obtain npm package JSON asset for %s@%s.", name, version) + return None, None + + if not (npm_asset.package_json or npm_asset.download(dest="")): + logger.debug("Failed to download npm package JSON for %s@%s.", name, version) + return None, None + + deprecated_msg = npm_asset.package_json.get("deprecated") + if deprecated_msg: + return True, str(deprecated_msg) + return False, None + + # Maven Central and other ecosystems do not expose a standard deprecation flag. + return None, None + + +def _get_latest_release_timestamp( + registry_info: PackageRegistryInfo, + name: str, + namespace: str | None, + version: str, +) -> datetime | None: + """Return the publish timestamp of the *latest* release of the package. + + This is used for the release-recency signal so that a pinned old version of + an actively maintained package is not incorrectly flagged as unmaintained. + + For PyPI the package-level JSON endpoint already exposes the latest + version's files under the ``urls`` key, so we reuse the already-cached + asset. For npm we resolve the latest version via the registry API and + then query its publish timestamp via deps.dev. + + Parameters + ---------- + registry_info : PackageRegistryInfo + The matched package registry information. + name : str + The package name. + namespace : str | None + The package namespace (used for scoped npm packages). + version : str + The specific version of the analysed PURL, used only as a cache key + when fetching the PyPI asset. + + Returns + ------- + datetime | None + The publish timestamp of the latest release, or ``None`` if it cannot + be determined. + """ + pkg_registry = registry_info.package_registry + + if isinstance(pkg_registry, PyPIRegistry): + pypi_asset = find_or_create_pypi_asset(name, version, registry_info) + if pypi_asset is None: + return None + if not (pypi_asset.package_json or pypi_asset.download(dest="")): + return None + upload_time_str = pypi_asset.get_latest_release_upload_time() + if upload_time_str: + try: + # PyPI upload_time strings use "%Y-%m-%dT%H:%M:%S" (no tz suffix); assume UTC. + return datetime.strptime(upload_time_str, "%Y-%m-%dT%H:%M:%S").replace(tzinfo=UTC) + except ValueError: + logger.debug("Could not parse PyPI latest release upload time %r.", upload_time_str) + return None + + if isinstance(pkg_registry, NPMRegistry): + latest_version = pkg_registry.get_latest_version(namespace, name) + if latest_version is None: + logger.debug("Could not determine latest version for npm package %s.", name) + return None + latest_purl = str(PackageURL(type="npm", namespace=namespace, name=name, version=latest_version)) + try: + return pkg_registry.find_publish_timestamp(latest_purl) + except InvalidHTTPResponseError as error: + logger.debug("Could not retrieve latest release timestamp for npm package %s: %s", name, error) + return None + + if isinstance(pkg_registry, MavenCentralRegistry) and namespace: + try: + return pkg_registry.find_latest_release_timestamp(namespace, name) + except InvalidHTTPResponseError as error: + logger.debug( + "Could not retrieve latest Maven release timestamp for %s:%s: %s", + namespace, + name, + error, + ) + return None + + return None + + +def _check_maven_no_version( + ctx: AnalyzeContext, + registry_infos: list[PackageRegistryInfo], +) -> CheckResultData | None: + """Attempt a release-recency check for a Maven PURL that has no pinned version. + + Maven Central allows querying for the latest release without specifying a + version, so we can still evaluate release recency even when the caller did + not pin a specific version in the PURL. + + Parameters + ---------- + ctx : AnalyzeContext + The object containing processed data for the target component. + registry_infos : list[PackageRegistryInfo] + The package registries available for this analysis run. + + Returns + ------- + CheckResultData | None + A ``CheckResultData`` with ``PASSED`` or ``FAILED`` if the latest + release date can be determined from Maven Central. Returns a + ``CheckResultData`` with ``UNKNOWN`` when the Maven Central API call + fails. Returns ``None`` only when this helper is not applicable + (non-Maven PURL, missing namespace, or no matching registry). + """ + if ctx.component.type != "maven": + return None + + parsed_purl = PackageURL.from_string(ctx.component.purl) + namespace: str | None = parsed_purl.namespace + if not namespace: + logger.debug( + "Maven PURL %s has no namespace; cannot query Maven Central.", + ctx.component.purl, + ) + return None + + for _registry_info in registry_infos: + if _registry_info.ecosystem != "maven": + continue + pkg_registry = _registry_info.package_registry + if not isinstance(pkg_registry, MavenCentralRegistry): + continue + + registry_url = f"https://central.sonatype.com/artifact/{namespace}/{ctx.component.name}" + try: + latest_dt = pkg_registry.find_latest_release_timestamp(namespace, ctx.component.name) + except InvalidHTTPResponseError as error: + logger.debug( + "Could not retrieve latest Maven release for %s: %s", + ctx.component.purl, + error, + ) + registry_name = type(pkg_registry).__name__.replace("Registry", "") + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + registry_name=registry_name, + registry_url=registry_url, + remediation=("Cannot determine registry status: Maven Central API is unavailable."), + confidence=Confidence.LOW, + ) + ], + result_type=CheckResultType.UNKNOWN, + ) + + now = datetime.now(UTC) + threshold: int = defaults.getint("registry_maintainability", "inactivity_threshold_days", fallback=365) + days_since_release = (now - latest_dt).days + last_release_date = latest_dt.strftime("%Y-%m-%d") + registry_name = type(pkg_registry).__name__.replace("Registry", "") + + if days_since_release > threshold: + # Apply the same GitHub rescue signal as the versioned path: if the + # GitHub repo shows recent commit activity, treat the package as + # still maintained despite the stale Maven Central release. + # An archived repo is never rescued — it always fails. + is_archived: bool | None = None + days_since_commit: int | None = None + git_service = ctx.dynamic_data.get("git_service") + if isinstance(git_service, GitHub) and ctx.component.repository: + repo = ctx.component.repository + full_name = repo.complete_name.removeprefix("github.com/") + repo_data = git_service.api_client.get_repo_data(full_name) + if repo_data: + is_archived = bool(repo_data.get("archived", False)) + if not is_archived: + pushed_at: str | None = repo_data.get("pushed_at") + if pushed_at: + try: + commit_dt = datetime.fromisoformat(pushed_at) + days_since_commit = (now - commit_dt).days + except ValueError: + logger.debug( + "Could not parse pushed_at timestamp %r for %s.", + pushed_at, + ctx.component.purl, + ) + + if is_archived: + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + registry_name=registry_name, + registry_url=registry_url, + days_since_release=days_since_release, + last_release_date=last_release_date, + is_archived=True, + remediation=_REMEDIATION_ARCHIVED, + confidence=Confidence.MEDIUM, + ) + ], + result_type=CheckResultType.FAILED, + ) + + if days_since_commit is not None and days_since_commit <= threshold: + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + registry_name=registry_name, + registry_url=registry_url, + days_since_release=days_since_release, + last_release_date=last_release_date, + days_since_commit=days_since_commit, + confidence=Confidence.MEDIUM, + ) + ], + result_type=CheckResultType.PASSED, + ) + + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + registry_name=registry_name, + registry_url=registry_url, + days_since_release=days_since_release, + last_release_date=last_release_date, + remediation=_REMEDIATION_GENERIC, + confidence=Confidence.MEDIUM, + ) + ], + result_type=CheckResultType.FAILED, + ) + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + registry_name=registry_name, + registry_url=registry_url, + days_since_release=days_since_release, + last_release_date=last_release_date, + confidence=Confidence.MEDIUM, + ) + ], + result_type=CheckResultType.PASSED, + ) + + return None + + +class RegistryMaintainabilityCheck(BaseCheck): + """Check whether a package exists in its public registry and is actively maintained. + + The check evaluates three independent signals when available: + + 1. **Registry presence and release recency** — the package must be found on + its expected public registry, and the most recent release must fall within + the configured inactivity threshold (``inactivity_threshold_days``). + 2. **Deprecated / yanked status** — PyPI yanked releases and npm deprecated + packages cause an immediate failure regardless of release age. + 3. **Source repository archived status and commit recency** — when the + component's source repository is hosted on GitHub, the check also + inspects whether the repository has been archived and how recently code + was pushed. + + The check returns ``UNKNOWN`` when it cannot determine a result (e.g. + unsupported ecosystem, no version in PURL, or an API error). + """ + + def __init__(self) -> None: + """Initialize the check instance.""" + check_id = "mcn_registry_maintainability_1" + description = "Check if the package exists in its expected public registry and is actively maintained." + super().__init__(check_id=check_id, description=description) + + def run_check(self, ctx: AnalyzeContext) -> CheckResultData: + """Run the registry maintainability check. + + Parameters + ---------- + ctx : AnalyzeContext + The object containing processed data for the target component. + + Returns + ------- + CheckResultData + The result of the check. + """ + # A specific version is required for the full check. For Maven we can + # still assess release recency by querying Maven Central for the latest + # release without a pinned version. + if not ctx.component.version: + logger.debug( + "No version found in PURL %s; attempting Maven-specific recency check.", + ctx.component.purl, + ) + registry_infos_nv: list[PackageRegistryInfo] = ctx.dynamic_data["package_registries"] + maven_no_version_result = _check_maven_no_version(ctx, registry_infos_nv) + if maven_no_version_result is not None: + return maven_no_version_result + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + remediation=("Cannot determine registry status: the PURL does not include a specific version."), + confidence=Confidence.LOW, + ) + ], + result_type=CheckResultType.UNKNOWN, + ) + + # Iterate over all registries to find one that matches the component ecosystem + # and can return a publish timestamp. We skip registries that raise + # NotImplementedError e.g. Maven Central or InvalidHTTPResponseError. + registry_infos: list[PackageRegistryInfo] = ctx.dynamic_data["package_registries"] + matched_registry_info: PackageRegistryInfo | None = None + publish_dt: datetime | None = None + + for _registry_info in registry_infos: + if _registry_info.ecosystem != ctx.component.type: + continue + try: + publish_dt = _registry_info.package_registry.find_publish_timestamp(ctx.component.purl) + matched_registry_info = _registry_info + break + except InvalidHTTPResponseError as error: + logger.debug( + "Could not retrieve publish timestamp for %s: %s", + ctx.component.purl, + error, + ) + except NotImplementedError: + continue + + if matched_registry_info is None or publish_dt is None: + logger.debug( + "Skipping %s: no matching package registry found for PURL %s.", + self.check_info.check_id, + ctx.component.purl, + ) + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + remediation=( + "No supported package registry found for this ecosystem " + "or the registry API is currently unavailable." + ), + confidence=Confidence.LOW, + ) + ], + result_type=CheckResultType.UNKNOWN, + ) + + registry_info = matched_registry_info + pkg_registry = registry_info.package_registry + registry_name: str = type(pkg_registry).__name__.replace("Registry", "") + + # Extract namespace from the PURL once for reuse across signals. + parsed_purl = PackageURL.from_string(ctx.component.purl) + namespace: str | None = parsed_purl.namespace + + now = datetime.now(UTC) + + # Use latest release date of the package for the recency signal. + latest_publish_dt = _get_latest_release_timestamp( + registry_info, ctx.component.name, namespace, ctx.component.version + ) + recency_dt = latest_publish_dt if latest_publish_dt is not None else publish_dt + days_since_release: int = (now - recency_dt).days + last_release_date: str = recency_dt.strftime("%Y-%m-%d") + + # Check for explicit deprecation/yanked flag. + is_deprecated, deprecation_reason = _check_deprecated( + registry_info, + ctx.component.name, + namespace, + ctx.component.version, + ) + + # Retrieve GitHub signals (archived status + last commit). + is_archived: bool | None = None + last_commit_date: str | None = None + days_since_commit: int | None = None + repository_url: str | None = None + + git_service = ctx.dynamic_data.get("git_service") + if isinstance(git_service, GitHub) and ctx.component.repository: + repo = ctx.component.repository + full_name = repo.complete_name.removeprefix("github.com/") + repo_data = git_service.api_client.get_repo_data(full_name) + + if repo_data: + is_archived = bool(repo_data.get("archived", False)) + pushed_at: str | None = repo_data.get("pushed_at") + if pushed_at: + try: + commit_dt = datetime.fromisoformat(pushed_at) + days_since_commit = (now - commit_dt).days + last_commit_date = commit_dt.strftime("%Y-%m-%d") + except ValueError: + logger.debug( + "Could not parse pushed_at timestamp %r for %s; skipping commit signal.", + pushed_at, + ctx.component.purl, + ) + repository_url = f"https://github.com/{full_name}" + else: + logger.debug( + "GitHub signals not available for %s: git service is not GitHub or no repository.", + ctx.component.purl, + ) + + # Determine result based on collected signals. + threshold: int = defaults.getint("registry_maintainability", "inactivity_threshold_days", fallback=365) + + registry_url = _build_registry_url(registry_info, ctx.component.name, namespace, ctx.component.version) + + result_type: CheckResultType + remediation: str | None + + if is_archived: + result_type = CheckResultType.FAILED + remediation = _REMEDIATION_ARCHIVED + elif is_deprecated: + reason_suffix = f": {deprecation_reason}" if deprecation_reason else "." + remediation = _REMEDIATION_DEPRECATED + reason_suffix + result_type = CheckResultType.FAILED + elif days_since_release > threshold: + # If the registry release is stale but the GitHub repo shows recent + # commit activity, treat the package as still maintained. This + # handles mono-repos where one sub-module has not had its own + # registry release in a while but the project as a whole is active. + if days_since_commit is not None and days_since_commit <= threshold: + result_type = CheckResultType.PASSED + remediation = None + else: + result_type = CheckResultType.FAILED + remediation = _REMEDIATION_GENERIC + elif days_since_commit is not None and days_since_commit > threshold: + result_type = CheckResultType.FAILED + remediation = _REMEDIATION_GENERIC + else: + result_type = CheckResultType.PASSED + remediation = None + + # Confidence is HIGH when we have definitive signals. Downgrade to MEDIUM + # when only the release-date signal is available (no GitHub API / deprecated flag). + if days_since_commit is not None or is_deprecated is not None: + confidence = Confidence.HIGH + else: + confidence = Confidence.MEDIUM + + return CheckResultData( + result_tables=[ + RegistryMaintainabilityFacts( + registry_name=registry_name, + registry_url=registry_url, + repository_url=repository_url, + last_release_date=last_release_date, + days_since_release=days_since_release, + is_deprecated=is_deprecated, + deprecation_reason=deprecation_reason, + is_archived=is_archived, + last_commit_date=last_commit_date, + days_since_commit=days_since_commit, + remediation=remediation, + confidence=confidence, + ) + ], + result_type=result_type, + ) + + +registry.register(RegistryMaintainabilityCheck()) diff --git a/src/macaron/slsa_analyzer/package_registry/maven_central_registry.py b/src/macaron/slsa_analyzer/package_registry/maven_central_registry.py index b682d3957..07317036b 100644 --- a/src/macaron/slsa_analyzer/package_registry/maven_central_registry.py +++ b/src/macaron/slsa_analyzer/package_registry/maven_central_registry.py @@ -239,6 +239,92 @@ def find_publish_timestamp(self, purl: str) -> datetime: raise InvalidHTTPResponseError(f"Invalid response from Maven central for {url}.") + def find_latest_release_timestamp(self, namespace: str, name: str) -> datetime: + """Return the timestamp of the most recent release of the package on Maven Central. + + Uses the same Maven Central Search REST API as ``find_publish_timestamp`` + but omits the version filter. It fetches up to 100 releases and selects + the newest timestamp client-side. For artifacts with more releases than + the requested page size, the true latest release may not be returned + unless pagination or server-side sorting is added in the future. This + allows the registry maintainability check to evaluate release recency + without requiring a pinned version in the PURL. + + Parameters + ---------- + namespace : str + The Maven group ID (e.g. ``com.intellectualsites.arkitektonika``). + name : str + The Maven artifact ID (e.g. ``Arkitektonika-Client``). + + Returns + ------- + datetime + A timezone-aware ``datetime`` object representing the + publication timestamp of the most recent release. + + Raises + ------ + InvalidHTTPResponseError + If the URL construction fails, the HTTP response is invalid, or the + expected timestamp is missing or malformed. + """ + query_params = [f"q=g:{namespace}", f"a:{name}"] + + try: + url = urllib.parse.urlunsplit( + urllib.parse.SplitResult( + scheme=self.search_scheme, + netloc=self.search_netloc, + path=f"/{self.search_endpoint}", + query="&".join( + [ + "+AND+".join(query_params), + "core=gav", + "rows=100", + "wt=json", + ] + ), + fragment="", + ) + ) + except ValueError as error: + raise InvalidHTTPResponseError( + "Failed to construct the latest-release search URL for Maven Central." + ) from error + + response = send_get_http_raw(url, headers=None, timeout=self.request_timeout) + if response: + try: + res_obj = response.json() + except requests.exceptions.JSONDecodeError as error: + raise InvalidHTTPResponseError(f"Failed to process response from Maven Central for {url}.") from error + if not res_obj: + raise InvalidHTTPResponseError(f"Empty response returned by {url}.") + if not res_obj.get("response"): + raise InvalidHTTPResponseError(f"The response returned by {url} misses `response` attribute.") + if not res_obj.get("response").get("docs"): + raise InvalidHTTPResponseError(f"No releases found for {namespace}:{name} on Maven Central.") + + # The Maven Central search API does not support server-side sorting, + # so we sort the returned releases by timestamp in Python to find the newest. + docs = res_obj["response"]["docs"] + docs_with_timestamp = [d for d in docs if d.get("timestamp")] + if not docs_with_timestamp: + raise InvalidHTTPResponseError(f"The timestamp is missing in the response returned by {url}.") + + latest_doc = max(docs_with_timestamp, key=lambda d: d["timestamp"]) + timestamp = latest_doc["timestamp"] + + logger.debug("Found latest release timestamp for %s:%s: %s.", namespace, name, timestamp) + + try: + return datetime.fromtimestamp(round(timestamp / 1000), tz=UTC) + except (OverflowError, OSError) as error: + raise InvalidHTTPResponseError(f"The timestamp returned by {url} is invalid.") from error + + raise InvalidHTTPResponseError(f"Invalid response from Maven Central for {url}.") + def get_artifact_hash(self, purl: PackageURL) -> str | None: """Return the hash of the artifact found by the passed purl relevant to the registry's URL. diff --git a/tests/integration/cases/IntellectualSites_Arkitektonika-Client/policy.dl b/tests/integration/cases/IntellectualSites_Arkitektonika-Client/policy.dl index a23f1d7a5..24ac91e90 100644 --- a/tests/integration/cases/IntellectualSites_Arkitektonika-Client/policy.dl +++ b/tests/integration/cases/IntellectualSites_Arkitektonika-Client/policy.dl @@ -16,7 +16,14 @@ Policy("test_policy", component_id, "") :- _, _ ), - check_facts(check_id, _, component_id,_,_). + check_facts(check_id, _, component_id,_,_), + check_passed(component_id, "mcn_registry_maintainability_1"). apply_policy_to("test_policy", component_id) :- is_component(component_id, "pkg:maven/com.intellectualsites.arkitektonika/Arkitektonika-Client@2.1.3"). + +Policy("registry_maintainability", component_id, "Require the package to be actively maintained.") :- + check_passed(component_id, "mcn_registry_maintainability_1"). + +apply_policy_to("registry_maintainability", component_id) :- + is_component(component_id, "pkg:maven/com.intellectualsites.arkitektonika/Arkitektonika-Client"). diff --git a/tests/integration/cases/IntellectualSites_Arkitektonika-Client/test.yaml b/tests/integration/cases/IntellectualSites_Arkitektonika-Client/test.yaml index 283c12c3f..05637b26b 100644 --- a/tests/integration/cases/IntellectualSites_Arkitektonika-Client/test.yaml +++ b/tests/integration/cases/IntellectualSites_Arkitektonika-Client/test.yaml @@ -1,8 +1,10 @@ -# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. description: | Analyzing repository authenticity of an artifact that uses Gradle as build tool. + Also verifies that the registry maintainability check passes for both the versioned + and unversioned PURLs of Arkitektonika-Client. tags: - macaron-python-package @@ -18,3 +20,13 @@ steps: kind: verify options: policy: policy.dl +- name: Run macaron analyze on the unversioned PURL + kind: analyze + options: + command_args: + - -purl + - pkg:maven/com.intellectualsites.arkitektonika/Arkitektonika-Client +- name: Run macaron verify-policy to verify the registry maintainability check passes without a version + kind: verify + options: + policy: policy.dl diff --git a/tests/integration/cases/npm_request/policy.dl b/tests/integration/cases/npm_request/policy.dl new file mode 100644 index 000000000..f3f923fbb --- /dev/null +++ b/tests/integration/cases/npm_request/policy.dl @@ -0,0 +1,10 @@ +/* Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. */ +/* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */ + +#include "prelude.dl" + +Policy("registry-maintainability", component_id, "Require package to be actively maintained.") :- + check_passed(component_id, "mcn_registry_maintainability_1"). + +apply_policy_to("registry-maintainability", component_id) :- + is_component(component_id, "pkg:npm/request@2.88.2"). diff --git a/tests/integration/cases/npm_request/policy_report.json b/tests/integration/cases/npm_request/policy_report.json new file mode 100644 index 000000000..1bf2dd9de --- /dev/null +++ b/tests/integration/cases/npm_request/policy_report.json @@ -0,0 +1,16 @@ +{ + "passed_policies": [], + "component_satisfies_policy": [], + "failed_policies": [ + [ + "registry-maintainability" + ] + ], + "component_violates_policy": [ + [ + "1", + "pkg:npm/request@2.88.2", + "registry-maintainability" + ] + ] +} diff --git a/tests/integration/cases/npm_request/test.yaml b/tests/integration/cases/npm_request/test.yaml new file mode 100644 index 000000000..6f1db6b10 --- /dev/null +++ b/tests/integration/cases/npm_request/test.yaml @@ -0,0 +1,29 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +description: | + Analyzing a deprecated npm package to verify that mcn_registry_maintainability_1 fails + when a package has been explicitly marked as deprecated on the npm registry. + +tags: +- macaron-python-package +- npm-registry-testcase + +steps: +- name: Run macaron analyze on deprecated npm request package. + kind: analyze + options: + command_args: + - -purl + - pkg:npm/request@2.88.2 +- name: Run macaron verify-policy to check maintainability fails. + kind: verify + options: + policy: policy.dl + expect_fail: true +- name: Compare verify policy result. + kind: compare + options: + kind: policy_report + result: output/policy_report.json + expected: policy_report.json diff --git a/tests/integration/cases/pypi_aiohttp/policy.dl b/tests/integration/cases/pypi_aiohttp/policy.dl new file mode 100644 index 000000000..48a686390 --- /dev/null +++ b/tests/integration/cases/pypi_aiohttp/policy.dl @@ -0,0 +1,10 @@ +/* Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. */ +/* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */ + +#include "prelude.dl" + +Policy("registry-maintainability", component_id, "Require package to be actively maintained.") :- + check_passed(component_id, "mcn_registry_maintainability_1"). + +apply_policy_to("registry-maintainability", component_id) :- + is_component(component_id, "pkg:pypi/aiohttp@3.9.3"). diff --git a/tests/integration/cases/pypi_aiohttp/policy_report.json b/tests/integration/cases/pypi_aiohttp/policy_report.json new file mode 100644 index 000000000..a5199cf50 --- /dev/null +++ b/tests/integration/cases/pypi_aiohttp/policy_report.json @@ -0,0 +1,16 @@ +{ + "passed_policies": [ + [ + "registry-maintainability" + ] + ], + "component_satisfies_policy": [ + [ + "1", + "pkg:pypi/aiohttp@3.9.3", + "registry-maintainability" + ] + ], + "failed_policies": [], + "component_violates_policy": [] +} diff --git a/tests/integration/cases/pypi_aiohttp/test.yaml b/tests/integration/cases/pypi_aiohttp/test.yaml new file mode 100644 index 000000000..3b4ecb662 --- /dev/null +++ b/tests/integration/cases/pypi_aiohttp/test.yaml @@ -0,0 +1,28 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +description: | + Analyzing PyPI package aiohttp to verify that mcn_registry_maintainability_1 passes + for an actively maintained package. Although this version is old, aiohttp continues to + receive regular releases, so the check correctly reports the package as maintained. + +tags: +- macaron-python-package + +steps: +- name: Run macaron analyze on an older aiohttp version. + kind: analyze + options: + command_args: + - -purl + - pkg:pypi/aiohttp@3.9.3 +- name: Run macaron verify-policy to check maintainability passes. + kind: verify + options: + policy: policy.dl +- name: Compare verify policy result. + kind: compare + options: + kind: policy_report + result: output/policy_report.json + expected: policy_report.json diff --git a/tests/integration/cases/pypi_arrow/policy.dl b/tests/integration/cases/pypi_arrow/policy.dl index 836cf5dfb..93abaa137 100644 --- a/tests/integration/cases/pypi_arrow/policy.dl +++ b/tests/integration/cases/pypi_arrow/policy.dl @@ -1,4 +1,4 @@ -/* Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. */ +/* Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. */ /* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */ #include "prelude.dl" @@ -10,3 +10,9 @@ Policy("has-hosted-build", component_id, "Require a hosted build and publishing apply_policy_to("has-hosted-build", component_id) :- is_component(component_id, purl), match("pkg:pypi/arrow.*", purl). + +Policy("registry-maintainability", component_id, "Require package to be actively maintained.") :- + check_passed(component_id, "mcn_registry_maintainability_1"). + +apply_policy_to("registry-maintainability", component_id) :- + is_component(component_id, "pkg:pypi/arrow@0.15.0"). diff --git a/tests/integration/cases/pypi_arrow/policy_report.json b/tests/integration/cases/pypi_arrow/policy_report.json index a53b03cfe..87cbf4519 100644 --- a/tests/integration/cases/pypi_arrow/policy_report.json +++ b/tests/integration/cases/pypi_arrow/policy_report.json @@ -1,6 +1,15 @@ { - "passed_policies": [], + "passed_policies": [ + [ + "registry-maintainability" + ] + ], "component_satisfies_policy": [ + [ + "1", + "pkg:pypi/arrow@0.15.0", + "registry-maintainability" + ], [ "2", "pkg:pypi/arrow@1.3.0", diff --git a/tests/integration/cases/pypi_arrow/test.yaml b/tests/integration/cases/pypi_arrow/test.yaml index 370241bfd..131f9eac7 100644 --- a/tests/integration/cases/pypi_arrow/test.yaml +++ b/tests/integration/cases/pypi_arrow/test.yaml @@ -1,4 +1,4 @@ -# Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. description: | diff --git a/tests/integration/cases/pypi_boto/policy.dl b/tests/integration/cases/pypi_boto/policy.dl new file mode 100644 index 000000000..8e57f1f0c --- /dev/null +++ b/tests/integration/cases/pypi_boto/policy.dl @@ -0,0 +1,10 @@ +/* Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. */ +/* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */ + +#include "prelude.dl" + +Policy("registry-maintainability", component_id, "Require package to be actively maintained.") :- + check_passed(component_id, "mcn_registry_maintainability_1"). + +apply_policy_to("registry-maintainability", component_id) :- + is_component(component_id, "pkg:pypi/boto@2.49.0"). diff --git a/tests/integration/cases/pypi_boto/policy_report.json b/tests/integration/cases/pypi_boto/policy_report.json new file mode 100644 index 000000000..4adbd1f1a --- /dev/null +++ b/tests/integration/cases/pypi_boto/policy_report.json @@ -0,0 +1,16 @@ +{ + "passed_policies": [], + "component_satisfies_policy": [], + "failed_policies": [ + [ + "registry-maintainability" + ] + ], + "component_violates_policy": [ + [ + "1", + "pkg:pypi/boto@2.49.0", + "registry-maintainability" + ] + ] +} diff --git a/tests/integration/cases/pypi_boto/test.yaml b/tests/integration/cases/pypi_boto/test.yaml new file mode 100644 index 000000000..6f584c0d5 --- /dev/null +++ b/tests/integration/cases/pypi_boto/test.yaml @@ -0,0 +1,28 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +description: | + Analyzing a stale PyPI package to verify that mcn_registry_maintainability_1 fails + when a package has not had a release within the inactivity threshold (365 days). + +tags: +- macaron-python-package + +steps: +- name: Run macaron analyze on stale boto release. + kind: analyze + options: + command_args: + - -purl + - pkg:pypi/boto@2.49.0 +- name: Run macaron verify-policy to check maintainability fails. + kind: verify + options: + policy: policy.dl + expect_fail: true +- name: Compare verify policy result. + kind: compare + options: + kind: policy_report + result: output/policy_report.json + expected: policy_report.json diff --git a/tests/slsa_analyzer/checks/test_registry_maintainability_check.py b/tests/slsa_analyzer/checks/test_registry_maintainability_check.py new file mode 100644 index 000000000..563162f18 --- /dev/null +++ b/tests/slsa_analyzer/checks/test_registry_maintainability_check.py @@ -0,0 +1,573 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Tests for the registry maintainability check.""" + +import os +from datetime import UTC, datetime, timedelta +from pathlib import Path +from unittest.mock import MagicMock, patch + +from macaron.config.defaults import load_defaults +from macaron.errors import InvalidHTTPResponseError +from macaron.slsa_analyzer.checks.check_result import CheckResultType +from macaron.slsa_analyzer.checks.registry_maintainability_check import RegistryMaintainabilityCheck +from macaron.slsa_analyzer.git_service.base_git_service import NoneGitService +from macaron.slsa_analyzer.git_service.github import GitHub +from macaron.slsa_analyzer.package_registry.maven_central_registry import MavenCentralRegistry +from macaron.slsa_analyzer.package_registry.npm_registry import NPMRegistry +from macaron.slsa_analyzer.package_registry.pypi_registry import PyPIRegistry +from macaron.slsa_analyzer.specs.package_registry_spec import PackageRegistryInfo +from tests.conftest import MockAnalyzeContext + +_PYPI_PURL = "pkg:pypi/requests@2.28.0" +_NPM_PURL = "pkg:npm/express@4.18.2" +_NO_VERSION_PURL = "pkg:pypi/requests" +_MAVEN_PURL = "pkg:maven/com.intellectualsites.arkitektonika/Arkitektonika-Client@2.1.3" +_MAVEN_PURL_NO_VERSION = "pkg:maven/com.intellectualsites.arkitektonika/Arkitektonika-Client" + + +def _make_github_service() -> GitHub: + """Return a GitHub git service instance with defaults loaded.""" + service = GitHub() + service.load_defaults() + return service + + +def _load_registry_config(tmp_path: Path, threshold_days: int = 365) -> None: + """Write a temporary ini file with [registry_maintainability] settings and load it.""" + config = f""" +[registry_maintainability] +inactivity_threshold_days = {threshold_days} +""" + config_path = os.path.join(tmp_path, "registry_config.ini") + with open(config_path, "w", encoding="utf-8") as fh: + fh.write(config) + load_defaults(config_path) + + +def _make_pypi_registry_info() -> PackageRegistryInfo: + """Build a minimal PyPI PackageRegistryInfo suitable for tests.""" + pypi_registry = PyPIRegistry() + pypi_registry.load_defaults() + return PackageRegistryInfo(ecosystem="pypi", package_registry=pypi_registry) + + +def _mock_pypi_ctx(macaron_path: Path, purl: str = _PYPI_PURL) -> MockAnalyzeContext: + """Return a MockAnalyzeContext wired up with a PyPI registry.""" + ctx = MockAnalyzeContext(macaron_path=macaron_path, output_dir="", purl=purl) + ctx.dynamic_data["package_registries"] = [_make_pypi_registry_info()] + ctx.dynamic_data["git_service"] = NoneGitService() + return ctx + + +# Tests + + +def test_unknown_no_version(macaron_path: Path, tmp_path: Path) -> None: + """The check returns UNKNOWN when the PURL has no version pinned.""" + _load_registry_config(tmp_path) + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path, purl=_NO_VERSION_PURL) + assert check.run_check(ctx).result_type == CheckResultType.UNKNOWN + + +def test_unknown_no_registries(macaron_path: Path, tmp_path: Path) -> None: + """The check returns UNKNOWN when no package registries are matched.""" + _load_registry_config(tmp_path) + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + ctx.dynamic_data["package_registries"] = [] + assert check.run_check(ctx).result_type == CheckResultType.UNKNOWN + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +def test_unknown_api_error(mock_timestamp: MagicMock, macaron_path: Path, tmp_path: Path) -> None: + """The check returns UNKNOWN when deps.dev raises InvalidHTTPResponseError.""" + _load_registry_config(tmp_path) + mock_timestamp.side_effect = InvalidHTTPResponseError("API unavailable") + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.UNKNOWN + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_pass_recent_release( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check passes when the last release is within the threshold.""" + _load_registry_config(tmp_path, threshold_days=365) + recent = datetime.now(UTC) - timedelta(days=30) + mock_timestamp.return_value = recent + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.PASSED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_fail_stale_release( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check fails when the last release exceeds the inactivity threshold.""" + _load_registry_config(tmp_path, threshold_days=365) + stale = datetime.now(UTC) - timedelta(days=500) + mock_timestamp.return_value = stale + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_fail_yanked_pypi( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check fails immediately when a PyPI release is yanked, regardless of age.""" + _load_registry_config(tmp_path) + recent = datetime.now(UTC) - timedelta(days=10) + mock_timestamp.return_value = recent + mock_deprecated.return_value = (True, "Security vulnerability discovered.") + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_fail_deprecated_npm( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check fails immediately when an npm package version is deprecated.""" + _load_registry_config(tmp_path) + recent = datetime.now(UTC) - timedelta(days=10) + mock_timestamp.return_value = recent + mock_deprecated.return_value = (True, "Use express@5 instead.") + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + npm_registry = NPMRegistry() + npm_registry.load_defaults() + registry_info = PackageRegistryInfo(ecosystem="npm", package_registry=npm_registry) + + ctx = MockAnalyzeContext(macaron_path=macaron_path, output_dir="", purl=_NPM_PURL) + ctx.dynamic_data["package_registries"] = [registry_info] + ctx.dynamic_data["git_service"] = NoneGitService() + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +@patch("macaron.slsa_analyzer.git_service.github.GitHub.api_client") +def test_fail_archived_repo( + mock_api_client: MagicMock, + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check fails when the GitHub repository is archived, even if release is recent.""" + _load_registry_config(tmp_path) + recent = datetime.now(UTC) - timedelta(days=10) + mock_timestamp.return_value = recent + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + mock_api_client.get_repo_data.return_value = { + "archived": True, + "pushed_at": (datetime.now(UTC) - timedelta(days=10)).strftime("%Y-%m-%dT%H:%M:%SZ"), + } + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + ctx.dynamic_data["git_service"] = _make_github_service() + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +@patch("macaron.slsa_analyzer.git_service.github.GitHub.api_client") +def test_fail_stale_commit( + mock_api_client: MagicMock, + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check fails when the last commit exceeds the threshold, even if release is recent.""" + _load_registry_config(tmp_path, threshold_days=365) + recent = datetime.now(UTC) - timedelta(days=30) + stale_push = datetime.now(UTC) - timedelta(days=500) + mock_timestamp.return_value = recent + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + mock_api_client.get_repo_data.return_value = { + "archived": False, + "pushed_at": stale_push.strftime("%Y-%m-%dT%H:%M:%SZ"), + } + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + ctx.dynamic_data["git_service"] = _make_github_service() + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_custom_threshold( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check respects a custom threshold loaded from config.""" + _load_registry_config(tmp_path, threshold_days=60) + # 90 days exceeds the 60-day threshold. + slightly_stale = datetime.now(UTC) - timedelta(days=90) + mock_timestamp.return_value = slightly_stale + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None # fall back to find_publish_timestamp value + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_boundary_at_threshold( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """The check passes when days_since_release equals the threshold exactly (threshold is exclusive).""" + _load_registry_config(tmp_path, threshold_days=365) + at_threshold = datetime.now(UTC) - timedelta(days=365) + mock_timestamp.return_value = at_threshold + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.PASSED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +@patch("macaron.slsa_analyzer.git_service.github.GitHub.api_client") +def test_skip_github_for_non_github( + mock_api_client: MagicMock, + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """No GitHub API call is made when the git service is not GitHub; check still runs correctly.""" + _load_registry_config(tmp_path) + recent = datetime.now(UTC) - timedelta(days=30) + mock_timestamp.return_value = recent + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + # git_service is NoneGitService (not GitHub) — API must not be called. + ctx.dynamic_data["git_service"] = NoneGitService() + result = check.run_check(ctx) + + mock_api_client.get_repo_data.assert_not_called() + assert result.result_type == CheckResultType.PASSED + + +def _make_maven_registry_info() -> PackageRegistryInfo: + """Build a minimal MavenCentral PackageRegistryInfo suitable for tests.""" + maven_registry = MavenCentralRegistry() + maven_registry.load_defaults() + return PackageRegistryInfo(ecosystem="maven", package_registry=maven_registry) + + +def _mock_maven_ctx(macaron_path: Path, purl: str = _MAVEN_PURL) -> MockAnalyzeContext: + """Return a MockAnalyzeContext wired up with a Maven Central registry.""" + ctx = MockAnalyzeContext(macaron_path=macaron_path, output_dir="", purl=purl) + ctx.dynamic_data["package_registries"] = [_make_maven_registry_info()] + ctx.dynamic_data["git_service"] = NoneGitService() + return ctx + + +@patch("macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_publish_timestamp") +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +def test_maven_pass_recent_release( + mock_latest: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Maven check passes when the latest release is within the threshold.""" + _load_registry_config(tmp_path, threshold_days=365) + recent = datetime.now(UTC) - timedelta(days=30) + mock_timestamp.return_value = recent # pinned version publish date (old) + mock_latest.return_value = recent # latest release date (recent) + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.PASSED + + +@patch("macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_publish_timestamp") +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +def test_maven_fail_stale_latest_release( + mock_latest: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Maven check fails when the latest release exceeds the inactivity threshold.""" + _load_registry_config(tmp_path, threshold_days=365) + pinned_date = datetime.now(UTC) - timedelta(days=2000) + stale = datetime.now(UTC) - timedelta(days=500) + mock_timestamp.return_value = pinned_date # pinned version (very old) + mock_latest.return_value = stale # latest release is also stale + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_publish_timestamp") +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +def test_maven_pass_old_pinned_but_recent_latest( + mock_latest: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Maven check passes even when the pinned version is old, as long as the latest release is recent. + + This is the core bug fix: previously the check used the pinned version's + publish date and incorrectly failed for old but actively maintained packages. + """ + _load_registry_config(tmp_path, threshold_days=365) + mock_timestamp.return_value = datetime.now(UTC) - timedelta(days=2000) # pinned very old + mock_latest.return_value = datetime.now(UTC) - timedelta(days=10) # latest is recent + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path) + assert check.run_check(ctx).result_type == CheckResultType.PASSED + + +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +def test_maven_no_version_pass( + mock_latest: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Maven check passes for a no-version PURL when the latest release is recent.""" + _load_registry_config(tmp_path, threshold_days=365) + mock_latest.return_value = datetime.now(UTC) - timedelta(days=30) + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path, purl=_MAVEN_PURL_NO_VERSION) + assert check.run_check(ctx).result_type == CheckResultType.PASSED + + +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +def test_maven_no_version_fail_stale( + mock_latest: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Maven check fails for a no-version PURL when the latest release is stale.""" + _load_registry_config(tmp_path, threshold_days=365) + mock_latest.return_value = datetime.now(UTC) - timedelta(days=500) + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path, purl=_MAVEN_PURL_NO_VERSION) + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +def test_maven_no_version_unknown_on_api_error( + mock_latest: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Maven check falls back to UNKNOWN for a no-version PURL when the Maven Central API fails.""" + _load_registry_config(tmp_path) + mock_latest.side_effect = InvalidHTTPResponseError("API unavailable") + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path, purl=_MAVEN_PURL_NO_VERSION) + assert check.run_check(ctx).result_type == CheckResultType.UNKNOWN + + +@patch( + "macaron.slsa_analyzer.package_registry.maven_central_registry.MavenCentralRegistry.find_latest_release_timestamp" +) +@patch("macaron.slsa_analyzer.git_service.github.GitHub.api_client") +def test_maven_no_version_archived_repo_fails( + mock_api_client: MagicMock, + mock_latest: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Archived GitHub repo always fails the no-version Maven check, even with a recent push date. + + An archived repo is explicitly frozen and should never be rescued by the + GitHub commit signal, mirroring the behaviour of the versioned path. + """ + _load_registry_config(tmp_path, threshold_days=365) + # Registry release is stale, which triggers the GitHub rescue path. + mock_latest.return_value = datetime.now(UTC) - timedelta(days=500) + # pushed_at is recent, but the repo is archived — rescue must NOT apply. + recent_push = datetime.now(UTC) - timedelta(days=5) + mock_api_client.get_repo_data.return_value = { + "archived": True, + "pushed_at": recent_push.strftime("%Y-%m-%dT%H:%M:%SZ"), + } + + check = RegistryMaintainabilityCheck() + ctx = _mock_maven_ctx(macaron_path, purl=_MAVEN_PURL_NO_VERSION) + ctx.dynamic_data["git_service"] = _make_github_service() + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +# --------------------------------------------------------------------------- +# GitHub rescue signal tests +# --------------------------------------------------------------------------- + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +@patch("macaron.slsa_analyzer.git_service.github.GitHub.api_client") +def test_stale_registry_rescued_by_recent_commit( + mock_api_client: MagicMock, + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Stale registry release does not fail the check when GitHub shows recent commit activity. + + This covers the mono-repo scenario where a sub-module has not had its own + registry release recently but the project as a whole is actively maintained. + """ + _load_registry_config(tmp_path, threshold_days=365) + stale = datetime.now(UTC) - timedelta(days=500) + recent_push = datetime.now(UTC) - timedelta(days=5) + mock_timestamp.return_value = stale + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None # latest = same as pinned (stale) + mock_api_client.get_repo_data.return_value = { + "archived": False, + "pushed_at": recent_push.strftime("%Y-%m-%dT%H:%M:%SZ"), + } + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + ctx.dynamic_data["git_service"] = _make_github_service() + assert check.run_check(ctx).result_type == CheckResultType.PASSED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +@patch("macaron.slsa_analyzer.git_service.github.GitHub.api_client") +def test_stale_registry_and_stale_commit_fails( + mock_api_client: MagicMock, + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Stale registry release fails the check when the GitHub commit is also stale.""" + _load_registry_config(tmp_path, threshold_days=365) + stale = datetime.now(UTC) - timedelta(days=500) + mock_timestamp.return_value = stale + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + mock_api_client.get_repo_data.return_value = { + "archived": False, + "pushed_at": stale.strftime("%Y-%m-%dT%H:%M:%SZ"), + } + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + ctx.dynamic_data["git_service"] = _make_github_service() + assert check.run_check(ctx).result_type == CheckResultType.FAILED + + +@patch("macaron.slsa_analyzer.package_registry.package_registry.PackageRegistry.find_publish_timestamp") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._check_deprecated") +@patch("macaron.slsa_analyzer.checks.registry_maintainability_check._get_latest_release_timestamp") +def test_stale_registry_no_github_data_fails( + mock_latest: MagicMock, + mock_deprecated: MagicMock, + mock_timestamp: MagicMock, + macaron_path: Path, + tmp_path: Path, +) -> None: + """Stale registry release fails when there is no GitHub repo to check as a rescue signal.""" + _load_registry_config(tmp_path, threshold_days=365) + stale = datetime.now(UTC) - timedelta(days=500) + mock_timestamp.return_value = stale + mock_deprecated.return_value = (False, None) + mock_latest.return_value = None + + check = RegistryMaintainabilityCheck() + ctx = _mock_pypi_ctx(macaron_path) + # No GitHub service — NoneGitService is the default in _mock_pypi_ctx, so no change needed. + assert check.run_check(ctx).result_type == CheckResultType.FAILED From 7703cb9982d64e43e4c3cebbf821a1deb91da937 Mon Sep 17 00:00:00 2001 From: Behnaz Hassanshahi Date: Thu, 6 Aug 2026 15:42:23 +1000 Subject: [PATCH 04/11] chore(deps): update the cryptography package to a patched version (#1442) Addresses GHSA-g6cj-pr64-35w5 reported for the cryptography library. Signed-off-by: behnazh-w --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 48b742271..d4c5e1507 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,7 @@ dependencies = [ "cyclonedx-python-lib[validation] >=9.0.0,<12.0.0", "beautifulsoup4 >=4.12.0,<5.0.0", "problog >=2.2.6,<3.0.0", - "cryptography >=48.0.1,<49.0.0", + "cryptography >=50.0.0,<51.0.0", "semgrep == 1.171.0", "email-validator >=2.2.0,<3.0.0", "rich >=13.5.3,<15.0.0", @@ -76,6 +76,7 @@ actions = [ ] dev = [ "flit >=3.2.0,<4.0.0", + "flit_core >=3.2.0,<4.0.0", "mypy >=2.0.0,<3.0.0", "types-pyyaml >=6.0.4,<7.0.0", "types-requests >=2.25.6,<3.0.0", From bd6f77d4022b2c887ef843e537a3cb30bbc09172 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 6 Aug 2026 17:57:57 +1000 Subject: [PATCH 05/11] chore(deps): bump actions/setup-python from 6.2.0 to 7.0.0 (#1429) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/_build.yaml | 2 +- .github/workflows/_build_docker.yaml | 2 +- .github/workflows/codeql-analysis.yaml | 2 +- .github/workflows/pr-conventional-commits.yaml | 2 +- .github/workflows/release.yaml | 4 ++-- .github/workflows/test_macaron_action.yaml | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/_build.yaml b/.github/workflows/_build.yaml index 6220736d9..5032700a7 100644 --- a/.github/workflows/_build.yaml +++ b/.github/workflows/_build.yaml @@ -57,7 +57,7 @@ jobs: fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: ${{ matrix.python }} diff --git a/.github/workflows/_build_docker.yaml b/.github/workflows/_build_docker.yaml index 55017f3ba..7fca1d388 100644 --- a/.github/workflows/_build_docker.yaml +++ b/.github/workflows/_build_docker.yaml @@ -33,7 +33,7 @@ jobs: # The Docker integration tests require Python 3.11. - name: Set up Python - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: '3.11' diff --git a/.github/workflows/codeql-analysis.yaml b/.github/workflows/codeql-analysis.yaml index a97dbd360..caa41487b 100644 --- a/.github/workflows/codeql-analysis.yaml +++ b/.github/workflows/codeql-analysis.yaml @@ -38,7 +38,7 @@ jobs: uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 - name: Set up Python - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: '3.11' diff --git a/.github/workflows/pr-conventional-commits.yaml b/.github/workflows/pr-conventional-commits.yaml index a9f1c4fbf..99e255874 100644 --- a/.github/workflows/pr-conventional-commits.yaml +++ b/.github/workflows/pr-conventional-commits.yaml @@ -30,7 +30,7 @@ jobs: fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: '3.11' diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 98daabfd6..e433eab4f 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -45,7 +45,7 @@ jobs: token: ${{ secrets.REPO_ACCESS_TOKEN }} - name: Set up Python - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: '3.11' @@ -215,7 +215,7 @@ jobs: rm -f "$CHECKSUMS" - name: Set up Python - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: '3.11' diff --git a/.github/workflows/test_macaron_action.yaml b/.github/workflows/test_macaron_action.yaml index dd580d425..31a9a5b06 100644 --- a/.github/workflows/test_macaron_action.yaml +++ b/.github/workflows/test_macaron_action.yaml @@ -103,7 +103,7 @@ jobs: upload_reports: 'false' - name: Setup Python for analyzed venv - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 with: python-version: 3.11.14 From 7d335c8544c4f78746cb9a7d551cf0f446f47a07 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 7 Aug 2026 08:30:36 +1000 Subject: [PATCH 06/11] chore(deps): bump github/codeql-action/init from 3.28.15 to 4.37.5 (#1428) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql-analysis.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codeql-analysis.yaml b/.github/workflows/codeql-analysis.yaml index caa41487b..02cca6bb0 100644 --- a/.github/workflows/codeql-analysis.yaml +++ b/.github/workflows/codeql-analysis.yaml @@ -50,7 +50,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@45775bd8235c68ba998cffa5171334d58593da47 # v3.28.15 + uses: github/codeql-action/init@d1ba80a13dd99fba24a470575428917156a28b43 # v4.37.5 with: languages: ${{ matrix.language }} config-file: .github/codeql/codeql-config.yaml From 2c8b394b170e2875fc0f16417ea751dcd8c4b46f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 15:18:21 +0200 Subject: [PATCH 07/11] chore(deps): bump github/codeql-action/upload-sarif from 3.28.15 to 4.37.9 (#1461) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/scorecards-analysis.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/scorecards-analysis.yaml b/.github/workflows/scorecards-analysis.yaml index 410dd523c..c59034d29 100644 --- a/.github/workflows/scorecards-analysis.yaml +++ b/.github/workflows/scorecards-analysis.yaml @@ -56,6 +56,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: Upload to code-scanning - uses: github/codeql-action/upload-sarif@45775bd8235c68ba998cffa5171334d58593da47 # v3.28.15 + uses: github/codeql-action/upload-sarif@cdf488f595d80d6e07e03d4674febd5ab45fa938 # v4.37.9 with: sarif_file: results.sarif From 16c6a1ecbda8b6f24563fa44de317bd0e59b7b70 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 22:58:07 +0200 Subject: [PATCH 08/11] chore(deps): bump actions/checkout from 6.0.2 to 7.0.1 (#1449) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/_build.yaml | 2 +- .github/workflows/_build_docker.yaml | 2 +- .github/workflows/_deploy-github-pages.yaml | 2 +- .github/workflows/_generate-rebase.yaml | 2 +- .github/workflows/build_base_image.yaml | 2 +- .github/workflows/build_semgrep_wheel.yaml | 2 +- .github/workflows/codeql-analysis.yaml | 2 +- .github/workflows/macaron-analysis.yaml | 2 +- .github/workflows/pr-conventional-commits.yaml | 2 +- .github/workflows/release.yaml | 6 +++--- .github/workflows/scorecards-analysis.yaml | 2 +- .github/workflows/test_macaron_action.yaml | 16 ++++++++-------- 12 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.github/workflows/_build.yaml b/.github/workflows/_build.yaml index 5032700a7..c9cf45cb0 100644 --- a/.github/workflows/_build.yaml +++ b/.github/workflows/_build.yaml @@ -52,7 +52,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 diff --git a/.github/workflows/_build_docker.yaml b/.github/workflows/_build_docker.yaml index 7fca1d388..f4611d5a0 100644 --- a/.github/workflows/_build_docker.yaml +++ b/.github/workflows/_build_docker.yaml @@ -27,7 +27,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 diff --git a/.github/workflows/_deploy-github-pages.yaml b/.github/workflows/_deploy-github-pages.yaml index 11067481a..a626a4b25 100644 --- a/.github/workflows/_deploy-github-pages.yaml +++ b/.github/workflows/_deploy-github-pages.yaml @@ -30,7 +30,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 diff --git a/.github/workflows/_generate-rebase.yaml b/.github/workflows/_generate-rebase.yaml index 7edc563be..db1534b77 100644 --- a/.github/workflows/_generate-rebase.yaml +++ b/.github/workflows/_generate-rebase.yaml @@ -36,7 +36,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 token: ${{ secrets.REPO_ACCESS_TOKEN }} diff --git a/.github/workflows/build_base_image.yaml b/.github/workflows/build_base_image.yaml index cc7609a12..b2439bbb5 100644 --- a/.github/workflows/build_base_image.yaml +++ b/.github/workflows/build_base_image.yaml @@ -19,7 +19,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 diff --git a/.github/workflows/build_semgrep_wheel.yaml b/.github/workflows/build_semgrep_wheel.yaml index 329c4926c..a26c9c740 100644 --- a/.github/workflows/build_semgrep_wheel.yaml +++ b/.github/workflows/build_semgrep_wheel.yaml @@ -26,7 +26,7 @@ jobs: # change the version tag in the 'name' description # change the 'ref' field to use the commit hash of that tag - name: Check out Semgrep v1.113.0 repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: semgrep/semgrep.git ref: 4729a05d24bf9cee8face447e8a6d418037d61d8 # v1.113.0 diff --git a/.github/workflows/codeql-analysis.yaml b/.github/workflows/codeql-analysis.yaml index 02cca6bb0..946c15e5b 100644 --- a/.github/workflows/codeql-analysis.yaml +++ b/.github/workflows/codeql-analysis.yaml @@ -35,7 +35,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Set up Python uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 diff --git a/.github/workflows/macaron-analysis.yaml b/.github/workflows/macaron-analysis.yaml index fd7005c7d..9533f617a 100644 --- a/.github/workflows/macaron-analysis.yaml +++ b/.github/workflows/macaron-analysis.yaml @@ -26,7 +26,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 persist-credentials: false diff --git a/.github/workflows/pr-conventional-commits.yaml b/.github/workflows/pr-conventional-commits.yaml index 99e255874..ab9b46f34 100644 --- a/.github/workflows/pr-conventional-commits.yaml +++ b/.github/workflows/pr-conventional-commits.yaml @@ -25,7 +25,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index e433eab4f..f2e379ca6 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -39,7 +39,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 token: ${{ secrets.REPO_ACCESS_TOKEN }} @@ -115,7 +115,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 @@ -305,7 +305,7 @@ jobs: # steps: # - name: Check out repository - # uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + # uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 # with: # fetch-depth: 0 diff --git a/.github/workflows/scorecards-analysis.yaml b/.github/workflows/scorecards-analysis.yaml index c59034d29..54ccd73b4 100644 --- a/.github/workflows/scorecards-analysis.yaml +++ b/.github/workflows/scorecards-analysis.yaml @@ -29,7 +29,7 @@ jobs: steps: - name: Check out repository - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false diff --git a/.github/workflows/test_macaron_action.yaml b/.github/workflows/test_macaron_action.yaml index 31a9a5b06..bddafa116 100644 --- a/.github/workflows/test_macaron_action.yaml +++ b/.github/workflows/test_macaron_action.yaml @@ -28,7 +28,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -78,7 +78,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -143,7 +143,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -197,7 +197,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -270,7 +270,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -304,7 +304,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -337,7 +337,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 @@ -378,7 +378,7 @@ jobs: MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} DOCKER_PULL: never steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download test Docker image artifact if: ${{ inputs.docker_image_artifact_name != '' }} uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 From 862d044fedd533827b5f6be2257a426bcce1551b Mon Sep 17 00:00:00 2001 From: Jeff Lenamon <85593689+lenamonj@users.noreply.github.com> Date: Wed, 9 Sep 2026 11:00:36 -0400 Subject: [PATCH 09/11] fix: use the JDK version from the JAR when no language version is recorded (#1466) Use the JDK version read from the Maven Central JAR when the database has no recorded language version. Signed-off-by: Jeff Lenamon --- .../common_spec/maven_spec.py | 2 +- .../common_spec/test_maven_spec.py | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/build_spec_generator/common_spec/test_maven_spec.py diff --git a/src/macaron/build_spec_generator/common_spec/maven_spec.py b/src/macaron/build_spec_generator/common_spec/maven_spec.py index 7f20ce741..cb5bd6c24 100644 --- a/src/macaron/build_spec_generator/common_spec/maven_spec.py +++ b/src/macaron/build_spec_generator/common_spec/maven_spec.py @@ -84,7 +84,7 @@ def resolve_fields(self, purl: PackageURL) -> None: existing = self.data["language_version"][0] if self.data["language_version"] else None # Select JDK from jar or another source, with a default of version 8. - selected_jdk_version = jdk_from_jar or existing if existing else "8" + selected_jdk_version = jdk_from_jar or existing or "8" major_jdk_version = normalize_jdk_version(selected_jdk_version) if not major_jdk_version: diff --git a/tests/build_spec_generator/common_spec/test_maven_spec.py b/tests/build_spec_generator/common_spec/test_maven_spec.py new file mode 100644 index 000000000..670419755 --- /dev/null +++ b/tests/build_spec_generator/common_spec/test_maven_spec.py @@ -0,0 +1,66 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""This module tests the Maven build specification field resolution.""" + +from unittest.mock import patch + +import pytest +from packageurl import PackageURL + +from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpecDict +from macaron.build_spec_generator.common_spec.maven_spec import MavenBuildSpec + +PURL_STRING = "pkg:maven/com.example/demo@1.0.0" + + +def _build_spec_dict(language_version: list[str]) -> BaseBuildSpecDict: + """Return a minimal Maven build spec dict carrying the passed language version list.""" + return BaseBuildSpecDict( + ecosystem="maven", + purl=PURL_STRING, + language="java", + build_tools=["maven"], + macaron_version="0.0.0", + group_id="com.example", + artifact_id="demo", + version="1.0.0", + language_version=list(language_version), + build_commands=[ + { + "build_tool": "maven", + "build_config_path": "pom.xml", + "command": ["mvn", "clean", "package"], + "confidence_score": 1.0, + } + ], + ) + + +def _resolve(language_version: list[str], jdk_from_jar: str | None) -> list[str]: + """Resolve a Maven build spec with the JAR lookup pinned, and return the resulting language version.""" + data = _build_spec_dict(language_version) + with patch( + "macaron.build_spec_generator.common_spec.maven_spec.find_jdk_version_from_central_maven_repo", + return_value=jdk_from_jar, + ): + MavenBuildSpec(data).resolve_fields(PackageURL.from_string(PURL_STRING)) + return data["language_version"] + + +@pytest.mark.parametrize( + ("existing", "jdk_from_jar", "expected"), + [ + # The JAR manifest is the strongest evidence and wins whether or not the database + # recorded a language version. The empty-list case is the ordinary one: core.py sets + # "language_version" to [] whenever the database holds no recorded version. + ([], "17", ["17"]), + (["11"], "17", ["17"]), + # No JAR evidence: fall back to what the database recorded, then to the default of 8. + (["11"], None, ["11"]), + ([], None, ["8"]), + ], +) +def test_resolve_fields_selects_jdk_version(existing: list[str], jdk_from_jar: str | None, expected: list[str]) -> None: + """The JDK read from the Maven Central JAR must not be discarded when the database recorded none.""" + assert _resolve(existing, jdk_from_jar) == expected From 24c2ee44752e65eda15670973787f15dc2ecf961 Mon Sep 17 00:00:00 2001 From: Behnaz Hassanshahi Date: Sat, 14 Nov 2026 10:17:54 +1000 Subject: [PATCH 10/11] feat: support GitHub Actions workflow lockfiles (#1492) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds support for GitHub Actions workflow lockfiles to Macaron’s existing analysis check and check-github-actions policy. Actions covered by an applicable lockfile no longer produce false unpinned findings, and vulnerability analysis uses the recorded commits, including transitive dependencies. Signed-off-by: behnazh-w --- .github/workflows/test_macaron_action.yaml | 65 ++++ .gitignore | 1 + Makefile | 15 +- .../detect_vulnerable_github_actions.rst | 33 +- pyproject.toml | 5 +- scripts/actions/write_job_summary.py | 38 ++ .../action_resolution.py | 93 +++++ .../gha_security_analysis/detect_injection.py | 54 ++- src/macaron/database/database_manager.py | 18 +- src/macaron/errors.py | 8 + .../parsers/github_actions_lockfile.py | 185 ++++++++++ .../datalog/check-github-actions.description | 2 +- .../policies/sql/check-github-actions.sql | 4 + .../github_actions_vulnerability_check.py | 345 +++++++++++++++--- .../package_registry/pypi_registry.py | 3 +- .../test_action_resolution.py | 67 ++++ tests/database/test_database_manager.py | 46 +++ .../oracle_coherence-js-client/policy.dl | 4 + .../org_apache_logging_log4j/policy_purl.dl | 9 +- .../policy_repo_url.dl | 9 +- .../test_lockfile_action_regression.py | 82 +++++ .../output_reporter/test_write_job_summary.py | 55 +++ .../graalvm-actions.lock | 59 +++ tests/parsers/test_github_actions_lockfile.py | 131 +++++++ .../test_github_actions_lockfile_policy.py | 207 +++++++++++ tests/provenance/test_provenance_finder.py | 3 +- ...test_github_actions_vulnerability_check.py | 328 ++++++++++++++++- .../assert_lockfile_regression.py | 86 +++++ 28 files changed, 1881 insertions(+), 74 deletions(-) create mode 100644 src/macaron/code_analyzer/gha_security_analysis/action_resolution.py create mode 100644 src/macaron/parsers/github_actions_lockfile.py create mode 100644 tests/code_analyzer/gha_security_analysis/test_action_resolution.py create mode 100644 tests/output_reporter/test_lockfile_action_regression.py create mode 100644 tests/parsers/resources/github_actions_lockfile/graalvm-actions.lock create mode 100644 tests/parsers/test_github_actions_lockfile.py create mode 100644 tests/policy_engine/test_github_actions_lockfile_policy.py create mode 100644 tests/tutorial_resources/detect_vulnerable_github_actions/assert_lockfile_regression.py diff --git a/.github/workflows/test_macaron_action.yaml b/.github/workflows/test_macaron_action.yaml index bddafa116..85772f8c6 100644 --- a/.github/workflows/test_macaron_action.yaml +++ b/.github/workflows/test_macaron_action.yaml @@ -158,12 +158,15 @@ jobs: uses: ./ with: repo_path: https://github.com/apache/logging-log4j2 + digest: 028e9fad03ae7bcbf2e49ab8d32d8cfb900f3587 output_dir: macaron_output/detect_vulnerable_github_actions upload_reports: 'false' write_job_summary: 'false' - name: Run Macaron (verify policy - github_actions_vulns for repo) id: verify_github_actions_vulns_repo_tutorial + # Log4j calls remote reusable workflows through mutable refs, so this policy fails. + continue-on-error: true uses: ./ with: policy_file: ./tests/tutorial_resources/detect_vulnerable_github_actions/check_github_actions_vuln_repo.dl @@ -172,6 +175,17 @@ jobs: reports_artifact_name: macaron-reports-vulnerable-actions-repo write_job_summary: 'true' + - name: Assert expected policy failure for the repository example + run: | + if [ "${{ steps.verify_github_actions_vulns_repo_tutorial.outcome }}" != "failure" ]; then + echo "Expected the repository policy to reject mutable reusable-workflow refs." + exit 1 + fi + jq -e '.failed_policies == [["github_actions_vulns_repo"]] and + (.component_violates_policy | length > 0)' \ + macaron_output/detect_vulnerable_github_actions/policy_report.json + test ! -e macaron_output/detect_vulnerable_github_actions/vsa.intoto.jsonl + - name: Run Macaron (analyze purl - log4j-core example) uses: ./ with: @@ -182,6 +196,7 @@ jobs: - name: Run Macaron (verify policy - github_actions_vulns for purl) id: verify_github_actions_vulns_purl_tutorial + continue-on-error: true uses: ./ with: policy_file: ./tests/tutorial_resources/detect_vulnerable_github_actions/check_github_actions_vuln_purl.dl @@ -190,6 +205,17 @@ jobs: reports_artifact_name: macaron-reports-vulnerable-actions-purl write_job_summary: 'true' + - name: Assert expected policy failure for the PURL example + run: | + if [ "${{ steps.verify_github_actions_vulns_purl_tutorial.outcome }}" != "failure" ]; then + echo "Expected the PURL policy to reject mutable reusable-workflow refs." + exit 1 + fi + jq -e '.failed_policies == [["github_actions_vulns_purl"]] and + (.component_violates_policy | length > 0)' \ + macaron_output/detect_vulnerable_github_actions/policy_report.json + test ! -e macaron_output/detect_vulnerable_github_actions/vsa.intoto.jsonl + tutorial-provenance: name: Provenance discovery, extraction, and verification runs-on: ubuntu-latest @@ -371,6 +397,45 @@ jobs: exit 1 fi + test-github-actions-lockfiles: + name: Recognizing workflow lockfile coverage + runs-on: ubuntu-latest + env: + MACARON_IMAGE_TAG: ${{ inputs.macaron_image_tag }} + DOCKER_PULL: never + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Download test Docker image artifact + if: ${{ inputs.docker_image_artifact_name != '' }} + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 + with: + name: ${{ inputs.docker_image_artifact_name }} + path: /tmp + - name: Load test Docker image + if: ${{ inputs.docker_image_artifact_name != '' }} + run: docker load --input /tmp/macaron-test-image.tar + - name: Analyze the pinned GraalVM workflow lockfile + id: lockfile_analysis + # Other security findings may fail this real project's policy. Inspect the facts below. + continue-on-error: true + uses: ./ + with: + repo_path: https://github.com/graalvm/setup-graalvm + digest: 35ad4c011e7cfc84f7370397221f82823c74ce35 + policy_file: check-github-actions + policy_purl: pkg:github.com/graalvm/setup-graalvm@.* + output_dir: macaron_output/github_actions_lockfiles + upload_reports: 'true' + reports_artifact_name: macaron-workflow-lockfile-regression + write_job_summary: 'true' + - name: Assert lockfile recognition and policy results + if: ${{ always() }} + env: + ACTION_OUTCOME: ${{ steps.lockfile_analysis.outcome }} + run: | + python3 tests/tutorial_resources/detect_vulnerable_github_actions/assert_lockfile_regression.py \ + macaron_output/github_actions_lockfiles "$ACTION_OUTCOME" + test-detect-potential-injection: name: How to detect vulnerable GitHub Actions runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index b535b7bd1..72a883a31 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ instance/ # Sphinx documentation docs/_build/ +docs/superpowers/ # PyBuilder target/ diff --git a/Makefile b/Makefile index ecfd3c2eb..ade3272b7 100644 --- a/Makefile +++ b/Makefile @@ -301,10 +301,13 @@ requirements.txt: pyproject.toml # a PyPI entry; also print out CVE description and potential fixes if audit # found an issue. If an advisory needs to be ignored, use the --ignore-vuln option. # -# GHSA-hvrp-rf83-w775 ignore-vuln GHSA-jpw9-pfvf-9f58, and ignore-vuln GHSA-vj7q-gjh5-988w are advisories -# for the mcp package, which is a dependency of semgrep and we cannot update it outselves. Macaron does -# not use this transitive dependency and we can ignore them for now. Remove them when semgrep is updated and uses -# a fixed version of mcp. +# Semgrep 1.185.0 pins mcp to 1.29.0 and restricts opentelemetry-api to 1.37.x. +# The four MCP advisories below affect MCP server/client functionality. Macaron +# invokes `semgrep scan` and does not use that functionality. The OpenTelemetry +# advisory affects parsing attacker-controlled tracestate headers; Semgrep's scan +# telemetry extracts only traceparent from its environment variables. +# Temporarily ignore these advisories until Semgrep allows mcp >=1.30.0 and +# opentelemetry-api >=1.45.1. Reassess these exceptions if Macaron's usage changes. .PHONY: audit audit: @@ -312,7 +315,9 @@ audit: echo "No package pip_audit installed, upgrade your environment!" && exit 1; \ fi; python -m pip_audit --skip-editable --desc on --fix --dry-run \ - --ignore-vuln GHSA-hvrp-rf83-w775 --ignore-vuln GHSA-jpw9-pfvf-9f58 --ignore-vuln GHSA-vj7q-gjh5-988w + --ignore-vuln GHSA-84m7-p3x7-pcfv --ignore-vuln GHSA-5h93-6whr-6q8j \ + --ignore-vuln GHSA-w4fh-qvv9-3v23 --ignore-vuln GHSA-rwrf-2pqf-9j8j \ + --ignore-vuln GHSA-xcq7-p3pf-p88h # Run some or all checks over the package code base. .PHONY: check check-code check-ruff check-lint check-mypy check-go check-actionlint diff --git a/docs/source/pages/tutorials/detect_vulnerable_github_actions.rst b/docs/source/pages/tutorials/detect_vulnerable_github_actions.rst index aa3c8c6aa..4f37e6138 100644 --- a/docs/source/pages/tutorials/detect_vulnerable_github_actions.rst +++ b/docs/source/pages/tutorials/detect_vulnerable_github_actions.rst @@ -1,4 +1,4 @@ -.. Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. +.. Copyright (c) 2025 - 2026, Oracle and/or its affiliates. All rights reserved. .. Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. .. _detect-vuln-gh-actions: @@ -95,6 +95,12 @@ Next, ensure that the ``mcn_githubactions_vulnerabilities_1`` check passes for t Run the ``verify-policy`` command to check if the ``mcn_githubactions_vulnerabilities_1`` check is successful. +The check also detects mutable references to remote reusable workflows. For +example, Log4j commit ``028e9fad03ae7bcbf2e49ab8d32d8cfb900f3587`` calls reusable +workflows through refs such as ``main`` and ``rel/12.1.1``, so it fails this policy +even when no known action vulnerability is reported. Review the findings before +deciding which workflow references need to be pinned. + .. code-block:: shell ./run_macaron.sh verify-policy --database ./output/macaron.db --file ./check_github_actions_vuln.dl @@ -183,6 +189,31 @@ For example, to pin the ``tj-actions/changed-files`` action to a specific versio Refer to GitHub's security hardening guide for more information on managing third-party actions securely: `GitHub Security `_. ++++++++++++++++++++++++++++ +Workflow dependency locking ++++++++++++++++++++++++++++ + +The same check and the predefined ``check-github-actions`` policy recognize +``.github/workflows/actions.lock`` in GA format ``v0.0.3``. You can pin an action +inline to its full commit SHA or use a tag backed by applicable lockfile coverage. +No additional Macaron action input or policy is needed. + +After reviewing the dependencies you want to execute, generate a lockfile with +GitHub's CLI extension and commit the generated changes: + +.. code-block:: shell + + gh extension install github/gh-actions-lock + gh actions-lock + +Lockfile adoption is per workflow. A dependency recorded only for another workflow +does not remove an unpinned finding. Missing lockfiles preserve inline SHA checks; +invalid or unsupported present lockfiles produce actionable findings. Regenerate +and review the lockfile when dependencies change. + +For further reading, see GitHub's announcement: +`GitHub Actions workflow lockfiles are generally available `_. + --------------------- Why This is Important --------------------- diff --git a/pyproject.toml b/pyproject.toml index d4c5e1507..11c98a970 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,8 @@ dependencies = [ "yamale >=6.0.0,<7.0.0", "packaging >=25.0,<27.0.0", "jinja2 >=3.1.2,<4.0.0", - "SQLAlchemy >=2.0.0,<3.0.0", + # SQLAlchemy 2.1 changes Select/Result typing; retain the 2.0 query annotations. + "SQLAlchemy >=2.0.0,<2.1.0", "defusedxml >=0.7.1,<1.0.0", "packageurl-python >=0.11.1,<1.0.0", "ruamel.yaml >=0.18.6,<1.0.0", @@ -39,7 +40,7 @@ dependencies = [ "beautifulsoup4 >=4.12.0,<5.0.0", "problog >=2.2.6,<3.0.0", "cryptography >=50.0.0,<51.0.0", - "semgrep == 1.171.0", + "semgrep == 1.185.0", "email-validator >=2.2.0,<3.0.0", "rich >=13.5.3,<15.0.0", "lark >=1.3.0,<2.0.0", diff --git a/scripts/actions/write_job_summary.py b/scripts/actions/write_job_summary.py index db86ad1fe..be2c6ffea 100644 --- a/scripts/actions/write_job_summary.py +++ b/scripts/actions/write_job_summary.py @@ -21,6 +21,27 @@ ] +def validate_github_actions_schema(conn: sqlite3.Connection) -> None: + """Reject an old findings schema before executing the extended diagnostics SQL. + + This script runs outside the Macaron container using only the standard + library, so database compatibility errors use sqlite's error hierarchy. + Both the physical table and generated view need the evidence columns. + """ + required = {"pinning_method", "resolved_commit", "lockfile_path", "dependency_chain"} + for table in ("_github_actions_vulnerabilities_check", "github_actions_vulnerabilities_check"): + exists = conn.execute("SELECT 1 FROM sqlite_master WHERE name = ?", (table,)).fetchone() + if exists is None: + continue + columns = {row[1] for row in conn.execute(f"PRAGMA table_info({table})")} + missing = required - columns + if missing: + raise sqlite3.DatabaseError( + f"Incompatible GitHub Actions findings schema in {table}: missing {', '.join(sorted(missing))}. " + "Reanalyze into a fresh output directory before generating policy diagnostics." + ) + + def _env(name: str, default: str = "") -> str: return os.environ.get(name, default) @@ -407,6 +428,21 @@ def write_compact_gha_vuln_diagnostics(summary_path: Path, columns: list[str], r pin_state = "yes" if bool(row[pin_idx]) else "no" _append_line(summary_path, f"- Pinned to full commit SHA: `{pin_state}`") + evidence_columns = { + "pinning_method": "Pinning method", + "resolved_commit": "Effective commit", + "lockfile_path": "Lockfile", + "dependency_chain": "Dependency chain", + } + for column, label in evidence_columns.items(): + index = col_index.get(column) + if index is None or not row[index]: + continue + value = str(row[index]) + if column == "pinning_method" and value == "lockfile": + value = "recorded lockfile coverage (upstream provenance not verified)" + _append_line(summary_path, f"- {label}: {_format_table_cell(value)}") + vul_idx = col_index.get("vuln_urls") if vul_idx is not None and row[vul_idx]: parsed = _parse_list_cell(str(row[vul_idx])) @@ -492,6 +528,8 @@ def _write_existing_policy_failure_diagnostics( if sql_path: sql_query = sql_path.read_text(encoding="utf-8") with sqlite3.connect(db_path) as conn: + if policy_name == "check-github-actions": + validate_github_actions_schema(conn) cols, rows = _query_sql(conn, sql_query) if cols and rows: _append_line(summary_path) diff --git a/src/macaron/code_analyzer/gha_security_analysis/action_resolution.py b/src/macaron/code_analyzer/gha_security_analysis/action_resolution.py new file mode 100644 index 000000000..c54859ec5 --- /dev/null +++ b/src/macaron/code_analyzer/gha_security_analysis/action_resolution.py @@ -0,0 +1,93 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Resolve action identity using only the owning repository's recorded lockfile.""" + +import re +from collections import deque +from dataclasses import dataclass +from typing import Literal + +from macaron.errors import ActionsLockfileError +from macaron.parsers.github_actions_lockfile import ( + ActionsLockfile, + LockedAction, + canonical_action_key, + validate_dependency_graph, +) + +PinningMethod = Literal["inline_sha", "lockfile", "same_repository", "none"] +ActionKind = Literal["remote", "workspace", "same_repository", "container", "reusable"] + + +@dataclass(frozen=True) +class ActionResolution: + """Declared reference and effective revision, with the source of pinning evidence.""" + + declared_reference: str + action_kind: ActionKind + resolved_commit: str | None + pinning_method: PinningMethod + + +@dataclass(frozen=True) +class LockedActionUse: + """A reachable locked dependency and a deterministic originating call chain.""" + + key: str + action: LockedAction + chain: tuple[str, ...] + + +def reachable_locked_dependencies(workflow_path: str, lockfile: ActionsLockfile) -> list[LockedActionUse]: + """Validate a workflow's recorded graph and retain the shortest chain per dependency. + + Traverse iteratively to avoid Python recursion limits on untrusted graphs. + Cycles and dangling edges are errors, not truncated successful coverage. + """ + roots = lockfile.workflows.get(workflow_path, ()) + validate_dependency_graph(lockfile.dependencies, roots, f"{lockfile.path}: {workflow_path}") + chains: dict[str, tuple[str, ...]] = {} + pending: deque[tuple[str, ...]] = deque((root,) for root in sorted(roots)) + while pending: + chain = pending.popleft() + key = chain[-1] + if key in chains and (len(chains[key]), chains[key]) <= (len(chain), chain): + continue + chains[key] = chain + pending.extend((*chain, child) for child in sorted(lockfile.dependencies[key].uses)) + return [LockedActionUse(key, lockfile.dependencies[key], chains[key]) for key in sorted(chains)] + + +def resolve_action_reference( + workflow_path: str, + uses: str, + lockfile: ActionsLockfile | None, + *, + reusable_workflow: bool = False, +) -> ActionResolution: + """Classify raw uses syntax and credit only applicable recorded action pins.""" + if uses.startswith("./"): + return ActionResolution(uses, "workspace", None, "none") + if uses.startswith("$/"): + return ActionResolution(uses, "same_repository", None, "same_repository") + if uses.startswith("docker://"): + return ActionResolution(uses, "container", None, "none") + kind: ActionKind = "reusable" if reusable_workflow else "remote" + key = canonical_action_key(uses, subpath=True) + ref = key.partition("@")[2] + resolved = None + if lockfile is not None and not reusable_workflow: + for dependency in reachable_locked_dependencies(workflow_path, lockfile): + if dependency.key == key: + if dependency.action.hostname != "github.com": + raise ActionsLockfileError( + f"{lockfile.path}: unsupported dependency host {dependency.action.hostname}" + ) + resolved = dependency.action.commit + break + if resolved is not None: + return ActionResolution(uses, kind, resolved, "lockfile") + if re.fullmatch(r"[a-f0-9]{40}|[a-f0-9]{64}", ref): + return ActionResolution(uses, kind, ref, "inline_sha") + return ActionResolution(uses, kind, None, "none") diff --git a/src/macaron/code_analyzer/gha_security_analysis/detect_injection.py b/src/macaron/code_analyzer/gha_security_analysis/detect_injection.py index 99ed534a4..3fa8b60b9 100644 --- a/src/macaron/code_analyzer/gha_security_analysis/detect_injection.py +++ b/src/macaron/code_analyzer/gha_security_analysis/detect_injection.py @@ -6,6 +6,7 @@ import json import os import re +from pathlib import Path from typing import TypedDict, cast from macaron.code_analyzer.dataflow_analysis import bash, core, facts @@ -14,9 +15,11 @@ from macaron.code_analyzer.dataflow_analysis.github import ( GitHubActionsActionStepNode, GitHubActionsNormalJobNode, + GitHubActionsReusableWorkflowCallNode, GitHubActionsRunStepNode, GitHubActionsWorkflowNode, ) +from macaron.code_analyzer.gha_security_analysis.action_resolution import resolve_action_reference from macaron.code_analyzer.gha_security_analysis.recommendation import ( Recommendation, parse_unpinned_action_issue, @@ -26,6 +29,7 @@ resolve_action_ref_to_tag, ) from macaron.parsers.bashparser_model import CallExpr, is_call_expr, is_lit, is_param_exp +from macaron.parsers.github_actions_lockfile import ActionsLockfile from macaron.parsers.github_workflow_model import Workflow from macaron.slsa_analyzer.git_url import is_commit_hash @@ -57,7 +61,13 @@ class WorkflowFinding(TypedDict): issues: list[PrioritizedIssue] -def detect_github_actions_security_issues(nodes: NodeForest) -> list[WorkflowFinding]: +def detect_github_actions_security_issues( + nodes: NodeForest, + *, + lockfile: ActionsLockfile | None = None, + repo_root: Path | None = None, + unresolved_workflows: frozenset[str] = frozenset(), +) -> list[WorkflowFinding]: """Detect security issues across GitHub Actions workflow nodes. Parameters @@ -76,12 +86,19 @@ def detect_github_actions_security_issues(nodes: NodeForest) -> list[WorkflowFin for root in nodes.root_nodes: for callee in traverse_bfs(root): if isinstance(callee, GitHubActionsWorkflowNode): - if result := analyze_workflow(callee, nodes=nodes): + applicable_lockfile = None if callee.context.ref.source_filepath in unresolved_workflows else lockfile + if result := analyze_workflow(callee, nodes=nodes, lockfile=applicable_lockfile, repo_root=repo_root): findings.append(result) return findings -def analyze_workflow(workflow_node: GitHubActionsWorkflowNode, nodes: NodeForest) -> WorkflowFinding | None: +def analyze_workflow( + workflow_node: GitHubActionsWorkflowNode, + nodes: NodeForest, + *, + lockfile: ActionsLockfile | None = None, + repo_root: Path | None = None, +) -> WorkflowFinding | None: """Analyze a GitHub Actions workflow for security issues. Parameters @@ -110,6 +127,9 @@ def analyze_workflow(workflow_node: GitHubActionsWorkflowNode, nodes: NodeForest seen_jobs: set[str] = set() workflow_permissions_defined = "permissions" in workflow_node.definition has_job_without_permissions = False + workflow_path = workflow_node.context.ref.source_filepath + if repo_root is not None: + workflow_path = Path(workflow_path).resolve().relative_to(repo_root.resolve()).as_posix() for node in core.traverse_bfs(workflow_node): if isinstance(node, GitHubActionsWorkflowNode): @@ -126,7 +146,23 @@ def analyze_workflow(workflow_node: GitHubActionsWorkflowNode, nodes: NodeForest continue if isinstance(node, GitHubActionsActionStepNode): - _append_action_step_findings(findings, node, on_keys) + raw_uses = node.definition["uses"] + covered = raw_uses.startswith(("./", "$/", "docker://")) + if "@" in raw_uses and not covered: + if lockfile is not None: + resolution = resolve_action_reference(workflow_path, raw_uses, lockfile) + covered = resolution.pinning_method != "none" + else: + covered = bool(re.fullmatch(r"[a-fA-F0-9]{40}|[a-fA-F0-9]{64}", raw_uses.rpartition("@")[2])) + _append_action_step_findings(findings, node, on_keys, covered=covered) + continue + + if isinstance(node, GitHubActionsReusableWorkflowCallNode): + raw_uses = node.definition["uses"] + if raw_uses.startswith("./"): + continue + if not re.fullmatch(r"[a-fA-F0-9]{40}|[a-fA-F0-9]{64}", raw_uses.rpartition("@")[2]): + _add_finding(findings, f"unpinned-third-party-action: {raw_uses}", PRIORITY_MIN) continue if isinstance(node, GitHubActionsRunStepNode): @@ -207,11 +243,19 @@ def _append_action_step_findings( findings: list[PrioritizedIssue], action_node: GitHubActionsActionStepNode, on_keys: set[str], + *, + covered: bool = False, ) -> None: """Append findings derived from an action step node.""" uses_name = action_node.uses_name uses_version = action_node.uses_version - if uses_name and not uses_name.startswith("./") and uses_version and not is_commit_hash(uses_version): + if ( + not covered + and uses_name + and not uses_name.startswith("./") + and uses_version + and not is_commit_hash(uses_version) + ): step_line = _extract_action_step_line(action_node) line_marker = f"[step-line={step_line}] " if step_line else "" _add_finding( diff --git a/src/macaron/database/database_manager.py b/src/macaron/database/database_manager.py index d8283a1c4..686bec2b7 100644 --- a/src/macaron/database/database_manager.py +++ b/src/macaron/database/database_manager.py @@ -10,12 +10,13 @@ import typing import sqlalchemy.exc -from sqlalchemy import create_engine, select +from sqlalchemy import create_engine, inspect, select from sqlalchemy.orm import DeclarativeBase, Session from macaron.config.defaults import defaults from macaron.config.global_config import global_config from macaron.database.views import create_view +from macaron.errors import DatabaseSchemaError logger: logging.Logger = logging.getLogger(__name__) @@ -54,6 +55,21 @@ def create_tables(self) -> None: Note: this is specifically to allow the tables to be loaded into souffle: https://souffle-lang.github.io/directives#input-directive """ + inspector = inspect(self.engine) + existing = set(inspector.get_table_names()) | set(inspector.get_view_names()) + for table_name, table in self._base.metadata.tables.items(): + names = (table_name, table_name[1:]) if table_name.startswith("_") else (table_name,) + for existing_name in names: + if existing_name not in existing: + continue + columns = {column["name"] for column in inspector.get_columns(existing_name)} + missing = set(table.columns.keys()) - columns + if missing: + raise DatabaseSchemaError( + f"Database {self.db_name} has an incompatible schema for {existing_name}: " + f"missing {', '.join(sorted(missing))}. Reanalyze into a fresh output directory; " + "the existing database has not been migrated." + ) try: self._base.metadata.create_all(self.engine, checkfirst=True) for table_name, table in self._base.metadata.tables.items(): diff --git a/src/macaron/errors.py b/src/macaron/errors.py index 569ec1817..71380263a 100644 --- a/src/macaron/errors.py +++ b/src/macaron/errors.py @@ -87,6 +87,14 @@ class ParseError(MacaronError): """The errors related to parsers.""" +class ActionsLockfileError(ParseError): + """A present GitHub Actions lockfile cannot be interpreted safely.""" + + +class DatabaseSchemaError(MacaronError): + """An existing database cannot store the current analysis schema.""" + + class CallGraphError(MacaronError): """The errors related to callgraphs.""" diff --git a/src/macaron/parsers/github_actions_lockfile.py b/src/macaron/parsers/github_actions_lockfile.py new file mode 100644 index 000000000..3064306d1 --- /dev/null +++ b/src/macaron/parsers/github_actions_lockfile.py @@ -0,0 +1,185 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Read GA workflow lockfiles without executing dependency code. + +Compatibility reference: github/actions-lockfile revision +54969dc7fee8b1e0e22b06bd4b591c44232f831c, go/pkg/lockfile. +This establishes recorded coverage, not upstream provenance verification. +""" + +import re +from collections.abc import Iterable +from dataclasses import dataclass +from pathlib import Path +from typing import cast + +from ruamel.yaml import YAML, YAMLError + +from macaron.errors import ActionsLockfileError + +LOCKFILE_PATH = ".github/workflows/actions.lock" +MAX_LOCKFILE_SIZE = 1 << 20 + + +@dataclass(frozen=True) +class LockedAction: + """Validated metadata for one repository/ref pair.""" + + ref: str + commit: str + owner_id: int + repo_id: int + hostname: str + uses: tuple[str, ...] + + +@dataclass(frozen=True) +class ActionsLockfile: + """Workflow membership and the recorded action graph.""" + + path: Path + workflows: dict[str, tuple[str, ...]] + dependencies: dict[str, LockedAction] + + +def validate_dependency_graph(dependencies: dict[str, LockedAction], roots: Iterable[str], source: str) -> None: + """Validate graph edges and acyclicity without recursion or loading action code. + + Three-state traversal distinguishes a dependency shared by different parents + from a back edge. The parser validates the whole document; consumers may also + validate a workflow scope for independently constructed typed records. + """ + completed: set[str] = set() + active: set[str] = set() + for root in roots: + stack = [(root, False)] + while stack: + key, leaving = stack.pop() + if leaving: + active.remove(key) + completed.add(key) + continue + if key in active: + raise ActionsLockfileError(f"{source}: dependency cycle at {key}") + if key in completed: + continue + if key not in dependencies: + raise ActionsLockfileError(f"{source}: missing dependency {key}") + active.add(key) + stack.append((key, True)) + stack.extend((child, False) for child in reversed(dependencies[key].uses)) + + +def canonical_action_key(reference: str, *, subpath: bool = False) -> str: + """Validate a remote pin, preserving ref case and optionally removing an action subpath.""" + repository, separator, ref = reference.partition("@") + parts = repository.split("/") + if not separator or len(parts) < 2 or (not subpath and len(parts) != 2): + raise ActionsLockfileError(f"Invalid action reference: {reference!r}") + if not all(re.fullmatch(r"[A-Za-z0-9_.-]+", part) and part not in {".", ".."} for part in parts): + raise ActionsLockfileError(f"Invalid action repository/path: {reference!r}") + if not ref or any(character.isspace() or ord(character) < 32 for character in ref): + raise ActionsLockfileError(f"Invalid action ref: {reference!r}") + if any(character in ref for character in "@:^~?*[\\") or ".." in ref or ref.endswith(("/", ".")): + raise ActionsLockfileError(f"Invalid action ref: {reference!r}") + if re.fullmatch(r"[A-Fa-f0-9]{40}|[A-Fa-f0-9]{64}", ref): + ref = ref.lower() + return f"{parts[0].lower()}/{parts[1].lower()}@{ref}" + + +def parse_actions_lockfile(path: Path) -> ActionsLockfile | None: + """Load a bounded, strict v0.0.3 document; distinguish absence from invalid content. + + Only expected file/encoding/YAML errors become lockfile diagnostics. Programming + errors propagate. Schema failures include the path and the affected field/key. + """ + try: + with path.open("rb") as stream: + contents = stream.read(MAX_LOCKFILE_SIZE + 1) + except FileNotFoundError: + return None + except OSError as error: + raise ActionsLockfileError(f"{path}: cannot read lockfile: {error}") from error + if len(contents) > MAX_LOCKFILE_SIZE: + raise ActionsLockfileError(f"{path}: lockfile exceeds 1 MiB") + try: + yaml = YAML(typ="safe") + yaml.allow_duplicate_keys = False + document: object = yaml.load(contents.decode("utf-8")) + except (YAMLError, UnicodeError) as error: + raise ActionsLockfileError(f"{path}: invalid lockfile YAML: {error}") from error + if not isinstance(document, dict): + raise ActionsLockfileError(f"{path}: expected a lockfile mapping") + data = cast(dict[str, object], document) + if data.get("version") != "v0.0.3": + raise ActionsLockfileError(f"{path}: unsupported lockfile version {data.get('version')!r}; supported: v0.0.3") + if set(data) - {"version", "workflows", "dependencies"}: + raise ActionsLockfileError(f"{path}: unknown lockfile fields") + raw_workflows = data.get("workflows", {}) + raw_dependencies = data.get("dependencies", {}) + if not isinstance(raw_workflows, dict) or not isinstance(raw_dependencies, dict): + raise ActionsLockfileError(f"{path}: workflows and dependencies must be mappings") + + workflows: dict[str, tuple[str, ...]] = {} + for workflow, references in raw_workflows.items(): + if ( + not isinstance(workflow, str) + or not workflow + or workflow.startswith("/") + or ".." in workflow.split("/") + or any(char in workflow for char in "\\:") + or any(ord(char) < 32 or ord(char) == 127 for char in workflow) + ): + raise ActionsLockfileError(f"{path}: invalid repository-relative workflow path {workflow!r}") + if not isinstance(references, list) or not all(isinstance(ref, str) for ref in references): + raise ActionsLockfileError(f"{path}: workflow {workflow!r} must list action references") + workflows[workflow] = tuple(canonical_action_key(ref) for ref in references) + + dependencies: dict[str, LockedAction] = {} + for reference, metadata in raw_dependencies.items(): + if not isinstance(reference, str) or not isinstance(metadata, dict): + raise ActionsLockfileError(f"{path}: dependency entries must map references to metadata") + key = canonical_action_key(reference) + required = {"ref", "commit", "owner_id", "repo_id"} + if not required <= set(metadata) or set(metadata) - required - {"hostname", "uses"}: + raise ActionsLockfileError(f"{path}: {key}: missing required or unknown metadata fields") + ref, commit = metadata["ref"], metadata["commit"] + owner_id, repo_id = metadata["owner_id"], metadata["repo_id"] + hostname, uses = metadata.get("hostname", "github.com"), metadata.get("uses", []) + if not isinstance(ref, str) or not ref or not isinstance(commit, str): + raise ActionsLockfileError(f"{path}: {key}: ref and commit must be non-empty strings") + if not re.fullmatch(r"sha1-[A-Fa-f0-9]{40}|sha256-[A-Fa-f0-9]{64}", commit): + raise ActionsLockfileError(f"{path}: {key}: invalid algorithm-prefixed commit") + if ( + not isinstance(owner_id, int) + or isinstance(owner_id, bool) + or not isinstance(repo_id, int) + or isinstance(repo_id, bool) + or not 0 < owner_id < 2**63 + or not 0 < repo_id < 2**63 + ): + raise ActionsLockfileError(f"{path}: {key}: repository IDs must be positive 64-bit integers") + if not isinstance(hostname, str) or not re.fullmatch( + r"github\.com|[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.ghe\.com", hostname + ): + raise ActionsLockfileError(f"{path}: {key}: invalid hostname") + if not isinstance(uses, list) or not all(isinstance(child, str) for child in uses): + raise ActionsLockfileError(f"{path}: {key}: uses must list action references") + key_ref = key.partition("@")[2] + digest = commit.partition("-")[2].lower() + if re.fullmatch(r"[a-f0-9]{40}|[a-f0-9]{64}", key_ref): + if key_ref != digest: + raise ActionsLockfileError(f"{path}: {key}: immutable ref disagrees with commit") + elif key_ref != ref: + raise ActionsLockfileError(f"{path}: {key}: metadata ref disagrees with dependency key") + action = LockedAction( + ref, digest, owner_id, repo_id, hostname, tuple(canonical_action_key(child) for child in uses) + ) + if key in dependencies and dependencies[key] != action: + raise ActionsLockfileError(f"{path}: conflicting canonical dependency {key}") + dependencies[key] = action + validate_dependency_graph(dependencies, dependencies, str(path)) + for workflow, references in workflows.items(): + validate_dependency_graph(dependencies, references, f"{path}: {workflow}") + return ActionsLockfile(path, workflows, dependencies) diff --git a/src/macaron/resources/policies/datalog/check-github-actions.description b/src/macaron/resources/policies/datalog/check-github-actions.description index 65292bbd8..de86224fb 100644 --- a/src/macaron/resources/policies/datalog/check-github-actions.description +++ b/src/macaron/resources/policies/datalog/check-github-actions.description @@ -1 +1 @@ -Detects whether a component was built using GitHub Actions that are known to be vulnerable or otherwise unsafe. The policy evaluates a check named `mcn_githubactions_vulnerabilities_1` and reports a passed/failed result for the component when applied. +Detects whether a component was built using GitHub Actions that are known to be vulnerable or otherwise unsafe. The check recognizes inline SHA pinning and workflow-scoped actions.lock v0.0.3 coverage, and checks applicable recorded commits for vulnerabilities. Locking does not establish that an action is safe or verify its upstream provenance. The policy evaluates `mcn_githubactions_vulnerabilities_1` and reports a passed/failed result for the component when applied. diff --git a/src/macaron/resources/policies/sql/check-github-actions.sql b/src/macaron/resources/policies/sql/check-github-actions.sql index 09ba2555b..7d1c10b65 100644 --- a/src/macaron/resources/policies/sql/check-github-actions.sql +++ b/src/macaron/resources/policies/sql/check-github-actions.sql @@ -12,6 +12,10 @@ SELECT gha_check.finding_message, gha_check.recommended_ref, gha_check.sha_pinned, + gha_check.pinning_method, + gha_check.resolved_commit, + gha_check.lockfile_path, + gha_check.dependency_chain, gha_check.caller_workflow AS vulnerable_workflow, analysis.analysis_time FROM github_actions_vulnerabilities_check AS gha_check diff --git a/src/macaron/slsa_analyzer/checks/github_actions_vulnerability_check.py b/src/macaron/slsa_analyzer/checks/github_actions_vulnerability_check.py index eaae86e36..55ab1a09b 100644 --- a/src/macaron/slsa_analyzer/checks/github_actions_vulnerability_check.py +++ b/src/macaron/slsa_analyzer/checks/github_actions_vulnerability_check.py @@ -6,16 +6,25 @@ import logging import os import re +from dataclasses import dataclass, replace from enum import StrEnum +from pathlib import Path -from sqlalchemy import Boolean, ForeignKey, Integer, String +from sqlalchemy import JSON, Boolean, ForeignKey, Integer, String from sqlalchemy.orm import Mapped, mapped_column from macaron.code_analyzer.dataflow_analysis.analysis import get_containing_github_job -from macaron.code_analyzer.dataflow_analysis.core import traverse_bfs +from macaron.code_analyzer.dataflow_analysis.core import NodeForest, traverse_bfs from macaron.code_analyzer.dataflow_analysis.github import ( GitHubActionsActionStepNode, GitHubActionsReusableWorkflowCallNode, + GitHubActionsWorkflowNode, +) +from macaron.code_analyzer.gha_security_analysis.action_resolution import ( + ActionKind, + PinningMethod, + reachable_locked_dependencies, + resolve_action_reference, ) from macaron.code_analyzer.gha_security_analysis.detect_injection import ( build_unpinned_action_recommendation, @@ -28,11 +37,18 @@ ) from macaron.database.db_custom_types import DBJsonList from macaron.database.table_definitions import CheckFacts -from macaron.errors import APIAccessError +from macaron.errors import ActionsLockfileError, APIAccessError from macaron.json_tools import json_extract +from macaron.parsers.github_actions_lockfile import ( + LOCKFILE_PATH, + ActionsLockfile, + canonical_action_key, + parse_actions_lockfile, +) from macaron.slsa_analyzer.analyze_context import AnalyzeContext from macaron.slsa_analyzer.checks.base_check import BaseCheck, CheckResultType from macaron.slsa_analyzer.checks.check_result import CheckResultData, Confidence, JustificationType +from macaron.slsa_analyzer.ci_service.github_actions.github_actions_ci import GitHubActions from macaron.slsa_analyzer.package_registry.osv_dev import OSVDevService from macaron.slsa_analyzer.registry import registry from macaron.slsa_analyzer.slsa_req import ReqName @@ -112,6 +128,23 @@ class GitHubActionsVulnsFacts(CheckFacts): #: Whether the action reference is pinned to a full commit SHA. sha_pinned: Mapped[bool | None] = mapped_column(Boolean, nullable=True) + #: Source of immutable resolution; recorded lockfile coverage is not provenance verification. + pinning_method: Mapped[str | None] = mapped_column( + String, nullable=True, info={"justification": JustificationType.TEXT} + ) + #: Effective commit used for vulnerability matching, preserving action_ref as declared. + resolved_commit: Mapped[str | None] = mapped_column( + String, nullable=True, info={"justification": JustificationType.TEXT} + ) + #: Repository-relative path of the applicable lockfile. + lockfile_path: Mapped[str | None] = mapped_column( + String, nullable=True, info={"justification": JustificationType.TEXT} + ) + #: Shortest recorded call chain from the originating workflow. + dependency_chain: Mapped[list[str] | None] = mapped_column( + JSON, nullable=True, info={"justification": JustificationType.TEXT} + ) + #: Related vulnerability URLs (for ``third_party_action_risk`` findings only). #: For workflow-security findings, this field is an empty list. vuln_urls: Mapped[list[str]] = mapped_column( @@ -123,11 +156,175 @@ class GitHubActionsVulnsFacts(CheckFacts): } +@dataclass(frozen=True) +class ResolvedWorkflowAction: + """One effective action invocation, retaining declaration and originating workflow.""" + + name: str + version: str + caller_path: str + resolved_commit: str | None + pinning_method: PinningMethod + chain: tuple[str, ...] + kind: ActionKind + + +@dataclass(frozen=True) +class WorkflowActionAnalysis: + """Independently resolved actions and source-scoped failures.""" + + actions: list[ResolvedWorkflowAction] + errors: dict[str, ActionsLockfileError] + + +def collect_resolved_workflow_actions( + callgraph: NodeForest, repo_root: Path | None, lockfile: ActionsLockfile | None +) -> WorkflowActionAnalysis: + """Collect declared and recorded nested dependencies without crossing repository boundaries. + + All adopted workflow graphs are validated before any effective versions are + returned. Original paths and refs remain available for report attribution. + """ + actions: dict[tuple[str, str, str], ResolvedWorkflowAction] = {} + workflows: dict[str, str] = {} + errors: dict[str, ActionsLockfileError] = {} + for root in callgraph.root_nodes: + for node in traverse_bfs(root): + if isinstance(node, GitHubActionsWorkflowNode): + source = node.context.ref.source_filepath + relative = Path(source).resolve().relative_to(repo_root.resolve()).as_posix() if repo_root else source + workflows[relative] = source + if not isinstance(node, (GitHubActionsActionStepNode, GitHubActionsReusableWorkflowCallNode)): + continue + uses = node.definition["uses"] + if uses.startswith(("./", "$/", "docker://")) or "@" not in uses: + continue + job = ( + get_containing_github_job(node, callgraph.parents) + if isinstance(node, GitHubActionsActionStepNode) + else node + ) + if job is None: + continue + source = job.context.ref.workflow_context.ref.source_filepath + relative = Path(source).resolve().relative_to(repo_root.resolve()).as_posix() if repo_root else source + reusable = isinstance(node, GitHubActionsReusableWorkflowCallNode) + try: + resolution = resolve_action_reference(relative, uses, lockfile, reusable_workflow=reusable) + key = canonical_action_key(uses, subpath=True) + if ( + lockfile + and relative in lockfile.workflows + and not reusable + and resolution.pinning_method != "lockfile" + ): + raise ActionsLockfileError(f"{lockfile.path}: {relative}: missing workflow pin for {uses}") + except ActionsLockfileError as error: + errors[source] = error + continue + name, _, version = key.partition("@") + declared_name, _, declared_ref = uses.partition("@") + if reusable: + name = declared_name + # Keep reusable workflow/sub-action identity for display; action repository for OSV. + invocation = ResolvedWorkflowAction( + name, + declared_ref, + source, + resolution.resolved_commit, + resolution.pinning_method, + (uses,), + resolution.action_kind, + ) + identity = (source, name, version) + previous = actions.get(identity) + if previous is None or (len(invocation.chain), invocation.chain) < (len(previous.chain), previous.chain): + actions[identity] = invocation + if lockfile: + if repo_root: + for relative in lockfile.workflows: + if relative not in workflows and (repo_root / relative).is_file(): + errors[str(repo_root / relative)] = ActionsLockfileError( + f"{lockfile.path}: adopted workflow {relative} could not be analyzed" + ) + for relative, source in workflows.items(): + try: + dependencies = reachable_locked_dependencies(relative, lockfile) + for dependency in dependencies: + if dependency.action.hostname != "github.com": + raise ActionsLockfileError( + f"{lockfile.path}: unsupported dependency host {dependency.action.hostname}" + ) + except ActionsLockfileError as error: + errors[source] = error + continue + for dependency in dependencies: + name, _, version = dependency.key.partition("@") + identity = (source, name, version) + previous = actions.get(identity) + # Direct declarations retain their own chain and ref. Nested entries retain the recorded path. + chain = previous.chain if previous else dependency.chain + actions[identity] = ResolvedWorkflowAction( + name, + previous.version if previous else version, + source, + dependency.action.commit, + "lockfile", + chain, + "remote", + ) + return WorkflowActionAnalysis( + sorted( + (action for action in actions.values() if action.caller_path not in errors), + key=lambda action: (action.caller_path, action.name, action.version), + ), + errors, + ) + + +def record_incomplete_vulnerability_analysis( + actions: list[ResolvedWorkflowAction], error: APIAccessError +) -> list[GitHubActionsVulnsFacts]: + """Keep locked dependencies from appearing clean when external matching cannot assess them. + + Existing mutable-reference API behavior is preserved. For a locked dependency, + uncertainty is a concrete failed analysis with its effective commit and source + chain; it is not classified as a confirmed known vulnerability. + """ + facts = [] + seen: set[tuple[str, str, str | None]] = set() + for action in actions: + identity = (action.caller_path, action.name, action.resolved_commit) + if action.pinning_method != "lockfile" or identity in seen: + continue + seen.add(identity) + facts.append( + GitHubActionsVulnsFacts( + caller_workflow=action.caller_path, + finding_type="vulnerability-analysis-incomplete", + finding_group=GitHubActionsFindingGroup.THIRD_PARTY_ACTION_RISK.value, + finding_message=f"Could not assess locked commit {action.resolved_commit}: {error}. " + "Review the commit and vulnerability data; the declared tag is not a safe fallback.", + finding_priority=80, + action_name=action.name, + action_ref=action.version, + sha_pinned=bool(re.fullmatch(r"[0-9a-fA-F]{40}|[0-9a-fA-F]{64}", action.version)), + pinning_method="lockfile", + resolved_commit=action.resolved_commit, + lockfile_path=LOCKFILE_PATH, + dependency_chain=list(action.chain), + vuln_urls=[], + confidence=Confidence.HIGH, + ) + ) + return facts + + class GitHubActionsVulnsCheck(BaseCheck): """This Check checks whether the GitHub Actions called from the corresponding repo have known vulnerabilities. - Note: This check analyzes the direct GitHub Actions dependencies only. - TODO: Check GitHub Actions dependencies recursively. + Covers declared dependencies and reachable transitive dependencies recorded + in applicable workflow lockfiles. It does not fetch remote workflow graphs. """ def __init__(self) -> None: @@ -153,12 +350,40 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData: """ ci_services = ctx.dynamic_data["ci_services"] result_tables: list[CheckFacts] = [] - external_workflows: dict[str, list] = {} + external_workflows: dict[str, list[ResolvedWorkflowAction]] = {} + repository = ctx.component.repository + repo_root = Path(repository.fs_path) if repository.fs_path else None + lockfile = None + lockfile_error: ActionsLockfileError | None = None + resolution_errors: list[ActionsLockfileError] = [] + if repo_root: + try: + lockfile = parse_actions_lockfile(repo_root / LOCKFILE_PATH) + except ActionsLockfileError as error: + lockfile_error = error for ci_info in ci_services: + if not isinstance(ci_info["service"], GitHubActions): + continue callgraph = ci_info["callgraph"] - gh_api_client = ci_info["service"].api_client if hasattr(ci_info["service"], "api_client") else None - if workflow_findings := detect_github_actions_security_issues(callgraph): + gh_api_client = ci_info["service"].api_client + resolved_actions: list[ResolvedWorkflowAction] = [] + action_analysis = collect_resolved_workflow_actions(callgraph, repo_root, lockfile) + resolution_errors.extend(action_analysis.errors.values()) + resolved_actions = action_analysis.actions + if lockfile_error is not None: + resolved_actions = [ + action + for action in resolved_actions + if action.pinning_method == "inline_sha" or action.kind == "reusable" + ] + workflow_findings = detect_github_actions_security_issues( + callgraph, + lockfile=lockfile, + repo_root=repo_root, + unresolved_workflows=frozenset(action_analysis.errors), + ) + if workflow_findings: for finding in workflow_findings: caller_workflow_link = "" if gh_api_client: @@ -217,47 +442,36 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData: confidence=Confidence.HIGH, ) ) - for root in callgraph.root_nodes: - for callee in traverse_bfs(root): - if isinstance(callee, (GitHubActionsReusableWorkflowCallNode, GitHubActionsActionStepNode)): - workflow_name = callee.uses_name - workflow_version = callee.uses_version - if workflow_version is None: - # Most likely we have encountered an internal reusable workflow, which - # can be skipped. - logger.debug("GitHub Actions workflow %s misses a version. Skipping...", workflow_name) - continue - - job = ( - get_containing_github_job(callee, callgraph.parents) - if isinstance(callee, GitHubActionsActionStepNode) - else callee - ) - - if not job: - continue - - caller_path = job.context.ref.workflow_context.ref.source_filepath - caller_path_link = caller_path - if gh_api_client: - caller_path_link = gh_api_client.get_file_link( - ctx.component.repository.full_name, - ctx.component.repository.commit_sha, - file_path=( - gh_api_client.get_relative_path_of_workflow(os.path.basename(caller_path)) - if caller_path - else "" - ), - ) - - ext_workflow: list = external_workflows.get(workflow_name, []) - ext_workflow.append( - { - "version": workflow_version, - "caller_path": caller_path_link, - } - ) - external_workflows[workflow_name] = ext_workflow + for action in resolved_actions: + caller_path = action.caller_path + if gh_api_client: + relative_path = ( + Path(caller_path).resolve().relative_to(repo_root.resolve()).as_posix() + if repo_root + else gh_api_client.get_relative_path_of_workflow(os.path.basename(caller_path)) + ) + caller_path = gh_api_client.get_file_link( + repository.full_name, repository.commit_sha, file_path=relative_path + ) + external_workflows.setdefault(action.name, []).append(replace(action, caller_path=caller_path)) + + if lockfile_error is not None: + resolution_errors.insert(0, lockfile_error) + for resolution_error in resolution_errors: + result_tables.append( + GitHubActionsVulnsFacts( + caller_workflow=LOCKFILE_PATH, + finding_type="invalid-actions-lockfile", + finding_group=GitHubActionsFindingGroup.WORKFLOW_SECURITY_ISSUE.value, + finding_message=f"{resolution_error}. Review workflow dependencies and regenerate the lockfile with gh actions-lock.", + finding_priority=80, + action_name=None, + action_ref=None, + sha_pinned=None, + vuln_urls=[], + confidence=Confidence.HIGH, + ) + ) # If no external GitHub Actions are found, no need to check for known vulnerabilities. if external_workflows: @@ -272,6 +486,11 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData: batch_vulns = OSVDevService.get_vulnerabilities_package_name_batch(batch_query) except APIAccessError as error: logger.debug(error) + result_tables.extend( + record_incomplete_vulnerability_analysis( + [action for actions in external_workflows.values() for action in actions], error + ) + ) for vuln_res in batch_vulns: vulns: list = [] @@ -282,24 +501,33 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData: ) except APIAccessError as error: logger.debug(error) + result_tables.extend( + record_incomplete_vulnerability_analysis(external_workflows[workflow_name], error) + ) continue for workflow_inv in external_workflows[workflow_name]: vuln_mapping = [] + matching_error: APIAccessError | None = None for vuln in vulns: if v_id := json_extract(vuln, ["id"], str): try: if OSVDevService.is_version_affected( vuln, workflow_name, - workflow_inv["version"], + workflow_inv.resolved_commit or workflow_inv.version, "GitHub Actions", source_repo=f"https://github.com/{workflow_name}", ): vuln_mapping.append(f"https://osv.dev/vulnerability/{v_id}") except APIAccessError as error: logger.debug(error) + matching_error = error + if matching_error is not None: + result_tables.extend(record_incomplete_vulnerability_analysis([workflow_inv], matching_error)) if vuln_mapping: - recommendation = recommend_for_osv_vulnerability(workflow_name, workflow_inv["version"]) + recommendation = recommend_for_osv_vulnerability( + workflow_name, workflow_inv.resolved_commit or workflow_inv.version + ) finding_type = GitHubActionsFindingType.KNOWN_VULNERABILITY.value result_tables.append( GitHubActionsVulnsFacts( @@ -307,12 +535,21 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData: finding_type=finding_type, finding_group=GitHubActionsFindingGroup.THIRD_PARTY_ACTION_RISK.value, action_name=workflow_name, - action_ref=workflow_inv["version"], - caller_workflow=workflow_inv["caller_path"], - sha_pinned=bool(re.fullmatch(r"[0-9a-f]{40}", workflow_inv["version"])), + action_ref=workflow_inv.version, + caller_workflow=workflow_inv.caller_path, + sha_pinned=bool(re.fullmatch(r"[0-9a-fA-F]{40}|[0-9a-fA-F]{64}", workflow_inv.version)), + pinning_method=workflow_inv.pinning_method, + resolved_commit=workflow_inv.resolved_commit, + lockfile_path=LOCKFILE_PATH if workflow_inv.pinning_method == "lockfile" else None, + dependency_chain=list(workflow_inv.chain), finding_message=( f"Summary: {_short_description_for_finding_type(finding_type)} " f"Recommendation: {recommendation.message}" + + ( + " Review and update the dependency, then regenerate and review actions.lock." + if workflow_inv.pinning_method == "lockfile" + else "" + ) ), finding_priority=100, recommended_ref=recommendation.recommended_ref, diff --git a/src/macaron/slsa_analyzer/package_registry/pypi_registry.py b/src/macaron/slsa_analyzer/package_registry/pypi_registry.py index f0ebf21ca..d198ab97d 100644 --- a/src/macaron/slsa_analyzer/package_registry/pypi_registry.py +++ b/src/macaron/slsa_analyzer/package_registry/pypi_registry.py @@ -209,7 +209,8 @@ def cleanup_sourcecode_directory( if error_message: logger.debug(error_message) try: - shutil.rmtree(directory, onerror=_handle_temp_dir_clean) + # Python 3.11 requires onerror; its replacement onexc was added in Python 3.12. + shutil.rmtree(directory, onerror=_handle_temp_dir_clean) # pylint: disable=deprecated-argument logger.debug("Successfully cleaned up temporary directory %s.", directory) except SourceCodeError as tempdir_exception: tempdir_exception_msg = ( diff --git a/tests/code_analyzer/gha_security_analysis/test_action_resolution.py b/tests/code_analyzer/gha_security_analysis/test_action_resolution.py new file mode 100644 index 000000000..e85b0dda4 --- /dev/null +++ b/tests/code_analyzer/gha_security_analysis/test_action_resolution.py @@ -0,0 +1,67 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Workflow-scoped pinning resolution and recorded dependency traversal.""" + +import json +from pathlib import Path + +import pytest + +from macaron.code_analyzer.gha_security_analysis.action_resolution import ( + reachable_locked_dependencies, + resolve_action_reference, +) +from macaron.parsers.github_actions_lockfile import ActionsLockfileError, parse_actions_lockfile +from tests.parsers.test_github_actions_lockfile import write_lockfile + + +@pytest.mark.parametrize( + ("workflow", "uses", "method", "commit"), + [ + (".github/workflows/ci.yml", "EXAMPLE/Parent/sub@v1", "lockfile", "a" * 40), + (".github/workflows/other.yml", "example/parent@v1", "none", None), + (".github/workflows/ci.yml", "example/orphan@v2", "none", None), + (".github/workflows/ci.yml", "example/child@Main", "lockfile", "b" * 40), + (".github/workflows/ci.yml", "example/child@main", "none", None), + (".github/workflows/ci.yml", "$/actions/build", "same_repository", None), + (".github/workflows/ci.yml", "./actions/build", "none", None), + (".github/workflows/ci.yml", "docker://alpine:latest", "none", None), + (".github/workflows/ci.yml", "example/other@" + "d" * 40, "inline_sha", "d" * 40), + ], +) +def test_workflow_scope(tmp_path: Path, workflow: str, uses: str, method: str, commit: str | None) -> None: + """Only applicable reachable entries grant lockfile pinning.""" + lockfile = parse_actions_lockfile(write_lockfile(tmp_path)) + result = resolve_action_reference(workflow, uses, lockfile) + assert result.pinning_method == method + assert result.resolved_commit == commit + + +def test_reusable_call_is_not_locked(tmp_path: Path) -> None: + """Caller lockfiles never pin remote reusable workflows.""" + lockfile = parse_actions_lockfile(write_lockfile(tmp_path)) + result = resolve_action_reference(".github/workflows/ci.yml", "example/parent@v1", lockfile, reusable_workflow=True) + assert result.pinning_method == "none" + + +def test_graph_preserves_chain_and_skips_orphans(tmp_path: Path) -> None: + """Recorded child actions are scanned under their originating workflow.""" + lockfile = parse_actions_lockfile(write_lockfile(tmp_path)) + assert lockfile is not None + uses = reachable_locked_dependencies(".github/workflows/ci.yml", lockfile) + assert [(use.key, use.chain) for use in uses] == [ + ("example/child@Main", ("example/parent@v1", "example/child@Main")), + ("example/parent@v1", ("example/parent@v1",)), + ] + + +@pytest.mark.parametrize("child", ["example/missing@v2", "example/parent@v1"]) +def test_broken_graph(tmp_path: Path, child: str) -> None: + """Missing dependency entries and cycles are diagnosed conservatively.""" + path = write_lockfile(tmp_path) + data = json.loads(path.read_text(encoding="utf-8")) + data["dependencies"]["example/parent@v1"]["uses"] = [child] + path.write_text(json.dumps(data), encoding="utf-8") + with pytest.raises(ActionsLockfileError): + parse_actions_lockfile(path) diff --git a/tests/database/test_database_manager.py b/tests/database/test_database_manager.py index aa64a2504..539bcde24 100644 --- a/tests/database/test_database_manager.py +++ b/tests/database/test_database_manager.py @@ -15,6 +15,7 @@ from sqlalchemy.sql.sqltypes import Integer, String from macaron.database.database_manager import DatabaseManager +from macaron.errors import MacaronError class Base(DeclarativeBase): @@ -48,6 +49,51 @@ def db_man() -> Iterable: os.remove(DB_PATH) +def test_existing_schema_must_not_silently_drop_new_evidence(tmp_path: Path) -> None: + """An old table must fail before create_all reuses it or writes incompatible facts.""" + path = tmp_path / "old.db" + with closing(sqlite3.connect(path)) as connection: + connection.executescript( + "CREATE TABLE _test_orm_table (id INTEGER PRIMARY KEY);" + "CREATE VIEW test_orm_table AS SELECT id FROM _test_orm_table;" + ) + manager = DatabaseManager(str(path), base=Base) + try: + with pytest.raises(MacaronError, match="fresh output directory"): + manager.create_tables() + with closing(sqlite3.connect(path)) as connection: + assert [row[1] for row in connection.execute("PRAGMA table_info(_test_orm_table)")] == ["id"] + finally: + manager.engine.dispose() + + +def test_existing_view_must_expose_current_columns(tmp_path: Path) -> None: + """A compatible physical table cannot hide an outdated generated view.""" + + class ViewBase(DeclarativeBase): + """Isolated metadata models a fresh process before any views have been registered.""" + + class ViewTable(ViewBase): + """The current table includes evidence the old view does not expose.""" + + __tablename__ = "_view_compatibility" + id = Column(Integer, primary_key=True) + evidence = Column(String) + + path = tmp_path / "view.db" + with closing(sqlite3.connect(path)) as connection: + connection.executescript( + "CREATE TABLE _view_compatibility (id INTEGER PRIMARY KEY, evidence TEXT);" + "CREATE VIEW view_compatibility AS SELECT id FROM _view_compatibility;" + ) + manager = DatabaseManager(str(path), base=ViewTable) + try: + with pytest.raises(MacaronError, match="fresh output directory"): + manager.create_tables() + finally: + manager.engine.dispose() + + @pytest.mark.parametrize( ("identifier", "test_value", "expect"), [ diff --git a/tests/integration/cases/oracle_coherence-js-client/policy.dl b/tests/integration/cases/oracle_coherence-js-client/policy.dl index 4406970d8..774dbba88 100644 --- a/tests/integration/cases/oracle_coherence-js-client/policy.dl +++ b/tests/integration/cases/oracle_coherence-js-client/policy.dl @@ -16,6 +16,10 @@ Policy("check-github-actions-vulnerabilities", component_id, "Check GitHub Actio "aquasecurity/trivy-action", "0.32.0", _, + _, + _, + _, + _, "[\"https://osv.dev/vulnerability/GHSA-69fq-xp46-6x23\", \"https://osv.dev/vulnerability/GHSA-9p44-j4g5-cfx5\"]" ). diff --git a/tests/integration/cases/org_apache_logging_log4j/policy_purl.dl b/tests/integration/cases/org_apache_logging_log4j/policy_purl.dl index 49df9eba0..0d43e32f7 100644 --- a/tests/integration/cases/org_apache_logging_log4j/policy_purl.dl +++ b/tests/integration/cases/org_apache_logging_log4j/policy_purl.dl @@ -10,7 +10,14 @@ Policy("test_policy", component_id, "") :- check_passed_with_confidence(component_id, "mcn_find_artifact_pipeline_1", confidence), confidence = 0.7, // Medium confidence because the pipeline was not found from a provenance. check_passed(component_id, "mcn_version_control_system_1"), - check_passed(component_id, "mcn_githubactions_vulnerabilities_1"), + // This revision calls remote reusable workflows through mutable refs. + check_failed(component_id, "mcn_githubactions_vulnerabilities_1"), + github_actions_vulnerabilities_check( + finding_id, _, "unpinned-third-party-action", "third_party_action_risk", + _, _, _, "apache/logging-parent/.github/workflows/build-reusable.yaml", + "rel/12.1.1", _, _, _, _, _, _ + ), + check_facts(finding_id, _, component_id, _, _), check_failed(component_id, "mcn_provenance_available_1"), check_failed(component_id, "mcn_provenance_derived_commit_1"), check_failed(component_id, "mcn_provenance_derived_repo_1"), diff --git a/tests/integration/cases/org_apache_logging_log4j/policy_repo_url.dl b/tests/integration/cases/org_apache_logging_log4j/policy_repo_url.dl index 8f34d5674..ebcbb5933 100644 --- a/tests/integration/cases/org_apache_logging_log4j/policy_repo_url.dl +++ b/tests/integration/cases/org_apache_logging_log4j/policy_repo_url.dl @@ -4,7 +4,14 @@ #include "prelude.dl" Policy("test_policy", component_id, "") :- - check_passed(component_id, "mcn_githubactions_vulnerabilities_1"). + // This revision calls remote reusable workflows through mutable refs. + check_failed(component_id, "mcn_githubactions_vulnerabilities_1"), + github_actions_vulnerabilities_check( + finding_id, _, "unpinned-third-party-action", "third_party_action_risk", + _, _, _, "apache/logging-parent/.github/workflows/build-reusable.yaml", + "rel/12.1.1", _, _, _, _, _, _ + ), + check_facts(finding_id, _, component_id, _, _). apply_policy_to("test_policy", component_id) :- is_repo_url(component_id, "https://github.com/apache/logging-log4j2"). diff --git a/tests/output_reporter/test_lockfile_action_regression.py b/tests/output_reporter/test_lockfile_action_regression.py new file mode 100644 index 000000000..a98eb27c3 --- /dev/null +++ b/tests/output_reporter/test_lockfile_action_regression.py @@ -0,0 +1,82 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Prove the workflow's assertions reject missing analysis and false pinning findings.""" + +import json +import sqlite3 +import subprocess +import sys +from contextlib import closing +from pathlib import Path + +import pytest + + +@pytest.mark.parametrize( + ("mode", "expected_code"), + [ + ("passed", 0), + ("unrelated", 0), + ("unpinned", 1), + ("invalid", 1), + ("missing_check", 1), + ("wrong_commit", 1), + ("missing_policy", 1), + ("wrong_outcome", 1), + ], +) +def test_workflow_assertions(tmp_path: Path, mode: str, expected_code: int) -> None: + """The same assertion program used in CI must fail for the original lockfile regression.""" + passed = mode in {"passed", "wrong_commit", "missing_check", "missing_policy", "wrong_outcome"} + commit = "35ad4c011e7cfc84f7370397221f82823c74ce35" if mode != "wrong_commit" else "a" * 40 + purl = "pkg:github.com/graalvm/setup-graalvm@" + commit + with closing(sqlite3.connect(tmp_path / "macaron.db")) as connection: + connection.executescript(""" + CREATE TABLE component (id INTEGER, purl TEXT); + CREATE TABLE repository (component_id INTEGER, full_name TEXT, commit_sha TEXT); + CREATE TABLE check_result (id INTEGER, component_id INTEGER, check_id TEXT, passed INTEGER); + CREATE TABLE check_facts (id INTEGER, component_id INTEGER, check_result_id INTEGER); + CREATE TABLE github_actions_vulnerabilities_check + (id INTEGER, finding_type TEXT, action_name TEXT, action_ref TEXT, caller_workflow TEXT); + """) + connection.execute("INSERT INTO component VALUES (1, ?)", (purl,)) + connection.execute("INSERT INTO repository VALUES (1, 'graalvm/setup-graalvm', ?)", (commit,)) + if mode != "missing_check": + connection.execute( + "INSERT INTO check_result VALUES (1, 1, 'mcn_githubactions_vulnerabilities_1', ?)", (int(passed),) + ) + if not passed: + finding = { + "unrelated": "overbroad-permissions", + "unpinned": "unpinned-third-party-action", + "invalid": "invalid-actions-lockfile", + }[mode] + connection.execute("INSERT INTO check_facts VALUES (1, 1, 1)") + connection.execute( + "INSERT INTO github_actions_vulnerabilities_check VALUES (1, ?, ?, ?, ?)", + (finding, "oracle/macaron", "v0.25.0", ".github/workflows/macaron-check-github-actions.yml"), + ) + connection.commit() + relation = "component_satisfies_policy" if passed else "component_violates_policy" + report = { + "passed_policies": [["github_actions_vulns"]] if passed else [], + "failed_policies": [] if passed else [["github_actions_vulns"]], + relation: [["1", purl, "github_actions_vulns"]], + } + if mode != "missing_policy": + (tmp_path / "policy_report.json").write_text(json.dumps(report), encoding="utf-8") + if passed: + (tmp_path / "vsa.intoto.jsonl").write_text("{}\n", encoding="utf-8") + script = ( + Path(__file__).resolve().parents[1] + / "tutorial_resources/detect_vulnerable_github_actions/assert_lockfile_regression.py" + ) + outcome = "failure" if not passed or mode == "wrong_outcome" else "success" + result = subprocess.run( # noqa: S603 - execute the checked-in assertion script with controlled test arguments. + [sys.executable, str(script), str(tmp_path), outcome], + check=False, + capture_output=True, + text=True, + ) + assert result.returncode == expected_code, result.stdout + result.stderr diff --git a/tests/output_reporter/test_write_job_summary.py b/tests/output_reporter/test_write_job_summary.py index 210dc7ac8..30fb25534 100644 --- a/tests/output_reporter/test_write_job_summary.py +++ b/tests/output_reporter/test_write_job_summary.py @@ -6,9 +6,13 @@ from __future__ import annotations import importlib.util +import sqlite3 +from contextlib import closing from pathlib import Path from types import ModuleType +import pytest + def _load_write_job_summary_module() -> ModuleType: """Load the write_job_summary script as a Python module for testing.""" @@ -22,6 +26,57 @@ def _load_write_job_summary_module() -> ModuleType: return module +def test_summary_preserves_locked_vulnerability_evidence(tmp_path: Path) -> None: + """A locked vulnerable tag shows the effective commit and dependency chain.""" + module = _load_write_job_summary_module() + summary = tmp_path / "summary.md" + columns = [ + "finding_group", + "finding_priority", + "finding_type", + "action_name", + "action_ref", + "vulnerable_workflow", + "finding_message", + "sha_pinned", + "pinning_method", + "resolved_commit", + "lockfile_path", + "dependency_chain", + ] + rows = [ + ( + "third_party_action_risk", + 100, + "known-vulnerability", + "example/child", + "Main", + "ci.yml", + "Locked commit is vulnerable", + 0, + "lockfile", + "b" * 40, + ".github/workflows/actions.lock", + '["example/parent@v1", "example/child@Main"]', + ) + ] + assert module.write_compact_gha_vuln_diagnostics(summary, columns, rows) is True + output = summary.read_text(encoding="utf-8") + assert "b" * 40 in output + assert "example/parent@v1" in output + assert ".github/workflows/actions.lock" in output + assert "recorded lockfile coverage" in output.lower() + + +def test_old_database_diagnostics_are_actionable(tmp_path: Path) -> None: + """The extended SQL must not run against a pre-feature view and fail obscurely.""" + module = _load_write_job_summary_module() + with closing(sqlite3.connect(tmp_path / "old.db")) as connection: + connection.execute("CREATE TABLE github_actions_vulnerabilities_check (id INTEGER)") + with pytest.raises(sqlite3.DatabaseError, match="fresh output directory"): + module.validate_github_actions_schema(connection) + + def test_workflow_security_table_includes_summary_column(tmp_path: Path) -> None: """Render workflow security findings with the short summary column in compact table output.""" module = _load_write_job_summary_module() diff --git a/tests/parsers/resources/github_actions_lockfile/graalvm-actions.lock b/tests/parsers/resources/github_actions_lockfile/graalvm-actions.lock new file mode 100644 index 000000000..db17e9fc5 --- /dev/null +++ b/tests/parsers/resources/github_actions_lockfile/graalvm-actions.lock @@ -0,0 +1,59 @@ +# This file is machine-generated by `gh actions-lock`. +# Do not edit by hand; run `gh actions-lock` to update. +# Docs: https://gh.io/actions-lockfile +version: 'v0.0.3' +workflows: + '.github/workflows/check-dist.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-node@v7.1.0' + - 'actions/upload-artifact@v7.0.2' + '.github/workflows/ci.yml': + - 'actions/checkout@v7.0.1' + - 'actions/setup-node@v7.1.0' + '.github/workflows/macaron-check-github-actions.yml': + - 'actions/checkout@v7.0.1' + - 'oracle/macaron@v0.25.0' + '.github/workflows/update-dependabot-dist.yml': + - 'actions/checkout@v7.0.1' + - 'actions/download-artifact@v8.0.2' + '.github/workflows/verify-lockfile.yml': + - 'actions/checkout@v7.0.1' +dependencies: + 'actions/attest@daf44fb950173508f38bd2406030372c1d1162b1': + ref: 'v3.0.0' + commit: 'sha1-daf44fb950173508f38bd2406030372c1d1162b1' + owner_id: 44036562 + repo_id: 760701061 + 'actions/checkout@v7.0.1': + ref: 'v7.0.1' + commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1' + owner_id: 44036562 + repo_id: 197814629 + 'actions/download-artifact@v8.0.2': + ref: 'v8.0.2' + commit: 'sha1-9000827ccba6bdab643e8b6fd33ac0654aef8333' + owner_id: 44036562 + repo_id: 192626254 + 'actions/setup-node@v7.1.0': + ref: 'v7.1.0' + commit: 'sha1-949feb2413d6458794dcd2491c4babbbce0c15c1' + owner_id: 44036562 + repo_id: 189476904 + 'actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f': + ref: 'v7.0.0' + commit: 'sha1-bbbca2ddaa5d8feaa63e36b76fdaad77386f024f' + owner_id: 44036562 + repo_id: 192625955 + 'actions/upload-artifact@v7.0.2': + ref: 'v7.0.2' + commit: 'sha1-cf430e030ddbb5b0abf93d22962f4752f3646cd9' + owner_id: 44036562 + repo_id: 192625955 + 'oracle/macaron@v0.25.0': + ref: 'v0.25.0' + commit: 'sha1-794eec32782979f577efb5ff71d585f683a3550f' + owner_id: 4430336 + repo_id: 574379491 + uses: + - 'actions/attest@daf44fb950173508f38bd2406030372c1d1162b1' + - 'actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f' diff --git a/tests/parsers/test_github_actions_lockfile.py b/tests/parsers/test_github_actions_lockfile.py new file mode 100644 index 000000000..cad6ef9dd --- /dev/null +++ b/tests/parsers/test_github_actions_lockfile.py @@ -0,0 +1,131 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Validate lockfiles without accepting ambiguous or incomplete dependency metadata.""" + +import json +from pathlib import Path + +import pytest + +from macaron.errors import ParseError +from macaron.parsers.github_actions_lockfile import ActionsLockfileError, parse_actions_lockfile + + +def write_lockfile(tmp_path: Path) -> Path: + """Write a small GA-format graph with one nested dependency and an orphan.""" + path = tmp_path / "actions.lock" + path.write_text( + json.dumps( + { + "version": "v0.0.3", + "workflows": {".github/workflows/ci.yml": ["Example/Parent@v1"]}, + "dependencies": { + "example/parent@v1": { + "ref": "v1", + "commit": "sha1-" + "a" * 40, + "owner_id": 1, + "repo_id": 2, + "uses": ["example/child@Main"], + }, + "example/child@Main": {"ref": "Main", "commit": "sha1-" + "b" * 40, "owner_id": 1, "repo_id": 3}, + "example/orphan@v2": {"ref": "v2", "commit": "sha1-" + "c" * 40, "owner_id": 1, "repo_id": 4}, + }, + } + ), + encoding="utf-8", + ) + return path + + +def test_parse_ga_lockfile(tmp_path: Path) -> None: + """Repository names are canonicalized while symbolic refs retain case.""" + lockfile = parse_actions_lockfile(write_lockfile(tmp_path)) + assert lockfile is not None + assert lockfile.workflows[".github/workflows/ci.yml"] == ("example/parent@v1",) + assert lockfile.dependencies["example/child@Main"].commit == "b" * 40 + assert "example/child@main" not in lockfile.dependencies + + +def test_absent_lockfile(tmp_path: Path) -> None: + """Absent lockfiles preserve normal SHA pinning behavior.""" + assert parse_actions_lockfile(tmp_path / "missing") is None + + +@pytest.mark.parametrize( + "contents", + [ + "", + "version: v99.0.0", + "version: v0.0.3\nversion: v0.0.3", + "[", + "version: v0.0.3\nunknown: true", + "version: v0.0.3\nworkflows: []", + ], +) +def test_reject_invalid_document(tmp_path: Path, contents: str) -> None: + """Present invalid files must not be treated as absent.""" + path = tmp_path / "actions.lock" + path.write_text(contents, encoding="utf-8") + with pytest.raises(ActionsLockfileError): + parse_actions_lockfile(path) + assert issubclass(ActionsLockfileError, ParseError) + + +@pytest.mark.parametrize( + ("field", "value"), + [ + ("commit", "sha1-deadbeef"), + ("owner_id", True), + ("repo_id", 0), + ("ref", "wrong"), + ("hostname", "https://github.com"), + ("uses", "example/child@Main"), + ("unknown", "field"), + ], +) +def test_reject_invalid_dependency(tmp_path: Path, field: str, value: str | int | bool) -> None: + """Malformed metadata must not grant pinning credit.""" + path = write_lockfile(tmp_path) + data = json.loads(path.read_text(encoding="utf-8")) + data["dependencies"]["example/parent@v1"][field] = value + path.write_text(json.dumps(data), encoding="utf-8") + with pytest.raises(ActionsLockfileError): + parse_actions_lockfile(path) + + +@pytest.mark.parametrize("field", ["ref", "commit", "owner_id", "repo_id"]) +def test_required_metadata(tmp_path: Path, field: str) -> None: + """Missing metadata cannot become a trusted pin.""" + path = write_lockfile(tmp_path) + data = json.loads(path.read_text(encoding="utf-8")) + del data["dependencies"]["example/parent@v1"][field] + path.write_text(json.dumps(data), encoding="utf-8") + with pytest.raises(ActionsLockfileError): + parse_actions_lockfile(path) + + +def test_unreadable_lockfile_is_diagnostic(tmp_path: Path) -> None: + """A directory in place of the file is an error, not missing coverage.""" + with pytest.raises(ActionsLockfileError): + parse_actions_lockfile(tmp_path) + + +def test_graalvm_committed_fixture() -> None: + """The exact reported commit's graph resolves Macaron and its nested upload action.""" + path = Path(__file__).parent / "resources/github_actions_lockfile/graalvm-actions.lock" + lockfile = parse_actions_lockfile(path) + assert lockfile is not None + assert "oracle/macaron@v0.25.0" in lockfile.workflows[".github/workflows/macaron-check-github-actions.yml"] + assert lockfile.dependencies["oracle/macaron@v0.25.0"].commit == "794eec32782979f577efb5ff71d585f683a3550f" + assert len(lockfile.dependencies["oracle/macaron@v0.25.0"].uses) == 2 + + +def test_invalid_orphan_cycle_is_not_accepted_as_valid_document(tmp_path: Path) -> None: + """The supported parser rejects cycles even outside the currently analyzed workflow.""" + path = write_lockfile(tmp_path) + data = json.loads(path.read_text(encoding="utf-8")) + data["dependencies"]["example/orphan@v2"]["uses"] = ["example/orphan@v2"] + path.write_text(json.dumps(data), encoding="utf-8") + with pytest.raises(ActionsLockfileError, match="cycle"): + parse_actions_lockfile(path) diff --git a/tests/policy_engine/test_github_actions_lockfile_policy.py b/tests/policy_engine/test_github_actions_lockfile_policy.py new file mode 100644 index 000000000..0dbaf7b84 --- /dev/null +++ b/tests/policy_engine/test_github_actions_lockfile_policy.py @@ -0,0 +1,207 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Exercise actual check facts, SQLite views, the shipped policy, and action diagnostics.""" + +import json +import sqlite3 +from contextlib import closing +from datetime import UTC, datetime +from pathlib import Path + +import pytest +from sqlalchemy.orm import Session + +from macaron.database.database_manager import DatabaseManager +from macaron.database.table_definitions import CheckFacts, MappedCheckResult, RepoFinderMetadata +from macaron.policy_engine.policy_engine import run_souffle +from macaron.repo_finder.repo_finder_enums import CommitFinderInfo, RepoFinderInfo +from macaron.slsa_analyzer.checks.check_result import CheckResultType, Confidence +from macaron.slsa_analyzer.checks.github_actions_vulnerability_check import ( + GitHubActionsVulnsCheck, + GitHubActionsVulnsFacts, +) +from macaron.slsa_analyzer.ci_service.base_ci_service import BaseCIService +from macaron.slsa_analyzer.package_registry.osv_dev import OSVDevService +from tests.conftest import MockAnalyzeContext +from tests.output_reporter.test_write_job_summary import _load_write_job_summary_module +from tests.parsers.test_github_actions_lockfile import write_lockfile +from tests.slsa_analyzer.checks.test_github_actions_vulnerability_check import get_ci_info + + +@pytest.mark.parametrize( + ("mode", "passed"), + [("locked", True), ("uncovered", False), ("invalid", False), ("vulnerable", False), ("permissions", False)], +) +def test_lockfile_policy_database_and_summary( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], + mode: str, + passed: bool, +) -> None: + """The shipped policy consumes real results instead of masking incorrect pinning or OSV versions.""" + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + source = directory / "ci.yml" + permissions = "write-all" if mode == "permissions" else "{}" + source.write_text( + f"""on: push +permissions: {permissions} +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: example/parent@v1 +""", + encoding="utf-8", + ) + path = write_lockfile(tmp_path) + data = json.loads(path.read_text(encoding="utf-8")) + if mode == "invalid": + data["version"] = "v99.0.0" + if mode != "uncovered": + (directory / "actions.lock").write_text(json.dumps(data), encoding="utf-8") + monkeypatch.setattr( + OSVDevService, + "get_vulnerabilities_package_name_batch", + lambda packages: packages if mode == "vulnerable" else [], + ) + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name", lambda **_: [{"id": "TEST-1"}]) + monkeypatch.setattr(OSVDevService, "is_version_affected", lambda *args, **_: args[2] == "b" * 40) + monkeypatch.setattr(ci_services["github_actions"].api_client, "get_commit_sha_from_ref", lambda *_: None) + ctx = MockAnalyzeContext(fs_path=str(tmp_path), purl="pkg:github.com/example/target@" + "d" * 40) + ctx.dynamic_data["ci_services"] = [get_ci_info(ci_services, "github_actions", str(source))] + ctx.component.type, ctx.component.name = "github.com", "target" + ctx.component.analysis.analysis_time = datetime.now(UTC) + ctx.component.analysis.macaron_version = "test" + ctx.component.repo_finder_metadata = RepoFinderMetadata( + repo_finder_outcome=RepoFinderInfo.NOT_USED, + commit_finder_outcome=CommitFinderInfo.NOT_USED, + found_url="", + found_commit="", + ) + repository = ctx.component.repository + repository.full_name, repository.type, repository.name = "example/target", "github.com", "target" + repository.remote_path, repository.commit_date = "https://github.com/example/target", "2026-11-13" + result = GitHubActionsVulnsCheck().run_check(ctx) + assert (result.result_type == CheckResultType.PASSED) is passed + manager = DatabaseManager(str(tmp_path / "macaron.db")) + manager.create_tables() + check = MappedCheckResult(check_id="mcn_githubactions_vulnerabilities_1", passed=passed, component=ctx.component) + for fact in result.result_tables: + fact.checkresult, fact.component = check, ctx.component + try: + with Session(manager.engine) as session: + session.add(ctx.component) + session.commit() + root = Path(__file__).resolve().parents[2] + resources = root / "src/macaron/resources/policies" + template = (resources / "datalog/check-github-actions.dl.template").read_text(encoding="utf-8") + policy = template.replace("", "pkg:github.com/example/target@.*") + output = run_souffle(str(tmp_path / "macaron.db"), policy) + assert output["passed_policies"] == ([["github_actions_vulns"]] if passed else []) + assert output["failed_policies"] == ([] if passed else [["github_actions_vulns"]]) + module = _load_write_job_summary_module() + with closing(sqlite3.connect(tmp_path / "macaron.db")) as connection: + module.validate_github_actions_schema(connection) + cursor = connection.execute((resources / "sql/check-github-actions.sql").read_text(encoding="utf-8")) + columns = [column[0] for column in cursor.description] + rows = cursor.fetchall() + assert bool(rows) is not passed + if rows: + summary = tmp_path / "summary.md" + assert module.write_compact_gha_vuln_diagnostics(summary, columns, rows) is True + text = summary.read_text(encoding="utf-8") + if mode == "vulnerable": + assert "b" * 40 in text + assert "example/parent@v1" in text + finally: + manager.engine.dispose() + + +@pytest.mark.parametrize("case", ["log4j_purl", "log4j_repo", "coherence"]) +@pytest.mark.parametrize("include_finding", [True, False]) +def test_integration_github_actions_policies(tmp_path: Path, case: str, include_finding: bool) -> None: + """Integration policies require the intended finding and compile against the current schema.""" + coherence = case == "coherence" + purl = ( + "pkg:github.com/oracle/coherence-js-client@39166341bc31f75b663ff439dae36170fb3e99a9" + if coherence + else "pkg:maven/org.apache.logging.log4j/log4j-core@2.25.3" + ) + ctx = MockAnalyzeContext(purl=purl) + ctx.component.analysis.analysis_time = datetime.now(UTC) + ctx.component.analysis.macaron_version = "test" + ctx.component.type, ctx.component.name = ( + ("github.com", "coherence-js-client") if coherence else ("maven", "log4j-core") + ) + ctx.component.repo_finder_metadata = RepoFinderMetadata( + repo_finder_outcome=RepoFinderInfo.NOT_USED, + commit_finder_outcome=CommitFinderInfo.NOT_USED, + found_url="", + found_commit="", + ) + repository = ctx.component.repository + repository.full_name = "oracle/coherence-js-client" if coherence else "apache/logging-log4j2" + repository.remote_path = f"https://github.com/{repository.full_name}" + repository.type, repository.name = "github.com", repository.full_name.split("/")[-1] + repository.commit_date = "2026-11-13" + check = MappedCheckResult(check_id="mcn_githubactions_vulnerabilities_1", passed=False, component=ctx.component) + for check_id, passed in { + "mcn_build_as_code_1": True, + "mcn_build_script_1": True, + "mcn_build_service_1": True, + "mcn_find_artifact_pipeline_1": True, + "mcn_version_control_system_1": True, + "mcn_provenance_available_1": False, + "mcn_provenance_derived_commit_1": False, + "mcn_provenance_derived_repo_1": False, + "mcn_provenance_expectation_1": False, + "mcn_provenance_witness_level_one_1": False, + "mcn_trusted_builder_level_three_1": False, + }.items(): + result = MappedCheckResult(check_id=check_id, passed=passed, component=ctx.component) + if check_id == "mcn_find_artifact_pipeline_1": + CheckFacts(checkresult=result, component=ctx.component, confidence=0.7) + if include_finding: + GitHubActionsVulnsFacts( + # Finding identifiers must not accidentally equal the component identifier. + id=101, + checkresult=check, + component=ctx.component, + confidence=Confidence.HIGH, + caller_workflow=( + "https://github.com/oracle/coherence-js-client/blob/39166341bc31f75b663ff439dae36170fb3e99a9/.github/workflows/trivy-scan.yml" + if coherence + else "https://github.com/apache/logging-log4j2/blob/028e9fad03ae7bcbf2e49ab8d32d8cfb900f3587/.github/workflows/build.yaml" + ), + finding_type="known-vulnerability" if coherence else "unpinned-third-party-action", + finding_group="third_party_action_risk", + finding_priority=20, + action_name="aquasecurity/trivy-action" + if coherence + else "apache/logging-parent/.github/workflows/build-reusable.yaml", + action_ref="0.32.0" if coherence else "rel/12.1.1", + sha_pinned=False, + vuln_urls=[ + "https://osv.dev/vulnerability/GHSA-69fq-xp46-6x23", + "https://osv.dev/vulnerability/GHSA-9p44-j4g5-cfx5", + ] + if coherence + else [], + ) + manager = DatabaseManager(str(tmp_path / "macaron.db")) + manager.create_tables() + try: + with Session(manager.engine) as session: + session.add(ctx.component) + session.commit() + directory = "oracle_coherence-js-client" if coherence else "org_apache_logging_log4j" + filename = "policy.dl" if coherence else "policy_purl.dl" if case == "log4j_purl" else "policy_repo_url.dl" + policy_path = Path(__file__).resolve().parents[1] / "integration/cases" / directory / filename + output = run_souffle(str(tmp_path / "macaron.db"), policy_path.read_text(encoding="utf-8")) + assert bool(output["passed_policies"]) is include_finding + assert bool(output["failed_policies"]) is not include_finding + finally: + manager.engine.dispose() diff --git a/tests/provenance/test_provenance_finder.py b/tests/provenance/test_provenance_finder.py index 2cca28242..822b9e0b8 100644 --- a/tests/provenance/test_provenance_finder.py +++ b/tests/provenance/test_provenance_finder.py @@ -98,7 +98,8 @@ def name(self) -> str: repo = Mock(tags=[MockTag()]) -class MockJFrogRegistry(JFrogMavenRegistry): +# Inherit JFrog's unsupported timestamp lookup; provenance tests only fetch assets. +class MockJFrogRegistry(JFrogMavenRegistry): # pylint: disable=abstract-method """Mock JFrogMavenRegistry class.""" def __init__(self, resource_dir: str): diff --git a/tests/slsa_analyzer/checks/test_github_actions_vulnerability_check.py b/tests/slsa_analyzer/checks/test_github_actions_vulnerability_check.py index 151ffb1e9..f44a6393c 100644 --- a/tests/slsa_analyzer/checks/test_github_actions_vulnerability_check.py +++ b/tests/slsa_analyzer/checks/test_github_actions_vulnerability_check.py @@ -14,9 +14,16 @@ from macaron.code_analyzer.dataflow_analysis.analysis import analyse_github_workflow_file from macaron.code_analyzer.dataflow_analysis.core import NodeForest from macaron.config.defaults import load_defaults +from macaron.json_tools import JsonType +from macaron.parsers.github_actions_lockfile import parse_actions_lockfile from macaron.slsa_analyzer.checks.check_result import CheckResultType -from macaron.slsa_analyzer.checks.github_actions_vulnerability_check import GitHubActionsVulnsCheck +from macaron.slsa_analyzer.checks.github_actions_vulnerability_check import ( + GitHubActionsVulnsCheck, + GitHubActionsVulnsFacts, + collect_resolved_workflow_actions, +) from macaron.slsa_analyzer.ci_service.base_ci_service import BaseCIService +from macaron.slsa_analyzer.package_registry.osv_dev import OSVDevService from macaron.slsa_analyzer.provenance.intoto import InTotoV01Payload from macaron.slsa_analyzer.specs.ci_spec import CIInfo from macaron.slsa_analyzer.specs.inferred_provenance import InferredProvenance @@ -104,3 +111,322 @@ def test_github_actions_vulns( ctx.dynamic_data["ci_services"] = [get_ci_info(ci_services, ci_name, gha_source_path)] assert check.run_check(ctx).result_type == expected + + +@pytest.mark.parametrize( + ("mode", "expected_types"), + [ + ("locked", set()), + ("absent", {"unpinned-third-party-action"}), + ("other_workflow", {"unpinned-third-party-action"}), + ("broken", {"invalid-actions-lockfile", "unpinned-third-party-action"}), + ("unsupported", {"invalid-actions-lockfile", "unpinned-third-party-action"}), + ("permissions", {"overbroad-permissions"}), + ], +) +def test_lockfile_check_integration( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], + mode: str, + expected_types: set[str], +) -> None: + """Real parsed workflows retain security findings while covered refs lose false pinning findings.""" + from tests.parsers.test_github_actions_lockfile import write_lockfile # pylint: disable=import-outside-toplevel + + workflow_dir = tmp_path / ".github/workflows" + workflow_dir.mkdir(parents=True) + source = workflow_dir / "ci.yml" + permissions = "write-all" if mode == "permissions" else "{}" + source.write_text( + f"""on: push +permissions: {permissions} +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: Example/Parent/sub@v1 + - uses: $/actions/local + - uses: ./actions/local + - uses: docker://alpine:latest +""", + encoding="utf-8", + ) + path = write_lockfile(tmp_path) + data = json.loads(path.read_text(encoding="utf-8")) + if mode == "other_workflow": + data["workflows"] = {".github/workflows/other.yml": ["example/parent@v1"]} + if mode == "broken": + del data["dependencies"]["example/child@Main"] + if mode == "unsupported": + data["version"] = "v99.0.0" + if mode != "absent": + (workflow_dir / "actions.lock").write_text(json.dumps(data), encoding="utf-8") + ctx = MockAnalyzeContext(fs_path=str(tmp_path), output_dir="") + ctx.dynamic_data["ci_services"] = [get_ci_info(ci_services, "github_actions", str(source))] + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name_batch", lambda _: []) + monkeypatch.setattr(ci_services["github_actions"].api_client, "get_commit_sha_from_ref", lambda *_: None) + monkeypatch.chdir(tmp_path.parent) + result = GitHubActionsVulnsCheck().run_check(ctx) + finding_types: set[str] = set() + for fact in result.result_tables: + assert isinstance(fact, GitHubActionsVulnsFacts) + finding_types.add(fact.finding_type) + assert finding_types == expected_types + assert result.result_type == (CheckResultType.FAILED if expected_types else CheckResultType.PASSED) + + +def test_locked_vulnerability_uses_effective_commit( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], +) -> None: + """A safe-looking tag cannot hide a vulnerable locked commit or recorded child.""" + from tests.parsers.test_github_actions_lockfile import write_lockfile # pylint: disable=import-outside-toplevel + + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + source = directory / "ci.yml" + source.write_text( + """on: push +permissions: {} +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: example/parent@v1 +""", + encoding="utf-8", + ) + path = write_lockfile(tmp_path) + (directory / "actions.lock").write_bytes(path.read_bytes()) + ctx = MockAnalyzeContext(fs_path=str(tmp_path), output_dir="") + ctx.dynamic_data["ci_services"] = [get_ci_info(ci_services, "github_actions", str(source))] + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name_batch", lambda packages: packages) + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name", lambda **_: [{"id": "TEST-1"}]) + observed: dict[str, str] = {} + + def affected(vulnerability: dict[str, str], name: str, version: str, ecosystem: str, *, source_repo: str) -> bool: + """Stand in for external OSV matching while preserving the exact version contract.""" + assert vulnerability["id"] == "TEST-1" + assert ecosystem == "GitHub Actions" + assert source_repo == f"https://github.com/{name}" + observed[name] = version + return version == "b" * 40 + + monkeypatch.setattr(OSVDevService, "is_version_affected", affected) + monkeypatch.setattr(ci_services["github_actions"].api_client, "get_commit_sha_from_ref", lambda *_: None) + result = GitHubActionsVulnsCheck().run_check(ctx) + assert observed == {"example/parent": "a" * 40, "example/child": "b" * 40} + assert result.result_type == CheckResultType.FAILED + assert len(result.result_tables) == 1 + fact = result.result_tables[0] + assert isinstance(fact, GitHubActionsVulnsFacts) + assert fact.finding_type == "known-vulnerability" + assert fact.action_ref == "Main" + assert fact.resolved_commit == "b" * 40 + assert fact.pinning_method == "lockfile" + assert fact.sha_pinned is False + assert fact.dependency_chain == ["example/parent@v1", "example/child@Main"] + assert "resolved_commit: " + "b" * 40 in str(result.justification_report) + + +@pytest.mark.parametrize( + ("reference", "unpinned"), + [ + ("example/parent/.github/workflows/build.yml@v1", True), + ("example/parent/.github/workflows/build.yml@" + "a" * 40, False), + ], +) +def test_parsed_reusable_workflow_boundary( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], + reference: str, + unpinned: bool, +) -> None: + """Remote job-level uses must remain SHA-pinned even if the caller locks that repository.""" + from tests.parsers.test_github_actions_lockfile import write_lockfile # pylint: disable=import-outside-toplevel + + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + source = directory / "ci.yml" + source.write_text(f"on: push\npermissions: {{}}\njobs:\n call:\n uses: {reference}\n", encoding="utf-8") + path = write_lockfile(tmp_path) + (directory / "actions.lock").write_bytes(path.read_bytes()) + ctx = MockAnalyzeContext(fs_path=str(tmp_path)) + ctx.dynamic_data["ci_services"] = [get_ci_info(ci_services, "github_actions", str(source))] + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name_batch", lambda _: []) + monkeypatch.setattr(ci_services["github_actions"].api_client, "get_commit_sha_from_ref", lambda *_: None) + result = GitHubActionsVulnsCheck().run_check(ctx) + finding_types: set[str] = set() + for fact in result.result_tables: + assert isinstance(fact, GitHubActionsVulnsFacts) + finding_types.add(fact.finding_type) + assert ("unpinned-third-party-action" in finding_types) is unpinned + + +def test_adopted_workflow_cannot_be_silently_skipped( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], +) -> None: + """A parser-skipped adopted workflow must not yield an empty successful analysis.""" + from tests.parsers.test_github_actions_lockfile import write_lockfile # pylint: disable=import-outside-toplevel + + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + source = directory / "ci.yml" + source.write_text("invalid workflow", encoding="utf-8") + path = write_lockfile(tmp_path) + (directory / "actions.lock").write_bytes(path.read_bytes()) + ci_info = get_ci_info(ci_services, "jenkins", str(source)) + ci_info["service"] = ci_services["github_actions"] + ctx = MockAnalyzeContext(fs_path=str(tmp_path)) + ctx.dynamic_data["ci_services"] = [ci_info] + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name_batch", lambda _: []) + result = GitHubActionsVulnsCheck().run_check(ctx) + assert result.result_type == CheckResultType.FAILED + assert len(result.result_tables) == 1 + fact = result.result_tables[0] + assert isinstance(fact, GitHubActionsVulnsFacts) + assert fact.finding_type == "invalid-actions-lockfile" + + +@pytest.mark.parametrize("mode", ["missing_pin", "unsupported_version"]) +def test_invalid_workflow_preserves_other_vulnerability_analysis( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], + mode: str, +) -> None: + """A broken adopted workflow cannot erase findings for an independently pinned workflow.""" + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + roots = [] + for name, reference in (("good", "example/good@" + "d" * 40), ("bad", "example/bad@v1")): + source = directory / f"{name}.yml" + source.write_text( + f"on: push\npermissions: {{}}\njobs:\n build:\n runs-on: ubuntu-latest\n" + f" steps:\n - uses: {reference}\n", + encoding="utf-8", + ) + roots.extend(get_ci_info(ci_services, "github_actions", str(source))["callgraph"].root_nodes) + data: dict[str, JsonType] = { + "version": "v99.0.0" if mode == "unsupported_version" else "v0.0.3", + "workflows": {".github/workflows/bad.yml": []}, + "dependencies": {}, + } + (directory / "actions.lock").write_text(json.dumps(data), encoding="utf-8") + ctx = MockAnalyzeContext(fs_path=str(tmp_path)) + info = get_ci_info(ci_services, "jenkins", "") + info["service"], info["callgraph"] = ci_services["github_actions"], NodeForest(roots) + ctx.dynamic_data["ci_services"] = [info] + vulnerability = { + "id": "TEST-1", + "affected": [ + { + "package": {"name": "example/good", "ecosystem": "GitHub Actions"}, + "ranges": [{"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"fixed": "2.0.0"}]}], + } + ], + } + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name_batch", lambda packages: packages) + monkeypatch.setattr(OSVDevService, "get_vulnerabilities_package_name", lambda **_: [vulnerability]) + monkeypatch.setattr( + "macaron.slsa_analyzer.package_registry.osv_dev.get_tags_via_git_remote", lambda _: {"v1.0.0": "d" * 40} + ) + monkeypatch.setattr(ci_services["github_actions"].api_client, "get_commit_sha_from_ref", lambda *_: None) + result = GitHubActionsVulnsCheck().run_check(ctx) + findings = [] + for fact in result.result_tables: + assert isinstance(fact, GitHubActionsVulnsFacts) + findings.append((fact.finding_type, fact.action_name)) + assert ("known-vulnerability", "example/good") in findings + assert ("invalid-actions-lockfile", None) in findings + + +@pytest.mark.parametrize("dependency", ["example/parent", "example/child"]) +@pytest.mark.parametrize("tags", [{}, {"v1.0.0": "c" * 40}]) +def test_untagged_locked_commit_does_not_pass_silently( + httpserver: HTTPServer, + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + ci_services: dict[str, BaseCIService], + dependency: str, + tags: dict[str, str], +) -> None: + """Real OSV matching errors become explicit uncertainty for direct and transitive locked code.""" + from tests.parsers.test_github_actions_lockfile import write_lockfile # pylint: disable=import-outside-toplevel + + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + source = directory / "ci.yml" + source.write_text( + "on: push\npermissions: {}\njobs:\n build:\n runs-on: ubuntu-latest\n" + " steps:\n - uses: example/parent@v1\n", + encoding="utf-8", + ) + path = write_lockfile(tmp_path) + (directory / "actions.lock").write_bytes(path.read_bytes()) + config = tmp_path / "osv.ini" + server = urllib.parse.urlparse(httpserver.url_for("")) + config.write_text(f"[osv_dev]\nurl_scheme = {server.scheme}\nurl_netloc = {server.netloc}\n", encoding="utf-8") + load_defaults(str(config)) + packages = [ + {"package": {"name": name, "ecosystem": "GitHub Actions"}} for name in ("example/child", "example/parent") + ] + results = [ + {"vulns": [{"id": "TEST-1"}]} if package["package"]["name"] == dependency else {} for package in packages + ] + httpserver.expect_request("/v1/querybatch", method="POST", json={"queries": packages}).respond_with_json( + {"results": results} + ) + vulnerability = { + "id": "TEST-1", + "affected": [ + { + "package": {"name": dependency, "ecosystem": "GitHub Actions"}, + "ranges": [{"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"fixed": "2.0.0"}]}], + } + ], + } + httpserver.expect_request( + "/v1/query", method="POST", json={"package": {"name": dependency, "ecosystem": "GitHub Actions"}} + ).respond_with_json({"vulns": [vulnerability]}) + monkeypatch.setattr("macaron.slsa_analyzer.package_registry.osv_dev.get_tags_via_git_remote", lambda _: tags) + ctx = MockAnalyzeContext(fs_path=str(tmp_path)) + ctx.dynamic_data["ci_services"] = [get_ci_info(ci_services, "github_actions", str(source))] + result = GitHubActionsVulnsCheck().run_check(ctx) + assert result.result_type == CheckResultType.FAILED + assert len(result.result_tables) == 1 + fact = result.result_tables[0] + assert isinstance(fact, GitHubActionsVulnsFacts) + assert fact.finding_type == "vulnerability-analysis-incomplete" + assert fact.action_name == dependency + assert fact.resolved_commit == ("a" * 40 if dependency == "example/parent" else "b" * 40) + + +@pytest.mark.parametrize("subpaths", [("b", "a"), ("a", "b")]) +def test_subaction_evidence_has_deterministic_attribution( + tmp_path: Path, + ci_services: dict[str, BaseCIService], + subpaths: tuple[str, str], +) -> None: + """Multiple subactions share OSV identity without traversal-order-dependent evidence.""" + from tests.parsers.test_github_actions_lockfile import write_lockfile # pylint: disable=import-outside-toplevel + + directory = tmp_path / ".github/workflows" + directory.mkdir(parents=True) + source = directory / "ci.yml" + steps = "".join(f" - uses: example/parent/{subpath}@v1\n" for subpath in subpaths) + source.write_text( + "on: push\npermissions: {}\njobs:\n build:\n runs-on: ubuntu-latest\n steps:\n" + steps, encoding="utf-8" + ) + lockfile = parse_actions_lockfile(write_lockfile(tmp_path)) + callgraph = get_ci_info(ci_services, "github_actions", str(source))["callgraph"] + analysis = collect_resolved_workflow_actions(callgraph, tmp_path, lockfile) + assert not analysis.errors + assert [action.chain for action in analysis.actions if action.name == "example/parent"] == [ + ("example/parent/a@v1",) + ] diff --git a/tests/tutorial_resources/detect_vulnerable_github_actions/assert_lockfile_regression.py b/tests/tutorial_resources/detect_vulnerable_github_actions/assert_lockfile_regression.py new file mode 100644 index 000000000..fbf4ae87d --- /dev/null +++ b/tests/tutorial_resources/detect_vulnerable_github_actions/assert_lockfile_regression.py @@ -0,0 +1,86 @@ +# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. + +"""Assert the pinned GraalVM lockfile regression from actual composite-action outputs.""" + +import json +import sqlite3 +import sys +from contextlib import closing +from pathlib import Path + +TARGET_COMMIT = "35ad4c011e7cfc84f7370397221f82823c74ce35" +COVERED_REFS = { + "check-dist.yml": { + ("actions/checkout", "v7.0.1"), + ("actions/setup-node", "v7.1.0"), + ("actions/upload-artifact", "v7.0.2"), + }, + "ci.yml": {("actions/checkout", "v7.0.1"), ("actions/setup-node", "v7.1.0")}, + "macaron-check-github-actions.yml": {("actions/checkout", "v7.0.1"), ("oracle/macaron", "v0.25.0")}, + "update-dependabot-dist.yml": {("actions/checkout", "v7.0.1"), ("actions/download-artifact", "v8.0.2")}, + "verify-lockfile.yml": {("actions/checkout", "v7.0.1")}, +} + + +def assert_lockfile_regression(output_dir: Path, action_outcome: str) -> None: + """Fail on missing analysis, false pinning findings, or inconsistent policy/action outcomes. + + Assertions deliberately allow unrelated security findings: this test checks + lockfile recognition, not the future vulnerability status of a real project. + SQLite is opened read-only so a missing database cannot become an empty one. + """ + database = output_dir / "macaron.db" + assert database.is_file(), "The action did not produce its analysis database" + policy_path = output_dir / "policy_report.json" + assert policy_path.is_file(), "The action did not produce its policy report" + report: object = json.loads(policy_path.read_text(encoding="utf-8")) + assert isinstance(report, dict), "Policy report must be a JSON object" + with closing(sqlite3.connect(database.as_uri() + "?mode=ro", uri=True)) as connection: + targets = connection.execute(""" + SELECT component.id, component.purl, repository.commit_sha + FROM component JOIN repository ON repository.component_id = component.id + WHERE repository.full_name = 'graalvm/setup-graalvm' + """).fetchall() + assert len(targets) == 1, f"Expected one analyzed GraalVM target, found {targets!r}" + component_id, purl, commit = targets[0] + assert commit == TARGET_COMMIT, f"Analyzed unexpected commit {commit}" + results = connection.execute( + """ + SELECT id, passed FROM check_result + WHERE component_id = ? AND check_id = 'mcn_githubactions_vulnerabilities_1' + """, + (component_id,), + ).fetchall() + assert len(results) == 1, "GitHub Actions check was missing or duplicated" + check_id, passed = results[0] + facts = connection.execute( + """ + SELECT gha.finding_type, gha.action_name, gha.action_ref, gha.caller_workflow + FROM github_actions_vulnerabilities_check AS gha + JOIN check_facts ON check_facts.id = gha.id + WHERE check_facts.component_id = ? AND check_facts.check_result_id = ? + """, + (component_id, check_id), + ).fetchall() + assert bool(passed) == (not facts), "Check was skipped, failed without diagnostics, or disagrees with its facts" + for finding_type, action_name, action_ref, caller in facts: + assert finding_type != "invalid-actions-lockfile", f"Supported target lockfile was rejected: {caller}" + if finding_type == "unpinned-third-party-action": + workflow = str(caller).split("#", 1)[0].rsplit("/", 1)[-1] + assert (action_name, action_ref) not in COVERED_REFS.get(workflow, set()), ( + f"Lockfile-covered call was incorrectly flagged: {caller}: {action_name}@{action_ref}" + ) + policy_relation = "component_satisfies_policy" if passed else "component_violates_policy" + assert [str(component_id), purl, "github_actions_vulns"] in report.get(policy_relation, []), ( + "Policy did not apply to the target or disagrees with the check" + ) + assert report.get("passed_policies") == ([["github_actions_vulns"]] if passed else []) + assert report.get("failed_policies") == ([] if passed else [["github_actions_vulns"]]) + assert action_outcome == ("success" if passed else "failure"), "Action outcome disagrees with policy" + assert (output_dir / "vsa.intoto.jsonl").is_file() == bool(passed), "VSA generation disagrees with policy" + + +if __name__ == "__main__": + assert len(sys.argv) == 3, "Usage: assert_lockfile_regression.py OUTPUT_DIR ACTION_OUTCOME" + assert_lockfile_regression(Path(sys.argv[1]).resolve(), sys.argv[2]) From f6729831d036248a8e7d7b3bfcd5e56c83251fc7 Mon Sep 17 00:00:00 2001 From: Behnaz Hassanshahi Date: Sat, 14 Nov 2026 14:31:15 +1000 Subject: [PATCH 11/11] =?UTF-8?q?chore(deps):=20ignore=20unreachable=20Ope?= =?UTF-8?q?nTelemetry=20audit=20advi=E2=80=A6=20(#1493)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: behnazh-w --- Makefile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index ade3272b7..029787573 100644 --- a/Makefile +++ b/Makefile @@ -303,11 +303,12 @@ requirements.txt: pyproject.toml # # Semgrep 1.185.0 pins mcp to 1.29.0 and restricts opentelemetry-api to 1.37.x. # The four MCP advisories below affect MCP server/client functionality. Macaron -# invokes `semgrep scan` and does not use that functionality. The OpenTelemetry -# advisory affects parsing attacker-controlled tracestate headers; Semgrep's scan -# telemetry extracts only traceparent from its environment variables. +# invokes `semgrep scan` and does not use that functionality. OpenTelemetry +# GHSA-xcq7-p3pf-p88h affects tracestate parsing; GHSA-mf5v-w3w3-f6cg affects +# traceparent parsing. Semgrep's scan telemetry constructs only traceparent from +# process environment variables, not scanned files or untrusted request metadata. # Temporarily ignore these advisories until Semgrep allows mcp >=1.30.0 and -# opentelemetry-api >=1.45.1. Reassess these exceptions if Macaron's usage changes. +# opentelemetry-api >=1.45.2. Reassess these exceptions if Macaron's usage changes. .PHONY: audit audit: @@ -317,7 +318,7 @@ audit: python -m pip_audit --skip-editable --desc on --fix --dry-run \ --ignore-vuln GHSA-84m7-p3x7-pcfv --ignore-vuln GHSA-5h93-6whr-6q8j \ --ignore-vuln GHSA-w4fh-qvv9-3v23 --ignore-vuln GHSA-rwrf-2pqf-9j8j \ - --ignore-vuln GHSA-xcq7-p3pf-p88h + --ignore-vuln GHSA-xcq7-p3pf-p88h --ignore-vuln GHSA-mf5v-w3w3-f6cg # Run some or all checks over the package code base. .PHONY: check check-code check-ruff check-lint check-mypy check-go check-actionlint