-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement tri-state verification in FTP detector #1604
Changes from all commits
4162658
8583f9b
e7e17ae
3e6a6cc
8081c4d
1101929
fa982bf
1b31e80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,11 @@ | |
|
||
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 @@ | |
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 @@ | |
{ | ||
DetectorType: detectorspb.DetectorType_FTP, | ||
Verified: true, | ||
Redacted: "ftp://dlpuser:*************************@ftp.dlptest.com", | ||
Redacted: "ftp://dlpuser:********@ftp.dlptest.com", | ||
Comment on lines
-51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why some of the tests were adjusted? Did our redaction code update? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess so? they were failing on |
||
}, | ||
}, | ||
wantErr: false, | ||
|
@@ -66,11 +69,49 @@ | |
{ | ||
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 @@ | |
} | ||
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 @@ | |
// } | ||
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) | ||
} | ||
Comment on lines
136
to
146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit / question: Could we add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good thought moving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the only reason we were iterating over the indices was to clear out the for _, gotResult := range got {
if (gotResult.Verification != nil) != tt.wantVerificationErr {
// ...
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, no, it's dumber than that. the root problem is that we know when we want to see a verification error, but we don't always know the exact shape of the error, so there's a top-level "is there an error" flag. but verification errors are per result, not per overall chunk flow, so we loop through and check the expected flag against each individual result object. there's probably a better way to do it, though - i haven't thought about it too hard. |
||
}) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool