Skip to content

Rework JWT detector to better block local IPs#4607

Merged
bradlarsen merged 7 commits intomainfrom
jwt-local-issuer
Jan 13, 2026
Merged

Rework JWT detector to better block local IPs#4607
bradlarsen merged 7 commits intomainfrom
jwt-local-issuer

Conversation

@bradlarsen
Copy link
Copy Markdown
Contributor

@bradlarsen bradlarsen commented Dec 17, 2025

This PR updates the JWT detector to more simply and more robustly prevent making requests to local IPs.

The previous implementation did its checking at the hostname level, but this approach doesn't work in general when redirects are involved. The new implementation has less code and does its checking at the HTTP transport level, after DNS resolution.

This PR also adds HTTP instrumentation to the DetectorHttpClientWith* clients, as already exists in SaneHttpClient. This change should give us insight into some blind spots, as the DetectorHttpClientWith* clients are used in a few dozen existing detectors and previously had no HTTP instrumentation.

@bradlarsen bradlarsen requested a review from a team December 17, 2025 19:14
@bradlarsen bradlarsen requested a review from a team as a code owner December 17, 2025 19:14
Copy link
Copy Markdown
Contributor

@shahzadhaider1 shahzadhaider1 left a comment

Choose a reason for hiding this comment

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

Approving to unblock. This looks good from a security standpoint, and I agree with using the detector HTTP client here.

One thing I hope we have considered: switching from common.SaneHttpClient means we lose the retry/instrumentation layer (metrics we currently export to Prometheus/Grafana).

Copy link
Copy Markdown
Contributor

@rosecodym rosecodym left a comment

Choose a reason for hiding this comment

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

I like the code consolidation but I think it's a bummer that this costs us some instrumentation. Did you look into whether we can get that easily as well? (If we can't, don't worry about it.)

I believe this PR does introduce a data race, though.

Comment thread pkg/detectors/jwt/jwt.go Outdated
Comment thread pkg/detectors/jwt/jwt.go
Comment thread pkg/detectors/http.go
Comment on lines +166 to +175
client := &http.Client{
Transport: NewDetectorTransport(nil),
Timeout: DefaultResponseTimeout,
}

for _, opt := range opts {
opt(httpClient)
opt(client)
}
return httpClient

client.Transport = common.NewInstrumentedTransport(client.Transport)
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.

@rosecodym I added instrumentation like we use in SaneHttpClient here in NewDetectorHttpClient. A few dozen detectors used these DetectorHttpClientWith* clients instead of SaneHttpClient, meaning they were totally missing the HTTP instrumentation before.

Does this look reasonable to you? Tests have passed locally for me.

@bradlarsen bradlarsen requested a review from rosecodym January 13, 2026 16:52
@bradlarsen bradlarsen merged commit 728d71f into main Jan 13, 2026
12 checks passed
@bradlarsen bradlarsen deleted the jwt-local-issuer branch January 13, 2026 20:13
peterfraedrich pushed a commit to peterfraedrich/trufflehog that referenced this pull request Mar 15, 2026
…on (trufflesecurity#4607)

* Rework JWT detector to better block local IPs
* Add comment about concurrency / data races to `Detector`
* Add instrumentation from `common.SaneHttpClient` to `detectors.NewDetectorHttpClient`
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.

4 participants