Skip to content

Find, verify, and analyze leaked credentials

[bug] - Add ASCII validation check for base64 decoding (#2671)

ahrav3cb7aedf4a37
diff --git a/pkg/decoders/base64.go b/pkg/decoders/base64.go
index 56b46409e..499ddb91c 100644
--- a/pkg/decoders/base64.go
+++ b/pkg/decoders/base64.go
@@ -3,6 +3,7 @@ package decoders
import (
"bytes"
"encoding/base64"
+ "unicode"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
@@ -33,15 +34,12 @@ func (d *Base64) FromChunk(chunk *sources.Chunk) *DecodableChunk {
for _, str := range encodedSubstrings {
dec, err := base64.StdEncoding.DecodeString(str)
- if err == nil {
- if len(dec) > 0 {
- decodedSubstrings[str] = dec
- }
- continue
+ if err == nil && len(dec) > 0 && isASCII(dec) {
+ decodedSubstrings[str] = dec
}
dec, err = base64.RawURLEncoding.DecodeString(str)
- if err == nil && len(dec) > 0 {
+ if err == nil && len(dec) > 0 && isASCII(dec) {
decodedSubstrings[str] = dec
}
}
@@ -69,6 +67,15 @@ func (d *Base64) FromChunk(chunk *sources.Chunk) *DecodableChunk {
return nil
}
+func isASCII(b []byte) bool {
+ for i := 0; i < len(b); i++ {
+ if b[i] > unicode.MaxASCII {
+ return false
+ }
+ }
+ return true
+}
+
func getSubstringsOfCharacterSet(data []byte, threshold int, charsetMapping [128]bool, endChars string) []string {
if len(data) == 0 {
return nil
diff --git a/pkg/decoders/base64_test.go b/pkg/decoders/base64_test.go
index a96d45151..153837f75 100644
--- a/pkg/decoders/base64_test.go
+++ b/pkg/decoders/base64_test.go
@@ -123,7 +123,14 @@ func TestBase64_FromChunk(t *testing.T) {
Data: []byte(`b64urlsafe-test-secret-underscores??`),
},
},
+ {
+ name: "invalid base64 string",
+ chunk: &sources.Chunk{
+ Data: []byte(`a3d3fa7c2bb99e469ba55e5834ce79ee4853a8a3`),
+ },
+ },
}
+
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Base64{}