Skip to content

Commit

Permalink
Issue #7277 - Fixing ForwardedRequestCustomizer impact
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Dec 14, 2021
1 parent 594d956 commit d40a97e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,14 @@ else if (forwarded._secureScheme)
host = requestURI.getHost();
}

if (port == MutableHostPort.UNSET) // is unset by headers
if (port == MutableHostPort.UNSET) // is unset by forwarding headers
{
port = requestURI.getPort();
port = requestURI.getPort(); // use request port
}
else if (port == MutableHostPort.IMPLIED) // port is implied by a header
{
port = HostPort.NO_PORT; // use NO_PORT to indicate that HostPortHttpField should not set it
}

// Don't change port if port == IMPLIED.

// Update authority if different from metadata
if (!host.equalsIgnoreCase(requestURI.getHost()) ||
Expand Down Expand Up @@ -642,8 +644,8 @@ private boolean updateForwardedHandle(MethodHandles.Lookup lookup, String header

private static class MutableHostPort
{
public static final int UNSET = -1;
public static final int IMPLIED = 0;
public static final int UNSET = HostPort.NO_PORT;
public static final int IMPLIED = -2;

String _host;
Source _hostSource = Source.UNSET;
Expand Down Expand Up @@ -853,7 +855,11 @@ public void handleForwardedServer(HttpField field)
*/
public void handleForwardedPort(HttpField field)
{
int port = HostPort.parsePort(getLeftMost(field.getValue()));
String rawPort = getLeftMost(field.getValue());
if (StringUtil.isBlank(rawPort))
throw new IllegalStateException("Invalid Forwarding Port");

int port = HostPort.parsePort(rawPort);

updatePort(port, Source.XFORWARDED_PORT);
}
Expand Down
12 changes: 8 additions & 4 deletions jetty-server/src/main/java/org/eclipse/jetty/server/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -1050,10 +1050,14 @@ public String getLocalName()
@Override
public int getLocalPort()
{
if (_channel == null)
return 0;
InetSocketAddress local = _channel.getLocalAddress();
return local == null ? 0 : local.getPort();
if (_channel != null)
{
InetSocketAddress local = _channel.getLocalAddress();
// only return valid ports (some connectors, like LocalConnector / UnixConnector cannot return a port here)
if (local != null && local.getPort() > 0)
return local.getPort();
}
return HttpURI.NO_PORT;
}

/*
Expand Down

0 comments on commit d40a97e

Please sign in to comment.