-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into apk-scanning
- Loading branch information
Showing
38 changed files
with
2,249 additions
and
1,239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
|
@@ -13,6 +14,47 @@ const ( | |
passwordRegex = `(?im)(?:pass|password)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([^:^<>;.*&|£\n\s]{4,40})` | ||
) | ||
|
||
func TestEmailRegexCheck(t *testing.T) { | ||
testEmails := ` | ||
// positive cases | ||
standard email = [email protected] | ||
subdomain email = [email protected] | ||
organization email = [email protected] | ||
test email = [email protected] | ||
with tag email = [email protected] | ||
hyphen domain = [email protected] | ||
service email = [email protected] | ||
underscore email = [email protected] | ||
departement email = [email protected] | ||
alphanumeric email = [email protected] | ||
local server email = [email protected] | ||
dot email = [email protected] | ||
special char email = [email protected] | ||
support email = [email protected] | ||
// negative cases | ||
not an email = abc.123@z | ||
looks like email = test@user <- no domain | ||
email but not = [email protected] <- capital letters not supported for domain | ||
random text = here's some information about local-user@edu user | ||
` | ||
|
||
expectedStr := []string{ | ||
"[email protected]", "[email protected]", | ||
"[email protected]", "[email protected]", "[email protected]", | ||
"[email protected]", "[email protected]", "[email protected]", | ||
"[email protected]", "[email protected]", "[email protected]", | ||
"[email protected]", "[email protected]", "[email protected]", | ||
} | ||
|
||
emailRegex := regexp.MustCompile(EmailPattern) | ||
|
||
emailMatches := emailRegex.FindAllString(testEmails, -1) | ||
|
||
assert.Exactly(t, emailMatches, expectedStr) | ||
|
||
} | ||
|
||
func TestUsernameRegexCheck(t *testing.T) { | ||
usernameRegexPat := UsernameRegexCheck(usernamePattern) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//go:build detectors | ||
// +build detectors | ||
|
||
package checkvist | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/kylelemons/godebug/pretty" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
func TestCheckvist_FromChunk(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2") | ||
if err != nil { | ||
t.Fatalf("could not get test secrets from GCP: %s", err) | ||
} | ||
user := testSecrets.MustGetField("CHECKVIST_EMAIL") | ||
secret := testSecrets.MustGetField("CHECKVIST") | ||
inactiveSecret := testSecrets.MustGetField("CHECKVIST_INACTIVE") | ||
|
||
type args struct { | ||
ctx context.Context | ||
data []byte | ||
verify bool | ||
} | ||
tests := []struct { | ||
name string | ||
s Scanner | ||
args args | ||
want []detectors.Result | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "found, verified", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a checkvist user %s with checkvist secret %s within", user, secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_Checkvist, | ||
Verified: true, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "found, unverified", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a checkvist user %s with checkvist secret %s within but not valid", user, inactiveSecret)), // the secret would satisfy the regex but not pass validation | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_Checkvist, | ||
Verified: false, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "not found", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte("You cannot find the secret within"), | ||
verify: true, | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := Scanner{} | ||
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Checkvist.FromData() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
for i := range got { | ||
if len(got[i].Raw) == 0 { | ||
t.Fatalf("no raw secret present: \n %+v", got[i]) | ||
} | ||
got[i].Raw = nil | ||
} | ||
if diff := pretty.Compare(got, tt.want); diff != "" { | ||
t.Errorf("Checkvist.FromData() %s diff: (-got +want)\n%s", tt.name, diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkFromData(benchmark *testing.B) { | ||
ctx := context.Background() | ||
s := Scanner{} | ||
for name, data := range detectors.MustGetBenchmarkData() { | ||
benchmark.Run(name, func(b *testing.B) { | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
s.FromData(ctx, false, data) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.