Skip to content

Commit

Permalink
Treat paths with embedded NUL bytes as invalid (#1765)
Browse files Browse the repository at this point in the history
Fixes #1763.
  • Loading branch information
wandernauta authored Jan 27, 2024
1 parent 44b3fe6 commit 4ef9ed8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,7 @@ inline bool is_valid_path(const std::string &path) {
// Read component
auto beg = i;
while (i < path.size() && path[i] != '/') {
if (path[i] == '\0') { return false; }
i++;
}

Expand Down
15 changes: 15 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ TEST(DecodeURLTest, PercentCharacter) {
R"(descrip=Gastos áéíóúñÑ 6)");
}

TEST(DecodeURLTest, PercentCharacterNUL) {
string expected;
expected.push_back('x');
expected.push_back('\0');
expected.push_back('x');

EXPECT_EQ(detail::decode_url("x%00x", false), expected);
}

TEST(EncodeQueryParamTest, ParseUnescapedChararactersTest) {
string unescapedCharacters = "-_.!~*'()";

Expand Down Expand Up @@ -2482,6 +2491,12 @@ TEST_F(ServerTest, GetMethodInvalidMountPath) {
EXPECT_EQ(StatusCode::NotFound_404, res->status);
}

TEST_F(ServerTest, GetMethodEmbeddedNUL) {
auto res = cli_.Get("/mount/dir/test.html%00.js");
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::NotFound_404, res->status);
}

TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
auto res = cli_.Get("/mount/../www2/dir/test.html");
ASSERT_TRUE(res);
Expand Down

0 comments on commit 4ef9ed8

Please sign in to comment.