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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ svr.Post("/multipart", [&](const Request& req, Response& res) {
auto safe = httplib::sanitize_filename(file.filename);
```

This function keeps only the part after the last `/` or `\`, removes null bytes, and trims leading and trailing spaces and tabs. It returns an empty string if what is left is empty, `.` or `..`.
This function keeps only the part after the last `/` or `\` (or, on Windows, `:`, which starts a drive prefix such as `C:` or an NTFS stream name), removes null bytes, and trims leading and trailing spaces and tabs. It returns an empty string if what is left is empty, `.` or `..`.

> [!NOTE]
> `name` and `filename` are read as quoted-strings (RFC 9110 §5.6.4), where `\"` and `\\` stand for `"` and `\`. Browsers send a backslash as it is, so from a browser, a value with `\\` in it arrives with a single `\`, and one that ends in `\` is not read correctly.
Expand Down
7 changes: 6 additions & 1 deletion httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11975,8 +11975,13 @@ inline std::string decode_query_component(const std::string &component,
}

inline std::string sanitize_filename(const std::string &filename) {
// Extract basename: find the last path separator (/ or \)
// Extract basename: find the last path separator (/ or \) and, on Windows,
// ':', which ends a drive prefix ("C:..") or names an NTFS stream ("a:b")
#ifdef _WIN32
auto pos = filename.find_last_of("/\\:");
#else
auto pos = filename.find_last_of("/\\");
#endif
auto result =
(pos != std::string::npos) ? filename.substr(pos + 1) : filename;

Expand Down
14 changes: 14 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,20 @@ TEST(SanitizeFilenameTest, VariousPatterns) {
EXPECT_EQ("", httplib::sanitize_filename(" "));
}

// On Windows a ':' ends a drive prefix or names an NTFS stream, so what comes
// before it is dropped; anywhere else it is an ordinary file name character.
TEST(SanitizeFilenameTest, ColonEndsPrefixOnlyOnWindows) {
#ifdef _WIN32
EXPECT_EQ("", httplib::sanitize_filename("C:.."));
EXPECT_EQ("evil.dll", httplib::sanitize_filename("C:evil.dll"));
EXPECT_EQ("stream", httplib::sanitize_filename("a.txt:stream"));
#else
EXPECT_EQ("C:..", httplib::sanitize_filename("C:.."));
EXPECT_EQ("C:evil.dll", httplib::sanitize_filename("C:evil.dll"));
EXPECT_EQ("a.txt:stream", httplib::sanitize_filename("a.txt:stream"));
#endif
}

// Forward declaration (see the base64_encode note below): split builds move the
// definition into httplib.cc, so re-declaring it here lets the test link.
namespace httplib {
Expand Down
Loading