Skip to content

Commit bdc85ac

Browse files
committed
chore: validating non-allowed circular redirect
1 parent 40175ce commit bdc85ac

File tree

1 file changed

+23
-5
lines changed
  • instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2

1 file changed

+23
-5
lines changed

instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/OtelExecChainHandler.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import jakarta.annotation.Nullable;
1212
import java.io.IOException;
1313
import java.net.URI;
14+
import org.apache.hc.client5.http.CircularRedirectException;
1415
import org.apache.hc.client5.http.ClientProtocolException;
1516
import org.apache.hc.client5.http.classic.ExecChain;
1617
import org.apache.hc.client5.http.classic.ExecChain.Scope;
@@ -105,7 +106,8 @@ private boolean pendingRedirect(
105106
HttpClientContext httpContext,
106107
HttpRequest request,
107108
ApacheHttpClient5Request instrumenterRequest,
108-
@Nullable ClassicHttpResponse response) {
109+
@Nullable ClassicHttpResponse response)
110+
throws HttpException {
109111
if (response == null) {
110112
return false;
111113
}
@@ -130,17 +132,18 @@ private boolean pendingRedirect(
130132
// a circular redirect, which happens before exec decorators run.
131133
RedirectLocations redirectLocations =
132134
(RedirectLocations) httpContext.getAttribute(HttpClientContext.REDIRECT_LOCATIONS);
133-
if (redirectLocations != null) {
135+
if (!redirectLocations.getAll().isEmpty()) {
134136
RedirectLocations copy = new RedirectLocations();
135137
for (URI uri : redirectLocations.getAll()) {
136138
copy.add(uri);
137139
}
138140

139141
try {
140-
DefaultRedirectStrategy.INSTANCE.getLocationURI(request, response, httpContext);
141-
} catch (HttpException e) {
142+
getLocationUri(request, response, httpContext, copy);
143+
} catch (ProtocolException e) {
142144
// We will not be returning to the Exec, finish the span.
143-
instrumenter.end(context, instrumenterRequest, response, new ClientProtocolException(e));
145+
instrumenter.end(
146+
context, instrumenterRequest, response, new ClientProtocolException(e.getMessage(), e));
144147
return true;
145148
} finally {
146149
httpContext.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, copy);
@@ -156,6 +159,21 @@ private boolean pendingRedirect(
156159
return true;
157160
}
158161

162+
private static URI getLocationUri(
163+
HttpRequest request,
164+
HttpResponse response,
165+
HttpClientContext httpContext,
166+
RedirectLocations redirectLocations)
167+
throws HttpException {
168+
URI redirectUri =
169+
DefaultRedirectStrategy.INSTANCE.getLocationURI(request, response, httpContext);
170+
if (!httpContext.getRequestConfig().isCircularRedirectsAllowed()
171+
&& redirectLocations.contains(redirectUri)) {
172+
throw new CircularRedirectException("Circular redirect to '" + redirectUri + "'");
173+
}
174+
return DefaultRedirectStrategy.INSTANCE.getLocationURI(request, response, httpContext);
175+
}
176+
159177
private static ApacheHttpClient5Request getApacheHttpClient5Request(
160178
ClassicHttpRequest request, Scope scope) {
161179
HttpHost host = null;

0 commit comments

Comments
 (0)