Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line Numbers #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/pipeleak/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/signal"
"path"
"regexp"
"strconv"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -220,3 +221,11 @@ func IsDirectory(path string) bool {

return fileInfo.IsDir()
}

func LineNRToWebURL(webUrl string, lineNumber int) string {
if (lineNumber < 0) {
return webUrl
}

return webUrl + "#L" + strconv.Itoa(lineNumber)
}
2 changes: 1 addition & 1 deletion src/pipeleak/scanner/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func analyzeJobTrace(git *gitlab.Client, item QueueItem, options *ScanOptions) {

findings := DetectHits(trace, options.MaxScanGoRoutines, options.TruffleHogVerification)
for _, finding := range findings {
log.Warn().Str("confidence", finding.Pattern.Pattern.Confidence).Str("ruleName", finding.Pattern.Pattern.Name).Str("value", finding.Text).Str("url", item.Meta.JobWebUrl).Str("jobName", item.Meta.JobName).Msg("HIT")
log.Warn().Str("confidence", finding.Pattern.Pattern.Confidence).Str("ruleName", finding.Pattern.Pattern.Name).Str("value", finding.Text).Str("url", helper.LineNRToWebURL(item.Meta.JobWebUrl, finding.LineNumber)).Str("jobName", item.Meta.JobName).Msg("HIT")
}
}

Expand Down
30 changes: 28 additions & 2 deletions src/pipeleak/scanner/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type PatternPattern struct {
type Finding struct {
Pattern PatternElement
Text string
LineNumber int
}

// hold patterns in memory during runtime
Expand Down Expand Up @@ -152,6 +153,7 @@ func DetectHits(text []byte, maxThreads int, enableTruffleHogVerification bool)
hits := m.FindAllIndex(text, -1)

for _, hit := range hits {
lineNumber := countNewlinesUntilIndex(text, hit[0])
// truncate output to max 1024 chars for output readability
hitStr := extractHitWithSurroundingText(text, hit, 50)
hitStr = cleanHitLine(hitStr)
Expand All @@ -160,7 +162,7 @@ func DetectHits(text []byte, maxThreads int, enableTruffleHogVerification bool)
}

if hitStr != "" {
findingsYml = append(findingsYml, Finding{Pattern: pattern, Text: hitStr})
findingsYml = append(findingsYml, Finding{Pattern: pattern, Text: hitStr, LineNumber: lineNumber})
}
}

Expand Down Expand Up @@ -190,7 +192,9 @@ func DetectHits(text []byte, maxThreads int, enableTruffleHogVerification bool)
if len(result.RawV2) > 0 {
secret = result.RawV2
}
finding := Finding{Pattern: PatternElement{Pattern: PatternPattern{Name: result.DetectorType.String(), Confidence: "high-verified"}}, Text: string(secret)}
resultIndex := strings.Index(string(text), string(secret))
lineNumber := countNewlinesUntilIndex(text, resultIndex)
finding := Finding{Pattern: PatternElement{Pattern: PatternPattern{Name: result.DetectorType.String(), Confidence: "high-verified"}}, Text: string(secret), LineNumber: lineNumber}

// if trufflehog verification is enalbed ONLY verified rules are reported
if result.Verified {
Expand Down Expand Up @@ -277,3 +281,25 @@ func cleanHitLine(text string) string {
text = strings.ReplaceAll(text, "\n", " ")
return stripansi.Strip(text)
}

func countNewlinesUntilIndex(s []byte, index int) int {
if index > len(s) {
index = len(s)
}
if index == 0 {
return 0
}
if index < 0 {
return -1
}
substring := s[:index]


count := 0
for i := 0; i < len(substring); i++ {
if substring[i] == '[' {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is buggy, \n does not work as well

count++
}
}
return count
}
Loading