diff --git a/plugins/semgrep_scanner/metadata.json b/plugins/semgrep_scanner/metadata.json index 8c0b8245b..ec5788b2f 100644 --- a/plugins/semgrep_scanner/metadata.json +++ b/plugins/semgrep_scanner/metadata.json @@ -74,5 +74,5 @@ "system_packages": [] }, "docker_image": "returntocorp/semgrep:latest", - "checksum": "8e59254f5ad937c0a5a2af47367cddf07e07b8a528d13ff303360d6e478a51d8" + "checksum": "2639e4aa47d346d9ab7caa64d6fe447203f8a6be5a0a354055eebebe9e674ae6" } diff --git a/plugins/semgrep_scanner/parser.py b/plugins/semgrep_scanner/parser.py index 26ab84120..08bf91d21 100644 --- a/plugins/semgrep_scanner/parser.py +++ b/plugins/semgrep_scanner/parser.py @@ -48,6 +48,7 @@ def parse(output: str) -> Dict[str, Any]: } ) except Exception: - pass + # If parsing fails, return empty findings + return {"findings": [], "count": 0} return {"findings": findings, "count": len(findings)} diff --git a/testing/test_semgrep_parser.py b/testing/test_semgrep_parser.py new file mode 100644 index 000000000..28ee1d6b9 --- /dev/null +++ b/testing/test_semgrep_parser.py @@ -0,0 +1,43 @@ +import pytest +import json +import sys +import os + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) + +from plugins.semgrep_scanner.parser import parse + +# Test 1: Valid JSON input +def test_valid_json(): + valid_input = json.dumps({ + "results": [ + { + "check_id": "python.security.test-rule", + "path": "app.py", + "extra": { + "message": "Test security issue", + "severity": "WARNING", + "lines": "eval(user_input)" + }, + "start": {"line": 10} + } + ] + }) + result = parse(valid_input) + assert result["count"] == 1 + assert result["findings"][0]["severity"] == "medium" + assert result["findings"][0]["category"] == "Code Security" + +# Test 2: Invalid JSON input +def test_invalid_json(): + invalid_input = "this is not json {{{broken" + result = parse(invalid_input) + assert result["count"] == 0 + assert result["findings"] == [] + +# Test 3: Mixed stdout (JSON mixed with other text) +def test_mixed_stdout(): + mixed_input = "Some random text\n{invalid json here}\nmore text" + result = parse(mixed_input) + assert result["count"] == 0 + assert result["findings"] == [] \ No newline at end of file