-
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.
implement tri-state verification in FTP detector (#1604)
This PR implements tri-state verification in the FTP detector. The verification timeout was made injectable to support a new test case. Some test cases that had already been broken have been fixed as well.
- Loading branch information
Showing
2 changed files
with
92 additions
and
24 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 |
---|---|---|
|
@@ -5,9 +5,11 @@ package ftp | |
|
||
import ( | ||
"context" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"testing" | ||
"time" | ||
|
||
"github.com/kylelemons/godebug/pretty" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
@@ -19,11 +21,12 @@ func TestFTP_FromChunk(t *testing.T) { | |
verify bool | ||
} | ||
tests := []struct { | ||
name string | ||
s Scanner | ||
args args | ||
want []detectors.Result | ||
wantErr bool | ||
name string | ||
s Scanner | ||
args args | ||
want []detectors.Result | ||
wantErr bool | ||
wantVerificationErr bool | ||
}{ | ||
{ | ||
name: "bad scheme", | ||
|
@@ -48,7 +51,7 @@ func TestFTP_FromChunk(t *testing.T) { | |
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: true, | ||
Redacted: "ftp://dlpuser:*************************@ftp.dlptest.com", | ||
Redacted: "ftp://dlpuser:********@ftp.dlptest.com", | ||
}, | ||
}, | ||
wantErr: false, | ||
|
@@ -66,11 +69,49 @@ func TestFTP_FromChunk(t *testing.T) { | |
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: false, | ||
Redacted: "ftp://dlpuser:*******@ftp.dlptest.com", | ||
Redacted: "ftp://dlpuser:********@ftp.dlptest.com", | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "bad host", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
// https://dlptest.com/ftp-test/ | ||
data: []byte("ftp://dlpuser:[email protected]"), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: false, | ||
Redacted: "ftp://dlpuser:********@ftp.dlptest.com.badhost", | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: true, | ||
}, | ||
{ | ||
name: "timeout", | ||
s: Scanner{verificationTimeout: 1 * time.Microsecond}, | ||
args: args{ | ||
ctx: context.Background(), | ||
// https://dlptest.com/ftp-test/ | ||
data: []byte("ftp://dlpuser:[email protected]"), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: false, | ||
Redacted: "ftp://dlpuser:********@ftp.dlptest.com", | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: true, | ||
}, | ||
{ | ||
name: "blocked FP", | ||
s: Scanner{}, | ||
|
@@ -84,8 +125,7 @@ func TestFTP_FromChunk(t *testing.T) { | |
} | ||
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) | ||
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("URI.FromData() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
|
@@ -95,9 +135,14 @@ func TestFTP_FromChunk(t *testing.T) { | |
// } | ||
for i := range got { | ||
got[i].Raw = nil | ||
|
||
if (got[i].VerificationError != nil) != tt.wantVerificationErr { | ||
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError) | ||
} | ||
} | ||
if diff := pretty.Compare(got, tt.want); diff != "" { | ||
t.Errorf("URI.FromData() %s diff: (-got +want)\n%s", tt.name, diff) | ||
opts := cmpopts.IgnoreFields(detectors.Result{}, "VerificationError") | ||
if diff := cmp.Diff(got, tt.want, opts); diff != "" { | ||
t.Errorf("FTP.FromData() %s diff: (-got +want)\n%s", tt.name, diff) | ||
} | ||
}) | ||
} | ||
|