Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#11729] Update agent's realipheader value to the list type. #11742

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ profiler.tomcat.tracerequestparam=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.tomcat.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.tomcat.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.tomcat.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.tomcat.realipemptyvalue}, Ignore header value.
Expand All @@ -386,7 +387,8 @@ profiler.jetty.tracerequestparam=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.jetty.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.jetty.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.jetty.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.jetty.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -426,7 +428,9 @@ profiler.jboss.tracerequestparam=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.jboss.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.jboss.realipheader=X-Forwarded-For, Forwarded

# nginx real ip header
#profiler.jboss.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.jboss.realipemptyvalue}, Ignore header value.
Expand All @@ -452,7 +456,8 @@ profiler.resin.hidepinpointheader=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.resin.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.resin.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.resin.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.resin.realipemptyvalue}, Ignore header value.
Expand All @@ -477,7 +482,8 @@ profiler.weblogic.hidepinpointheader=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.weblogic.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.weblogic.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.weblogic.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.weblogic.realipemptyvalue}, Ignore header value.
Expand All @@ -502,7 +508,8 @@ profiler.websphere.hidepinpointheader=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.websphere.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.websphere.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.websphere.realipheader=X-Real-IP
#profiler.websphere.realipemptyvalue=unknown
Expand Down Expand Up @@ -534,7 +541,8 @@ profiler.vertx.http.server.excludeurl=
# profiler.vertx.http.server.trace.excludemethod=
# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.vertx.http.server.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.vertx.http.server.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.vertx.http.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.vertx.http.server.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -579,7 +587,8 @@ profiler.undertow.excludeurl=

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.undertow.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.undertow.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.undertow.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.undertow.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -613,7 +622,8 @@ profiler.reactor-netty.server.excludeurl=

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.reactor-netty.server.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.reactor-netty.server.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.reactor-netty.server.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.reactor-netty.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -651,7 +661,8 @@ profiler.ktor.server.excludeurl=

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.ktor.server.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.ktor.server.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.ktor.server.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.ktor.realipemptyvalue}, Ignore header value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ profiler.tomcat.tracerequestparam=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.tomcat.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.tomcat.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.tomcat.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.tomcat.realipemptyvalue}, Ignore header value.
Expand All @@ -383,7 +384,8 @@ profiler.jetty.tracerequestparam=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.jetty.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.jetty.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.jetty.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.jetty.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -423,7 +425,8 @@ profiler.jboss.tracerequestparam=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.jboss.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.jboss.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.jboss.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.jboss.realipemptyvalue}, Ignore header value.
Expand All @@ -449,7 +452,8 @@ profiler.resin.hidepinpointheader=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.resin.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.resin.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.resin.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.resin.realipemptyvalue}, Ignore header value.
Expand All @@ -474,7 +478,8 @@ profiler.weblogic.hidepinpointheader=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.weblogic.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.weblogic.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.weblogic.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.weblogic.realipemptyvalue}, Ignore header value.
Expand All @@ -499,7 +504,8 @@ profiler.websphere.hidepinpointheader=true

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.websphere.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.websphere.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.websphere.realipheader=X-Real-IP
#profiler.websphere.realipemptyvalue=unknown
Expand Down Expand Up @@ -531,7 +537,8 @@ profiler.vertx.http.server.excludeurl=
# profiler.vertx.http.server.trace.excludemethod=
# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.vertx.http.server.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.vertx.http.server.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.vertx.http.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.vertx.http.server.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -577,7 +584,8 @@ profiler.undertow.excludeurl=

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.undertow.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.undertow.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.undertow.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.undertow.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -610,7 +618,8 @@ profiler.reactor-netty.server.excludeurl=

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.reactor-netty.server.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.reactor-netty.server.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.reactor-netty.server.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.reactor-netty.realipemptyvalue}, Ignore header value.
Expand Down Expand Up @@ -648,7 +657,8 @@ profiler.ktor.server.excludeurl=

