Skip to content

Commit 1254b46

Browse files
committed
Limit the length of outbound X-Correlation-ID header values to 512 bytes to avoid risk of pathological client sending an enormous correlation id and this causing unexpected 400 errors for internal calls
1 parent f9bf004 commit 1254b46

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

guice/common/src/main/java/com/peterphi/std/guice/restclient/resteasy/impl/TracingClientRequestFilter.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.peterphi.std.util.tracing.Tracing;
44
import com.peterphi.std.util.tracing.TracingConstants;
5+
import org.apache.commons.lang.StringUtils;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

@@ -14,7 +15,9 @@
1415
public class TracingClientRequestFilter implements ClientRequestFilter
1516
{
1617
private static final Logger log = LoggerFactory.getLogger(TracingClientRequestFilter.class);
18+
private static final int MAX_TRACE_SIZE = 512;
1719

20+
private static boolean hasLoggedOversizedCorrelationId = false;
1821

1922
@Override
2023
public void filter(final ClientRequestContext requestContext) throws IOException
@@ -26,7 +29,30 @@ public void filter(final ClientRequestContext requestContext) throws IOException
2629
if (requestContext.getHeaders().containsKey(TracingConstants.HTTP_HEADER_CORRELATION_ID))
2730
log.warn("Duplicate call to tracing filter {} for {} for {}", this, requestContext, requestContext.getUri());
2831

29-
requestContext.getHeaders().putSingle(TracingConstants.HTTP_HEADER_CORRELATION_ID, traceId);
32+
final String val;
33+
if (traceId.length() <= MAX_TRACE_SIZE)
34+
{
35+
val = traceId;
36+
}
37+
else
38+
{
39+
val = StringUtils.abbreviateMiddle(traceId, "...", MAX_TRACE_SIZE);
40+
41+
if (!hasLoggedOversizedCorrelationId)
42+
{
43+
log.warn(
44+
"Correlation ID {} is over max size {}, truncating. Future instances of this error in this webapp will be logged at DEBUG.",
45+
traceId,
46+
MAX_TRACE_SIZE);
47+
hasLoggedOversizedCorrelationId = true;
48+
}
49+
else if (log.isDebugEnabled())
50+
{
51+
log.debug("Correlation ID {} is over limit, truncating", traceId);
52+
}
53+
}
54+
55+
requestContext.getHeaders().putSingle(TracingConstants.HTTP_HEADER_CORRELATION_ID, val);
3056

3157
final Tracing trace = Tracing.peek();
3258

0 commit comments

Comments
 (0)