Skip to content

[INS-345] Add New Relic Insights Query Key detector#4781

Open
mustansir14 wants to merge 8 commits intotrufflesecurity:mainfrom
mustansir14:INS-345-Detector-Visible-New-Relic-Query-Key
Open

[INS-345] Add New Relic Insights Query Key detector#4781
mustansir14 wants to merge 8 commits intotrufflesecurity:mainfrom
mustansir14:INS-345-Detector-Visible-New-Relic-Query-Key

Conversation

@mustansir14
Copy link
Copy Markdown
Contributor

@mustansir14 mustansir14 commented Mar 3, 2026

Description:

This PR adds the New Relic Insights Query Key Detector.

Regex:
Key: \b(NRIQ-[a-zA-Z0-9-_]{25})
Account ID: detectors.PrefixRegex([]string{"relic", "account", "id"}) + '\b(\d{4,10})\b'
The key is the actual credential but account ID is required for verification because the verification endpoint requires an account ID in the path, and there is no other deterministic way to verify the credential without specifying the valid account ID (invalid or malformed account IDs return the same response as an invalid key)

Verification:
For verification, we use the Insights Query API with a simple select query: https://insights-api.newrelic.com/v1/accounts/[account_id]/query?nrql=SELECT%%201.
We send a GET request. A response code of 200 means the key is valid. 401 means it is an invalid/rotated key.
Note: For EU region keys, the host should be insights-api.eu.newrelic.com

Corpora Test:
The detector does not appear in the list.
image
image

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

Note

Medium Risk
Adds a new secret detector with live HTTP verification against New Relic’s API, which can affect scan behavior (extra network calls) and introduce false positives/negatives if the regex or verification semantics are off.

Overview
Adds a new NewRelicInsightsQueryKey detector that matches NRIQ-... keys and associated New Relic account IDs, emitting combined RawV2 values for multipart correlation.

Implements optional verification by calling the legacy Insights Query API against both US and EU endpoints (setting X-Query-Key) and records the verified region in ExtraData.

Wires the detector into the default detector list and registers a new protobuf DetectorType enum value (NewRelicInsightsQueryKey = 1045), with unit + integration tests and a benchmark for the new scanner.

Reviewed by Cursor Bugbot for commit a767a92. Bugbot is set up for automated code reviews on this repo. Configure here.

@mustansir14 mustansir14 requested a review from a team March 3, 2026 11:02
@mustansir14 mustansir14 requested review from a team as code owners March 3, 2026 11:02
Comment thread pkg/detectors/newrelicinsightsquerykey/newrelicinsightsquerykey.go Outdated
Comment thread pkg/detectors/newrelicinsightsquerykey/newrelicinsightsquerykey.go
uniqueAccountIDMatches[match[1]] = struct{}{}
}

for _, keyMatch := range keyMatches {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key matches not deduplicated unlike other multi-part detectors

Medium Severity

Account ID matches are deduplicated into uniqueAccountIDMatches, but keyMatches is iterated directly from FindAllStringSubmatch without deduplication. Other multi-part detectors in the codebase (e.g., adobeio, airbrakeprojectkey, airship) consistently deduplicate both parts into maps before the nested loops. This inconsistency means duplicate keys in scanned data produce duplicate results and redundant verification API calls.

Fix in Cursor Fix in Web

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended. The idea is to report multiple results if the key appears in multiple places, so that the user can remove it from all of those places.

Account ID matches are deduplicated because they are not the primary credential.

regionUrls := map[string]string{
"us": fmt.Sprintf("https://insights-api.newrelic.com/v1/accounts/%s/query?nrql=SELECT%%201", accountID),
"eu": fmt.Sprintf("https://insights-api.eu.newrelic.com/v1/accounts/%s/query?nrql=SELECT%%201", accountID),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) suggestion: use net/url insted of fmt.Sprintf()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would unnecessarily complicate the code. We prefer net/url when we are concatenating user inputted URLs. Here we are simply inserting the account ID (which also comes via a strict regex so it does not have any chances of being malformed) into a URL.

Comment on lines +107 to +110
res, err := client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error making request: %w", err)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the US request times out or fails at the network level, you bail out entirely without trying the EU region endpoint. You probably want to try the next region instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm good catch. I agree. I'll just extract the inside of the loop into a separate method, which should also fix the deferred response body cleanup issue by bugbot.

Comment thread pkg/detectors/newrelicinsightsquerykey/newrelicinsightsquerykey.go Outdated
Comment thread pkg/detectors/newrelicinsightsquerykey/newrelicinsightsquerykey.go Outdated
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

results = append(results, s1)
}

}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No results emitted when account ID is absent

High Severity

When no account ID is found in the data chunk, uniqueAccountIDMatches is empty, so the inner for accountID := range uniqueAccountIDMatches loop never executes. This means the detector silently produces zero results even when valid NRIQ- keys are present. The PR description states the key is the actual credential and account ID is only needed for verification. Other multi-part detectors in this codebase (e.g., Atlassian v2) handle this by inserting an empty string entry into the map when no secondary matches are found, ensuring unverified results are still reported.

Fix in Cursor Fix in Web

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Account ID is required for verification. The atlassian detector you mentioned captures the organization_id only to pass it into AnalysisInfo. It's not needed for verification

Copy link
Copy Markdown
Contributor

@shahzadhaider1 shahzadhaider1 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the key is useless without an account ID? It can't be used to access any sort of data on its own?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all endpoints that it can access require an account ID

Redacted: keyResMatch[:8] + "...",
}

if verify && accountIDResMatch != "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in what scenarios, accountIDResMatch can be empty at this point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot. Good catch. I'll remove the check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants