Fix getDispatchedPaths() leaking mutable nested lists - #13039
Conversation
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
left a comment
There was a problem hiding this comment.
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.
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>
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>
Problem
DefaultDependencyResolverResult#getDispatchedPaths()returned an unmodifiable outer map, but theList<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:
That mutation can desynchronize the correlated
paths,dispatchedPaths, anddependenciesviews maintained byDefaultDependencyResolverResult.Fix
Wrap each nested path list with
Collections.unmodifiableListbefore 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 throughgetDispatchedPaths().This follows the same immutability expectation as the surrounding resolver-result collection APIs.
Tests
Adds regression coverage verifying that:
List<Path>values are also unmodifiablePathTypeinsertion order is preservedpathsordependenciesviewsOn 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.