Skip to content

do not store responses to authenticated requests in the shared cache - #941

Merged
arturobernalg merged 2 commits into
apache:masterfrom
Hsnmohd:cache-authenticated-response
Nov 16, 2026
Merged

arturobernalg merged 2 commits into
apache:masterfrom
Hsnmohd:cache-authenticated-response

Conversation

@Hsnmohd

@Hsnmohd Hsnmohd commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

CachingExec and AsyncCachingExec run above ProtocolExec, so the RFC 9111 3.5 shared-cache check never sees the Authorization header generated from the CredentialsProvider or AuthCache (the classic exec always sends a copy down the chain, and both protocol execs copy the request on a non-tunnelled proxy route), which let a 200 with Cache-Control: max-age=60 returned to alice's authenticated request be stored in the default shared cache and served to a context with no credentials; the fix treats the response as authenticated when the route target's AuthExchange is in SUCCESS state.

* A shared cache must not store a response to a request that the protocol layer
* authenticated with credentials from the credentials provider.
*/
class TestCachingExecAuthenticatedResponse {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hsnmohd Please move this test case to TestClientAuthentication

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with your follow-up instead: the tests now live in new caching client suites under org.apache.hc.client5.testing.caching, and the class in httpclient5-cache is gone.

@ok2c

ok2c commented Nov 14, 2026

Copy link
Copy Markdown
Member

@Hsnmohd Better yet, consider starting new test suites for caching clients (classic and async). There are TestAsyncServerResources, TestServerResources, CachingHttpAsyncClientResource, CachingHttpClientResource tests extensions that one can use to set up test resources required for such tests suites.

@arturobernalg arturobernalg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced a case not covered by this fix: preemptive authentication sends Authorization while AuthExchange remains UNCHALLENGED, so isTargetAuthenticated() returns false.

Both classic and async caching clients serve Alice's cached private response to an anonymous request instead of returning 401.

We should cover this case before merging.

Check the request last sent to the backend for an Authorization header in addition to the target AuthExchange state, so responses to requests authenticated preemptively from the AuthCache are not stored either. Move the regression tests to new classic and async caching client test suites in httpclient5-testing.
@Hsnmohd
Hsnmohd force-pushed the cache-authenticated-response branch from e7b313e to 117f1d7 Compare November 16, 2026 10:40
@Hsnmohd

Hsnmohd commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

@arturobernalg good catch. With preemptive auth the exchange stays UNCHALLENGED, so isTargetAuthenticated now also checks the request last sent to the backend (context.getRequest(), which the main exec sets after ProtocolExec has added Authorization). I kept the SUCCESS check for connection-based schemes, since they stop resending the header once the exchange succeeds. Both suites have a preemptive case now. On the previous revision those fail with expected: <401> but was: <200> for classic and async, and all four cases fail on master.

@ok2c the tests are now in caching/sync/TestCachingClientAuthentication and caching/async/TestCachingAsyncClientAuthentication, built on TestServerResources / TestAsyncServerResources and CachingHttpClientResource / CachingHttpAsyncClientResource. The async suite uses the test server as a plain proxy, since that's the route where the cache never sees the header. I rebased on master to pick up TestAsyncServerResources.

@ok2c ok2c left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hsnmohd Looks good to me. It would be nice though to fix the deprecation warning and remove the reference to the deprecated method

@arturobernalg Please review.

@arturobernalg arturobernalg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arturobernalg
arturobernalg merged commit c8b7384 into apache:master Nov 16, 2026
12 checks passed
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.

3 participants