From dbcb13f473ee312998d8586024ff5a9ce9f249f2 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 9 Jan 2023 08:38:19 +0100 Subject: [PATCH] patterns: Disallow `application/octet-stream` to be used as MIME type --- lib/libimhex/include/hex/helpers/magic.hpp | 2 ++ lib/libimhex/source/helpers/magic.cpp | 14 ++++++++++++++ plugins/builtin/source/content/pl_pragmas.cpp | 7 ++++--- .../source/content/views/view_pattern_editor.cpp | 5 +++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/magic.hpp b/lib/libimhex/include/hex/helpers/magic.hpp index 0450b212c..97c867f18 100644 --- a/lib/libimhex/include/hex/helpers/magic.hpp +++ b/lib/libimhex/include/hex/helpers/magic.hpp @@ -21,4 +21,6 @@ namespace hex::magic { std::string getMIMEType(const std::vector &data); std::string getMIMEType(prv::Provider *provider, size_t size = 100_KiB); + bool isValidMIMEType(const std::string &mimeType); + } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/magic.cpp b/lib/libimhex/source/helpers/magic.cpp index 101908669..3898c6b0d 100644 --- a/lib/libimhex/source/helpers/magic.cpp +++ b/lib/libimhex/source/helpers/magic.cpp @@ -96,4 +96,18 @@ namespace hex::magic { return getMIMEType(buffer); } + bool isValidMIMEType(const std::string &mimeType) { + // MIME types always contain a slash + if (!mimeType.contains("/")) + return false; + + // The MIME type "application/octet-stream" is a fallback type for arbitrary binary data. + // Specifying this in a pattern would make it get suggested for every single unknown binary that's being loaded. + // We don't want that, so we ignore it here + if (mimeType == "application/octet-stream") + return false; + + return true; + } + } \ No newline at end of file diff --git a/plugins/builtin/source/content/pl_pragmas.cpp b/plugins/builtin/source/content/pl_pragmas.cpp index 7699e3914..3da29a872 100644 --- a/plugins/builtin/source/content/pl_pragmas.cpp +++ b/plugins/builtin/source/content/pl_pragmas.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -8,8 +9,6 @@ namespace hex::plugin::builtin { void registerPatternLanguagePragmas() { ContentRegistry::PatternLanguage::addPragma("base_address", [](pl::PatternLanguage &runtime, const std::string &value) { - hex::unused(runtime); - auto baseAddress = strtoull(value.c_str(), nullptr, 0); ImHexApi::Provider::get()->setBaseAddress(baseAddress); @@ -18,7 +17,9 @@ namespace hex::plugin::builtin { return true; }); - ContentRegistry::PatternLanguage::addPragma("MIME", [](pl::PatternLanguage&, const std::string &value) { return !value.empty(); }); + ContentRegistry::PatternLanguage::addPragma("MIME", [](pl::PatternLanguage&, const std::string &value) { + return magic::isValidMIMEType(value); + }); } } \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 52fd2bf84..d736077e4 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -773,6 +773,9 @@ namespace hex::plugin::builtin { runtime->addPragma("MIME", [&mimeType, &foundCorrectType](pl::PatternLanguage &runtime, const std::string &value) { hex::unused(runtime); + if (!magic::isValidMIMEType(value)) + return false; + if (value == mimeType) { foundCorrectType = true; return true; @@ -802,8 +805,6 @@ namespace hex::plugin::builtin { } } - runtime->addPragma("MIME", [](pl::PatternLanguage&, const std::string &value) { return !value.empty(); }); - if (!this->m_possiblePatternFiles.empty()) { this->m_selectedPatternFile = 0; EventManager::post("hex.builtin.view.pattern_editor.accept_pattern"_lang);