Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ixwebsocket/IXSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
38 changes: 24 additions & 14 deletions ixwebsocket/IXUrlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<unsigned char>(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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -172,7 +181,7 @@ namespace
current += 2;

// Step 3: locate the authority boundary (<authority> 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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -443,7 +453,7 @@ namespace ix
path = '/' + path;
}

if (!query.empty())
if (res.m_HasQuery)
{
path += "?";
path += query;
Expand Down
24 changes: 24 additions & 0 deletions test/IXSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h>
#include <string.h>
#include <thread>

using namespace ix;

Expand Down Expand Up @@ -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<bool> 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,
Expand Down
83 changes: 82 additions & 1 deletion test/IXUrlParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "IXTest.h"
#include <catch_amalgamated.hpp>
#include <clocale>
#include <iostream>
#include <ixwebsocket/IXUrlParser.h>
#include <string.h>
Expand Down Expand Up @@ -219,7 +220,7 @@ namespace ix
SECTION("reject malformed userinfo")
{
std::vector<std::string> 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
};

Expand All @@ -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));
}
Loading