Skip to content

Commit 3faf5b0

Browse files
feature: semgrep
1 parent 9a3ae32 commit 3faf5b0

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ tools:
66
77
88
9+

cmd/analyze.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile s
226226
}
227227
}
228228

229+
func runSemgrepAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
230+
semgrep := config.Config.Tools()["semgrep"]
231+
if semgrep == nil {
232+
log.Fatal("Semgrep tool configuration not found")
233+
}
234+
235+
err := tools.RunSemgrep(workDirectory, semgrep, pathsToCheck, outputFile, outputFormat)
236+
if err != nil {
237+
log.Fatalf("Failed to run Semgrep analysis: %v", err)
238+
}
239+
}
240+
229241
var analyzeCmd = &cobra.Command{
230242
Use: "analyze",
231243
Short: "Runs all configured linters.",
@@ -312,6 +324,8 @@ func runTool(workDirectory string, toolName string, args []string, outputFile st
312324
runPmdAnalysis(workDirectory, args, outputFile, outputFormat)
313325
case "pylint":
314326
runPylintAnalysis(workDirectory, args, outputFile, outputFormat)
327+
case "semgrep":
328+
runSemgrepAnalysis(workDirectory, args, outputFile, outputFormat)
315329
default:
316330
log.Printf("Warning: Unsupported tool: %s\n", toolName)
317331
}

plugins/tools/semgrep/plugin.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: semgrep
2+
description: Static Analysis Security Testing (SAST) tool
3+
runtime: python
4+
runtime_binaries:
5+
package_manager: python3
6+
execution: python3
7+
binaries:
8+
- name: python
9+
path: "venv/bin/python3"
10+
formatters:
11+
- name: json
12+
flag: "--json"
13+
output_options:
14+
file_flag: "--output"
15+
analysis_options:
16+
default_path: "."

tools/semgrepRunner.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package tools
2+
3+
import (
4+
"codacy/cli-v2/plugins"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
)
10+
11+
// RunSemgrep executes Semgrep analysis on the specified directory
12+
func RunSemgrep(workDirectory string, toolInfo *plugins.ToolInfo, files []string, outputFile string, outputFormat string) error {
13+
// Get Python binary from venv
14+
pythonPath := filepath.Join(toolInfo.InstallDir, "venv", "bin", "python3")
15+
16+
// Construct base command with -m semgrep to run semgrep module
17+
cmdArgs := []string{"-m", "semgrep", "scan"}
18+
19+
// Add output format if specified
20+
if outputFormat == "sarif" {
21+
cmdArgs = append(cmdArgs, "--sarif")
22+
}
23+
24+
// add --config auto
25+
cmdArgs = append(cmdArgs, "--config", "auto")
26+
27+
// Add files to analyze - if no files specified, analyze current directory
28+
if len(files) > 0 {
29+
cmdArgs = append(cmdArgs, files...)
30+
} else {
31+
cmdArgs = append(cmdArgs, ".")
32+
}
33+
34+
// Create Semgrep command
35+
cmd := exec.Command(pythonPath, cmdArgs...)
36+
cmd.Dir = workDirectory
37+
38+
// If output file is specified, create it and redirect output
39+
var outputWriter *os.File
40+
var err error
41+
if outputFile != "" {
42+
outputWriter, err = os.Create(filepath.Clean(outputFile))
43+
if err != nil {
44+
return fmt.Errorf("failed to create output file: %w", err)
45+
}
46+
defer outputWriter.Close()
47+
cmd.Stdout = outputWriter
48+
} else {
49+
cmd.Stdout = os.Stdout
50+
}
51+
cmd.Stderr = os.Stderr
52+
53+
// Run Semgrep
54+
if err := cmd.Run(); err != nil {
55+
// Semgrep returns non-zero exit code when it finds issues, which is expected
56+
if _, ok := err.(*exec.ExitError); !ok {
57+
return fmt.Errorf("failed to run semgrep: %w", err)
58+
}
59+
}
60+
61+
return nil
62+
}

0 commit comments

Comments
 (0)