Skip to content

Commit 4ef9ed8

Browse files
authored
Treat paths with embedded NUL bytes as invalid (yhirose#1765)
Fixes yhirose#1763.
1 parent 44b3fe6 commit 4ef9ed8

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

httplib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,7 @@ inline bool is_valid_path(const std::string &path) {
24132413
// Read component
24142414
auto beg = i;
24152415
while (i < path.size() && path[i] != '/') {
2416+
if (path[i] == '\0') { return false; }
24162417
i++;
24172418
}
24182419

test/test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ TEST(DecodeURLTest, PercentCharacter) {
7171
R"(descrip=Gastos áéíóúñÑ 6)");
7272
}
7373

74+
TEST(DecodeURLTest, PercentCharacterNUL) {
75+
string expected;
76+
expected.push_back('x');
77+
expected.push_back('\0');
78+
expected.push_back('x');
79+
80+
EXPECT_EQ(detail::decode_url("x%00x", false), expected);
81+
}
82+
7483
TEST(EncodeQueryParamTest, ParseUnescapedChararactersTest) {
7584
string unescapedCharacters = "-_.!~*'()";
7685

@@ -2482,6 +2491,12 @@ TEST_F(ServerTest, GetMethodInvalidMountPath) {
24822491
EXPECT_EQ(StatusCode::NotFound_404, res->status);
24832492
}
24842493

2494+
TEST_F(ServerTest, GetMethodEmbeddedNUL) {
2495+
auto res = cli_.Get("/mount/dir/test.html%00.js");
2496+
ASSERT_TRUE(res);
2497+
EXPECT_EQ(StatusCode::NotFound_404, res->status);
2498+
}
2499+
24852500
TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
24862501
auto res = cli_.Get("/mount/../www2/dir/test.html");
24872502
ASSERT_TRUE(res);

0 commit comments

Comments
 (0)