Skip to content

Fix getDispatchedPaths() leaking mutable nested lists - #13039

Open
mcc0nnell wants to merge 1 commit into
apache:masterfrom
mcc0nnell:fix-unmodifiable-dispatched-paths
Open

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

Conversation

@mcc0nnell

@mcc0nnell mcc0nnell commented Sep 4, 2026

Copy link
Copy Markdown

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:

  • the returned outer map is unmodifiable;
  • nested List 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.

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:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the Core IT successfully.

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.

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.

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.

2 participants