From 83235ddfbb0e35959a0a51e32628a819bccd5124 Mon Sep 17 00:00:00 2001 From: Muneeb Ullah Khan <100969065+MuneebUllahKhan222@users.noreply.github.com> Date: Wed, 10 Dec 2025 13:07:25 +0500 Subject: [PATCH] =?UTF-8?q?[INS-37]=20Fixed=20the=20issue=20of=20ftp=20con?= =?UTF-8?q?nection=20ignoring=20the=20detector's=20co=E2=80=A6=20(#4590)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [INS-37] Fixed the issue of ftp connection ignoring the detector's context timeout * fix: ensure FTP connection is properly closed by adding c.Quit() * Add error handling for FTP connection cleanup (c.Quit) * Refactor code as a 3-part if else statement --------- Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> --- pkg/detectors/ftp/ftp.go | 14 ++++++++++++-- pkg/detectors/ftp/ftp_integration_test.go | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/detectors/ftp/ftp.go b/pkg/detectors/ftp/ftp.go index dff83faba..c9c751e9d 100644 --- a/pkg/detectors/ftp/ftp.go +++ b/pkg/detectors/ftp/ftp.go @@ -78,8 +78,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } if verify { - timeout := s.verificationTimeout - if timeout == 0 { + var timeout time.Duration + if s.verificationTimeout > 0 { + // Use to configure a simulate timeout for interation tests + timeout = s.verificationTimeout + } else if dl, ok := ctx.Deadline(); ok { + // Here we assign the remaining time before our context expires + // This help us cater the timelimit set through --detector-timeout flag + timeout = time.Until(dl) + } else { timeout = defaultVerificationTimeout } verificationErr := verifyFTP(timeout, parsedURL) @@ -126,6 +133,9 @@ func verifyFTP(timeout time.Duration, u *url.URL) error { return err } + defer func() { + _ = c.Quit() + }() password, _ := u.User.Password() return c.Login(u.User.Username(), password) } diff --git a/pkg/detectors/ftp/ftp_integration_test.go b/pkg/detectors/ftp/ftp_integration_test.go index ce4a3db33..2e546945b 100644 --- a/pkg/detectors/ftp/ftp_integration_test.go +++ b/pkg/detectors/ftp/ftp_integration_test.go @@ -149,7 +149,7 @@ func TestFTP_FromChunk(t *testing.T) { t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError()) } } - opts := cmpopts.IgnoreFields(detectors.Result{}, "verificationError") + opts := cmpopts.IgnoreFields(detectors.Result{}, "verificationError", "primarySecret") if diff := cmp.Diff(got, tt.want, opts); diff != "" { t.Errorf("FTP.FromData() %s diff: (-got +want)\n%s", tt.name, diff) }