From 5303ca008e760e4e4707c0816449fb83f278426b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 29 Nov 2022 17:54:12 +0100 Subject: Matrix: set filesize limit to int64 and remove 300mb limit for non-image files --- include/FileAnalyzer.hpp | 4 ++-- plugins/Matrix.hpp | 6 +++--- src/FileAnalyzer.cpp | 6 +++--- src/Storage.cpp | 4 ++-- src/plugins/Matrix.cpp | 22 +++++++++++----------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/include/FileAnalyzer.hpp b/include/FileAnalyzer.hpp index 4ae95ff..7be154f 100644 --- a/include/FileAnalyzer.hpp +++ b/include/FileAnalyzer.hpp @@ -56,7 +56,7 @@ namespace QuickMedia { const std::string& get_filepath() const; ContentType get_content_type() const; - size_t get_file_size() const; + int64_t get_file_size() const; std::optional get_dimensions() const; std::optional get_duration_seconds() const; private: @@ -65,7 +65,7 @@ namespace QuickMedia { private: std::string filepath; ContentType content_type; - size_t file_size; + int64_t file_size; std::optional dimensions; std::optional duration_seconds; bool loaded; diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index 8b9dcc9..0801418 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -260,7 +260,7 @@ namespace QuickMedia { struct UploadInfo { ContentType content_type; - size_t file_size; + int64_t file_size = 0; std::optional dimensions; std::optional duration_seconds; std::string content_uri; @@ -718,7 +718,7 @@ namespace QuickMedia { std::string message_get_author_displayname(Message *message) const; // Cached - PluginResult get_config(int *upload_size); + PluginResult get_config(int64_t *upload_size); std::shared_ptr get_me(RoomData *room); @@ -804,7 +804,7 @@ namespace QuickMedia { std::string homeserver; std::string homeserver_domain; std::string well_known_base_url; - std::optional upload_limit; + std::optional upload_limit; std::string next_batch; std::string next_notifications_token; std::mutex next_batch_mutex; diff --git a/src/FileAnalyzer.cpp b/src/FileAnalyzer.cpp index 65c33b5..361211f 100644 --- a/src/FileAnalyzer.cpp +++ b/src/FileAnalyzer.cpp @@ -240,9 +240,9 @@ namespace QuickMedia { dimensions = std::nullopt; duration_seconds = std::nullopt; - struct stat stat; + struct stat64 stat; memset(&stat, 0, sizeof(stat)); - if(fstat(fileno(file), &stat) == -1) { + if(fstat64(fileno(file), &stat) == -1) { perror(filepath); fclose(file); return false; @@ -315,7 +315,7 @@ namespace QuickMedia { return content_type; } - size_t FileAnalyzer::get_file_size() const { + int64_t FileAnalyzer::get_file_size() const { return file_size; } diff --git a/src/Storage.cpp b/src/Storage.cpp index 7554b73..c09e23b 100644 --- a/src/Storage.cpp +++ b/src/Storage.cpp @@ -191,10 +191,10 @@ namespace QuickMedia { } int file_get_size(const Path &path, int64_t *size) { - struct stat file_stat; + struct stat64 file_stat; memset(&file_stat, 0, sizeof(file_stat)); int ret; - EINTR_RETRY(ret, stat(path.data.c_str(), &file_stat)); + EINTR_RETRY(ret, stat64(path.data.c_str(), &file_stat)); if(ret == 0 && S_ISREG(file_stat.st_mode)) { *size = file_stat.st_size; return 0; diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 6040edd..38ab6d0 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -4870,20 +4870,14 @@ namespace QuickMedia { if(filename.empty()) filename = file_get_filename(filepath); - int upload_limit; + int64_t upload_limit; PluginResult config_result = get_config(&upload_limit); if(config_result != PluginResult::OK) { err_msg = "Failed to get file size limit from server"; return config_result; } - // Checking for sane file size limit client side, to prevent loading a huge file and crashing - if(file_analyzer.get_file_size() > 300 * 1024 * 1024) { // 300mb - err_msg = "File is too large! client-side limit is set to 300mb"; - return PluginResult::ERR; - } - - if((int)file_analyzer.get_file_size() > upload_limit) { + if(file_analyzer.get_file_size() > upload_limit) { err_msg = "File is too large! max upload size on your homeserver is " + std::to_string((double)upload_limit / 1024.0 / 1024.0) + " mb, the file you tried to upload is " + std::to_string((double)file_analyzer.get_file_size() / 1024.0 / 1024.0) + " mb"; return PluginResult::ERR; } @@ -4912,6 +4906,12 @@ namespace QuickMedia { fprintf(stderr, "Failed to create temporary file for video thumbnail, ignoring thumbnail...\n"); } } else if(upload_thumbnail && is_content_type_image(file_analyzer.get_content_type())) { + // Checking for sane file size limit client side, to prevent loading a huge file and crashing + if(file_analyzer.get_file_size() > 300 * 1024 * 1024) { // 300mb + err_msg = "File is too large! client-side limit for images is set to 300mb"; + return PluginResult::ERR; + } + char tmp_filename[] = "/tmp/quickmedia_thumbnail_XXXXXX"; int tmp_file = mkstemp(tmp_filename); if(tmp_file != -1) { @@ -5758,7 +5758,7 @@ namespace QuickMedia { return message->user->room->get_user_display_name(message->user); } - PluginResult Matrix::get_config(int *upload_size) { + PluginResult Matrix::get_config(int64_t *upload_size) { // TODO: What if the upload limit changes? is it possible for the upload limit to change while the server is running? if(upload_limit) { *upload_size = upload_limit.value(); @@ -5782,10 +5782,10 @@ namespace QuickMedia { return PluginResult::ERR; const rapidjson::Value &upload_size_json = GetMember(json_root, "m.upload.size"); - if(!upload_size_json.IsInt()) + if(!upload_size_json.IsInt64()) return PluginResult::ERR; - upload_limit = upload_size_json.GetInt(); + upload_limit = upload_size_json.GetInt64(); *upload_size = upload_limit.value(); return PluginResult::OK; } -- cgit v1.2.3