Refuse to serve a path segment ending in a dot or a space

Windows trims a trailing '.' or space from a file name, so a mount
point there served a file under a path other than the one a pre-routing
handler checked. A mount point now serves no path with a segment
ending in '.' or a space, on every platform; "." and ".." are among
them. The README no longer lists such names among the ones the file
system can still alias.
This commit is contained in:
yhirose committed 2026-10-22 11:16:27 -04:00
1 parent 70bed02bc9
commit b4b24589ad
3 files changed
+19 -9

No files matched your search

+12 -3
View File
@@ -566,6 +566,7 @@ find_content_type(const std::string &path,
const std::string &default_content_type);
bool can_compress_content_type(const std::string &content_type);
bool is_prohibited_header_name(const std::string &name);
bool is_valid_path(const std::string &path);
#ifdef CPPHTTPLIB_SSL_ENABLED
bool match_hostname(const std::string &pattern, const std::string &hostname);
#endif
@@ -693,6 +694,12 @@ TEST(FindContentTypeTest, ComparesWholeExtensions) {
EXPECT_EQ("", detail::find_content_type("/a.l5", {}, ""));
}
TEST(IsValidPathTest, RejectsSegmentsEndingInDotOrSpace) {
for (auto path : {"/a.", "/a ", "/dir./a", "/dir /a", "/..."}) {
EXPECT_FALSE(detail::is_valid_path(path)) << path;
}
}
TEST(CanCompressContentTypeTest, ComparesWholeTypes) {
EXPECT_TRUE(detail::can_compress_content_type("application/json"));
EXPECT_TRUE(detail::can_compress_content_type("text/plain; charset=utf-8"));
@@ -12607,8 +12614,9 @@ TEST(MountTest, MultibytesPathName) {
EXPECT_EQ(U8("日本語コンテンツ"), res->body);
}
// A mount point serves no path with an empty, "." or ".." segment, so a guard
// in the pre-routing handler sees the path of every file that is served
// A mount point serves no path with an empty segment or one ending in '.' or
// a space, so a guard in the pre-routing handler sees the path of every file
// that is served
TEST(MountTest, AliasedPathDoesNotPassPreRoutingGuard) {
Server svr;
ASSERT_TRUE(svr.set_mount_point("/", "./www"));
@@ -12633,7 +12641,8 @@ TEST(MountTest, AliasedPathDoesNotPassPreRoutingGuard) {
Client cli(HOST, port);
for (auto path :
{"/./dir/test.html", "/dir/./test.html", "//dir/test.html",
"/dir//test.html", "/dir/../dir/test.html", "/%2e/dir/test.html"}) {
"/dir//test.html", "/dir/../dir/test.html", "/%2e/dir/test.html",
"/dir/test.html.", "/dir/test.html%20"}) {
SCOPED_TRACE(path);
auto res = cli.Get(path);
ASSERT_TRUE(res) << "Error: " << to_string(res.error());