Skip to content

Commit 1735892

Browse files
fix github detector (#472)
1 parent b6b5479 commit 1735892

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

pkg/detectors/github/github.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var (
2323
// https://developer.github.com/v3/#oauth2-token-sent-in-a-header
2424
// Token type list:
2525
// https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/
26-
keyPat = regexp.MustCompile(`\b((?:ghp|gho|ghu|ghs|ghr)_[a-zA-Z0-9]{36,255}\b)`)
26+
keyPat = regexp.MustCompile(`\b((?:ghp|gho|ghu|ghs|ghr)_[a-zA-Z0-9]{36,255})\b`)
2727

2828
//TODO: Oauth2 client_id and client_secret
2929
// https://developer.github.com/v3/#oauth2-keysecret
@@ -74,11 +74,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7474
req.Header.Add("Authorization", fmt.Sprintf("token %s", token))
7575
res, err := client.Do(req)
7676
if err == nil {
77-
var userResponse userRes
78-
err = json.NewDecoder(res.Body).Decode(&userResponse)
79-
res.Body.Close()
80-
if err == nil {
81-
s.Verified = true
77+
if res.StatusCode >= 200 && res.StatusCode < 300 {
78+
var userResponse userRes
79+
err = json.NewDecoder(res.Body).Decode(&userResponse)
80+
res.Body.Close()
81+
if err == nil {
82+
s.Verified = true
83+
}
8284
}
8385
}
8486
}

pkg/detectors/github/github_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestGitHub_FromChunk(t *testing.T) {
1616
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
1717
defer cancel()
18-
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2")
18+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
1919
if err != nil {
2020
t.Fatalf("could not get test secrets from GCP: %s", err)
2121
}
@@ -123,7 +123,7 @@ func TestGitHub_FromChunk(t *testing.T) {
123123
s: Scanner{},
124124
args: args{
125125
ctx: context.Background(),
126-
data: []byte(unverifiedGhpLong),
126+
data: []byte(fmt.Sprintf("You can find a github secret %s within", unverifiedGhpLong)),
127127
verify: true,
128128
},
129129
want: []detectors.Result{

0 commit comments

Comments
 (0)