From 80f74df3008bf12a9bca95cbe9365e6719900418 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Wed, 25 Nov 2026 11:14:05 +0100 Subject: [PATCH 1/2] Fix state-matching defect in RouteSegmentedConnPool compatible(needed, have) treated a null requested state as a wildcard, so pollAvailable() would hand a stateless lease the first available entry regardless of its bound state. A connection carrying another user's state (NTLM/Kerberos/TLS client-cert principal) could therefore be reused by a request that presented no credentials at all, silently breaking UserTokenHandler's documented contract that user-specific resources are never shared across users. StrictConnPool and LaxConnPool already require an exact state match; align RouteSegmentedConnPool (@Experimental) with that semantics. This commit: the fix itself. Test rewrite and new coverage follow in the next commit. --- .../org/apache/hc/core5/pool/RouteSegmentedConnPool.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/RouteSegmentedConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/RouteSegmentedConnPool.java index 22b5ed0d5..5d6ab26e9 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/RouteSegmentedConnPool.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/RouteSegmentedConnPool.java @@ -685,9 +685,6 @@ private void offerAvailable(final Segment seg, final PoolEntry p) { } private PoolEntry pollAvailable(final Segment seg, final Object neededState) { - if (neededState == null) { - return seg.available.pollFirst(); - } for (final PoolEntry p : seg.available) { if (compatible(neededState, p.getState())) { if (seg.available.remove(p)) { @@ -699,7 +696,7 @@ private PoolEntry pollAvailable(final Segment seg, final Object neededStat } private boolean compatible(final Object needed, final Object have) { - return needed == null || Objects.equals(needed, have); + return Objects.equals(needed, have); } private boolean handOffToCompatibleWaiter(final PoolEntry entry, final Segment seg) { From 8ee72cbeea39907c8ab9465cec7c02ce1f8915df Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Wed, 25 Nov 2026 11:15:07 +0100 Subject: [PATCH 2/2] Rewrite the test that locked in the old null-wildcard behavior, add coverage stateCompatibilityNullMatchesAnything asserted the buggy behavior as intentional; renamed to stateCompatibilityNullOnlyMatchesNull and inverted the assertion. Added statelessLeaseMustNotReuseAnotherUsersStatefulEntry and strictAndLaxPoolsDoNotReuseAnotherUsersStatefulEntry, the latter pinning StrictConnPool/LaxConnPool as the reference semantics this change aligns RouteSegmentedConnPool with. --- .../pool/RouteSegmentedConnPoolTest.java | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/httpcore5/src/test/java/org/apache/hc/core5/pool/RouteSegmentedConnPoolTest.java b/httpcore5/src/test/java/org/apache/hc/core5/pool/RouteSegmentedConnPoolTest.java index fb190b40b..09bd15ffe 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/pool/RouteSegmentedConnPoolTest.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/pool/RouteSegmentedConnPoolTest.java @@ -30,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -112,9 +113,9 @@ void perRouteAndTotalLimits() throws Exception { } @Test - void stateCompatibilityNullMatchesAnything() throws Exception { + void stateCompatibilityNullOnlyMatchesNull() throws Exception { final RouteSegmentedConnPool pool = - newPool(1, 1, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close); + newPool(2, 2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close); final PoolEntry e = pool.lease("r", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); e.assignConnection(new FakeConnection()); @@ -122,10 +123,11 @@ void stateCompatibilityNullMatchesAnything() throws Exception { e.updateExpiry(TimeValue.ofSeconds(30)); pool.release(e, true); - // waiter with null state must match + // a lease with null state must not be handed an entry bound to state "X" final PoolEntry got = pool.lease("r", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); - assertSame(e, got); + assertNotSame(e, got); + assertFalse(got.hasConnection()); pool.release(got, false); pool.close(CloseMode.IMMEDIATE); } @@ -519,4 +521,53 @@ void cancelledAndReenqueuedLiveSegmentDoesNotBlockAnotherPendingRoute() throws E pool.close(CloseMode.IMMEDIATE); } + @Test + void statelessLeaseMustNotReuseAnotherUsersStatefulEntry() throws Exception { + final RouteSegmentedConnPool pool = + newPool(2, 2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close); + + final PoolEntry aliceEntry = + pool.lease("route", "alice", Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + final FakeConnection aliceConnection = new FakeConnection(); + aliceEntry.assignConnection(aliceConnection); + aliceEntry.updateState("alice"); + pool.release(aliceEntry, true); + + final PoolEntry anonEntry = + pool.lease("route", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + + assertFalse(anonEntry.hasConnection(), + "A lease with no state must not be handed alice's authenticated connection"); + + pool.release(anonEntry, false); + pool.close(CloseMode.IMMEDIATE); + } + + @Test + void strictAndLaxPoolsDoNotReuseAnotherUsersStatefulEntry() throws Exception { + final StrictConnPool strict = + new StrictConnPool<>(2, 2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close, null); + final PoolEntry strictAlice = + strict.lease("route", "alice", Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + strictAlice.assignConnection(new FakeConnection()); + strictAlice.updateState("alice"); + strict.release(strictAlice, true); + final PoolEntry strictAnon = + strict.lease("route", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + assertFalse(strictAnon.hasConnection(), "StrictConnPool must not reuse alice's entry for a stateless lease"); + strict.close(CloseMode.IMMEDIATE); + + final LaxConnPool lax = + new LaxConnPool<>(2, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, FakeConnection::close, null); + final PoolEntry laxAlice = + lax.lease("route", "alice", Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + laxAlice.assignConnection(new FakeConnection()); + laxAlice.updateState("alice"); + lax.release(laxAlice, true); + final PoolEntry laxAnon = + lax.lease("route", null, Timeout.ofSeconds(1), null).get(1, TimeUnit.SECONDS); + assertFalse(laxAnon.hasConnection(), "LaxConnPool must not reuse alice's entry for a stateless lease"); + lax.close(CloseMode.IMMEDIATE); + } + }