Skip to content

Fix getDispatchedPaths() leaking mutable nested lists - #13039

Merged
gnodet merged 1 commit into
apache:masterfrom
mcc0nnell:fix-unmodifiable-dispatched-paths
Sep 6, 2026
Merged

Fix getDispatchedPaths() leaking mutable nested lists#13039
gnodet merged 1 commit into
apache:masterfrom
mcc0nnell:fix-unmodifiable-dispatched-paths

Conversation

@mcc0nnell

@mcc0nnell mcc0nnell commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Problem

DefaultDependencyResolverResult#getDispatchedPaths() returned an unmodifiable outer map, but the List<Path> values inside were still the resolver's mutable internal lists.

A caller could therefore mutate resolver state through what appears to be a read-only API:

result.getDispatchedPaths().get(JavaPathType.CLASSES).clear();

That mutation can desynchronize the correlated paths, dispatchedPaths, and dependencies views maintained by DefaultDependencyResolverResult.

Fix

Wrap each nested path list with Collections.unmodifiableList before exposing it through the returned map. The returned map remains unmodifiable and preserves insertion order. Internal lists stay mutable while Maven constructs the resolver result, but callers can no longer mutate them through getDispatchedPaths().

This follows the same immutability expectation as the surrounding resolver-result collection APIs.

Tests

Adds regression coverage verifying that:

  • the returned outer map is unmodifiable
  • nested List<Path> values are also unmodifiable
  • path and PathType insertion order is preserved
  • rejected mutation attempts do not alter the correlated paths or dependencies views

On the previous implementation, the nested-list regression fails because clear() succeeds.

Related

Found while reviewing the collection-mutability coverage around #10389 and #11410. #11410 adds tests around other resolver-result collection getters but does not cover the nested lists returned by getDispatchedPaths(). This PR addresses that separate mutability leak.

The outer map returned by DependencyResolverResult.getDispatchedPaths()
was unmodifiable, but the List<Path> values were the internal ArrayList
instances. Callers could mutate those lists and desynchronize the
correlated paths, dispatchedPaths, and dependencies views.

Wrap each nested list as well as the outer map before returning, while
preserving PathType and path insertion order.

Signed-off-by: Robert McConnell <robert@mcc0nnell.org>

@gnodet gnodet left a comment

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.

Clean fix for a real mutability leak. The nested List<Path> values inside getDispatchedPaths() were the internal ArrayList instances — callers could silently corrupt resolver state through what looks like a read-only API.

The defensive-copy approach (new LinkedHashMap + Collections.unmodifiableList per entry) is the right call here. Caching the unmodifiable view would be stale during construction (while addPathElement() is still populating), and the call sites are few enough that per-call allocation is negligible.

Test coverage is solid: outer map immutability, nested list immutability, insertion-order preservation, and sibling-view integrity. The regression test correctly fails on the previous implementation.

Backport note: The same vulnerable code exists on maven-4.0.x — worth a port/4.0.x label.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet added the fix label Sep 6, 2026
@gnodet gnodet added this to the 4.1.0 milestone Sep 6, 2026
@gnodet
gnodet merged commit cc4a0b7 into apache:master Sep 6, 2026
22 checks passed
@gnodet gnodet added bug Something isn't working and removed fix labels Sep 6, 2026
gnodet pushed a commit that referenced this pull request Sep 6, 2026
The outer map returned by DependencyResolverResult.getDispatchedPaths()
was unmodifiable, but the List<Path> values were the internal ArrayList
instances. Callers could mutate those lists and desynchronize the
correlated paths, dispatchedPaths, and dependencies views.

Wrap each nested list as well as the outer map before returning, while
preserving PathType and path insertion order.

Signed-off-by: Robert McConnell <robert@mcc0nnell.org>
gnodet added a commit that referenced this pull request Sep 6, 2026
The outer map returned by DependencyResolverResult.getDispatchedPaths()
was unmodifiable, but the List<Path> values were the internal ArrayList
instances. Callers could mutate those lists and desynchronize the
correlated paths, dispatchedPaths, and dependencies views.

Wrap each nested list as well as the outer map before returning, while
preserving PathType and path insertion order.

Signed-off-by: Robert McConnell <robert@mcc0nnell.org>
Co-authored-by: Robert McConnell <robert@mcc0nnell.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants