Skip to content
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

Add test for % decoder #56

Merged
merged 1 commit into from
Dec 29, 2024
Merged
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
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
Loading