diff --git a/plugins/semgrep_scanner/metadata.json b/plugins/semgrep_scanner/metadata.json index d0b4e028..64d58437 100644 --- a/plugins/semgrep_scanner/metadata.json +++ b/plugins/semgrep_scanner/metadata.json @@ -77,5 +77,5 @@ "capabilities": [ "filesystem" ], - "checksum": "9fdbb9bf1cab5ec0a7a80aabfd5a056a04050dfad04b0ff4b835f5d1e00fc050" + "checksum": "d5bfd6112a3790837dac9cda0bd1a55b00db084384f2a0441d26dd84c201d955" } diff --git a/plugins/semgrep_scanner/parser.py b/plugins/semgrep_scanner/parser.py index 26ab8412..08bf91d2 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 00000000..28ee1d6b --- /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