Skip to content

Commit bd45a70

Browse files
authored
updated the flightlabs api detector (#4393)
1 parent a1cee48 commit bd45a70

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

pkg/detectors/flightlabs/flightlabs.go

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8-
"strings"
98

109
regexp "github.com/wasilibs/go-re2"
1110

@@ -14,57 +13,54 @@ import (
1413
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
1514
)
1615

17-
type Scanner struct{}
16+
type Scanner struct {
17+
client *http.Client
18+
}
1819

1920
// Ensure the Scanner satisfies the interface at compile time
2021
var _ detectors.Detector = (*Scanner)(nil)
2122

2223
var (
23-
client = common.SaneHttpClient()
24+
defaultClient = common.SaneHttpClient()
2425

2526
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives
26-
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"flightlabs"}) + `\b(ey[a-zA-Z0-9]{34}.ey[a-zA-Z0-9._-]{300,350})\b`)
27+
keyPat = regexp.MustCompile(`\b(eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9\.ey[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]{86})\b`)
2728
)
2829

2930
// Keywords are used for efficiently pre-filtering chunks.
3031
// Use identifiers in the secret preferably, or the provider name.
3132
func (s Scanner) Keywords() []string {
32-
return []string{"flightlabs"}
33+
return []string{"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9"}
34+
}
35+
36+
func (s Scanner) getClient() *http.Client {
37+
client := s.client
38+
if client == nil {
39+
client = defaultClient
40+
}
41+
42+
return client
3343
}
3444

3545
// FromData will find and optionally verify FlightLabs secrets in a given set of bytes.
3646
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
3747
dataStr := string(data)
3848

39-
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
40-
41-
for _, match := range matches {
42-
resMatch := strings.TrimSpace(match[1])
49+
uniqueKeys := make(map[string]struct{})
50+
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
51+
uniqueKeys[match[1]] = struct{}{}
52+
}
4353

54+
for key := range uniqueKeys {
4455
s1 := detectors.Result{
4556
DetectorType: detectorspb.DetectorType_FlightLabs,
46-
Raw: []byte(resMatch),
57+
Raw: []byte(key),
4758
}
4859

4960
if verify {
50-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://app.goflightlabs.com/airports?access_key=%s", resMatch), nil)
51-
if err != nil {
52-
continue
53-
}
54-
55-
res, err := client.Do(req)
56-
if err == nil {
57-
bodyBytes, err := io.ReadAll(res.Body)
58-
if err != nil {
59-
continue
60-
}
61-
bodyString := string(bodyBytes)
62-
validResponse := strings.Contains(bodyString, `"id"`)
63-
defer res.Body.Close()
64-
if res.StatusCode >= 200 && res.StatusCode < 300 && validResponse {
65-
s1.Verified = true
66-
}
67-
}
61+
isVerified, verificationErr := verifyMatch(ctx, s.getClient(), key)
62+
s1.Verified = isVerified
63+
s1.SetVerificationError(verificationErr, key)
6864
}
6965

7066
results = append(results, s1)
@@ -73,6 +69,34 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7369
return results, nil
7470
}
7571

72+
func verifyMatch(ctx context.Context, client *http.Client, secret string) (bool, error) {
73+
// API Reference: https://www.goflightlabs.com/airports-by-filters
74+
75+
url := fmt.Sprintf("https://www.goflightlabs.com/airports-by-filter?access_key=%s&iata_code=JFK", secret)
76+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
77+
if err != nil {
78+
return false, err
79+
}
80+
81+
res, err := client.Do(req)
82+
if err != nil {
83+
return false, err
84+
}
85+
defer func() {
86+
_, _ = io.Copy(io.Discard, res.Body)
87+
_ = res.Body.Close()
88+
}()
89+
90+
switch res.StatusCode {
91+
case http.StatusOK:
92+
return true, nil
93+
case http.StatusUnauthorized:
94+
return false, nil
95+
default:
96+
return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)
97+
}
98+
}
99+
76100
func (s Scanner) Type() detectorspb.DetectorType {
77101
return detectorspb.DetectorType_FlightLabs
78102
}

pkg/detectors/flightlabs/flightlabs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ var (
1919
"authentication_type": "",
2020
"verification_url": "https://api.example.com/example",
2121
"test_secrets": {
22-
"flightlabs_secret": "eyBejkA9vwoG6xGr6awmKPT3xdznWqM4jpbz.ey3UspgSVM9j311NMff2N27tGEWSmr8sl1SguOxwzelJSYPOOVp-8BwHsdHqWKWpoVvZAc4kXKJ2kpROZ1RY_0xSj51iWOoi5UvvxOlaIHTzMEEiudOJRQuzYxwtqtl1rZyRlFuxTm0YR5wWPFM0GlWzmCf_yKz.dmSGyVThf_F.atNcL556uLcZ9D6MTIlQoC9hD1u3EbBqL6nb32cgFowGosYnqkSgbCFPLg6LIhK_PADfDzUY2bTEsk7uEIbGxPXofrLgJ_aiR2H09gEOtMAvPgkWLAbKoJg7X3UKF5BrK0GFdHAWvsB"
22+
"flightlabs_secret": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.ey3UspgSVM9j311NMff2N27tGEWSmr8sl1SguOxwzelJSYPOOVp-8BwHsdHqWKWpoVvZAc4kXKJ2kpROZ1RY_0xSj51iWOoi5UvvxOlaIHTzMEEiudOJRQuzYxwtqtl1rZyRlFuxTm0YR5wWPFM0GlWzmCf_yKz.atNcL556uLcZ9D6MTIlQoC9hD1u3EbBqL6nb32cgFowGosYnqkSgbCFPLg6LIhK_PADfDzUY2bTEsk7uEIbGxP"
2323
},
2424
"expected_response": "200",
2525
"method": "GET",
2626
"deprecated": false
2727
}]`
28-
secret = "eyBejkA9vwoG6xGr6awmKPT3xdznWqM4jpbz.ey3UspgSVM9j311NMff2N27tGEWSmr8sl1SguOxwzelJSYPOOVp-8BwHsdHqWKWpoVvZAc4kXKJ2kpROZ1RY_0xSj51iWOoi5UvvxOlaIHTzMEEiudOJRQuzYxwtqtl1rZyRlFuxTm0YR5wWPFM0GlWzmCf_yKz.dmSGyVThf_F.atNcL556uLcZ9D6MTIlQoC9hD1u3EbBqL6nb32cgFowGosYnqkSgbCFPLg6LIhK_PADfDzUY2bTEsk7uEIbGxPXofrLgJ_aiR2H09gEOtMAvPgkWLAbKoJg7X3UKF5BrK0GFdHAWvsB"
28+
secret = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.ey3UspgSVM9j311NMff2N27tGEWSmr8sl1SguOxwzelJSYPOOVp-8BwHsdHqWKWpoVvZAc4kXKJ2kpROZ1RY_0xSj51iWOoi5UvvxOlaIHTzMEEiudOJRQuzYxwtqtl1rZyRlFuxTm0YR5wWPFM0GlWzmCf_yKz.atNcL556uLcZ9D6MTIlQoC9hD1u3EbBqL6nb32cgFowGosYnqkSgbCFPLg6LIhK_PADfDzUY2bTEsk7uEIbGxP"
2929
)
3030

3131
func TestFlightLabs_Pattern(t *testing.T) {

0 commit comments

Comments
 (0)