From 2264574ac2b42e9064c94460c938ba3d37424b14 Mon Sep 17 00:00:00 2001 From: HawkForge <75696486+HawkForge@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:06:25 +0600 Subject: [PATCH] Accept PROXY v2 headers with TLVs after the address block Fixes #1957 --- src/ProxyParser.h | 9 +++----- tests/Makefile | 2 ++ tests/ProxyParser.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 tests/ProxyParser.cpp diff --git a/src/ProxyParser.h b/src/ProxyParser.h index ffe4bf676..1b44041ad 100644 --- a/src/ProxyParser.h +++ b/src/ProxyParser.h @@ -157,11 +157,6 @@ struct ProxyParser { return {false, 0}; } - /* Payload cannot be more than sizeof proxy_addr */ - if (sizeof(proxy_addr) < hostLength) { - return {false, 0}; - } - //printf("Family: %d\n", (header.fam & 0xf0) >> 4); //printf("Transport: %d\n", (header.fam & 0x0f)); @@ -170,7 +165,9 @@ struct ProxyParser { /* We have 0 family by default, and UNSPEC is 0 as well */ family = header.fam; - memcpy(&addr, data.data() + 16, hostLength); + /* The address block may be followed by TLVs (such as ALPN from + * HAProxy), which we consume but do not keep */ + memcpy(&addr, data.data() + 16, hostLength < sizeof(proxy_addr) ? hostLength : sizeof(proxy_addr)); done = true; } diff --git a/tests/Makefile b/tests/Makefile index 4e4338b70..e7ffe8f24 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -13,6 +13,8 @@ default: ./ExtensionsNegotiator $(CXX) -std=c++17 -fsanitize=address HttpParser.cpp -o HttpParser ./HttpParser + $(CXX) -std=c++17 -fsanitize=address ProxyParser.cpp -o ProxyParser + ./ProxyParser $(MAKE) -C ../uSockets $(CXX) -std=c++17 -fsanitize=address -flto=auto -I../src -I../uSockets/src HttpResponse.cpp ../uSockets/*.o -lz -pthread -o HttpResponse ./HttpResponse diff --git a/tests/ProxyParser.cpp b/tests/ProxyParser.cpp new file mode 100644 index 000000000..964ef5cde --- /dev/null +++ b/tests/ProxyParser.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#define UWS_WITH_PROXY +#include "../src/ProxyParser.h" + +/* PROXY v2 header for a TCP connection with the given family byte, address block and trailing TLVs */ +static std::string proxyHeader(uint8_t fam, std::string addresses, std::string tlvs) { + std::string header("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12); + uint16_t length = (uint16_t) (addresses.length() + tlvs.length()); + header += (char) 0x21; + header += (char) fam; + header += (char) (length >> 8); + header += (char) (length & 0xff); + return header + addresses + tlvs; +} + +int main() { + /* ALPN TLV as sent by HAProxy's send-proxy-v2 for an h2 client */ + std::string alpn("\x01\x00\x02h2", 5); + + /* IPv6 with a TLV makes the payload longer than the address block (36 + 5 bytes) */ + std::string ipv6(36, '\0'); + ipv6[15] = 1; + ipv6[31] = 2; + ipv6[32] = 0x1f; + ipv6[33] = 0x90; + std::string v6 = proxyHeader(0x21, ipv6, alpn); + std::string request = v6 + "GET / HTTP/1.1\r\n\r\n"; + + uWS::ProxyParser v6Parser; + auto [v6Done, v6Consumed] = v6Parser.parse(request); + assert(v6Done); + assert(v6Consumed == v6.length()); + assert(v6Parser.getSourceAddress() == std::string_view(ipv6.data(), 16)); + assert(v6Parser.getSourcePort() == 0x901f); + + /* IPv4 with the same TLV keeps working */ + std::string ipv4("\x7f\x00\x00\x01\x7f\x00\x00\x01\x1f\x90\x00\x50", 12); + std::string v4 = proxyHeader(0x11, ipv4, alpn); + + uWS::ProxyParser v4Parser; + auto [v4Done, v4Consumed] = v4Parser.parse(v4); + assert(v4Done); + assert(v4Consumed == v4.length()); + assert(v4Parser.getSourceAddress() == std::string_view(ipv4.data(), 4)); + + /* A truncated payload still waits for more data */ + uWS::ProxyParser truncated; + assert(!truncated.parse(std::string_view(v6.data(), v6.length() - 1)).first); +}