Skip to content

Commit

Permalink
🐛 Fix some producers failing if unable to extract code
Browse files Browse the repository at this point in the history
  • Loading branch information
flowirtz committed Jun 7, 2024
1 parent db05659 commit 729cff1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
7 changes: 6 additions & 1 deletion components/producers/golang-gosec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"

v1 "github.com/ocurity/dracon/api/proto/v1"
"github.com/ocurity/dracon/pkg/context"
Expand Down Expand Up @@ -50,11 +51,15 @@ func parseIssues(out *GoSecOut) ([]*v1.Issue, error) {
Confidence: v1.Confidence(v1.Confidence_value[fmt.Sprintf("CONFIDENCE_%s", r.Confidence)]),
Description: r.Code,
}

// Extract the code snippet, if possible
code, err := context.ExtractCode(iss)
if err != nil {
return nil, err
slog.Warn("Failed to extract code snippet", "error", err)
code = ""
}
iss.ContextSegment = &code

issues = append(issues, iss)
}
return issues, nil
Expand Down
12 changes: 8 additions & 4 deletions components/producers/kics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"log"
"log/slog"

v1 "github.com/ocurity/dracon/api/proto/v1"
"github.com/ocurity/dracon/components/producers"
Expand Down Expand Up @@ -80,13 +81,16 @@ func parseOut(results types.KICSOut) ([]*v1.Issue, error) {
file.ResourceName),
Description: string(description),
}
cs, err := context.ExtractCode(iss)

// Extract the code snippet, if possible
code, err := context.ExtractCode(iss)
if err != nil {
return nil, err
slog.Warn("Failed to extract code snippet", "error", err)
code = ""
}
iss.ContextSegment = &cs
iss.ContextSegment = &code

issues = append(issues, iss)

}
}
return issues, nil
Expand Down
13 changes: 9 additions & 4 deletions components/producers/python-bandit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"
"strings"

v1 "github.com/ocurity/dracon/api/proto/v1"
Expand Down Expand Up @@ -50,7 +51,7 @@ func parseResult(r *BanditResult) (*v1.Issue, error) {
for _, r := range r.LineRange {
rng = append(rng, fmt.Sprintf("%d", r))
}
iss := v1.Issue{
iss := &v1.Issue{
Target: fmt.Sprintf("%s:%s", r.Filename, strings.Join(rng, "-")),
Type: r.TestID,
Title: r.TestName,
Expand All @@ -59,12 +60,16 @@ func parseResult(r *BanditResult) (*v1.Issue, error) {
Confidence: v1.Confidence(v1.Confidence_value[fmt.Sprintf("CONFIDENCE_%s", r.IssueConfidence)]),
Description: fmt.Sprintf("%s\ncode:%s", r.IssueText, r.Code),
}
code, err := context.ExtractCode(&iss) // Bandit only extracts a small code sample, we think it's better to have more

// Extract the code snippet, if possible
code, err := context.ExtractCode(iss)
if err != nil {
return nil, err
slog.Warn("Failed to extract code snippet", "error", err)
code = ""
}
iss.ContextSegment = &code
return &iss, nil

return iss, nil
}

// BanditOut represents the output of a bandit run.
Expand Down
11 changes: 8 additions & 3 deletions components/producers/semgrep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"

v1 "github.com/ocurity/dracon/api/proto/v1"
"github.com/ocurity/dracon/components/producers/semgrep/types"
Expand Down Expand Up @@ -63,11 +64,15 @@ func parseIssues(out types.SemgrepResults) ([]*v1.Issue, error) {
Confidence: v1.Confidence_CONFIDENCE_MEDIUM,
Description: fmt.Sprintf("%s\n extra lines: %s", r.Extra.Message, r.Extra.Lines),
}
cs, err := context.ExtractCode(iss)

// Extract the code snippet, if possible
code, err := context.ExtractCode(iss)
if err != nil {
return nil, err
slog.Warn("Failed to extract code snippet", "error", err)
code = ""
}
iss.ContextSegment = &cs
iss.ContextSegment = &code

issues = append(issues, iss)
}
return issues, nil
Expand Down
11 changes: 8 additions & 3 deletions components/producers/terraform-tfsec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"log"
"log/slog"

v1 "github.com/ocurity/dracon/api/proto/v1"
"github.com/ocurity/dracon/components/producers"
Expand Down Expand Up @@ -75,11 +76,15 @@ func parseOut(results types.TfSecOut) ([]*v1.Issue, error) {
Confidence: v1.Confidence_CONFIDENCE_MEDIUM,
Description: string(description),
}
cs, err := context.ExtractCode(iss)

// Extract the code snippet, if possible
code, err := context.ExtractCode(iss)
if err != nil {
return nil, err
slog.Warn("Failed to extract code snippet", "error", err)
code = ""
}
iss.ContextSegment = &cs
iss.ContextSegment = &code

issues = append(issues, iss)
}
return issues, nil
Expand Down
11 changes: 8 additions & 3 deletions components/producers/typescript-eslint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"

v1 "github.com/ocurity/dracon/api/proto/v1"
"github.com/ocurity/dracon/components/producers/typescript-eslint/types"
Expand Down Expand Up @@ -61,11 +62,15 @@ func parseIssues(out []types.ESLintIssue) ([]*v1.Issue, error) {
Confidence: v1.Confidence_CONFIDENCE_MEDIUM,
Description: msg.Message,
}
cs, err := context.ExtractCode(iss)

// Extract the code snippet, if possible
code, err := context.ExtractCode(iss)
if err != nil {
return nil, err
slog.Warn("Failed to extract code snippet", "error", err)
code = ""
}
iss.ContextSegment = &cs
iss.ContextSegment = &code

issues = append(issues, iss)
}
}
Expand Down

0 comments on commit 729cff1

Please sign in to comment.