Skip to content

feat(matcher/java): add per-request timeout for Maven SHA1 lookups (#1624)#3414

Open
ChrisJr404 wants to merge 1 commit into
anchore:mainfrom
ChrisJr404:add-maven-search-timeout
Open

feat(matcher/java): add per-request timeout for Maven SHA1 lookups (#1624)#3414
ChrisJr404 wants to merge 1 commit into
anchore:mainfrom
ChrisJr404:add-maven-search-timeout

Conversation

@ChrisJr404

Copy link
Copy Markdown

Description

Closes #1624.

Adds a new external-sources.maven.timeout config field that is wired through to the HTTP client used by the Java matcher's Maven SHA1 upstream search.

external-sources:
  enable: true
  maven:
    search-upstream: true
    timeout: 5s   # new — per-request limit for the SHA1 lookup

The default is 10s, and a value of 0 disables the limit (preserving the previous behavior of http.DefaultClient with no timeout).

Why this fixes the reported pain

The issue describes the scenario where Maven Central (or any configured base-url) is unresponsive: grype falls back to a SHA1 lookup, the request hangs indefinitely, and the user sees a stuck scan with no console signal until they enable --debug. With this change, a per-request timeout fires, the lookup returns an error, and the scan continues with the remaining matchers.

Per-request vs cumulative time

@tgerla suggested in the issue that "total time would be a good starting point" with cascading global + per-source overrides. This PR ships the smallest correct unit first — a per-request timeout — because a single hung request was the actual failure mode in the report. A cumulative time budget can build on top of this once a second external source lands; right now there is only one (Maven), so a global abort-after would be functionally identical to the per-source one.

If the project would prefer a different shape (cumulative-only, or abort-after instead of timeout), happy to revise — small enough patch.

Implementation notes

  • New Timeout time.Duration field on the maven struct in cmd/grype/cli/options/datasources.go, default 10s, mapped through ToJavaMatcherConfig() as MavenTimeout.
  • New MavenTimeout time.Duration field on java.ExternalSearchConfig.
  • NewJavaMatcher constructs a dedicated *http.Client with the configured timeout when it is non-zero, so the timeout does not leak onto unrelated callers of http.DefaultClient. When the timeout is zero, the existing http.DefaultClient is used unchanged.

Tests

Two new sub-tests in TestNewJavaMatcherTimeout:

  • timeout aborts hanging upstream — boots an httptest.Server whose handler blocks on a never-closed channel, configures a MavenTimeout of 100ms, and asserts that GetMavenPackageBySha returns an error within ~2s.
  • zero timeout disables the per-request limit — asserts the underlying http.Client.Timeout is 0 when no MavenTimeout is configured (so default behavior is preserved).

Test plan

  • go build ./... clean
  • go test ./grype/matcher/java/ -run TestNewJavaMatcherTimeout passes
  • go test ./grype/matcher/java/ ./cmd/grype/cli/options/ — no regressions
  • grype --help and YAML config still load with the new field set, default value populated

Closes anchore#1624

Adds a new `external-sources.maven.timeout` config field that is wired
through to the HTTP client used by the Java matcher's Maven SHA1
upstream search. The default is 10s, and a value of 0 disables the
limit (preserving the previous behavior of using `http.DefaultClient`
with no timeout).

When grype encounters a Java archive whose POM data is missing, it
falls back to Maven Central via SHA1 lookup. If Maven Central is
unresponsive, the scan would previously hang indefinitely with nothing
visible in the default UI. With this change a per-request timeout
fires and the scan continues with the remaining matchers.

Per @tgerla's note on the issue, total time would be a good starting
point — this PR ships a per-request timeout as the smallest correct
unit (a hung individual request was the failure mode the issue
described). A cumulative time budget across all upstream lookups can
build on top of this once another external source lands.

Adds two test cases:
- `timeout aborts hanging upstream` — boots an httptest server that
  hangs forever and asserts the matcher returns an error within ~2s
  when configured with a 100ms timeout.
- `zero timeout disables the per-request limit` — asserts the
  underlying `http.Client.Timeout` is zero when the config is unset.

Signed-off-by: Chris (ChrisJr404) <11917633+ChrisJr404@users.noreply.github.com>
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.

Allow configurting timeout for external-sources

1 participant