-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(google): add oauth2 token detector
- Loading branch information
Showing
3 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
pkg/detectors/googleoauth2/googleoauth2_access_token.go
This file contains 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,145 @@ | ||
package googleoauth2 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
||
"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() | ||
|
||
// There is conflicting information about the expected length of access tokens. | ||
// 10 seems like a reasonable minimum that will weed out placeholders. | ||
// | ||
// https://cloud.google.com/docs/authentication/token-types#access | ||
// https://github.com/GoogleChrome/developer.chrome.com/blob/51dd7dd5d510ed85d86f5a91cb8fde50b62351c7/site/en/docs/webstore/using_webstore_api/index.md?plain=1#L95 | ||
keyPat = regexp.MustCompile(`\b(ya29\.(?i:[a-z0-9_-]{10,}))(?:[^a-z0-9_-]|\z)`) | ||
) | ||
|
||
// 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{"ya29."} | ||
} | ||
|
||
// FromData will find and optionally verify Googleoauth2 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) | ||
|
||
tokens := make(map[string]struct{}) | ||
for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) { | ||
tokens[matches[1]] = struct{}{} | ||
} | ||
|
||
for token := range tokens { | ||
s1 := detectors.Result{ | ||
DetectorType: s.Type(), | ||
Raw: []byte(token), | ||
RawV2: []byte(token), | ||
} | ||
|
||
if verify { | ||
if s.client == nil { | ||
s.client = defaultClient | ||
} | ||
|
||
verified, extraData, vErr := s.verify(ctx, token) | ||
s1.Verified = verified | ||
s1.ExtraData = extraData | ||
s1.SetVerificationError(vErr) | ||
} | ||
|
||
// This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key. | ||
if !s1.Verified && detectors.IsKnownFalsePositive(token, detectors.DefaultFalsePositives, true) { | ||
continue | ||
} | ||
|
||
results = append(results, s1) | ||
} | ||
return | ||
} | ||
|
||
func (s Scanner) verify(ctx context.Context, token string) (bool, map[string]string, error) { | ||
// Based on https://stackoverflow.com/a/66957524 | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token="+token, nil) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
|
||
res, err := s.client.Do(req) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
defer func() { | ||
_ = res.Body.Close() | ||
_, _ = io.Copy(io.Discard, res.Body) | ||
}() | ||
|
||
if res.StatusCode == http.StatusOK { | ||
var token tokenInfo | ||
if err := json.NewDecoder(res.Body).Decode(&token); err != nil { | ||
return false, nil, fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
|
||
extraData := map[string]string{ | ||
"Google_email": token.Email, | ||
"Email_verified": token.EmailVerified, | ||
"Scope": token.Scope, | ||
"Access_type": token.AccessType, | ||
} | ||
|
||
exp, err := strconv.ParseInt(token.Expiry, 10, 64) | ||
if err == nil { | ||
extraData["Expires_at"] = time.Unix(exp, 0).String() | ||
} | ||
return true, extraData, nil | ||
} else if res.StatusCode == http.StatusBadRequest { | ||
var errInfo errorInfo | ||
if err := json.NewDecoder(res.Body).Decode(&errInfo); err != nil { | ||
return false, nil, fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
|
||
if errInfo.Error == "Invalid Value" { | ||
// Definitively false. | ||
return false, nil, nil | ||
} else { | ||
return false, nil, fmt.Errorf("unexpected error description '%s' for %s", errInfo.Error, req.URL) | ||
} | ||
} else { | ||
return false, nil, fmt.Errorf("unexpected respones %d for %s", res.StatusCode, req.URL) | ||
} | ||
} | ||
|
||
type tokenInfo struct { | ||
Expiry string `json:"exp"` | ||
Scope string `json:"scope"` | ||
Email string `json:"email"` | ||
EmailVerified string `json:"email_verified"` | ||
AccessType string `json:"access_type"` | ||
} | ||
|
||
// {"error_description": "Invalid Value"} | ||
type errorInfo struct { | ||
Error string `json:"error_description"` | ||
} | ||
|
||
func (s Scanner) Type() detectorspb.DetectorType { | ||
return detectorspb.DetectorType_GoogleOauth2 | ||
} |
162 changes: 162 additions & 0 deletions
162
pkg/detectors/googleoauth2/googleoauth2_access_token_test.go
This file contains 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,162 @@ | ||
//go:build detectors | ||
// +build detectors | ||
|
||
package googleoauth2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
func TestGoogleoauth2_FromChunk(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5") | ||
if err != nil { | ||
t.Fatalf("could not get test secrets from GCP: %s", err) | ||
} | ||
secret := testSecrets.MustGetField("GOOGLEOAUTH2") | ||
inactiveSecret := testSecrets.MustGetField("GOOGLEOAUTH2_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 googleoauth2 secret %s within", secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_GoogleOauth2, | ||
Verified: true, | ||
}, | ||
}, | ||
wantErr: false, | ||
wantVerificationErr: false, | ||
}, | ||
{ | ||
name: "found, unverified", | ||
s: Scanner{}, | ||
args: args{ | ||
ctx: context.Background(), | ||
data: []byte(fmt.Sprintf("You can find a googleoauth2 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_GoogleOauth2, | ||
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 googleoauth2 secret %s within", secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_GoogleOauth2, | ||
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 googleoauth2 secret %s within", secret)), | ||
verify: true, | ||
}, | ||
want: []detectors.Result{ | ||
{ | ||
DetectorType: detectorspb.DetectorType_GoogleOauth2, | ||
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("Googleoauth2.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") | ||
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" { | ||
t.Errorf("Googleoauth2.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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains 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