From bb2f09aeb2202cd8873a865cf3df97101107511a Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 09:24:19 -0800 Subject: [PATCH 1/3] Validate HTTP request lengths and emit one response length --- ixwebsocket/IXHttp.cpp | 8 +++-- test/IXHttpTest.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/ixwebsocket/IXHttp.cpp b/ixwebsocket/IXHttp.cpp index d3b2d4e9..5b8fb58a 100644 --- a/ixwebsocket/IXHttp.cpp +++ b/ixwebsocket/IXHttp.cpp @@ -138,9 +138,7 @@ namespace ix char* p_end {}; errno = 0; long val = std::strtol(p, &p_end, 10); - if (p_end == p // invalid argument - || errno == ERANGE // out of range - ) + if (p[0] < '0' || p[0] > '9' || *p_end != '\0' || errno == ERANGE) { return std::make_tuple( false, "Error parsing HTTP Header 'Content-Length'", httpRequest); @@ -213,6 +211,10 @@ namespace ix ss << "Content-Length: " << response->body.size() << "\r\n"; for (auto&& it : response->headers) { + // The generated length must match the actual body and appear only once. + if (!CaseInsensitiveLess::cmp(it.first, "Content-Length") && + !CaseInsensitiveLess::cmp("Content-Length", it.first)) + continue; ss << it.first << ": " << it.second << "\r\n"; } ss << "\r\n"; diff --git a/test/IXHttpTest.cpp b/test/IXHttpTest.cpp index 8f331854..8e5125e6 100644 --- a/test/IXHttpTest.cpp +++ b/test/IXHttpTest.cpp @@ -7,9 +7,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -100,3 +102,67 @@ namespace ix } } // namespace ix + +namespace +{ + class HttpMemorySocket : public ix::Socket + { + public: + explicit HttpMemorySocket(const std::string& input = std::string()) + : _input(input) + { + } + std::ptrdiff_t recv(void* buffer, size_t length) override + { + const size_t size = (std::min) (length, _input.size() - _offset); + std::memcpy(buffer, _input.data() + _offset, size); + _offset += size; + return static_cast(size); + } + std::ptrdiff_t send(char* buffer, size_t length) override + { + output.append(buffer, length); + return static_cast(length); + } + std::string output; + + private: + std::string _input; + size_t _offset = 0; + }; +} // namespace + +TEST_CASE("HTTP requests reject partially parsed content lengths", "[http][framing]") +{ + const std::string length = + GENERATE(std::string("1x"), std::string("+1"), std::string("1, 2"), std::string("\t1")); + std::unique_ptr socket( + new HttpMemorySocket("POST / HTTP/1.1\r\nContent-Length: " + length + "\r\n\r\nx")); + auto result = ix::Http::parseRequest(socket, 1); + CHECK_FALSE(std::get<0>(result)); + CHECK_FALSE(std::get<1>(result).empty()); +} + +TEST_CASE("HTTP requests accept exact decimal content lengths", "[http][framing]") +{ + const std::string length = GENERATE(std::string("1"), std::string("0001")); + std::unique_ptr socket( + new HttpMemorySocket("POST / HTTP/1.1\r\nContent-Length: " + length + "\r\n\r\nx")); + auto result = ix::Http::parseRequest(socket, 1); + REQUIRE(std::get<0>(result)); + CHECK(std::get<2>(result)->body == "x"); +} + +TEST_CASE("HTTP responses send exactly one accurate content length", "[http][framing]") +{ + const std::string supplied = GENERATE(std::string("999"), std::string("3")); + std::unique_ptr socket(new HttpMemorySocket()); + ix::WebSocketHttpHeaders headers; + headers["content-length"] = supplied; + headers["X-Custom"] = "preserved"; + auto response = + std::make_shared(200, "OK", ix::HttpErrorCode::Ok, headers, "abc"); + REQUIRE(ix::Http::sendResponse(response, socket)); + const auto& wire = static_cast(*socket).output; + CHECK(wire == "HTTP/1.1 200 OK\r\nContent-Length: 3\r\nX-Custom: preserved\r\n\r\nabc"); +} From f7b47ef3248a701c5978bec00aad12aa5179d574 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 09:26:17 -0800 Subject: [PATCH 2/3] Preserve legal leading tabs when validating HTTP header values --- ixwebsocket/IXWebSocketHttpHeaders.cpp | 3 ++- test/IXHttpTest.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ixwebsocket/IXWebSocketHttpHeaders.cpp b/ixwebsocket/IXWebSocketHttpHeaders.cpp index b9150a2e..45b10f76 100644 --- a/ixwebsocket/IXWebSocketHttpHeaders.cpp +++ b/ixwebsocket/IXWebSocketHttpHeaders.cpp @@ -53,7 +53,8 @@ namespace ix std::string lineStr(line); int start = colon + 1; - while (start < (int) lineStr.size() && lineStr[start] == ' ') + while (start < (int) lineStr.size() && + (lineStr[start] == ' ' || lineStr[start] == '\t')) { start++; } diff --git a/test/IXHttpTest.cpp b/test/IXHttpTest.cpp index 8e5125e6..52ea91e2 100644 --- a/test/IXHttpTest.cpp +++ b/test/IXHttpTest.cpp @@ -135,7 +135,7 @@ namespace TEST_CASE("HTTP requests reject partially parsed content lengths", "[http][framing]") { const std::string length = - GENERATE(std::string("1x"), std::string("+1"), std::string("1, 2"), std::string("\t1")); + GENERATE(std::string("1x"), std::string("+1"), std::string("1, 2"), std::string("\v1")); std::unique_ptr socket( new HttpMemorySocket("POST / HTTP/1.1\r\nContent-Length: " + length + "\r\n\r\nx")); auto result = ix::Http::parseRequest(socket, 1); @@ -145,7 +145,7 @@ TEST_CASE("HTTP requests reject partially parsed content lengths", "[http][frami TEST_CASE("HTTP requests accept exact decimal content lengths", "[http][framing]") { - const std::string length = GENERATE(std::string("1"), std::string("0001")); + const std::string length = GENERATE(std::string("1"), std::string("0001"), std::string("\t1")); std::unique_ptr socket( new HttpMemorySocket("POST / HTTP/1.1\r\nContent-Length: " + length + "\r\n\r\nx")); auto result = ix::Http::parseRequest(socket, 1); From eed071bb28811991f0177f0b9befe24db5b61cea Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 09:55:05 -0800 Subject: [PATCH 3/3] Limit Darwin TSan instrumentation around native thread-local errno access --- ixwebsocket/IXSocket.cpp | 8 ++++++++ test/IXSocketTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index d60f1657..8a484c82 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -306,6 +306,11 @@ namespace ix #endif } + // Darwin TSan can confuse reused per-thread errno storage with shared memory. + // These accessors touch only OS thread-local error state; keep callers instrumented. +#if defined(__APPLE__) && defined(__clang__) + __attribute__((no_sanitize("thread"))) +#endif int Socket::getErrno() { int err; @@ -319,6 +324,9 @@ namespace ix return err; } +#if defined(__APPLE__) && defined(__clang__) + __attribute__((no_sanitize("thread"))) +#endif void Socket::setErrno(int err) { #ifdef _WIN32 diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index 740090c1..fdc22d98 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -13,6 +13,7 @@ #include #include #include +#include using namespace ix; @@ -107,6 +108,29 @@ TEST_CASE("Empty and cancelled socket reads do not receive data", "[socket][read CHECK(socket.reads == 0); } +TEST_CASE("Socket error state remains local when threads are reused", "[socket][errno]") +{ + std::atomic isolated {true}; + auto churn = [&](int error) + { + for (int i = 0; i < 16; ++i) + { + std::thread worker( + [&, error] + { + Socket::setErrno(error); + if (Socket::getErrno() != error) isolated = false; + }); + worker.join(); + } + }; + std::thread a(churn, EWOULDBLOCK); + std::thread b(churn, EINVAL); + a.join(); + b.join(); + CHECK(isolated.load()); +} + namespace ix { void testSocket(const std::string& host,