gnodet-bot commented on code in PR #31135:
URL: https://github.com/apache/camel/pull/31135#discussion_r4744439317
##########
components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java:
##########
@@ -344,6 +354,32 @@ protected HttpClient createHttpClient() {
clientBuilder.setRedirectStrategy(DefaultRedirectStrategy.INSTANCE);
}
+ if (ObjectHelper.isNotEmpty(oauthProfile)) {
+ final String profile = oauthProfile;
+ final URI endpointUri = getHttpUri();
+ clientBuilder.addRequestInterceptorFirst((HttpRequest request,
EntityDetails entity, HttpContext context) -> {
+ try {
+ if (!OAuth2ClientConfigurer.isTargetAuthority(request,
endpointUri)) {
+ LOG.debug(
+ "Not attaching the oauthProfile bearer token
to {}, which is not the endpoint's authority {}",
+ request.getRequestUri(), endpointUri);
+ return;
+ }
+ String token =
OAuthHelper.resolveOAuthToken(getCamelContext(), profile);
+ if (token == null || token.isBlank()) {
+ throw new HttpException(
+ "OAuth profile '" + profile + "' resolved a
null or blank token");
+ }
+ request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " +
token);
+ } catch (HttpException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new HttpException(
+ "Failed to resolve OAuth token for profile '" +
profile + "': " + e.getMessage(), e);
+ }
+ });
+ }
Review Comment:
⚠️ **`oauthProfile` interceptor registered before
`OAuth2ClientConfigurer.configureHttpClient()` runs — token-POST client
inherits it.**
`OAuth2ClientConfigurer.configureHttpClient()` calls `clientBuilder.build()`
as its first line to snapshot the builder and create the token-endpoint HTTP
client. At that point the `oauthProfile` interceptor is already in the builder
(registered via `addRequestInterceptorFirst` on the lines above), so it is
baked into the token-POST client.
For the common case — token endpoint on a different host than the API
endpoint — `isTargetAuthority` returns `false` and the interceptor skips. No
harm. But when the token endpoint shares the same authority as the API endpoint
(a gateway that handles both, or a local IdP stub in a dev environment),
`isTargetAuthority` returns `true`, the interceptor calls
`request.setHeader(AUTHORIZATION, "Bearer " + token)`, **overwriting** the
`Authorization` header the `OAuth2ClientConfigurer` interceptor set for the
token POST. The grant request lands with a Bearer token instead of client
credentials and is rejected by the token endpoint.
Fix: move this block to after
`configurer.configureHttpClient(clientBuilder)`:
```java
HttpClientConfigurer configurer = getHttpClientConfigurer();
if (configurer != null) {
configurer.configureHttpClient(clientBuilder);
}
if (ObjectHelper.isNotEmpty(oauthProfile)) {
final String profile = oauthProfile;
final URI endpointUri = getHttpUri();
clientBuilder.addRequestInterceptorFirst((HttpRequest request,
EntityDetails entity, HttpContext context) -> {
// ... (same body as today)
});
}
```
The `OAuth2ClientConfigurer` interceptor is registered inside
`configureHttpClient()`, not on the builder before the call — moving the
`oauthProfile` block after has no effect on the interceptor ordering for the
main client path.
##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -151,16 +151,37 @@ public void configureHttpClient(HttpClientBuilder
clientBuilder) {
}
private boolean isTargetAuthority(URI requestUri) {
- if (targetUri == null) {
+ return sameAuthority(targetUri, requestUri);
+ }
+
+ /**
+ * Whether the given HTTP request targets the same authority (scheme,
host, effective port) as {@code endpointUri}.
+ * <p>
+ * Used by {@link HttpProducer} to guard against sending bearer tokens to
a redirect target chosen by the remote
+ * server rather than by the route author.
+ */
+ static boolean isTargetAuthority(HttpRequest request, URI endpointUri) {
Review Comment:
**Nit: stale Javadoc — `{@link HttpProducer}` should be `{@link
HttpEndpoint}`.**
After the refactoring the interceptor is registered in
`HttpEndpoint.createHttpClient()`, not in `HttpProducer`.
```suggestion
/**
* Whether the given HTTP request targets the same authority (scheme,
host, effective port) as {@code endpointUri}.
* <p>
* Used by {@link HttpEndpoint} to guard against sending bearer tokens
to a redirect target chosen by the remote
* server rather than by the route author.
*/
static boolean isTargetAuthority(HttpRequest request, URI endpointUri) {
```
##########
components/camel-http/src/test/java/org/apache/camel/component/http/HttpOAuth2RedirectTokenLeakTest.java:
##########
@@ -129,6 +129,24 @@ public void
theBearerTokenIsNotSentToARedirectTarget(boolean differentHost) {
.as("the bearer token must not be re-attached to the authority
the Location header named").isNull();
}
+ @ParameterizedTest
+ @ValueSource(booleans = { true, false })
+ public void
theBearerTokenFromOauthProfileIsNotSentToARedirectTarget(boolean differentHost)
{
+ HttpComponent http = context.getComponent("http", HttpComponent.class);
+ http.setFollowRedirects(true);
+
+ String redirectPath = differentHost ? "/redirect-to-different-host" :
"/redirect-to-different-port";
+ String uri = "http://localhost:" + localServer.getLocalPort() +
redirectPath + "?oauthProfile=myprofile";
+
+ authorizationSeenAfterRedirect.set(null);
+ String body = fluentTemplate.to(uri).request(String.class);
+
+ assertThat(body).as("the redirect should still have been
followed").isEqualTo("Bye World");
+ assertThat(authorizationSeenAfterRedirect.get())
+ .as("the oauthProfile bearer token must not be re-attached to
the authority the Location header named")
+ .isNull();
Review Comment:
**Nit: test passes even if `oauthProfile` does nothing — first-hop assertion
missing.**
Asserting only `authorizationSeenAfterRedirect.isNull()` also passes when
the `oauthProfile` interceptor is not registered at all. Add a capture on the
original server handler (the one at `redirectPath`, not the redirect target)
and assert it received `Bearer stub-token-for-myprofile`. That makes the test a
proper round-trip proof: token present on the first hop, absent on the
cross-authority redirect.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]