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.
DefaultDependencyResolverResult#getDispatchedPaths() returned an unmodifiable outer map, but the List values inside that map were still the resolver’s mutable internal lists.
A caller could therefore mutate resolver state through what appears to be a read-only result API:
result.getDispatchedPaths().get(JavaPathType.CLASSES).clear();
That mutation can desynchronize the correlated paths, dispatchedPaths, and dependencies views maintained by DefaultDependencyResolverResult.
Change
Wrap each nested path list with Collections.unmodifiableList before exposing it through the returned map.
The returned map remains unmodifiable and preserves insertion order. The internal lists remain 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:
On the previous implementation, the nested-list regression fails because clear() succeeds.
Relation to #10389 / #11410
This was 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.
Verification
Ran:
mvn -pl impl/maven-impl -am test -Dtest=DefaultDependencyResolverResultTest
mvn -pl impl/maven-impl verify
The module verification completed with 676 tests and 0 failures.
The Core IT suite has not been run.
Following this checklist to help us incorporate your
contribution quickly and easily:
Note that commits might be squashed by a maintainer on merge.
This may not always be possible but is a best-practice.
A more thorough check will be performed on your pull request automatically.
If your pull request is about ~20 lines of code you don’t need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.
To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.