diff --git a/pkg/reporting/trackers/jira/jira.go b/pkg/reporting/trackers/jira/jira.go index ebfbfd9cb..1fe02b085 100644 --- a/pkg/reporting/trackers/jira/jira.go +++ b/pkg/reporting/trackers/jira/jira.go @@ -437,6 +437,49 @@ func (i *Integration) FindExistingIssue(event *output.ResultEvent, useStatus boo jql = fmt.Sprintf("%s AND status != \"%s\"", jql, i.options.StatusNot) } + // Hotfix for Jira Cloud: use Enhanced Search API (v3) to avoid deprecated v2 path + if i.options.Cloud { + params := url.Values{} + params.Set("jql", jql) + params.Set("maxResults", "1") + params.Set("fields", "id,key") + + req, err := i.jira.NewRequest("GET", "/rest/api/3/search/jql"+"?"+params.Encode(), nil) + if err != nil { + return jira.Issue{}, err + } + + var searchResult struct { + Issues []struct { + ID string `json:"id"` + Key string `json:"key"` + } `json:"issues"` + IsLast bool `json:"isLast"` + NextPageToken string `json:"nextPageToken"` + } + + resp, err := i.jira.Do(req, &searchResult) + if err != nil { + var data string + if resp != nil && resp.Body != nil { + d, _ := io.ReadAll(resp.Body) + data = string(d) + } + return jira.Issue{}, fmt.Errorf("%w => %s", err, data) + } + + if len(searchResult.Issues) == 0 { + return jira.Issue{}, nil + } + first := searchResult.Issues[0] + base := strings.TrimRight(i.options.URL, "/") + return jira.Issue{ + ID: first.ID, + Key: first.Key, + Self: fmt.Sprintf("%s/rest/api/3/issue/%s", base, first.ID), + }, nil + } + searchOptions := &jira.SearchOptionsV2{ MaxResults: 1, // if any issue exists, then we won't create a new one Fields: []string{"summary", "description", "issuetype", "status", "priority", "project"}, diff --git a/pkg/reporting/trackers/jira/jira_test.go b/pkg/reporting/trackers/jira/jira_test.go index d9b27adf7..a2e94290b 100644 --- a/pkg/reporting/trackers/jira/jira_test.go +++ b/pkg/reporting/trackers/jira/jira_test.go @@ -1,6 +1,8 @@ package jira import ( + "net/http" + "os" "strings" "testing" @@ -9,9 +11,23 @@ import ( "github.com/projectdiscovery/nuclei/v3/pkg/model/types/stringslice" "github.com/projectdiscovery/nuclei/v3/pkg/output" "github.com/projectdiscovery/nuclei/v3/pkg/reporting/trackers/filters" + "github.com/projectdiscovery/retryablehttp-go" "github.com/stretchr/testify/require" ) +type recordingTransport struct { + inner http.RoundTripper + paths []string +} + +func (rt *recordingTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if rt.inner == nil { + rt.inner = http.DefaultTransport + } + rt.paths = append(rt.paths, req.URL.Path) + return rt.inner.RoundTrip(req) +} + func TestLinkCreation(t *testing.T) { jiraIntegration := &Integration{} link := jiraIntegration.CreateLink("ProjectDiscovery", "https://projectdiscovery.io") @@ -188,3 +204,67 @@ Priority: {{if eq .Severity "critical"}}{{.Severity | upper}}{{else}}{{.Severity require.Contains(t, result, "CRITICAL") }) } + +// Live test to verify SearchV2JQL hits /rest/api/3/search/jql when creds are provided via env +func TestJiraLive_SearchV2UsesJqlEndpoint(t *testing.T) { + jiraURL := os.Getenv("JIRA_URL") + jiraEmail := os.Getenv("JIRA_EMAIL") + jiraAccountID := os.Getenv("JIRA_ACCOUNT_ID") + jiraToken := os.Getenv("JIRA_TOKEN") + jiraPAT := os.Getenv("JIRA_PAT") + jiraProjectName := os.Getenv("JIRA_PROJECT_NAME") + jiraProjectID := os.Getenv("JIRA_PROJECT_ID") + jiraStatusNot := os.Getenv("JIRA_STATUS_NOT") + jiraCloud := os.Getenv("JIRA_CLOUD") + + if jiraURL == "" || (jiraPAT == "" && jiraToken == "") || (jiraEmail == "" && jiraAccountID == "") || (jiraProjectName == "" && jiraProjectID == "") { + t.Skip("live Jira test skipped: missing JIRA_* env vars") + } + + statusNot := jiraStatusNot + if statusNot == "" { + statusNot = "Done" + } + + isCloud := !strings.EqualFold(jiraCloud, "false") && jiraCloud != "0" + + rec := &recordingTransport{} + rc := retryablehttp.NewClient(retryablehttp.DefaultOptionsSingle) + rc.HTTPClient.Transport = rec + + opts := &Options{ + Cloud: isCloud, + URL: jiraURL, + Email: jiraEmail, + AccountID: jiraAccountID, + Token: jiraToken, + PersonalAccessToken: jiraPAT, + ProjectName: jiraProjectName, + ProjectID: jiraProjectID, + IssueType: "Task", + StatusNot: statusNot, + HttpClient: rc, + } + + integration, err := New(opts) + require.NoError(t, err) + + event := &output.ResultEvent{ + Host: "example.com", + Info: model.Info{ + Name: "Nuclei Live Verify", + SeverityHolder: severity.Holder{Severity: severity.Low}, + }, + } + + _, _ = integration.FindExistingIssue(event, true) + + var hitSearchV2 bool + for _, p := range rec.paths { + if strings.HasSuffix(p, "/rest/api/3/search/jql") { + hitSearchV2 = true + break + } + } + require.True(t, hitSearchV2, "expected client to call /rest/api/3/search/jql, got paths: %v", rec.paths) +}