From 177fefa9ad851f5164697b3c108a6182be0fa7e6 Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Sun, 15 Nov 2026 21:39:30 +0000 Subject: [PATCH] Validate the PROXY v2 command, family and address length --- src/ProxyParser.h | 37 ++++++++++++++++--- tests/Makefile | 2 + tests/ProxyParser.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 tests/ProxyParser.cpp diff --git a/src/ProxyParser.h b/src/ProxyParser.h index ffe4bf676..cd104edfc 100644 --- a/src/ProxyParser.h +++ b/src/ProxyParser.h @@ -146,6 +146,11 @@ struct ProxyParser { return {false, 0}; } + uint8_t command = header.ver_cmd & 0x0f; + if (command > 1) { + return {false, 0}; + } + //printf("Version: %d\n", (header.ver_cmd & 0xf0) >> 4); //printf("Command: %d\n", (header.ver_cmd & 0x0f)); @@ -157,9 +162,28 @@ struct ProxyParser { return {false, 0}; } - /* Payload cannot be more than sizeof proxy_addr */ - if (sizeof(proxy_addr) < hostLength) { - return {false, 0}; + unsigned int addressLength = 0; + if (command == 1) { + switch (header.fam) { + case 0x00: + case 0x31: + case 0x32: + break; + case 0x11: + case 0x12: + addressLength = 12; + break; + case 0x21: + case 0x22: + addressLength = 36; + break; + default: + return {false, 0}; + } + + if (hostLength < addressLength) { + return {false, 0}; + } } //printf("Family: %d\n", (header.fam & 0xf0) >> 4); @@ -168,9 +192,10 @@ struct ProxyParser { /* Copy payload (only if not already done so before) */ if (!done) { /* We have 0 family by default, and UNSPEC is 0 as well */ - family = header.fam; - - memcpy(&addr, data.data() + 16, hostLength); + if (addressLength) { + family = header.fam; + memcpy(&addr, data.data() + 16, addressLength); + } 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..76e0f6042 --- /dev/null +++ b/tests/ProxyParser.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include + +#define UWS_WITH_PROXY +#include "../src/ProxyParser.h" + +static std::string header(uint8_t verCmd, uint8_t fam, uint16_t len) { + std::string h("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12); + h += (char) verCmd; + h += (char) fam; + h += (char) (len >> 8); + h += (char) (len & 0xff); + for (uint16_t i = 0; i < len; i++) { + h += (char) (i + 1); + } + return h; +} + +int main() { + + { + uWS::ProxyParser pp; + std::string h = header(0x21, 0x11, 12); + assert(pp.parse(h) == std::make_pair(true, 28u)); + assert(pp.getSourceAddress() == std::string_view("\x01\x02\x03\x04", 4)); + } + + { + uWS::ProxyParser pp; + std::string h = header(0x21, 0x21, 36); + assert(pp.parse(h) == std::make_pair(true, 52u)); + assert(pp.getSourceAddress().length() == 16); + } + + { + uWS::ProxyParser pp; + std::string h = header(0x21, 0x11, 40); + assert(pp.parse(h) == std::make_pair(true, 56u)); + assert(pp.getSourceAddress() == std::string_view("\x01\x02\x03\x04", 4)); + } + + { + uWS::ProxyParser pp; + std::string h = header(0x20, 0x11, 12); + assert(pp.parse(h) == std::make_pair(true, 28u)); + assert(pp.getSourceAddress().empty()); + } + + { + uWS::ProxyParser pp; + assert(pp.parse(header(0x22, 0x11, 12)).first == false); + } + + { + uWS::ProxyParser pp; + assert(pp.parse(header(0x21, 0x41, 12)).first == false); + assert(pp.parse(header(0x21, 0x01, 0)).first == false); + assert(pp.parse(header(0x21, 0x13, 12)).first == false); + } + + { + uWS::ProxyParser pp; + assert(pp.parse(header(0x21, 0x11, 0)).first == false); + assert(pp.parse(header(0x21, 0x21, 12)).first == false); + } + + { + uWS::ProxyParser pp; + std::string h = header(0x21, 0x00, 0); + assert(pp.parse(h) == std::make_pair(true, 16u)); + assert(pp.getSourceAddress().empty()); + } + + { + uWS::ProxyParser pp; + std::string h = header(0x21, 0x31, 216); + assert(pp.parse(h) == std::make_pair(true, 232u)); + assert(pp.getSourceAddress().empty()); + } + + return 0; +}