1
0
mirror of synced 2025-01-31 03:53:44 +01:00

patterns: Disallow application/octet-stream to be used as MIME type

This commit is contained in:
WerWolv 2023-01-09 08:38:19 +01:00
parent c1359a71d6
commit dbcb13f473
4 changed files with 23 additions and 5 deletions

View File

@ -21,4 +21,6 @@ namespace hex::magic {
std::string getMIMEType(const std::vector<u8> &data);
std::string getMIMEType(prv::Provider *provider, size_t size = 100_KiB);
bool isValidMIMEType(const std::string &mimeType);
}

View File

@ -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;
}
}

View File

@ -1,5 +1,6 @@
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/magic.hpp>
#include <pl/core/evaluator.hpp>
@ -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);
});
}
}

View File

@ -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<RequestOpenPopup>("hex.builtin.view.pattern_editor.accept_pattern"_lang);