Skip to content

Commit 6312b0c

Browse files
committed
refactor: run source code analysis by default and remove cli option
Signed-off-by: Carl Flottmann <[email protected]>
1 parent 35dd417 commit 6312b0c

File tree

6 files changed

+24
-52
lines changed

6 files changed

+24
-52
lines changed

src/macaron/__main__.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ def analyze_slsa_levels_single(analyzer_single_args: argparse.Namespace) -> None
9696

9797
global_config.local_maven_repo = user_provided_local_maven_repo
9898

99-
if analyzer_single_args.force_analyze_source and not analyzer_single_args.analyze_source:
100-
logger.error("'--force-analyze-source' requires '--analyze-source'.")
101-
sys.exit(os.EX_USAGE)
102-
10399
analyzer = Analyzer(global_config.output_path, global_config.build_log_path)
104100

105101
# Initiate reporters.
@@ -177,7 +173,6 @@ def analyze_slsa_levels_single(analyzer_single_args: argparse.Namespace) -> None
177173
deps_depth,
178174
provenance_payload=prov_payload,
179175
verify_provenance=analyzer_single_args.verify_provenance,
180-
analyze_source=analyzer_single_args.analyze_source,
181176
force_analyze_source=analyzer_single_args.force_analyze_source,
182177
)
183178
sys.exit(status_code)
@@ -481,23 +476,11 @@ def main(argv: list[str] | None = None) -> None:
481476
),
482477
)
483478

484-
single_analyze_parser.add_argument(
485-
"--analyze-source",
486-
required=False,
487-
action="store_true",
488-
help=(
489-
"For improved malware detection, analyze the source code of the"
490-
+ " (PyPI) package using a textual scan and dataflow analysis."
491-
),
492-
)
493-
494479
single_analyze_parser.add_argument(
495480
"--force-analyze-source",
496481
required=False,
497482
action="store_true",
498-
help=(
499-
"Forces PyPI sourcecode analysis to run regardless of other heuristic results. Requires '--analyze-source'."
500-
),
483+
help=("Forces PyPI sourcecode analysis to run regardless of other heuristic results."),
501484
)
502485

