Skip to content

Commit

Permalink
feat: add line content to 2ms results (#249)
Browse files Browse the repository at this point in the history
To analyse the entire value of the line where the secret was detected,
2ms now takes the `Line` information from gitleaks.

**Checklist**

- [x] I covered my changes with tests.
- [ ] I Updated the documentation that is affected by my changes:
  - [ ] Change in the CLI arguments
  - [ ] Change in the configuration file
  • Loading branch information
cx-ruio authored Oct 3, 2024
1 parent cb8048a commit 3ad660a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (e *Engine) Detect(item plugins.ISourceItem, secretsChannel chan *secrets.S
EndLine: endLine,
EndColumn: value.EndColumn,
Value: value.Secret,
LineContent: value.Line,
}
if !isSecretIgnored(secret, &e.ignoredIds, &e.allowedValues) {
secretsChannel <- secret
Expand Down
10 changes: 5 additions & 5 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"fmt"
"github.com/stretchr/testify/assert"
"sync"
"testing"

Expand Down Expand Up @@ -159,11 +160,10 @@ func TestSecrets(t *testing.T) {

s := <-secretsChan

if s == nil && secret.ShouldFind {
t.Errorf("secret \"%s\" not found", secret.Name)
}
if s != nil && !secret.ShouldFind {
t.Errorf("should not find")
if secret.ShouldFind {
assert.Equal(t, s.LineContent, secret.Content)
} else {
assert.Nil(t, s)
}
})
}
Expand Down
8 changes: 6 additions & 2 deletions lib/reporting/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package reporting
import (
"encoding/json"
"fmt"

"github.com/checkmarx/2ms/lib/config"
"github.com/checkmarx/2ms/lib/secrets"
"strings"
)

func writeSarif(report Report, cfg *config.Config) (string, error) {
Expand Down Expand Up @@ -92,6 +92,9 @@ func getLocation(secret *secrets.Secret) []Locations {
EndColumn: secret.EndColumn,
Snippet: Snippet{
Text: secret.Value,
Properties: Properties{
"lineContent": strings.TrimSpace(secret.LineContent),
},
},
},
},
Expand Down Expand Up @@ -134,7 +137,8 @@ type Region struct {
}

type Snippet struct {
Text string `json:"text"`
Text string `json:"text"`
Properties Properties `json:"properties,omitempty"`
}

type PhysicalLocation struct {
Expand Down
1 change: 1 addition & 0 deletions lib/secrets/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Secret struct {
RuleID string `json:"ruleId"`
StartLine int `json:"startLine"`
EndLine int `json:"endLine"`
LineContent string `json:"lineContent"`
StartColumn int `json:"startColumn"`
EndColumn int `json:"endColumn"`
Value string `json:"value"`
Expand Down

0 comments on commit 3ad660a

Please sign in to comment.