# original IP address header
# https://en.wikipedia.org/wiki/X-Forwarded-For
#profiler.ktor.server.realipheader=X-Forwarded-For
# https://datatracker.ietf.org/doc/html/rfc7239
#profiler.ktor.server.realipheader=X-Forwarded-For, Forwarded
# nginx real ip header
#profiler.ktor.server.realipheader=X-Real-IP
# optional parameter, If the header value is ${profiler.ktor.realipemptyvalue}, Ignore header value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.bootstrap.plugin.request.RequestAdaptor;
import com.navercorp.pinpoint.common.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -33,42 +34,44 @@
private static final String FORWARDED_HEADER_NAME = "Forwarded";
private static final Pattern FORWARDED_FOR_PATTERN = Pattern.compile("(?i:for)=\"?([^;,\"]+)\"?");

private final String realIpHeaderName;
private final List<String> realIpHeaderNameList;
private final String realIpHeaderEmptyValue;

public RealIpHeaderResolver(final String realIpHeaderName, final String realIpHeaderEmptyValue) {
this.realIpHeaderName = Objects.requireNonNull(realIpHeaderName, "realIpHeaderName");
Objects.requireNonNull(realIpHeaderName, "realIpHeaderName");
this.realIpHeaderNameList = StringUtils.tokenizeToStringList(realIpHeaderName, ",");

Check warning on line 42 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L41-L42

Added lines #L41 - L42 were not covered by tests
this.realIpHeaderEmptyValue = realIpHeaderEmptyValue;
}