503486
single_analyze_parser.add_argument(

src/macaron/slsa_analyzer/analyze_context.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class ChecksOutputs(TypedDict):
5151
"""The provenance and related information."""
5252
local_artifact_paths: list[str]
5353
"""The local artifact absolute paths."""
54-
analyze_source: bool
55-
"""True when PyPI source code analysis has been enabled."""
5654
force_analyze_source: bool
5755
"""When True, enforces running source code analysis, regardless of other heuristic results."""
5856

@@ -108,7 +106,6 @@ def __init__(
108106
expectation=None,
109107
provenance_info=None,
110108
local_artifact_paths=[],
111-
analyze_source=False,
112109
force_analyze_source=False,
113110
)
114111

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def run(
148148
deps_depth: int = 0,
149149
provenance_payload: InTotoPayload | None = None,
150150
verify_provenance: bool = False,
151-
analyze_source: bool = False,
152151
force_analyze_source: bool = False,
153152
) -> int:
154153
"""Run the analysis and write results to the output path.
@@ -168,8 +167,6 @@ def run(
168167
The provenance intoto payload for the main software component.
169168
verify_provenance: bool
170169
Enable provenance verification if True.
171-
analyze_source : bool
172-
When true, triggers source code analysis for PyPI packages. Defaults to False.
173170
force_analyze_source : bool
174171
When true, enforces running source code analysis regardless of other heuristic results. Defaults to False.
175172
@@ -205,7 +202,6 @@ def run(
205202
analysis,
206203
provenance_payload=provenance_payload,
207204
verify_provenance=verify_provenance,
208-
analyze_source=analyze_source,
209205
force_analyze_source=force_analyze_source,
210206
)
211207

@@ -325,7 +321,6 @@ def run_single(
325321
existing_records: dict[str, Record] | None = None,
326322
provenance_payload: InTotoPayload | None = None,
327323
verify_provenance: bool = False,
328-
analyze_source: bool = False,
329324
force_analyze_source: bool = False,
330325
) -> Record:
331326
"""Run the checks for a single repository target.
@@ -345,8 +340,6 @@ def run_single(
345340
The provenance intoto payload for the analyzed software component.
346341
verify_provenance: bool
347342
Enable provenance verification if True.
348-
analyze_source : bool
349-
When true, triggers source code analysis for PyPI packages. Defaults to False.
350343
force_analyze_source : bool
351344
When true, enforces running source code analysis regardless of other heuristic results. Defaults to False.
352345
@@ -583,7 +576,6 @@ def run_single(
583576
# TODO Add release digest.
584577
)
585578

586-
analyze_ctx.dynamic_data["analyze_source"] = analyze_source
587579
analyze_ctx.dynamic_data["force_analyze_source"] = force_analyze_source
588580

589581
if local_artifact_dirs:

src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _should_skip(
107107

108108
def analyze_source(
109109
self, pypi_package_json: PyPIPackageJsonAsset, results: dict[Heuristics, HeuristicResult], force: bool = False
110-
) -> tuple[HeuristicResult, dict[str, JsonType]]:
110+
) -> tuple[dict[Heuristics, HeuristicResult], dict[str, JsonType]]:
111111
"""Analyze the source code of the package with a textual scan, looking for malicious code patterns.
112112
113113
Parameters
@@ -122,7 +122,7 @@ def analyze_source(
122122
123123
Returns
124124
-------
125-
tuple[HeuristicResult, dict[str, JsonType]]
125+
tuple[dict[Heuristics, HeuristicResult], dict[str, JsonType]]
126126
Containing the analysis results and relevant patterns identified.
127127
128128
Raises
@@ -136,11 +136,13 @@ def analyze_source(
136136
analyzer = PyPISourcecodeAnalyzer()
137137

138138
if not force and analyzer.depends_on and self._should_skip(results, analyzer.depends_on):
139-
return HeuristicResult.SKIP, {}
139+
return {analyzer.heuristic: HeuristicResult.SKIP}, {}
140140

141141
try:
142142
with pypi_package_json.sourcecode():
143-
return analyzer.analyze(pypi_package_json)
143+
result, detail_info = analyzer.analyze(pypi_package_json)
144+
return {analyzer.heuristic: result}, detail_info
145+
144146
except SourceCodeError as error:
145147
error_msg = f"Unable to perform analysis, source code not available: {error}"
146148
logger.debug(error_msg)
@@ -310,23 +312,22 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData:
310312
confidence = Confidence.HIGH
311313
result_type = CheckResultType.PASSED
312314

313-
# optional sourcecode analysis feature
314-
if ctx.dynamic_data["analyze_source"]:
315-
try:
316-
sourcecode_result, sourcecode_detail_info = self.analyze_source(
317-
pypi_package_json, heuristic_results, force=ctx.dynamic_data["force_analyze_source"]
318-
)
319-
except (HeuristicAnalyzerValueError, ConfigurationError):
320-
return CheckResultData(result_tables=[], result_type=CheckResultType.UNKNOWN)
321-
322-
heuristic_results[Heuristics.SUSPICIOUS_PATTERNS] = sourcecode_result
323-
heuristics_detail_info.update(sourcecode_detail_info)
324-
325-
if sourcecode_result == HeuristicResult.FAIL:
326-
if result_type == CheckResultType.PASSED:
327-
# heuristics determined it benign, so lower the confidence
328-
confidence = Confidence.LOW
329-
result_type = CheckResultType.FAILED
315+
# Source code analysis
316+
try:
317+
sourcecode_result, sourcecode_detail_info = self.analyze_source(
318+
pypi_package_json, heuristic_results, force=ctx.dynamic_data["force_analyze_source"]
319+
)
320+
except (HeuristicAnalyzerValueError, ConfigurationError):
321+
return CheckResultData(result_tables=[], result_type=CheckResultType.UNKNOWN)
322+
323+
heuristic_results.update(sourcecode_result)
324+
heuristics_detail_info.update(sourcecode_detail_info)
325+
326+
if sourcecode_result[Heuristics.SUSPICIOUS_PATTERNS] == HeuristicResult.FAIL:
327+
if result_type == CheckResultType.PASSED:
328+
# heuristics determined it benign, so lower the confidence
329+
confidence = Confidence.LOW
330+
result_type = CheckResultType.FAILED
330331

331332
result_tables.append(
332333
MaliciousMetadataFacts(

tests/integration/cases/django_with_dep_resolution_virtual_env_as_input/test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ steps:
7979
- pkg:pypi/[email protected]
8080
- --python-venv
8181
- ./django_venv
82-
- --analyze-source
8382
- --force-analyze-source
8483
- name: Run macaron verify-policy to check the package was not marked as malicious.
8584
kind: verify

tests/slsa_analyzer/checks/test_detect_malicious_metadata_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_detect_malicious_metadata(
5959
pypi_registry = PyPIRegistry()
6060
ctx.dynamic_data["package_registries"] = [PackageRegistryInfo("pip", "pypi", pypi_registry)]
6161
if sourcecode_analysis:
62-
ctx.dynamic_data["analyze_source"] = True
62+
ctx.dynamic_data["force_analyze_source"] = True
6363

6464
mock_global_config.resources_path = os.path.join(MACARON_PATH, "resources")
6565

0 commit comments

Comments
 (0)