Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
fix(jiratoken/v1): only verify when the response body contains the authenticated user (#5122)
Shahzad Haider09c910786129
diff --git a/pkg/detectors/jiratoken/v1/jiratoken.go b/pkg/detectors/jiratoken/v1/jiratoken.goindex 66e3bdd3d..b63b132f3 100644--- a/pkg/detectors/jiratoken/v1/jiratoken.go+++ b/pkg/detectors/jiratoken/v1/jiratoken.go@@ -178,7 +178,9 @@ func VerifyJiraToken(ctx context.Context, client *http.Client, email, domain, toreturn false, nil // can't decode response in case of 200 OK = not valid JIRA domain}- return true, nil+ // A 200 on its own isn't enough - unrelated hosts can also return JSON and get flagged as verified.+ // Only trust it when the body actually carries the authenticated user's name.+ return jiraResp.Data.Me.User.Name != "", nilcase http.StatusUnauthorized:return false, nildefault:diff --git a/pkg/detectors/jiratoken/v1/jiratoken_integration_test.go b/pkg/detectors/jiratoken/v1/jiratoken_integration_test.goindex cb97bf962..8eb9252d7 100644--- a/pkg/detectors/jiratoken/v1/jiratoken_integration_test.go+++ b/pkg/detectors/jiratoken/v1/jiratoken_integration_test.go@@ -154,7 +154,7 @@ func TestJiraToken_FromChunk(t *testing.T) {t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())}}- ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "primarySecret")+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "primarySecret", "SecretParts", "chunkOffset", "chunkOffsetSet")if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {t.Errorf("JiraToken.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}diff --git a/pkg/detectors/jiratoken/v1/jiratoken_test.go b/pkg/detectors/jiratoken/v1/jiratoken_test.goindex d3352347d..c61fd8572 100644--- a/pkg/detectors/jiratoken/v1/jiratoken_test.go+++ b/pkg/detectors/jiratoken/v1/jiratoken_test.go@@ -7,6 +7,7 @@ import ("github.com/google/go-cmp/cmp"+ "github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick")@@ -88,3 +89,45 @@ func TestJiraToken_Pattern(t *testing.T) {})}}++func TestJiraToken_Verify(t *testing.T) {+ tests := []struct {+ name string+ statusCode int+ body string+ wantVerified bool+ }{+ {+ name: "authenticated user in body is verified",+ statusCode: 200,+ body: `{"data":{"me":{"user":{"name":"Jira User"}}}}`,+ wantVerified: true,+ },+ {+ // A non-Atlassian host can happily return 200 with unrelated JSON, so the status code alone can't be trusted.+ name: "200 without a jira user is not verified",+ statusCode: 200,+ body: `{"method":"POST","host":"example.com"}`,+ wantVerified: false,+ },+ {+ name: "unauthorized is not verified",+ statusCode: 401,+ body: `{"code":401,"message":"Unauthorized"}`,+ wantVerified: false,+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ client := common.ConstantResponseHttpClient(test.statusCode, test.body)+ verified, err := VerifyJiraToken(context.Background(), client, "[email protected]", "example.atlassian.net", validTokenPattern)+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if verified != test.wantVerified {+ t.Errorf("got verified = %v, want %v", verified, test.wantVerified)+ }+ })+ }+}