2022-01-13 20:02:24 +00:00
|
|
|
package decoders
|
|
|
|
|
|
|
|
import (
|
2023-10-23 15:02:01 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
2022-02-10 18:54:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
2022-01-13 20:02:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func DefaultDecoders() []Decoder {
|
|
|
|
return []Decoder{
|
2023-07-11 20:48:00 +00:00
|
|
|
// UTF8 must be first for duplicate detection
|
2022-11-15 17:36:01 +00:00
|
|
|
&UTF8{},
|
2022-04-15 19:09:01 +00:00
|
|
|
&Base64{},
|
2023-04-20 22:07:49 +00:00
|
|
|
&UTF16{},
|
2024-03-02 19:27:44 +00:00
|
|
|
&EscapedUnicode{},
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 15:02:01 +00:00
|
|
|
// DecodableChunk is a chunk that includes the type of decoder used.
|
|
|
|
// This allows us to avoid a type assertion on each decoder.
|
|
|
|
type DecodableChunk struct {
|
|
|
|
*sources.Chunk
|
|
|
|
DecoderType detectorspb.DecoderType
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
type Decoder interface {
|
2023-10-23 15:02:01 +00:00
|
|
|
FromChunk(chunk *sources.Chunk) *DecodableChunk
|
2024-08-09 19:19:16 +00:00
|
|
|
Type() detectorspb.DecoderType
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fuzz is an entrypoint for go-fuzz, which is an AFL-style fuzzing tool.
|
|
|
|
// This one attempts to uncover any panics during decoding.
|
|
|
|
func Fuzz(data []byte) int {
|
|
|
|
decoded := false
|
|
|
|
for i, decoder := range DefaultDecoders() {
|
|
|
|
// Skip the first decoder (plain), because it will always decode and give
|
2022-04-01 23:47:27 +00:00
|
|
|
// priority to the input (return 1).
|
2022-01-13 20:02:24 +00:00
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
chunk := decoder.FromChunk(&sources.Chunk{Data: data})
|
|
|
|
if chunk != nil {
|
|
|
|
decoded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if decoded {
|
|
|
|
return 1 // prioritize the input
|
|
|
|
}
|
2022-04-01 23:47:27 +00:00
|
|
|
return -1 // Don't add input to the corpus.
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|