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 1/2] 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); +} From 573688ba9a1c62783fcddee42ed8f4c628efaec0 Mon Sep 17 00:00:00 2001 From: HawkForge <75696486+HawkForge@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:37:18 +0600 Subject: [PATCH 2/2] Report no address for unsupported PROXY v2 families, derive port in test from bytes --- src/ProxyParser.h | 6 ++++++ tests/ProxyParser.cpp | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ProxyParser.h b/src/ProxyParser.h index 1b44041ad..4a63c0a7e 100644 --- a/src/ProxyParser.h +++ b/src/ProxyParser.h @@ -169,6 +169,12 @@ struct ProxyParser { * HAProxy), which we consume but do not keep */ memcpy(&addr, data.data() + 16, hostLength < sizeof(proxy_addr) ? hostLength : sizeof(proxy_addr)); + /* Only INET (1) and INET6 (2) addresses are supported. Anything else, such as + * AF_UNIX with its 216 byte address block, is consumed but reports no address */ + if ((family >> 4) != 1 && (family >> 4) != 2) { + family = 0; + } + done = true; } diff --git a/tests/ProxyParser.cpp b/tests/ProxyParser.cpp index 964ef5cde..948ee63a2 100644 --- a/tests/ProxyParser.cpp +++ b/tests/ProxyParser.cpp @@ -36,7 +36,10 @@ int main() { assert(v6Done); assert(v6Consumed == v6.length()); assert(v6Parser.getSourceAddress() == std::string_view(ipv6.data(), 16)); - assert(v6Parser.getSourcePort() == 0x901f); + /* The port is the raw network order bytes read in host order */ + uint16_t v6Port; + memcpy(&v6Port, ipv6.data() + 32, sizeof(v6Port)); + assert(v6Parser.getSourcePort() == v6Port); /* IPv4 with the same TLV keeps working */ std::string ipv4("\x7f\x00\x00\x01\x7f\x00\x00\x01\x1f\x90\x00\x50", 12); @@ -48,6 +51,18 @@ int main() { assert(v4Consumed == v4.length()); assert(v4Parser.getSourceAddress() == std::string_view(ipv4.data(), 4)); + /* AF_UNIX (family 0x31) has a 216 byte address block, which is consumed but gives no address */ + std::string unixAddresses(216, 'x'); + std::string unixHeader = proxyHeader(0x31, unixAddresses, alpn); + std::string unixRequest = unixHeader + "GET / HTTP/1.1\r\n\r\n"; + + uWS::ProxyParser unixParser; + auto [unixDone, unixConsumed] = unixParser.parse(unixRequest); + assert(unixDone); + assert(unixConsumed == unixHeader.length()); + assert(unixParser.getSourceAddress().empty()); + assert(unixParser.getSourcePort() == 0); + /* A truncated payload still waits for more data */ uWS::ProxyParser truncated; assert(!truncated.parse(std::string_view(v6.data(), v6.length() - 1)).first);