Skip to content
Open
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 @@ -441,6 +441,8 @@ public int getMaxProxyHeader()

public void setMaxProxyHeader(int maxProxyHeader)
{
if (maxProxyHeader < ProxyProtocolV2Connection.HEADER_LENGTH)
throw new IllegalArgumentException("Max proxy header must be >= " + ProxyProtocolV2Connection.HEADER_LENGTH);
_maxProxyHeader = maxProxyHeader;
}

Expand Down Expand Up @@ -468,7 +470,9 @@ protected ProxyProtocolV2Connection(EndPoint endp, Connector connector, Connecti
super(endp, connector.getExecutor());
_connector = connector;
_next = next;
_buffer = _connector.getByteBufferPool().acquire(getInputBufferSize(), true);
// The whole PROXY v2 header must fit in the buffer, as it is parsed in one go.
int bufferSize = Math.max(getInputBufferSize(), HEADER_LENGTH + getMaxProxyHeader());
Comment thread
lorban marked this conversation as resolved.
Resolved
_buffer = _connector.getByteBufferPool().acquire(bufferSize, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ProxyConnectionTest
{
Expand Down Expand Up @@ -276,41 +277,14 @@ public void testMaxHeaderLengthV2(RequestProcessor p) throws Exception
p.customize((connector) ->
{
ProxyConnectionFactory factory = (ProxyConnectionFactory)connector.getConnectionFactory("[proxy]");
factory.setMaxProxyHeader(11); // just one byte short
assertThrows(IllegalArgumentException.class, () -> factory.setMaxProxyHeader(11));
});
String proxy =
// Preamble
"0D0A0D0A000D0A515549540A" +

// V2, PROXY
"21" +

// 0x1 : AF_INET 0x1 : STREAM.
"11" +

// Address length is 2*4 + 2*2 = 12 bytes.
// length of remaining header (4+4+2+2 = 12)
"000C" +

// uint32_t src_addr; uint32_t dst_addr; uint16_t src_port; uint16_t dst_port;
"C0A80001" +
"7f000001" +
"3039" +
"1F90";
String http = "GET /path HTTP/1.1\n" +
"Host: server:80\n" +
"Connection: close\n" +
"\n";

String response = p.sendRequestWaitingForResponse(StringUtil.fromHexString(proxy), http.getBytes(StandardCharsets.US_ASCII));

assertThat(response, Matchers.is(Matchers.nullValue()));
}

abstract static class RequestProcessor
public abstract static class RequestProcessor
{
private final Server _server;
protected LocalConnector _connector;
private Server _server;

public RequestProcessor()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

Expand All @@ -28,8 +29,11 @@
import org.eclipse.jetty.util.StringUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -39,9 +43,14 @@ public class ProxyProtocolTest
private ServerConnector connector;

private void start(Handler handler) throws Exception
{
start(new ProxyConnectionFactory(), handler);
}

private void start(ProxyConnectionFactory proxyConnectionFactory, Handler handler) throws Exception
{
server = new Server();
connector = new ServerConnector(server, new ProxyConnectionFactory(), new HttpConnectionFactory());
connector = new ServerConnector(server, proxyConnectionFactory, new HttpConnectionFactory());
server.addConnector(connector);
server.setHandler(handler);
server.start();
Expand Down Expand Up @@ -300,4 +309,68 @@ public boolean handle(Request request, Response response, Callback callback)
}
}
}

@ParameterizedTest
@ValueSource(ints = {1500, 5000, 65535})
public void testProxyProtocolV2LargeHeader(int proxyHeaderLength) throws Exception
{
String remoteAddr = "192.168.0.1";
int remotePort = 12345;
ProxyConnectionFactory proxyConnectionFactory = new ProxyConnectionFactory();
proxyConnectionFactory.setMaxProxyHeader(65535);
start(proxyConnectionFactory, new Handler.Abstract.NonBlocking()
{
@Override
public boolean handle(Request request, Response response, Callback callback)
{
if (remoteAddr.equals(Request.getRemoteAddr(request)) && remotePort == Request.getRemotePort(request))
callback.succeeded();
else
callback.failed(new Throwable("wrong address"));
return true;
}
});

// Address block (12 bytes) followed by a single NOOP TLV (3 bytes + value) padding the header to the desired length.
int noopValueLength = proxyHeaderLength - 12 - 3;
ByteBuffer proxy = ByteBuffer.allocate(16 + proxyHeaderLength);
proxy.put(StringUtil.fromHexString(
// Preamble
"0D0A0D0A000D0A515549540A" +
// V2, PROXY
"21" +
// 0x1 : AF_INET 0x1 : STREAM.
"11"));
proxy.putChar((char)proxyHeaderLength);
proxy.put(StringUtil.fromHexString(
// uint32_t src_addr; uint32_t dst_addr; uint16_t src_port; uint16_t dst_port;
"C0A80001" +
"7f000001" +
"3039" +
"1F90"));
// NOOP TLV.
proxy.put((byte)0x04);
proxy.putChar((char)noopValueLength);
proxy.put(new byte[noopValueLength]);
assertThat(proxy.remaining(), is(0));

try (Socket socket = new Socket("localhost", connector.getLocalPort()))
{
socket.setSoTimeout(5000);
String request =
"GET / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Connection: close\r\n" +
"\r\n";
OutputStream output = socket.getOutputStream();
output.write(proxy.array());
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();

InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
String response = reader.readLine();
assertThat(response, startsWith("HTTP/1.1 200 "));
}
}
}
Loading