Skip to content

Commit

Permalink
fix github detector (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin-decker authored Apr 22, 2022
1 parent b6b5479 commit 1735892
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions pkg/detectors/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
// https://developer.github.com/v3/#oauth2-token-sent-in-a-header
// Token type list:
// https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
keyPat = regexp.MustCompile(`\b((?:ghp|gho|ghu|ghs|ghr)_[a-zA-Z0-9]{36,255}\b)`)
keyPat = regexp.MustCompile(`\b((?:ghp|gho|ghu|ghs|ghr)_[a-zA-Z0-9]{36,255})\b`)

//TODO: Oauth2 client_id and client_secret
// https://developer.github.com/v3/#oauth2-keysecret
Expand Down Expand Up @@ -74,11 +74,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
req.Header.Add("Authorization", fmt.Sprintf("token %s", token))
res, err := client.Do(req)
if err == nil {
var userResponse userRes
err = json.NewDecoder(res.Body).Decode(&userResponse)
res.Body.Close()
if err == nil {
s.Verified = true
if res.StatusCode >= 200 && res.StatusCode < 300 {
var userResponse userRes
err = json.NewDecoder(res.Body).Decode(&userResponse)
res.Body.Close()
if err == nil {
s.Verified = true
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/detectors/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestGitHub_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2")
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestGitHub_FromChunk(t *testing.T) {
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(unverifiedGhpLong),
data: []byte(fmt.Sprintf("You can find a github secret %s within", unverifiedGhpLong)),
verify: true,
},
want: []detectors.Result{
Expand Down

0 comments on commit 1735892

Please sign in to comment.