Percent/URL Decoding Support (#1831)

* Add initial percent decoding support

* Refactor multi encoding support

* Add detect tests to confirm positions

* Avoid a few extra passes during decoding

* Do multiple passes for finding encodings re

* Fix issue with overlapping encodings when doing separate passes
This commit is contained in:
bplaxco authored and GitHub committed 2025-05-12 11:22:36 -05:00
1 parent 6f967cad68
commit 78eebaca9d
14 files changed
+945 -373

No files matched your search

+11 -9
View File
@@ -12,6 +12,7 @@ import (
"time"
"github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/detect/codec"
"github.com/zricethezav/gitleaks/v8/logging"
"github.com/zricethezav/gitleaks/v8/regexp"
"github.com/zricethezav/gitleaks/v8/report"
@@ -247,9 +248,9 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
// setup variables to handle different decoding passes
currentRaw := fragment.Raw
encodedSegments := []EncodedSegment{}
encodedSegments := []*codec.EncodedSegment{}
currentDecodeDepth := 0
decoder := NewDecoder()
decoder := codec.NewDecoder()
for {
// build keyword map for prefiltering rules
@@ -286,7 +287,7 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
}
// decode the currentRaw for the next pass
currentRaw, encodedSegments = decoder.decode(currentRaw, encodedSegments)
currentRaw, encodedSegments = decoder.Decode(currentRaw, encodedSegments)
// stop the loop when there's nothing else to decode
if len(encodedSegments) == 0 {
@@ -298,7 +299,7 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
}
// detectRule scans the given fragment for the given rule and returns a list of findings
func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rule, encodedSegments []EncodedSegment) []report.Finding {
func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rule, encodedSegments []*codec.EncodedSegment) []report.Finding {
var (
findings []report.Finding
logger = func() zerolog.Logger {
@@ -370,14 +371,15 @@ func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rul
// Check if the decoded portions of the segment overlap with the match
// to see if its potentially a new match
if len(encodedSegments) > 0 {
if segment := segmentWithDecodedOverlap(encodedSegments, matchIndex[0], matchIndex[1]); segment != nil {
matchIndex = segment.adjustMatchIndex(matchIndex)
metaTags = append(metaTags, segment.tags()...)
currentLine = segment.currentLine(currentRaw)
} else {
segments := codec.SegmentsWithDecodedOverlap(encodedSegments, matchIndex[0], matchIndex[1])
if len(segments) == 0 {
// This item has already been added to a finding
continue
}
matchIndex = codec.AdjustMatchIndex(segments, matchIndex)
metaTags = append(metaTags, codec.Tags(segments)...)
currentLine = codec.CurrentLine(segments, currentRaw)
} else {
// Fixes: https://github.com/gitleaks/gitleaks/issues/1352
// removes the incorrectly following line that was detected by regex expression '\n'