Skip to content

Find, verify, and analyze leaked credentials

Make prints to stdout serial. (#1597)

ahravb1947246d95f
diff --git a/pkg/output/github_actions.go b/pkg/output/github_actions.go
index 077a4da7f..d8d99eaf2 100644
--- a/pkg/output/github_actions.go
+++ b/pkg/output/github_actions.go
@@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
+ "sync"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
@@ -13,7 +14,7 @@ import (
var dedupeCache = make(map[string]struct{})
// GitHubActionsPrinter is a printer that prints results in GitHub Actions format.
-type GitHubActionsPrinter struct{}
+type GitHubActionsPrinter struct{ mu sync.Mutex }
func (p *GitHubActionsPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error {
out := gitHubActionsOutputFormat{
@@ -56,6 +57,8 @@ func (p *GitHubActionsPrinter) Print(_ context.Context, r *detectors.ResultWithM
}
dedupeCache[key] = struct{}{}
+ p.mu.Lock()
+ defer p.mu.Unlock()
message := fmt.Sprintf("Found %s %s result πŸ·πŸ”‘\n", verifiedStatus, out.DetectorType)
if r.Result.DecoderType != detectorspb.DecoderType_PLAIN {
message = fmt.Sprintf("Found %s %s result with %s encoding πŸ·πŸ”‘\n", verifiedStatus, out.DetectorType, out.DecoderType)
diff --git a/pkg/output/json.go b/pkg/output/json.go
index af15be63b..2510fdaf5 100644
--- a/pkg/output/json.go
+++ b/pkg/output/json.go
@@ -3,6 +3,7 @@ package output
import (
"encoding/json"
"fmt"
+ "sync"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
@@ -12,7 +13,7 @@ import (
)
// JSONPrinter is a printer that prints results in JSON format.
-type JSONPrinter struct{}
+type JSONPrinter struct{ mu sync.Mutex }
func (p *JSONPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error {
v := &struct {
@@ -60,6 +61,9 @@ func (p *JSONPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata)
if err != nil {
return fmt.Errorf("could not marshal result: %w", err)
}
+
+ p.mu.Lock()
fmt.Println(string(out))
+ p.mu.Unlock()
return nil
}
diff --git a/pkg/output/legacy_json.go b/pkg/output/legacy_json.go
index bbeea627a..5ea3f1d3d 100644
--- a/pkg/output/legacy_json.go
+++ b/pkg/output/legacy_json.go
@@ -7,6 +7,7 @@ import (
"net/url"
"os"
"strings"
+ "sync"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
@@ -20,7 +21,7 @@ import (
)
// LegacyJSONPrinter is a printer that prints results in legacy JSON format for backwards compatibility.
-type LegacyJSONPrinter struct{}
+type LegacyJSONPrinter struct{ mu sync.Mutex }
func (p *LegacyJSONPrinter) Print(ctx context.Context, r *detectors.ResultWithMetadata) error {
var repo string
@@ -52,7 +53,10 @@ func (p *LegacyJSONPrinter) Print(ctx context.Context, r *detectors.ResultWithMe
if err != nil {
return fmt.Errorf("could not marshal result: %w", err)
}
+
+ p.mu.Lock()
fmt.Println(string(out))
+ p.mu.Unlock()
return nil
}
diff --git a/pkg/output/plain.go b/pkg/output/plain.go
index ec70f8ae0..9629e4393 100644
--- a/pkg/output/plain.go
+++ b/pkg/output/plain.go
@@ -5,6 +5,7 @@ import (
"fmt"
"sort"
"strings"
+ "sync"
"github.com/fatih/color"
"golang.org/x/text/cases"
@@ -22,7 +23,7 @@ var (
)
// PlainPrinter is a printer that prints results in plain text format.
-type PlainPrinter struct{}
+type PlainPrinter struct{ mu sync.Mutex }
func (p *PlainPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error {
out := outputFormat{
@@ -39,6 +40,8 @@ func (p *PlainPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata)
}
printer := greenPrinter
+ p.mu.Lock()
+ defer p.mu.Unlock()
if out.Verified {
yellowPrinter.Print("Found verified result πŸ·πŸ”‘\n")