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/ixwebsocket/IXUrlParser.cpp b/ixwebsocket/IXUrlParser.cpp index e17d3a08..0c545e76 100644 --- a/ixwebsocket/IXUrlParser.cpp +++ b/ixwebsocket/IXUrlParser.cpp @@ -67,6 +67,7 @@ namespace std::string m_Port; std::string m_Path; std::string m_Query; + bool m_HasQuery = false; std::string m_Fragment; std::string m_UserName; std::string m_Password; @@ -96,14 +97,19 @@ namespace } }; + static bool IsAsciiAlpha(unsigned char c) + { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); + } + static bool IsSchemeValid(const std::string& SchemeName) { - if (SchemeName.empty() || !isalpha(static_cast(SchemeName[0]))) return false; + if (SchemeName.empty() || !IsAsciiAlpha(SchemeName[0])) return false; for (unsigned char c : SchemeName) { - if (!isalnum(c) && c != '+' && c != '-' && c != '.') return false; + if (!IsAsciiAlpha(c) && !(c >= '0' && c <= '9') && c != '+' && c != '-' && c != '.') + return false; } - return true; } @@ -140,6 +146,9 @@ namespace // Note: this is not a full RFC validator; it is a fast parser with targeted syntax checks. clParseURL clParseURL::ParseURL(const std::string& URL) { + if (std::any_of( + URL.begin(), URL.end(), [](unsigned char c) { return c <= 0x20 || c == 0x7f; })) + return clParseURL(LUrlParserError_NoUrlCharacter); clParseURL Result; const std::size_t npos = std::string::npos; std::size_t current = 0; @@ -160,7 +169,7 @@ namespace std::transform(Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), - ::tolower); + [](unsigned char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; }); current = schemeEnd + 1; @@ -172,7 +181,7 @@ namespace current += 2; // Step 3: locate the authority boundary ( ends at '/' or '?'). - std::size_t authorityEnd = URL.find_first_of("/?", current); + std::size_t authorityEnd = URL.find_first_of("/?#", current); if (authorityEnd == npos) { authorityEnd = URL.size(); @@ -210,7 +219,7 @@ namespace } // Step 5: parse host and optional port from authority tail. - authorityEnd = URL.find_first_of("/?", current); + authorityEnd = URL.find_first_of("/?#", current); if (authorityEnd == npos) { authorityEnd = URL.size(); @@ -329,7 +338,7 @@ namespace } // Step 7: parse path start (either '/' or query-only '?'). - if (URL[current] != '/' && URL[current] != '?') + if (URL[current] != '/' && URL[current] != '?' && URL[current] != '#') { return clParseURL(LUrlParserError_NoSlash); } @@ -354,6 +363,7 @@ namespace // Step 9: parse optional query after '?', up to '#'. if (current < URL.size() && URL[current] == '?') { + Result.m_HasQuery = true; ++current; std::size_t queryEnd = URL.find('#', current); @@ -407,12 +417,12 @@ namespace ix } bool UrlParser::parse(const std::string& url, - std::string& protocol, - std::string& host, - std::string& path, - std::string& query, - int& port, - bool& isProtocolDefaultPort) + std::string& protocol, + std::string& host, + std::string& path, + std::string& query, + int& port, + bool& isProtocolDefaultPort) { clParseURL res = clParseURL::ParseURL(url); @@ -443,7 +453,7 @@ namespace ix path = '/' + path; } - if (!query.empty()) + if (res.m_HasQuery) { path += "?"; path += query; 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, diff --git a/test/IXUrlParserTest.cpp b/test/IXUrlParserTest.cpp index a9ba17ed..364065f3 100644 --- a/test/IXUrlParserTest.cpp +++ b/test/IXUrlParserTest.cpp @@ -6,6 +6,7 @@ #include "IXTest.h" #include +#include #include #include #include @@ -219,7 +220,7 @@ namespace ix SECTION("reject malformed userinfo") { std::vector malformedUrls = { - "ws://user@@127.0.0.1/", // multiple @ separators in authority + "ws://user@@127.0.0.1/", // multiple @ separators in authority "ws://user:pass@@[::1]:9001/" // multiple @ separators in authority }; @@ -235,3 +236,83 @@ namespace ix } } // namespace ix + +TEST_CASE("URL components preserve authority boundaries and empty queries", "[url][components]") +{ + const std::string url = GENERATE(std::string("http://example.com#section"), + std::string("http://example.com:8080#section"), + std::string("http://[::1]#section"), + std::string("http://example.com/path?"), + std::string("http://example.com?")); + std::string protocol, host, path, query; + int port = 0; + REQUIRE(ix::UrlParser::parse(url, protocol, host, path, query, port)); + CHECK(host == (url.find("[::1]") != std::string::npos ? "::1" : "example.com")); + CHECK(port == (url.find("8080") != std::string::npos ? 8080 : 80)); + CHECK(path == (url.find("/path") != std::string::npos ? "/path?" + : url.back() == '?' ? "/?" + : "/")); +} + +TEST_CASE("Explicit invalid URL ports do not fall back to a default", "[url][components]") +{ + const std::string portText = GENERATE(std::string("0"), + std::string("65536"), + std::string("4294967376"), + std::string("99999999999999999999999999999999")); + const std::string hostText = GENERATE(std::string("example.com"), std::string("[::1]")); + std::string protocol, host, path, query; + int port = 0; + CHECK_FALSE(ix::UrlParser::parse( + "http://" + hostText + ":" + portText + "/", protocol, host, path, query, port)); +} + +TEST_CASE("URL schemes follow the ASCII scheme grammar", "[url][components]") +{ + const std::string scheme = GENERATE(std::string(""), + std::string("+http"), + std::string(".http"), + std::string("1http"), + std::string("h\xe9ttp")); + std::string protocol, host, path, query; + int port = 0; + CHECK_FALSE( + ix::UrlParser::parse(scheme + "://example.com/", protocol, host, path, query, port)); + REQUIRE(ix::UrlParser::parse("SOCKS5://example.com:1080/", protocol, host, path, query, port)); + CHECK(protocol == "socks5"); + CHECK(port == 1080); +} + +TEST_CASE("URL schemes stay ASCII in single byte locales", "[url][components]") +{ + const char* current = std::setlocale(LC_CTYPE, nullptr); + const std::string saved = current ? current : "C"; + struct RestoreLocale + { + std::string name; + ~RestoreLocale() + { + std::setlocale(LC_CTYPE, name.c_str()); + } + } restore {saved}; + if (!std::setlocale(LC_CTYPE, "en_US.ISO8859-1")) + SKIP("Single-byte test locale is unavailable"); + std::string protocol, host, path, query; + int port = 0; + CHECK_FALSE(ix::UrlParser::parse("h\xe9ttp://example.com/", protocol, host, path, query, port)); + REQUIRE(ix::UrlParser::parse("HTTPS://example.com/", protocol, host, path, query, port)); + CHECK(protocol == "https"); +} + +TEST_CASE("URLs reject raw whitespace and control characters", "[url][components]") +{ + const char character = GENERATE(' ', '\t', '\r', '\n', '\0', '\x7f'); + const std::string prefix = GENERATE(std::string("http://exam"), + std::string("http://example.com/path"), + std::string("http://example.com/?query=")); + std::string protocol, host, path, query; + int port = 0; + CHECK_FALSE( + ix::UrlParser::parse(prefix + character + "text", protocol, host, path, query, port)); + CHECK(ix::UrlParser::parse("http://example.com/a%20b", protocol, host, path, query, port)); +}