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

Check for closed channel in HandleFile #895

Merged
merged 3 commits into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions pkg/handlers/archive_test.go
@@ -1,11 +1,15 @@
package handlers

import (
"context"
"net/http"
"regexp"
"strings"
"testing"

diskbufferreader "github.com/bill-rich/disk-buffer-reader"
"github.com/stretchr/testify/assert"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)

func TestArchiveHandler(t *testing.T) {
Expand Down Expand Up @@ -94,3 +98,26 @@ func TestArchiveHandler(t *testing.T) {
}
}
}

func TestHandleFile(t *testing.T) {
ch := make(chan *sources.Chunk, 2)

// Context cancels the operation.
canceledCtx, cancel := context.WithCancel(context.Background())
cancel()
assert.False(t, HandleFile(canceledCtx, strings.NewReader("file"), &sources.Chunk{}, ch))

// Only one chunk is sent on the channel.
// TODO: Embed a zip without making an HTTP request.
resp, err := http.Get("https://raw.githubusercontent.com/bill-rich/bad-secrets/master/aws-canary-creds.zip")
assert.NoError(t, err)
defer resp.Body.Close()
archive := Archive{}
archive.New()
reader, err := diskbufferreader.New(resp.Body)
assert.NoError(t, err)

assert.Equal(t, 0, len(ch))
assert.True(t, HandleFile(context.Background(), reader, &sources.Chunk{}, ch))
assert.Equal(t, 1, len(ch))
}
40 changes: 25 additions & 15 deletions pkg/handlers/handlers.go
Expand Up @@ -20,28 +20,38 @@ type Handler interface {
}

func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {
for _, handler := range DefaultHandlers() {
// Find a handler for this file.
var handler Handler
for _, handler = range DefaultHandlers() {
handler.New()
var isType bool
file, isType = handler.IsFiletype(file)
if !isType {
continue
if file, isType = handler.IsFiletype(file); isType {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice change 🙌

break
}
handlerChan := handler.FromFile(file)
for {
}
if handler == nil {
return false
}

// Process the file and read all []byte chunks from handlerChan.
handlerChan := handler.FromFile(file)
for {
select {
case data, open := <-handlerChan:
if !open {
// We finished reading everything from handlerChan.
return true
}
chunk := *chunkSkel
chunk.Data = data
// Send data on chunksChan.
select {
case data := <-handlerChan:
chunk := *chunkSkel
chunk.Data = data
chunksChan <- &chunk
case chunksChan <- &chunk:
case <-ctx.Done():
return false
}
if handlerChan == nil {
break
}
case <-ctx.Done():
return false
}
return true
}
return false
}