@Override
public String resolve(RequestAdaptor<T> requestAdaptor, T request) {
final String realIpHeaderValue = requestAdaptor.getHeader(request, realIpHeaderName);
if (StringUtils.isEmpty(realIpHeaderValue)) {
return null;
}

if (realIpHeaderEmptyValue != null && realIpHeaderEmptyValue.equalsIgnoreCase(realIpHeaderValue)) {
return null;
}

String firstRealIpHeaderValue = getFirstRealIpHeaderValue(realIpHeaderValue);

if ("Forwarded".equalsIgnoreCase(realIpHeaderName)) {
Matcher matcher = FORWARDED_FOR_PATTERN.matcher(firstRealIpHeaderValue);
if (matcher.find()) {
String ip = matcher.group(1).trim();
int portSeparatorIdx = ip.lastIndexOf(':');
int squareBracketIdx = ip.lastIndexOf(']');
if (portSeparatorIdx > squareBracketIdx) {
return ip.substring(0, portSeparatorIdx);
for (String realIpHeaderName : realIpHeaderNameList) {
final String realIpHeaderValue = requestAdaptor.getHeader(request, realIpHeaderName);

Check warning on line 49 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L49

Added line #L49 was not covered by tests
if (StringUtils.isEmpty(realIpHeaderValue)) {
continue;

Check warning on line 51 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L51

Added line #L51 was not covered by tests
}
if (realIpHeaderEmptyValue != null && realIpHeaderEmptyValue.equalsIgnoreCase(realIpHeaderValue)) {
continue;

Check warning on line 54 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L54

Added line #L54 was not covered by tests
}
String firstRealIpHeaderValue = getFirstRealIpHeaderValue(realIpHeaderValue);

Check warning on line 56 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L56

Added line #L56 was not covered by tests
if (FORWARDED_HEADER_NAME.equalsIgnoreCase(realIpHeaderName)) {
Matcher matcher = FORWARDED_FOR_PATTERN.matcher(firstRealIpHeaderValue);

Check warning on line 58 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L58

Added line #L58 was not covered by tests
if (matcher.find()) {
String ip = matcher.group(1).trim();
int portSeparatorIdx = ip.lastIndexOf(':');
int squareBracketIdx = ip.lastIndexOf(']');

Check warning on line 62 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L60-L62

Added lines #L60 - L62 were not covered by tests
if (portSeparatorIdx > squareBracketIdx) {
return ip.substring(0, portSeparatorIdx);

Check warning on line 64 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L64

Added line #L64 was not covered by tests
}

return ip;

Check warning on line 67 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L67

Added line #L67 was not covered by tests
}

return ip;
}

return firstRealIpHeaderValue;

Check warning on line 71 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L71

Added line #L71 was not covered by tests
}

return firstRealIpHeaderValue;
return null;

Check warning on line 74 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/request/util/RealIpHeaderResolver.java#L74

Added line #L74 was not covered by tests
}

private static String getFirstRealIpHeaderValue(String realIp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void getRemoteAddress0() throws Exception {
assertEquals("127.0.0.1", requestAdaptor.getRemoteAddress(httpServletRequest));
}


@Test
public void getRemoteAddress1() throws Exception {
RequestAdaptor<HttpServletRequest> requestAdaptor = new HttpServletRequestAdaptor();
Expand Down Expand Up @@ -134,4 +133,48 @@ public void getRemoteAddress7() throws Exception {

assertEquals("192.0.2.43", requestAdaptor.getRemoteAddress(httpServletRequest));
}

@Test
public void getRemoteAddress8() throws Exception {
RequestAdaptor<HttpServletRequest> requestAdaptor = new HttpServletRequestAdaptor();
requestAdaptor = RemoteAddressResolverFactory.wrapRealIpSupport(requestAdaptor, FORWARDED, UNKNOWN);
final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
// invalid format
when(httpServletRequest.getHeader(FORWARDED)).thenReturn("fjlafjlkajflkfk");
when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.2");
assertEquals("fjlafjlkajflkfk", requestAdaptor.getRemoteAddress(httpServletRequest));
}

@Test
public void getRemoteAddress9() throws Exception {
final HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);

RequestAdaptor<HttpServletRequest> requestAdaptor1 = new HttpServletRequestAdaptor();
requestAdaptor1 = RemoteAddressResolverFactory.wrapRealIpSupport(requestAdaptor1, "forwarded, x-forwarded-for", UNKNOWN);
when(httpServletRequest.getHeader(FORWARDED)).thenReturn("for=192.0.2.43");
when(httpServletRequest.getHeader(X_FORWARDED_FOR)).thenReturn("198.51.100.17");
when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.2");
assertEquals("192.0.2.43", requestAdaptor1.getRemoteAddress(httpServletRequest));

RequestAdaptor<HttpServletRequest> requestAdaptor2 = new HttpServletRequestAdaptor();
requestAdaptor2 = RemoteAddressResolverFactory.wrapRealIpSupport(requestAdaptor2, "x-forwarded-for, forwarded", UNKNOWN);
when(httpServletRequest.getHeader(FORWARDED)).thenReturn("for=192.0.2.43");
when(httpServletRequest.getHeader(X_FORWARDED_FOR)).thenReturn("198.51.100.17");
when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.2");
assertEquals("198.51.100.17", requestAdaptor2.getRemoteAddress(httpServletRequest));

RequestAdaptor<HttpServletRequest> requestAdaptor3 = new HttpServletRequestAdaptor();
requestAdaptor3 = RemoteAddressResolverFactory.wrapRealIpSupport(requestAdaptor3, "", UNKNOWN);
when(httpServletRequest.getHeader(FORWARDED)).thenReturn("for=192.0.2.43");
when(httpServletRequest.getHeader(X_FORWARDED_FOR)).thenReturn("198.51.100.17");
when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.2");
assertEquals("127.0.0.2", requestAdaptor3.getRemoteAddress(httpServletRequest));

RequestAdaptor<HttpServletRequest> requestAdaptor4 = new HttpServletRequestAdaptor();
requestAdaptor4 = RemoteAddressResolverFactory.wrapRealIpSupport(requestAdaptor3, "X-RealIP", UNKNOWN);
when(httpServletRequest.getHeader(FORWARDED)).thenReturn("for=192.0.2.43");
when(httpServletRequest.getHeader(X_FORWARDED_FOR)).thenReturn("198.51.100.17");
when(httpServletRequest.getRemoteAddr()).thenReturn("127.0.0.2");
assertEquals("127.0.0.2", requestAdaptor4.getRemoteAddress(httpServletRequest));
}
}