2023-10-30 19:34:33 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-30 22:17:17 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/custom_detectors"
|
2023-10-30 19:34:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
|
2023-10-30 22:17:17 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/custom_detectorspb"
|
2023-10-30 19:34:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TestDetectorType = -1
|
|
|
|
|
|
|
|
type testDetectorV1 struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV1) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) {
|
|
|
|
return make([]detectors.Result, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV1) Keywords() []string {
|
2023-11-03 16:26:18 +00:00
|
|
|
return []string{"a", "b"}
|
2023-10-30 19:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV1) Type() detectorspb.DetectorType {
|
|
|
|
return TestDetectorType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV1) Version() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
type testDetectorV2 struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV2) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) {
|
|
|
|
return make([]detectors.Result, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV2) Keywords() []string {
|
2023-11-03 16:26:18 +00:00
|
|
|
return []string{"a"}
|
2023-10-30 19:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV2) Type() detectorspb.DetectorType {
|
|
|
|
return TestDetectorType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d testDetectorV2) Version() int {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ detectors.Detector = (*testDetectorV1)(nil)
|
|
|
|
var _ detectors.Detector = (*testDetectorV2)(nil)
|
|
|
|
var _ detectors.Versioner = (*testDetectorV1)(nil)
|
|
|
|
var _ detectors.Versioner = (*testDetectorV2)(nil)
|
|
|
|
|
2023-10-30 22:17:17 +00:00
|
|
|
func TestAhoCorasickCore_MultipleCustomDetectorsMatchable(t *testing.T) {
|
|
|
|
customDetector1, err := custom_detectors.NewWebhookCustomRegex(&custom_detectorspb.CustomRegex{
|
|
|
|
Name: "custom detector 1",
|
|
|
|
Keywords: []string{"a"},
|
|
|
|
Regex: map[string]string{"": ""},
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
customDetector2, err := custom_detectors.NewWebhookCustomRegex(&custom_detectorspb.CustomRegex{
|
|
|
|
Name: "custom detector 2",
|
2023-11-03 16:26:18 +00:00
|
|
|
Keywords: []string{"a"},
|
2023-10-30 22:17:17 +00:00
|
|
|
Regex: map[string]string{"": ""},
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2023-11-03 16:26:18 +00:00
|
|
|
allDetectors := []detectors.Detector{customDetector1, customDetector2}
|
2023-10-30 22:17:17 +00:00
|
|
|
|
|
|
|
ac := NewAhoCorasickCore(allDetectors)
|
|
|
|
|
2023-11-03 16:26:18 +00:00
|
|
|
detectorsMap := make(map[DetectorKey]detectors.Detector, 2)
|
|
|
|
ac.PopulateMatchingDetectors("a", detectorsMap)
|
|
|
|
matchingDetectors := make([]detectors.Detector, 0, 2)
|
|
|
|
for _, d := range detectorsMap {
|
|
|
|
matchingDetectors = append(matchingDetectors, d)
|
2023-10-30 22:17:17 +00:00
|
|
|
}
|
2023-11-03 16:26:18 +00:00
|
|
|
assert.ElementsMatch(t, allDetectors, matchingDetectors)
|
2023-10-30 22:17:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 19:34:33 +00:00
|
|
|
func TestAhoCorasickCore_MultipleDetectorVersionsMatchable(t *testing.T) {
|
2023-11-03 16:26:18 +00:00
|
|
|
v1 := testDetectorV1{}
|
|
|
|
v2 := testDetectorV2{}
|
|
|
|
allDetectors := []detectors.Detector{v1, v2}
|
|
|
|
|
|
|
|
ac := NewAhoCorasickCore(allDetectors)
|
2023-10-30 19:34:33 +00:00
|
|
|
|
2023-11-03 16:26:18 +00:00
|
|
|
detectorsMap := make(map[DetectorKey]detectors.Detector, 2)
|
|
|
|
ac.PopulateMatchingDetectors("a", detectorsMap)
|
|
|
|
matchingDetectors := make([]detectors.Detector, 0, 2)
|
|
|
|
for _, d := range detectorsMap {
|
|
|
|
matchingDetectors = append(matchingDetectors, d)
|
2023-10-30 19:34:33 +00:00
|
|
|
}
|
2023-11-03 16:26:18 +00:00
|
|
|
assert.ElementsMatch(t, allDetectors, matchingDetectors)
|
|
|
|
}
|
2023-10-30 19:34:33 +00:00
|
|
|
|
2023-11-03 16:26:18 +00:00
|
|
|
func TestAhoCorasickCore_NoDuplicateDetectorsMatched(t *testing.T) {
|
|
|
|
d := testDetectorV1{}
|
|
|
|
allDetectors := []detectors.Detector{d}
|
2023-10-30 19:34:33 +00:00
|
|
|
|
2023-11-03 16:26:18 +00:00
|
|
|
ac := NewAhoCorasickCore(allDetectors)
|
2023-10-30 19:34:33 +00:00
|
|
|
|
2023-11-03 16:26:18 +00:00
|
|
|
detectorsMap := make(map[DetectorKey]detectors.Detector, 2)
|
|
|
|
ac.PopulateMatchingDetectors("a a b b", detectorsMap)
|
|
|
|
matchingDetectors := make([]detectors.Detector, 0, 2)
|
|
|
|
for _, d := range detectorsMap {
|
|
|
|
matchingDetectors = append(matchingDetectors, d)
|
2023-10-30 19:34:33 +00:00
|
|
|
}
|
2023-11-03 16:26:18 +00:00
|
|
|
assert.ElementsMatch(t, allDetectors, matchingDetectors)
|
2023-10-30 19:34:33 +00:00
|
|
|
}
|