Skip to content

Line Numbers #97

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

Open
wants to merge 2 commits 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
38 changes: 34 additions & 4 deletions src/pipeleak/scanner/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type PatternPattern struct {
}

type Finding struct {
Pattern PatternElement
Text string
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, text[hit[0]:hit[1]])
// 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,8 @@ 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)}
lineNumber := countNewlinesUntilIndex(text, secret)
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 +280,30 @@ func cleanHitLine(text string) string {
text = strings.ReplaceAll(text, "\n", " ")
return stripansi.Strip(text)
}

func countNewlinesUntilIndex(text []byte, secret []byte) int {

// needed to count the same way as on GitLab Web
re := regexp.MustCompile("(?m)[\r\n]+^.*(section_start|section_end).*$")
no_sections := re.ReplaceAllString(string(text), "")

index := strings.Index(string(no_sections), string(secret))
if index > len(no_sections) {
index = len(no_sections)
}
if index == 0 {
return 0
}
if index < 0 {
return -1
}
substring := no_sections[:index]

count := 0
for i := 0; i < len(substring); i++ {
if substring[i] == '\n' {
count++
}
}
return count
}
Loading