diff --git a/pkg/detectors/postgres/postgres.go b/pkg/detectors/postgres/postgres.go index cb22536cd..45a29a299 100644 --- a/pkg/detectors/postgres/postgres.go +++ b/pkg/detectors/postgres/postgres.go @@ -36,8 +36,7 @@ func (s Scanner) Keywords() []string { func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) { var results []detectors.Result - var pgURLs []url.URL - pgURLs = append(pgURLs, findUriMatches(string(data))) + pgURLs := findUriMatches(string(data)) pgURLs = append(pgURLs, findComponentMatches(verify, string(data))...) for _, pgURL := range pgURLs { @@ -80,18 +79,19 @@ func getDeadlineInSeconds(ctx context.Context) int { return int(duration.Seconds()) } -func findUriMatches(dataStr string) url.URL { - var pgURL url.URL - for _, uri := range uriPattern.FindAllString(dataStr, -1) { +func findUriMatches(dataStr string) []url.URL { + var results []url.URL + all := uriPattern.FindAllString(dataStr, -1) + for _, uri := range all { pgURL, err := url.Parse(uri) if err != nil { continue } if pgURL.User != nil { - return *pgURL + results = append(results, *pgURL) } } - return pgURL + return results } // check if postgres is running diff --git a/pkg/detectors/postgres/postgres_test.go b/pkg/detectors/postgres/postgres_test.go index 1e2fe8b67..5b4e3868d 100644 --- a/pkg/detectors/postgres/postgres_test.go +++ b/pkg/detectors/postgres/postgres_test.go @@ -98,29 +98,6 @@ func TestPostgres_FromChunk(t *testing.T) { }, wantErr: false, }, - { - name: "found with single line credentials next to invalid credentials, verified", - s: Scanner{}, - args: func() args { - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) - defer cancel() - return args{ - ctx: ctx, - data: []byte(fmt.Sprintf(` - postgresql://user:secret@foobar.com:5432/mydb?sslmode=disable - postgresql://%s:%s@%s:%s/postgres`, - postgresUser, postgresPass, postgresHost, postgresPort)), - verify: true, - } - }(), - want: []detectors.Result{ - { - DetectorType: detectorspb.DetectorType_Postgres, - Verified: true, - }, - }, - wantErr: false, - }, { name: "found with json credentials, verified", s: Scanner{}, @@ -268,6 +245,35 @@ func TestPostgres_FromChunk(t *testing.T) { }(), wantErr: false, }, + { + name: "verified credentials next to bad host credentials", + s: Scanner{}, + args: func() args { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + return args{ + ctx: ctx, + data: []byte(fmt.Sprintf(` + postgresql://user:secret@foobar.com:5432/mydb?sslmode=disable + postgresql://%s:%s@%s:%s/postgres`, + postgresUser, postgresPass, postgresHost, postgresPort)), + verify: true, + } + }(), + want: func() []detectors.Result { + first := detectors.Result{ + DetectorType: detectorspb.DetectorType_Postgres, + Verified: false, + } + first.SetVerificationError(errors.New("i/o timeout")) + second := detectors.Result{ + DetectorType: detectorspb.DetectorType_Postgres, + Verified: true, + } + return []detectors.Result{first, second} + }(), + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {