Skip to content

Commit

Permalink
test(decoders): add test for % decoder (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmz authored Dec 29, 2024
1 parent f927f96 commit 3b22337
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
7 changes: 2 additions & 5 deletions pkg/decoders/percent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package decoders

import (
"bytes"
"fmt"
"regexp"
"sync"

Expand Down Expand Up @@ -119,7 +118,6 @@ var percentEncodedPat = regexp.MustCompile(`(?i)%[a-f0-9]{2}`)

func decoderPercent(logger logr.Logger, input []byte) []byte {
var (
encoded string
decoded = make([]byte, 0, len(input))
lastIndex = 0
)
Expand All @@ -132,10 +130,9 @@ func decoderPercent(logger logr.Logger, input []byte) []byte {
decoded = append(decoded, input[lastIndex:startIndex]...)

// Append the decoded byte
encoded = string(input[startIndex:endIndex])
char, ok := percentEncodingToChar[encoded]
char, ok := percentEncodingToChar[string(input[startIndex:endIndex])]
if !ok {
logger.Error(fmt.Errorf("unrecognized encoding"), "Unable to decode percent entity", "match", encoded)
// logger.Error(fmt.Errorf("unrecognized encoding"), "Unable to decode percent entity", "match", encoded)
continue
}
decoded = append(decoded, []byte(char)...)
Expand Down
13 changes: 11 additions & 2 deletions pkg/decoders/percent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)

func TestUrlDecoder_FromChunk(t *testing.T) {
func TestPercentDecoder_FromChunk(t *testing.T) {
tests := []struct {
name string
chunk *sources.Chunk
Expand All @@ -35,10 +35,19 @@ func TestUrlDecoder_FromChunk(t *testing.T) {
Data: []byte("https://r2.cloudflarestorage.com/codegeex/codegeex_13b.tar.gz.0?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=b279482b3a1b5758740371cde86a9b62/20230112/us-east-1/s3/aws4_request&X-Amz-Date=20230112T035544Z&X-Amz-Expires=259200&X-Amz-Signature=eaeb7b40bc57c63bbe33991620240e5bdb4bb97f51bc382b32a1a699a47a94ff&X-Amz-SignedHeaders=host\n"),
},
},
{
name: "preserve invalid matches",
chunk: &sources.Chunk{
Data: []byte(`password=%22%C2l3a@521!%22`),
},
want: &sources.Chunk{
Data: []byte(`password="%C2l3a@521!"`),
},
},

// Invalid
{
name: "no escaped",
name: "not escaped",
chunk: &sources.Chunk{
Data: []byte(`-//npm.fontawesome.com/:_authToken=%YOUR_TOKEN%`),
},
Expand Down

0 comments on commit 3b22337

Please sign in to comment.