Skip to content

Commit 20e0233

Browse files
Fixed jira detector invalid domain issue (#4250)
1 parent 0f6e0cd commit 20e0233

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

pkg/detectors/jiratoken/v1/jiratoken.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"net/http"
1011
"strings"
1112

1213
regexp "github.com/wasilibs/go-re2"
1314

15+
"github.com/trufflesecurity/trufflehog/v3/pkg/cache/simple"
1416
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1517
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1618
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
@@ -34,8 +36,22 @@ var (
3436
tokenPat = regexp.MustCompile(detectors.PrefixRegex([]string{"atlassian", "confluence", "jira"}) + `\b([a-zA-Z-0-9]{24})\b`)
3537
domainPat = regexp.MustCompile(detectors.PrefixRegex([]string{"atlassian", "confluence", "jira"}) + `\b((?:[a-zA-Z0-9-]{1,24}\.)+[a-zA-Z0-9-]{2,24}\.[a-zA-Z0-9-]{2,16})\b`)
3638
emailPat = regexp.MustCompile(detectors.PrefixRegex([]string{"atlassian", "confluence", "jira"}) + common.EmailPattern)
39+
40+
invalidHosts = simple.NewCache[struct{}]()
41+
42+
errNoHost = errors.New("no such host")
3743
)
3844

45+
type JIRAGraphQLResponse struct {
46+
Data struct {
47+
Me struct {
48+
User struct {
49+
Name string `json:"name"`
50+
} `json:"user"`
51+
} `json:"me"`
52+
} `json:"data"`
53+
}
54+
3955
func (s Scanner) getClient() *http.Client {
4056
if s.client != nil {
4157
return s.client
@@ -76,6 +92,11 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7692
for email := range uniqueEmails {
7793
for token := range uniqueTokens {
7894
for domain := range uniqueDomains {
95+
if invalidHosts.Exists(domain) {
96+
delete(uniqueDomains, domain)
97+
continue
98+
}
99+
79100
s1 := detectors.Result{
80101
DetectorType: detectorspb.DetectorType_JiraToken,
81102
Raw: []byte(token),
@@ -90,7 +111,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
90111
client := s.getClient()
91112
isVerified, verificationErr := VerifyJiraToken(ctx, client, email, domain, token)
92113
s1.Verified = isVerified
93-
s1.SetVerificationError(verificationErr, token)
114+
if verificationErr != nil {
115+
if errors.Is(verificationErr, errNoHost) {
116+
invalidHosts.Set(domain, struct{}{})
117+
}
118+
119+
s1.SetVerificationError(verificationErr, token)
120+
}
94121
}
95122

96123
results = append(results, s1)
@@ -104,7 +131,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
104131
func VerifyJiraToken(ctx context.Context, client *http.Client, email, domain, token string) (bool, error) {
105132
// wrap the query in a JSON body
106133
body := map[string]string{
107-
"query": `verify { me { user {name}}}`,
134+
"query": `query verify { me { user { name } } }`,
108135
}
109136

110137
// encode the body as JSON
@@ -125,6 +152,11 @@ func VerifyJiraToken(ctx context.Context, client *http.Client, email, domain, to
125152

126153
resp, err := client.Do(req)
127154
if err != nil {
155+
// lookup foo.test.net: no such host
156+
if strings.Contains(err.Error(), "no such host") {
157+
return false, errNoHost
158+
}
159+
128160
return false, err
129161
}
130162

@@ -136,6 +168,11 @@ func VerifyJiraToken(ctx context.Context, client *http.Client, email, domain, to
136168
// the API returns 200 if the token is valid
137169
switch resp.StatusCode {
138170
case http.StatusOK:
171+
var jiraResp JIRAGraphQLResponse
172+
if err := json.NewDecoder(resp.Body).Decode(&jiraResp); err != nil {
173+
return false, nil // can't decode response in case of 200 OK = not valid JIRA domain
174+
}
175+
139176
return true, nil
140177
case http.StatusUnauthorized:
141178
return false, nil

0 commit comments

Comments
 (0)