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
12 changes: 6 additions & 6 deletions lib/src/HttpRequestImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ void HttpRequestImpl::parseParameters() const
if (!input.empty())
{
std::string_view::size_type pos = 0;
while ((input[pos] == '?' ||
isspace(static_cast<unsigned char>(input[pos]))) &&
pos < input.length())
while (pos < input.length() &&
(input[pos] == '?' ||
isspace(static_cast<unsigned char>(input[pos]))))
{
++pos;
}
Expand Down Expand Up @@ -139,9 +139,9 @@ void HttpRequestImpl::parseParameters() const
type.find("application/x-www-form-urlencoded") != std::string::npos)
{
std::string_view::size_type pos = 0;
while ((input[pos] == '?' ||
isspace(static_cast<unsigned char>(input[pos]))) &&
pos < input.length())
while (pos < input.length() &&
(input[pos] == '?' ||
isspace(static_cast<unsigned char>(input[pos]))))
{
++pos;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/MultiPart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ int MultiPartParser::parse(const HttpRequestPtr &req,
std::string_view boundary{boundaryData, boundaryLen};
if (boundary.size() > 2 && boundary[0] == '\"')
boundary = boundary.substr(1, boundary.size() - 2);
if (boundary.empty())
return -1;
std::string_view::size_type pos1, pos2;
pos1 = 0;
auto content = static_cast<HttpRequestImpl *>(req.get())->bodyView();
Expand All @@ -190,11 +192,15 @@ int MultiPartParser::parse(const HttpRequestPtr &req,
if (pos1 == std::string_view::npos)
break;
pos1 += boundary.length();
if (content.size() - pos1 < 2)
return -1;
if (content[pos1] == '\r' && content[pos1 + 1] == '\n')
pos1 += 2;
pos2 = content.find(boundary, pos1);
if (pos2 == std::string_view::npos)
break;
if (pos2 - pos1 < 4)
return -1;
bool flag = false;
if (content[pos2 - 4] == '\r' && content[pos2 - 3] == '\n' &&
content[pos2 - 2] == '-' && content[pos2 - 1] == '-')
Expand Down
17 changes: 17 additions & 0 deletions lib/tests/unittests/MultiPartParserTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ DROGON_TEST(MultiPartParserDashBoundary)
CHECK(parser.getParameters().at("key1") == "value1");
}

DROGON_TEST(MultiPartParserRejectsNearbyBoundary)
{
auto req = drogon::HttpRequest::newHttpRequest();
req->setMethod(drogon::Post);
req->addHeader("content-type", "multipart/form-data; boundary=A");
// The second boundary starts only three bytes into the body.
req->setBody(std::string("A\r\nA") + std::string(100, 'x'));

drogon::MultiPartParser parser;
CHECK(parser.parse(req) == -1);
Comment thread
marty1885 marked this conversation as resolved.
Resolved

// A boundary at the end leaves no bytes for the CRLF check.
req->setBody("A");
drogon::MultiPartParser parser2;
CHECK(parser2.parse(req) == -1);
}

DROGON_TEST(MultiPartDispositionParametersAreOrderIndependent)
{
static const std::string contentType =
Expand Down
Loading