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

Issue #325 - fixes and testing for HttpServletRequest getServerName #327

Merged
merged 3 commits into from
Jan 2, 2025
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 @@ -124,7 +124,8 @@ public JettyRequestAPIData(

HttpFields.Mutable fields = HttpFields.build();
for (HttpField field : request.getHeaders()) {
// If it has a HttpHeader it is one of the standard headers so won't match any appengine specific header.
// If it has a HttpHeader it is one of the standard headers so won't match any appengine
// specific header.
if (field.getHeader() != null) {
fields.add(field);
continue;
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is b3de558#diff-28a36762cb84139431767aee493287f06752c86ae601c79580d77dece0d4d821L350 safe as well? I just realised that the method name is LocalHostName and that is the description of the parent method but we are returning the Local IP instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure exactly what you are referring to here.

But we are not doing any reverse DNS lookups so we never resolve IPs to a hostname.

Copy link
Collaborator

@maigovannon maigovannon Jan 2, 2025

Choose a reason for hiding this comment

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

Sorry I should have clarified the link better:

If you see we are returning 0.0.0.0 when actually this is supposed to return the host name rather than ip as per https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getLocalName()

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import static com.google.apphosting.runtime.AppEngineConstants.X_FORWARDED_PROTO;
import static com.google.apphosting.runtime.AppEngineConstants.X_GOOGLE_INTERNAL_PROFILER;
import static com.google.apphosting.runtime.AppEngineConstants.X_GOOGLE_INTERNAL_SKIPADMINCHECK;
import static com.google.apphosting.runtime.jetty9.RpcConnection.NORMALIZE_INET_ADDR;
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved

import com.google.apphosting.base.protos.HttpPb;
import com.google.apphosting.base.protos.RuntimePb;
Expand All @@ -67,12 +68,12 @@
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.HostPort;
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved

/**
* Implementation for the {@link RequestAPIData} to allow for the Jetty {@link Request} to be used
Expand Down Expand Up @@ -128,7 +129,8 @@ public JettyRequestAPIData(

HttpFields fields = new HttpFields();
for (HttpField field : request.getHttpFields()) {
// If it has a HttpHeader it is one of the standard headers so won't match any appengine specific header.
// If it has a HttpHeader it is one of the standard headers so won't match any appengine
// specific header.
if (field.getHeader() != null) {
fields.add(field);
continue;
Expand Down Expand Up @@ -286,7 +288,7 @@ public JettyRequestAPIData(
traceContext = TraceContextProto.getDefaultInstance();
}

String finalUserIp = userIp;
String finalUserIp = NORMALIZE_INET_ADDR ? HostPort.normalizeHost(userIp) : userIp;
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] Just went and checked the source of this change and came across this CL: https://critique.corp.google.com/cl/400610752. Curious if this flag is ever used by the customers? There is a typo in the naming of the property in that it is is called nomalize_inet_addr instead of normalize_inet_addr

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure which customers could be using this. But this should maintain the behavior of this flag for the HttpConnector mode on the jetty9.4 runtime.

We don't currently have this flag available for the Jetty 12 runtimes. It behaves as if com.google.appengine.nomalize_inet_addr == false because the correct behavior for getRemoteAddr and getRemoteHost is to not include the [] brackets.

But would maybe @gregw or @ludoch have more context on this issue and if customers are require com.google.appengine.nomalize_inet_addr == true behavior.

this.httpServletRequest =
new HttpServletRequestWrapper(httpServletRequest) {

Expand Down Expand Up @@ -335,11 +337,6 @@ public String getRemoteAddr() {
return finalUserIp;
}

@Override
public String getServerName() {
return UNSPECIFIED_IP;
}

@Override
public String getRemoteHost() {
return finalUserIp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.util.Collection;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpVersion;
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -54,6 +56,7 @@ public static Collection<Object[]> data() {
private final boolean httpMode;
private final String environment;
private RuntimeContext<?> runtime;
private String url;
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved

public RemoteAddressTest(String environment, boolean httpMode) {
this.environment = environment;
Expand All @@ -67,6 +70,7 @@ public void before() throws Exception {
copyAppToDir(app, temp.getRoot().toPath());
httpClient.start();
runtime = runtimeContext();
url = runtime.jettyUrl("/");
System.err.println("==== Using Environment: " + environment + " " + httpMode + " ====");
}

Expand All @@ -77,20 +81,123 @@ public void after() throws Exception {
}

@Test
public void test() throws Exception {
String url = runtime.jettyUrl("/");
ContentResponse response = httpClient.newRequest(url)
public void testWithHostHeader() throws Exception {
ContentResponse response =
httpClient
.newRequest(url)
.header("Host", "foobar:1234")
.header("X-AppEngine-User-IP", "203.0.113.1")
.send();

assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String contentReceived = response.getContentAsString();
assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(contentReceived, containsString("getServerName: foobar"));
assertThat(contentReceived, containsString("getServerPort: 1234"));
}

@Test
public void testWithIPv6() throws Exception {
// Test the host header to be IPv6 with a port.
ContentResponse response =
httpClient
.newRequest(url)
.header("Host", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234")
.header("X-AppEngine-User-IP", "203.0.113.1")
.send();
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String contentReceived = response.getContentAsString();
assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(
contentReceived, containsString("getServerName: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(contentReceived, containsString("getServerPort: 1234"));

// Test the user IP to be IPv6 with a port.
response =
httpClient
.newRequest(url)
.header("Host", "203.0.113.1:1234")
.header("X-AppEngine-User-IP", "2001:db8:85a3:8d3:1319:8a2e:370:7348")
.send();
assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
contentReceived = response.getContentAsString();
if ("jetty94".equals(environment)) {
assertThat(
contentReceived, containsString("getRemoteAddr: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
assertThat(
contentReceived, containsString("getRemoteHost: [2001:db8:85a3:8d3:1319:8a2e:370:7348]"));
} else {
// The correct behaviour for getRemoteAddr and getRemoteHost is to not include []
// because they return raw IP/hostname and not URI-formatted addresses.
assertThat(
contentReceived, containsString("getRemoteAddr: 2001:db8:85a3:8d3:1319:8a2e:370:7348"));
assertThat(
contentReceived, containsString("getRemoteHost: 2001:db8:85a3:8d3:1319:8a2e:370:7348"));
}
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(contentReceived, containsString("getServerName: 203.0.113.1"));
assertThat(contentReceived, containsString("getServerPort: 1234"));
}

@Test
public void testWithoutHostHeader() throws Exception {
ContentResponse response =
httpClient
.newRequest(url)
.version(HttpVersion.HTTP_1_0)
.header("X-AppEngine-User-IP", "203.0.113.1")
.onRequestHeaders(request -> request.getHeaders().remove("Host"))
.send();

assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String contentReceived = response.getContentAsString();
assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(contentReceived, containsString("getServerName: 127.0.0.1"));
assertThat(contentReceived, containsString("getServerPort: " + runtime.getPort()));
}

@Test
public void testForwardedHeadersIgnored() throws Exception {
ContentResponse response =
httpClient
.newRequest(url)
.header("Host", "foobar:1234")
.header("X-AppEngine-User-IP", "203.0.113.1")
.header(HttpHeader.X_FORWARDED_FOR, "test1:2221")
.header(HttpHeader.X_FORWARDED_PROTO, "test2:2222")
.header(HttpHeader.X_FORWARDED_HOST, "test3:2223")
.header(HttpHeader.X_FORWARDED_PORT, "test4:2224")
.header(HttpHeader.FORWARDED, "test5:2225")
.send();

assertThat(response.getStatus(), equalTo(HttpStatus.OK_200));
String contentReceived = response.getContentAsString();
assertThat(contentReceived, containsString("getRemoteAddr: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemoteHost: 203.0.113.1"));
assertThat(contentReceived, containsString("getRemotePort: 0"));
assertThat(contentReceived, containsString("getLocalAddr: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalName: 0.0.0.0"));
assertThat(contentReceived, containsString("getLocalPort: 0"));
assertThat(contentReceived, containsString("getServerName: 0.0.0.0"));
assertThat(contentReceived, containsString("getServerName: foobar"));
assertThat(contentReceived, containsString("getServerPort: 1234"));
}

private RuntimeContext<?> runtimeContext() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
resp.setContentType("text/plain");
PrintWriter writer = resp.getWriter();
writer.println("getRemoteAddr: " + req.getRemoteAddr());
writer.println("getLocalAddr: " + req.getLocalAddr());
writer.println("getServerPort: " + req.getServerPort());
writer.println("getRemoteHost: " + req.getRemoteHost());
writer.println("getRemotePort: " + req.getRemotePort());
writer.println("getLocalAddr: " + req.getLocalAddr());
writer.println("getLocalName: " + req.getLocalName());
writer.println("getLocalPort: " + req.getLocalPort());
writer.println("getServerName: " + req.getServerName());
writer.println("getServerPort: " + req.getServerPort());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
resp.setContentType("text/plain");
PrintWriter writer = resp.getWriter();
writer.println("getRemoteAddr: " + req.getRemoteAddr());
writer.println("getLocalAddr: " + req.getLocalAddr());
writer.println("getServerPort: " + req.getServerPort());
writer.println("getRemoteHost: " + req.getRemoteHost());
writer.println("getRemotePort: " + req.getRemotePort());
writer.println("getLocalAddr: " + req.getLocalAddr());
writer.println("getLocalName: " + req.getLocalName());
writer.println("getLocalPort: " + req.getLocalPort());
writer.println("getServerName: " + req.getServerName());
writer.println("getServerPort: " + req.getServerPort());
}
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
}
Loading