Files
trufflehog/pkg/detectors/copy_metadata_test.go
Dustin DeckerandCursor Agent 648aca62d5 Thread original chunk data through engine pipeline (#4780)
* [secret-storage] Thread original chunk data through engine pipeline

Adds OriginalData/ChunkData fields to preserve pre-decode source data
through the scan pipeline:

1. Chunk.OriginalData: captures chunk.Data before iterativeDecode
2. engine.go: sets chunk.OriginalData = chunk.Data before decode
3. ResultWithMetadata.ChunkData: populated by CopyMetadata from
   OriginalData (falls back to Data when nil)

This enables downstream consumers (e.g. the dispatcher in thog) to
access the original source data for secret storage encryption.

* Update TestChunkSize for OriginalData field addition

Chunk struct grew from 80 to 104 bytes with the OriginalData []byte
slice header (24 bytes). Field placement is already optimal (adjacent
to Data []byte).

* fix: preserve OriginalData field in EscapedUnicode decoder

The EscapedUnicode decoder constructed a new sources.Chunk manually
copying fields but omitted OriginalData. This caused CopyMetadata to
fall back to the decoded Data instead of the original pre-decode content,
defeating the purpose of preserving original chunk data for secret
storage encryption.

* Address PR review feedback: use testify/assert, add nil-guard comment, remove stale alignment comment

---------

Co-authored-by: Cursor Agent <[email protected]>
2026-03-03 14:34:05 -05:00

41 lines
854 B
Go

package detectors
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)
func TestCopyMetadata_ChunkDataFromOriginalData(t *testing.T) {
chunk := &sources.Chunk{
Data: []byte("decoded-data"),
OriginalData: []byte("original-source-data"),
SourceName: "test-source",
}
result := Result{
DetectorType: 1,
Raw: []byte("secret"),
}
rwm := CopyMetadata(chunk, result)
assert.Equal(t, "original-source-data", string(rwm.ChunkData))
}
func TestCopyMetadata_ChunkDataFallsBackToData(t *testing.T) {
chunk := &sources.Chunk{
Data: []byte("only-data"),
SourceName: "test-source",
}
result := Result{
DetectorType: 1,
Raw: []byte("secret"),
}
rwm := CopyMetadata(chunk, result)
assert.Equal(t, "only-data", string(rwm.ChunkData))
}