What this change means
Deduplication changed. Changes keywords, regex patterns or pairing.
| @@ -7,7 +7,10 @@ import ( | ||
| 7 | 7 | "crypto/sha256" |
| 8 | 8 | "encoding/base64" |
| 9 | 9 | "encoding/binary" |
| 10 | added: "errors" | |
| 10 | 11 | "fmt" |
| 12 | added: "io" | |
| 13 | added: "net" | |
| 11 | 14 | "net/http" |
| 12 | 15 | "net/url" |
| 13 | 16 | "strconv" |
| @@ -16,6 +19,7 @@ import ( | ||
| 16 | 19 | |
| 17 | 20 | regexp "github.com/wasilibs/go-re2" |
| 18 | 21 | |
| 22 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple" | |
| 19 | 23 | "github.com/trufflesecurity/trufflehog/v3/pkg/common" |
| 20 | 24 | "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 21 | 25 | "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" |
| @@ -41,6 +45,9 @@ var ( | ||
| 41 | 45 | tokenSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"netsuite", "token", "secret"}) + `\b([a-zA-Z0-9]{64})\b`) |
| 42 | 46 | |
| 43 | 47 | accountIDPat = regexp.MustCompile(detectors.PrefixRegex([]string{"netsuite", "account", "id"}) + `\b([a-zA-Z0-9-_]{6,15})\b`) |
| 48 | added: | |
| 49 | added: // invalidHosts holds account hosts that do not resolve, so later combinations skip the lookup. | |
| 50 | added: invalidHosts = simple.NewCache[struct{}]() | |
| 44 | 51 | ) |
| 45 | 52 | |
| 46 | 53 | type credentialSet struct { |
| @@ -68,8 +75,23 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 68 | 75 | tokenSecretMatches := trimUniqueMatches(tokenSecretPat.FindAllStringSubmatch(dataStr, -1)) |
| 69 | 76 | accountIDMatches := trimUniqueMatches(accountIDPat.FindAllStringSubmatch(dataStr, -1)) |
| 70 | 77 | |
| 78 | added: // Every combination needs a token key, token secret and account ID. | |
| 79 | added: if len(tokenKeyMatches) == 0 || len(tokenSecretMatches) == 0 || len(accountIDMatches) == 0 { | |
| 80 | added: return nil, nil | |
| 81 | added: } | |
| 82 | added: | |
| 83 | added: // Raw and RawV2 hold only the consumer key and secret, so every combination for a pair is a duplicate of the same | |
| 84 | added: // finding. Report one result per pair rather than one per combination of all five parts. The token key, token | |
| 85 | added: // secret and account ID decide whether the pair is reported and whether it verifies. | |
| 71 | 86 | for consumerKey := range consumerKeyMatches { |
| 72 | 87 | for consumerSecret := range consumerSecretMatches { |
| 88 | added: // No combination can use the same value as both consumer key and secret. | |
| 89 | added: if consumerKey == consumerSecret { | |
| 90 | added: continue | |
| 91 | added: } | |
| 92 | added: | |
| 93 | added: var pairResult *detectors.Result | |
| 94 | added: combinations: | |
| 73 | 95 | for tokenKey := range tokenKeyMatches { |
| 74 | 96 | for tokenSecret := range tokenSecretMatches { |
| 75 | 97 | for accountID := range accountIDMatches { |
| @@ -107,10 +129,28 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 107 | 129 | s1.Verified = isVerified |
| 108 | 130 | s1.SetVerificationError(err, consumerKey) |
| 109 | 131 | } |
| 110 | removed: results = append(results, s1) | |
| 132 | added: | |
| 133 | added: // Keep the verified result, else the first indeterminate one, else the first one. | |
| 134 | added: if pairResult == nil || s1.Verified || (s1.VerificationError() != nil && pairResult.VerificationError() == nil) { | |
| 135 | added: pairResult = &s1 | |
| 136 | added: } | |
| 137 | added: if !verify || s1.Verified { | |
| 138 | added: break combinations | |
| 139 | added: } | |
| 140 | added: if err := ctx.Err(); err != nil { | |
| 141 | added: // Combinations left untried might have verified. | |
| 142 | added: if pairResult.VerificationError() == nil { | |
| 143 | added: pairResult.SetVerificationError(err, consumerKey) | |
| 144 | added: } | |
| 145 | added: break combinations | |
| 146 | added: } | |
| 111 | 147 | } |
| 112 | 148 | } |
| 113 | 149 | } |
| 150 | added: | |
| 151 | added: if pairResult != nil { | |
| 152 | added: results = append(results, *pairResult) | |
| 153 | added: } | |
| 114 | 154 | } |
| 115 | 155 | } |
| @@ -126,9 +166,20 @@ func (s Scanner) Description() string { | ||
| 126 | 166 | } |
| 127 | 167 | |
| 128 | 168 | func verifyCredentials(ctx context.Context, client *http.Client, cs credentialSet) (bool, error) { |
| 169 | added: // The engine waits for FromData to return even after its deadline, so send no request once the context is done. | |
| 170 | added: if err := ctx.Err(); err != nil { | |
| 171 | added: return false, err | |
| 172 | added: } | |
| 173 | added: | |
| 129 | 174 | // for url, filter or replace underscore in accountID if needed and lower case the accountID |
| 130 | 175 | urlAccountId := strings.ToLower(strings.ReplaceAll(cs.accountID, "_", "-")) |
| 131 | 176 | |
| 177 | added: // An earlier combination already found that this account's host does not resolve, so skip the lookup and | |
| 178 | added: // return what the lookup would. | |
| 179 | added: if invalidHosts.Exists(urlAccountId) { | |
| 180 | added: return false, nil | |
| 181 | added: } | |
| 182 | added: | |
| 132 | 183 | baseUrl := "https://" + urlAccountId + ".suitetalk.api.netsuite.com" |
| 133 | 184 | |
| 134 | 185 | const path = "/services/rest/record/v1/metadata-catalog/check" |
| @@ -164,12 +215,17 @@ func verifyCredentials(ctx context.Context, client *http.Client, cs credentialSe | ||
| 164 | 215 | // Make the request |
| 165 | 216 | res, err := client.Do(req) |
| 166 | 217 | if err != nil { |
| 167 | removed: if strings.Contains(err.Error(), "no such host") { | |
| 218 | added: var dnsErr *net.DNSError | |
| 219 | added: if errors.As(err, &dnsErr) && dnsErr.IsNotFound { | |
| 220 | added: invalidHosts.Set(urlAccountId, struct{}{}) | |
| 168 | 221 | return false, nil |
| 169 | 222 | } |
| 170 | 223 | return false, err |
| 171 | 224 | } |
| 172 | removed: defer func() { _ = res.Body.Close() }() | |
| 225 | added: defer func() { | |
| 226 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 227 | added: _ = res.Body.Close() | |
| 228 | added: }() | |
| 173 | 229 | switch res.StatusCode { |
| 174 | 230 | case http.StatusOK: |
| 175 | 231 | return true, nil |