-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Added azure COSMOSDB detector #3951
base: main
Are you sure you want to change the base?
Changes from 1 commit
9664c0a
f6d26e0
1b50e6e
0bb1f48
1ea9456
7fd20dd
690d360
31d8f48
092e486
aa2ff79
d260cc6
3da7426
834ea15
fc87132
0817adb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
@@ -13,6 +14,7 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
|
@@ -28,6 +30,10 @@ var ( | |
dbKeyPattern = regexp.MustCompile(`([A-Za-z0-9+/]{86}==)`) | ||
// account name can contain only lowercase letters, numbers and the `-` character, must be between 3 and 44 characters long. | ||
accountUrlPattern = regexp.MustCompile(`(https://[a-z0-9-]{3,44}.documents\.azure\.com:443)`) | ||
|
||
invalidHosts = simple.NewCache[struct{}]() | ||
|
||
noSuchHostErr = errors.New("no such host") | ||
) | ||
|
||
func (s Scanner) getClient() *http.Client { | ||
|
@@ -68,6 +74,11 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | |
|
||
for key := range uniqueKeyMatches { | ||
for accountUrl := range uniqueAccountMatches { | ||
if invalidHosts.Exists(accountUrl) { | ||
delete(uniqueAccountMatches, accountUrl) | ||
continue | ||
} | ||
|
||
s1 := detectors.Result{ | ||
DetectorType: detectorspb.DetectorType_AzureCosmosDB, | ||
Raw: []byte(key), | ||
|
@@ -77,7 +88,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | |
if verify { | ||
verified, verificationErr := verifyCosmosDB(s.getClient(), accountUrl, key) | ||
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**: If the host url is invalid or does not exists, then we should not be spending iteration to verify other keys on that. Richard has already implemented this in AzureContainerRegistry 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. This is great approach ❤️ Thanks for sharing @abmussani |
||
s1.Verified = verified | ||
s1.SetVerificationError(verificationErr) | ||
if verificationErr != nil { | ||
if errors.Is(verificationErr, noSuchHostErr) { | ||
invalidHosts.Set(accountUrl, struct{}{}) | ||
continue | ||
} | ||
|
||
s1.SetVerificationError(verificationErr) | ||
} | ||
} | ||
|
||
results = append(results, s1) | ||
|
@@ -111,7 +129,12 @@ func verifyCosmosDB(client *http.Client, accountUrl, key string) (bool, error) { | |
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return false, fmt.Errorf("request failed: %v", err) | ||
// lookup foo.documents.azure.com: no such host | ||
if strings.Contains(err.Error(), "no such host") { | ||
return false, noSuchHostErr | ||
} | ||
|
||
return false, err | ||
} | ||
defer func() { | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
|
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.
The URL should also be added. #3938 (comment)