-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(detectors): added make.com api_token and mcp_token detectors #4347
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
Open
Jeff-Rowell
wants to merge
30
commits into
trufflesecurity:main
Choose a base branch
from
Jeff-Rowell:feat/make-detector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
98b545e
added make.com api_token detector
Jeff-Rowell 7a26c6f
updated make.com api_token keywords
Jeff-Rowell ceac003
added make.com mcp_token detector
Jeff-Rowell e5d369d
Merge branch 'trufflesecurity:main' into feat/make-detector
Jeff-Rowell bcfbe0b
added matching for make.com API URLs and use the found URLs for verif…
Jeff-Rowell 04c28e6
changed detector type nums
Jeff-Rowell e1187a9
feat: add webexbot support (#4322)
jonathongardner 33026a3
Regenerate protobufs with the correct protoc (etc) version (#4349)
camgunz d859144
fix test (#4350)
dustin-decker 36fd8fa
[Detector]-Detector for tableau personal access token (#4261)
SyedAliHamad fa2aa99
changed detector type nums
Jeff-Rowell 29aefbd
Merge branch 'main' into feat/make-detector
Jeff-Rowell e65ec10
make protos
Jeff-Rowell 9b3bce4
Merge branch 'main' into feat/make-detector
kashifkhan0771 bfd263e
implemented EndpointCustomizer interface
Jeff-Rowell b037651
Merge branch 'main' into feat/make-detector
Jeff-Rowell f6d3b51
- added make api token to exception list next to tableau and artifactory
Jeff-Rowell 17f9659
Merge branch 'main' into feat/make-detector
kashifkhan0771 4203204
Merge branch 'main' into feat/make-detector
amanfcp 13f4b89
Update pkg/detectors/make/api_token/api_token.go
Jeff-Rowell f977a10
Merge branch 'main' into feat/make-detector
Jeff-Rowell 31dce6b
- updated make endpoint regex
Jeff-Rowell 017112b
make protos
Jeff-Rowell 1a51dd2
Merge branch 'main' into feat/make-detector
amanfcp 8fce583
Merge branch 'main' into feat/make-detector
kashifkhan0771 74acac4
- deduplicated found make.com urls
Jeff-Rowell 3cc083f
Merge branch 'main' into feat/make-detector
kashifkhan0771 14b6008
Added defer
kashifkhan0771 9f32ec8
Made a few minor updates per review:
Jeff-Rowell 8fb98a5
Merge branch 'main' into feat/make-detector
Jeff-Rowell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package api_token | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
regexp "github.com/wasilibs/go-re2" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
type Scanner struct { | ||
client *http.Client | ||
} | ||
|
||
// Ensure the Scanner satisfies the interface at compile time. | ||
var _ detectors.Detector = (*Scanner)(nil) | ||
|
||
var ( | ||
defaultClient = common.SaneHttpClient() | ||
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives. | ||
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"make"}) + `\b([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\b`) | ||
// Pattern to match Make.com URLs in the data | ||
urlPat = regexp.MustCompile(`\bhttps://(eu[12]|us[12])\.make\.(?:com|celonis\.com)/api/v2/`) | ||
) | ||
|
||
// Keywords are used for efficiently pre-filtering chunks. | ||
// Use identifiers in the secret preferably, or the provider name. | ||
func (s Scanner) Keywords() []string { | ||
return []string{"make"} | ||
Jeff-Rowell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// FromData will find and optionally verify Make secrets in a given set of bytes. | ||
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
dataStr := string(data) | ||
|
||
uniqueMatches := make(map[string]struct{}) | ||
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | ||
uniqueMatches[match[1]] = struct{}{} | ||
} | ||
|
||
// Extract Make URLs from the data | ||
var foundURLs []string | ||
for _, match := range urlPat.FindAllStringSubmatch(dataStr, -1) { | ||
Jeff-Rowell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foundURLs = append(foundURLs, match[0]) | ||
} | ||
|
||
// If URLs were found, create combinations of tokens and URLs | ||
if len(foundURLs) > 0 { | ||
for match := range uniqueMatches { | ||
for _, foundURL := range foundURLs { | ||
s1 := detectors.Result{ | ||
DetectorType: detectorspb.DetectorType_MakeApiToken, | ||
Raw: []byte(match), | ||
RawV2: []byte(match + ":" + foundURL), | ||
} | ||
|
||
if verify { | ||
client := s.client | ||
if client == nil { | ||
client = defaultClient | ||
} | ||
|
||
isVerified, extraData, verificationErr := verifyMatch(ctx, client, match, foundURLs) | ||
s1.Verified = isVerified | ||
s1.ExtraData = extraData | ||
s1.SetVerificationError(verificationErr, match) | ||
} | ||
|
||
results = append(results, s1) | ||
} | ||
} | ||
} else { | ||
// No URLs found, just return tokens | ||
for match := range uniqueMatches { | ||
s1 := detectors.Result{ | ||
DetectorType: detectorspb.DetectorType_MakeApiToken, | ||
Raw: []byte(match), | ||
} | ||
|
||
if verify { | ||
client := s.client | ||
if client == nil { | ||
client = defaultClient | ||
} | ||
|
||
isVerified, extraData, verificationErr := verifyMatch(ctx, client, match, foundURLs) | ||
s1.Verified = isVerified | ||
s1.ExtraData = extraData | ||
s1.SetVerificationError(verificationErr, match) | ||
} | ||
|
||
results = append(results, s1) | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func verifyMatch(ctx context.Context, client *http.Client, token string, foundURLs []string) (bool, map[string]string, error) { | ||
baseURLs := []string{ | ||
Jeff-Rowell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"https://eu1.make.com/api/v2/", | ||
"https://eu2.make.com/api/v2/", | ||
"https://us1.make.com/api/v2/", | ||
"https://us2.make.com/api/v2/", | ||
"https://us1.make.celonis.com/api/v2/", | ||
"https://eu1.make.celonis.com/api/v2/", | ||
} | ||
|
||
|
||
// If we found URLs in the data, try those first | ||
if len(foundURLs) > 0 { | ||
for _, foundURL := range foundURLs { | ||
verified, err := tryURL(ctx, client, foundURL, token) | ||
if verified { | ||
return true, nil, nil | ||
} | ||
if err != nil { | ||
// If the matched URL failed, continue to try all base URLs | ||
break | ||
} | ||
// If the matched URL returned a determinate failure (401), we still try other base URLs | ||
} | ||
} | ||
|
||
// Try all base URLs | ||
var lastErr error | ||
for _, baseURL := range baseURLs { | ||
verified, err := tryURL(ctx, client, baseURL, token) | ||
if verified { | ||
return true, nil, nil | ||
} | ||
if err != nil { | ||
lastErr = err | ||
continue | ||
} | ||
// Continue to next URL on determinate failures (401) | ||
} | ||
|
||
// If we got here, either all endpoints failed or we had errors | ||
if lastErr != nil { | ||
return false, nil, lastErr | ||
} | ||
return false, nil, nil | ||
} | ||
|
||
func tryURL(ctx context.Context, client *http.Client, baseURL, token string) (bool, error) { | ||
// This endpoint returns 200 OK if the token is valid and the correct FQDN for the API is used. | ||
// https://developers.make.com/api-documentation/api-reference/users-greater-than-me#get-users-me-current-authorization | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"users/me/current-authorization", nil) | ||
if err != nil { | ||
return false, err | ||
} | ||
req.Header.Set("Authorization", "Token "+token) | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
func() { | ||
_, _ = io.Copy(io.Discard, res.Body) | ||
_ = res.Body.Close() | ||
}() | ||
|
||
switch res.StatusCode { | ||
case http.StatusOK: | ||
return true, nil | ||
case http.StatusUnauthorized: | ||
// Determinate failure - invalid token | ||
return false, nil | ||
default: | ||
// Indeterminate failure - unexpected response | ||
return false, fmt.Errorf("unexpected status code: %d", res.StatusCode) | ||
} | ||
} | ||
|
||
func (s Scanner) Type() detectorspb.DetectorType { | ||
return detectorspb.DetectorType_MakeApiToken | ||
} | ||
|
||
func (s Scanner) Description() string { | ||
return "Make.com is a low-code/no-code automation platform that allows users to connect apps and services typically to automate business workflows. This detector identifies API tokens used for Make.com integrations." | ||
} |
161 changes: 161 additions & 0 deletions
161
pkg/detectors/make/api_token/api_token_integration_test.go
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,161 @@ | ||
//go:build detectors | ||
// +build detectors | ||
|
||
package api_token | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
func TestMake_FromChunk(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5") | ||
Jeff-Rowell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
t.Fatalf("could not get test secrets from GCP: %s", err) | ||
} | ||
secret := testSecrets.MustGetField("MAKE_API_TOKEN") | ||
inactiveSecret := testSecrets.MustGetField("MAKE_API_TOKEN_INACTIVE") | ||
|
||
type args struct { | ||
ctx context.Context | ||
data []byte | ||
verify bool | ||
} | ||
tests := []struct { | ||
name string | ||
s Scanner | ||
args args | ||
want []detectors.Result | ||
wantErr bool | ||
wantVerificationErr bool | ||
}{ | ||
{ | ||
name: "found, verified", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a make secret %s within", secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_MakeApiToken, | ||
Verified: true, | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: false, | ||
}, | ||
{ | ||
name: "found, unverified", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a make secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_MakeApiToken, | ||
Verified: false, | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: false, | ||
}, | ||
{ | ||
name: "not found", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte("You cannot find the secret within"), | ||
verify: true, | ||
}, | ||
want: nil, | ||
wantErr: false, | ||
wantVerificationErr: false, | ||
}, | ||
{ | ||
name: "found, would be verified if not for timeout", | ||
s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a make secret %s within", secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_MakeApiToken, | ||
Verified: false, | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: true, | ||
}, | ||
{ | ||
name: "found, verified but unexpected api surface", | ||
s: Scanner{client: common.ConstantResponseHttpClient(404, "")}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a make secret %s within", secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_MakeApiToken, | ||
Verified: false, | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Make.FromData() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
for i := range got { | ||
if len(got[i].Raw) == 0 { | ||
t.Fatalf("no raw secret present: \n %+v", got[i]) | ||
} | ||
if (got[i].VerificationError() != nil) != tt.wantVerificationErr { | ||
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError()) | ||
} | ||
} | ||
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "primarySecret") | ||
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" { | ||
t.Errorf("Make.FromData() %s diff: (-got +want)\n%s", tt.name, diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkFromData(benchmark *testing.B) { | ||
ctx := context.Background() | ||
s := Scanner{} | ||
for name, data := range detectors.MustGetBenchmarkData() { | ||
benchmark.Run(name, func(b *testing.B) { | ||
b.ResetTimer() | ||
for n := 0; n < b.N; n++ { | ||
_, err := s.FromData(ctx, false, data) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.