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

[chore] - correctly handle input shorter than 512 bytes #2077

Merged
merged 6 commits into from
Oct 31, 2023
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
4 changes: 3 additions & 1 deletion pkg/handlers/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,11 @@ func (a *Archive) handleNestedFileMIME(ctx logContext.Context, tempEnv tempEnv,
// determineMimeType reads from the provided reader to detect the MIME type.
// It returns the detected MIME type and a new reader that includes the read portion.
func determineMimeType(reader io.Reader) (mimeType, io.Reader, error) {
// A buffer of 512 bytes is used since many file formats store their magic numbers within the first 512 bytes.
// If fewer bytes are read, MIME type detection may still succeed.
buffer := make([]byte, 512)
n, err := reader.Read(buffer)
if err != nil {
if err != nil && !errors.Is(err, io.EOF) {
return "", nil, fmt.Errorf("unable to read file for MIME type detection: %w", err)
}

Expand Down
77 changes: 76 additions & 1 deletion pkg/handlers/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"testing"
"time"

diskbufferreader "github.com/trufflesecurity/disk-buffer-reader"
"github.com/h2non/filetype"
"github.com/stretchr/testify/assert"
diskbufferreader "github.com/trufflesecurity/disk-buffer-reader"

logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
Expand Down Expand Up @@ -271,3 +272,77 @@ func TestNestedDirArchive(t *testing.T) {
}
assert.Equal(t, want, count)
}

func TestDetermineMimeType(t *testing.T) {
filetype.AddMatcher(filetype.NewType("txt", "text/plain"), func(buf []byte) bool {
return strings.HasPrefix(string(buf), "text:")
})

pngBytes := []byte("\x89PNG\r\n\x1a\n")
jpegBytes := []byte{0xFF, 0xD8, 0xFF}
textBytes := []byte("text: This is a plain text")
rpmBytes := []byte("\xed\xab\xee\xdb")

tests := []struct {
name string
input io.Reader
expected mimeType
shouldFail bool
}{
{
name: "PNG file",
input: bytes.NewReader(pngBytes),
expected: mimeType("image/png"),
shouldFail: false,
},
{
name: "JPEG file",
input: bytes.NewReader(jpegBytes),
expected: mimeType("image/jpeg"),
shouldFail: false,
},
{
name: "Text file",
input: bytes.NewReader(textBytes),
expected: mimeType("text/plain"),
shouldFail: false,
},
{
name: "RPM file",
input: bytes.NewReader(rpmBytes),
expected: rpmMimeType,
shouldFail: false,
},
{
name: "Truncated JPEG file",
input: io.LimitReader(bytes.NewReader(jpegBytes), 2),
expected: mimeType("unknown"),
shouldFail: true,
},
{
name: "Empty reader",
input: bytes.NewReader([]byte{}),
shouldFail: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
originalData, _ := io.ReadAll(io.TeeReader(tt.input, &bytes.Buffer{}))
tt.input = bytes.NewReader(originalData) // Reset the reader

mime, reader, err := determineMimeType(tt.input)
if err != nil && !tt.shouldFail {
t.Fatalf("unexpected error: %v", err)
}

if !tt.shouldFail {
assert.Equal(t, tt.expected, mime)
}

// Ensure the reader still contains all the original data.
data, _ := io.ReadAll(reader)
assert.Equal(t, originalData, data)
})
}
}
2 changes: 1 addition & 1 deletion pkg/sources/chunker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"
"testing/iotest"

diskbufferreader "github.com/trufflesecurity/disk-buffer-reader"
"github.com/stretchr/testify/assert"
diskbufferreader "github.com/trufflesecurity/disk-buffer-reader"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: not sure how/why this got updated, but i'm going to leave it since its just a import reorder.


"github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
Expand Down
Loading