Skip to content

Commit

Permalink
tweak(citizen-server-impl): change how postmap strings are parsed. Ad…
Browse files Browse the repository at this point in the history
…d sv_maxClientEndpointRequestSize limit
  • Loading branch information
LWSS committed Jan 26, 2024
1 parent eac42c1 commit 23f68a2
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions code/components/citizen-server-impl/src/ClientHttpHandler.cpp
Expand Up @@ -12,6 +12,7 @@
using json = nlohmann::json;

static std::shared_ptr<ConVar<bool>> g_threadedHttpVar;
static std::shared_ptr<ConVar<int>> g_maxClientEndpointRequestSize;

namespace fx
{
Expand All @@ -31,32 +32,46 @@ namespace fx
{
std::map<std::string, std::string> postMap;

// split the string by the usual post map characters
int curPos = 0;

while (true)
for (int i = 0; i < postDataString.size(); i++)
{
int endPos = postDataString.find_first_of('&', curPos);
int keyLen = 0;
for (int keyItr = i; keyItr < postDataString.size(); keyItr++)
{
if (postDataString[keyItr] == '=')
{
break;
}
keyLen++;
}

std::string key(&postDataString[i], keyLen);

int equalsPos = postDataString.find_first_of('=', curPos);
i = (i + keyLen + 1);

std::string key;
std::string value;
int valueLen = 0;
for (int valueItr = i; valueItr < postDataString.size(); valueItr++)
{
if (postDataString[valueItr] == '&')
{
break;
}
valueLen++;
}

UrlDecode(std::string(postDataString.substr(curPos, equalsPos - curPos)), key);
UrlDecode(std::string(postDataString.substr(equalsPos + 1, endPos - equalsPos - 1)), value);
if (valueLen)
{
std::string value(&postDataString[i], valueLen);

postMap[key] = value;
std::string keyDecoded;
std::string valueDecoded;

// save and continue
curPos = endPos;
UrlDecode(key, keyDecoded);
UrlDecode(value, valueDecoded);

if (curPos == std::string::npos)
{
break;
postMap[keyDecoded] = valueDecoded;
}

curPos++;
i += valueLen;
}

return postMap;
Expand All @@ -80,6 +95,12 @@ namespace fx
response->End(json::object({ {"error", error} }).dump(-1, ' ', false, json::error_handler_t::replace));
};

if (postData.size() > g_maxClientEndpointRequestSize->GetValue())
{
endError("POST data too big");
return;
}

auto postMap = ParsePOSTString(std::string(postData.begin(), postData.end()));

auto method = postMap.find("method");
Expand Down Expand Up @@ -165,6 +186,7 @@ static InitFunction initFunction([]()
fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance)
{
g_threadedHttpVar = instance->AddVariable<bool>("sv_threadedClientHttp", ConVar_None, true);
g_maxClientEndpointRequestSize = instance->AddVariable<int>("sv_maxClientEndpointRequestSize", ConVar_None, 1024 * 100);

instance->SetComponent(new fx::ClientMethodRegistry());

Expand Down

0 comments on commit 23f68a2

Please sign in to comment.