Skip to content

Commit c29ef51

Browse files
fix: semgrep sarif to not put all rules in output (#120)
- Updated logging in dartanalyzerRunner.go and enigmaRunner.go to use log.Println instead of fmt.Println for better log management. - Enhanced semgrepRunner.go to include SARIF output processing, filtering rule definitions, and writing filtered results to specified output files.
1 parent a7a158c commit c29ef51

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

tools/dartanalyzerRunner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8+
"log"
89
"os"
910
"os/exec"
1011
"path/filepath"
@@ -41,9 +42,9 @@ func RunDartAnalyzer(workDirectory string, installationDirectory string, binary
4142
}
4243

4344
if !configExists {
44-
fmt.Println("No config file found, using tool defaults")
45+
log.Println("No config file found, using tool defaults")
4546
} else {
46-
fmt.Println("Config file found, using it")
47+
log.Println("Config file found, using it")
4748
}
4849

4950
// For SARIF output, we need to capture the output and transform it

tools/enigmaRunner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tools
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -32,10 +33,10 @@ func RunEnigma(workDirectory string, installationDirectory string, binary string
3233
}
3334

3435
if configExists != "" {
35-
println("Config file found, using it")
36+
log.Println("Config file found, using it")
3637
args = append(args, "--configuration-file", configExists)
3738
} else {
38-
println("No config file found, using tool defaults")
39+
log.Println("No config file found, using tool defaults")
3940

4041
}
4142

utils/sarif.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,14 @@ func MergeSarifOutputs(inputFiles []string, outputFile string) error {
204204
return fmt.Errorf("failed to read SARIF file %s: %w", file, err)
205205
}
206206

207+
// Filter out rule definitions from each input file
208+
filteredData, err := FilterRuleDefinitions(data)
209+
if err != nil {
210+
return fmt.Errorf("failed to filter rules from SARIF file %s: %w", file, err)
211+
}
212+
207213
var sarif SimpleSarifReport
208-
if err := json.Unmarshal(data, &sarif); err != nil {
214+
if err := json.Unmarshal(filteredData, &sarif); err != nil {
209215
return fmt.Errorf("failed to parse SARIF file %s: %w", file, err)
210216
}
211217

@@ -227,3 +233,19 @@ func MergeSarifOutputs(inputFiles []string, outputFile string) error {
227233

228234
return nil
229235
}
236+
237+
// FilterRuleDefinitions removes rule definitions from SARIF output
238+
func FilterRuleDefinitions(sarifData []byte) ([]byte, error) {
239+
var report SarifReport
240+
if err := json.Unmarshal(sarifData, &report); err != nil {
241+
return nil, fmt.Errorf("failed to parse SARIF data: %w", err)
242+
}
243+
244+
// Remove rules from each run
245+
for i := range report.Runs {
246+
report.Runs[i].Tool.Driver.Rules = nil
247+
}
248+
249+
// Marshal back to JSON with indentation
250+
return json.MarshalIndent(report, "", " ")
251+
}

0 commit comments

Comments
 (0)