Add unsafe/unsanitized load functions

This commit is contained in:
Dominique Jacquel 2022-02-11 09:48:33 +00:00
parent c44fec82bf
commit 787506350f
2 changed files with 20 additions and 1 deletions

View File

@ -218,6 +218,12 @@ namespace crow
void set_static_file_info(std::string path) void set_static_file_info(std::string path)
{ {
utility::sanitize_filename(path); utility::sanitize_filename(path);
set_static_file_info_unsafe(path);
}
/// Return a static file as the response body without sanitizing the path (use set_static_file_info instead)
void set_static_file_info_unsafe(std::string path)
{
file_info.path = path; file_info.path = path;
file_info.statResult = stat(file_info.path.c_str(), &file_info.statbuf); file_info.statResult = stat(file_info.path.c_str(), &file_info.statbuf);
#ifdef CROW_ENABLE_COMPRESSION #ifdef CROW_ENABLE_COMPRESSION
@ -243,6 +249,7 @@ namespace crow
else else
{ {
code = 404; code = 404;
file_info.path.clear();
this->end(); this->end();
} }
} }

View File

@ -631,6 +631,13 @@ namespace crow
} }
inline std::string load_text(const std::string& filename) inline std::string load_text(const std::string& filename)
{
std::string filename_sanitized(filename);
utility::sanitize_filename(filename_sanitized);
return detail::get_loader_ref()(filename_sanitized);
}
inline std::string load_text_unsafe(const std::string& filename)
{ {
return detail::get_loader_ref()(filename); return detail::get_loader_ref()(filename);
} }
@ -641,5 +648,10 @@ namespace crow
utility::sanitize_filename(filename_sanitized); utility::sanitize_filename(filename_sanitized);
return compile(detail::get_loader_ref()(filename_sanitized)); return compile(detail::get_loader_ref()(filename_sanitized));
} }
inline template_t load_unsafe(const std::string& filename)
{
return compile(detail::get_loader_ref()(filename));
}
} // namespace mustache } // namespace mustache
} // namespace crow } // namespace crow