From aed0ca31f7007320a53b27c69f0b73a57edcf096 Mon Sep 17 00:00:00 2001 From: Albert S Date: Mon, 30 May 2022 19:51:13 +0200 Subject: [PATCH] shared: SandboxedProcessor: Perform MIME-type detection Detect mime types, and for text/*, run the default text processor. The added benefit is that we can now add plaintext files without extensions, or many other text files (e. g. source code). --- shared/sandboxedprocessor.cpp | 28 +++++++++++++++++++++++----- shared/sandboxedprocessor.h | 2 ++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/shared/sandboxedprocessor.cpp b/shared/sandboxedprocessor.cpp index 1b4ede5..3e08754 100644 --- a/shared/sandboxedprocessor.cpp +++ b/shared/sandboxedprocessor.cpp @@ -22,7 +22,7 @@ static QMap processors{ {"py", defaultTextProcessor}, {"xml", nothingProcessor}, {"html", tagStripperProcessor}, {"java", defaultTextProcessor}, {"js", defaultTextProcessor}, {"cpp", defaultTextProcessor}, {"c", defaultTextProcessor}, {"sql", defaultTextProcessor}, {"odt", odtProcessor}, - {"ods", odsProcessor}}; + {"ods", odsProcessor}, {"svg", nothingProcessor}}; void SandboxedProcessor::enableSandbox(QString readablePath) { @@ -50,7 +50,7 @@ void SandboxedProcessor::enableSandbox(QString readablePath) int ret = exile_enable_policy(policy); if(ret != 0) { - qDebug() << "Failed to establish sandbox: " << ret; + qCritical() << "Failed to establish sandbox: " << ret; exit(EXIT_FAILURE); } exile_free_policy(policy); @@ -74,9 +74,27 @@ void SandboxedProcessor::printResults(const QVector &pageData) int SandboxedProcessor::process() { QFileInfo fileInfo(this->filePath); - Processor *processor = processors.value(fileInfo.suffix(), nothingProcessor); - - if(processor == nothingProcessor) + Processor *processor = processors.value(fileInfo.suffix(), nullptr); + if(processor == nullptr) + { + /* TODO: This is not sandboxed yet ... */ + QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileInfo); + if(mimeType.name().startsWith("text/")) + { + processor = defaultTextProcessor; + } + else + { + for(QString &str : mimeType.allAncestors()) + { + if(str.startsWith("text/")) + { + processor = defaultTextProcessor; + } + } + } + } + if(processor == nullptr || processor == nothingProcessor) { /* Nothing to do */ return NOTHING_PROCESSED; diff --git a/shared/sandboxedprocessor.h b/shared/sandboxedprocessor.h index 416d2e0..cf4a575 100644 --- a/shared/sandboxedprocessor.h +++ b/shared/sandboxedprocessor.h @@ -1,12 +1,14 @@ #ifndef SANDBOXEDPROCESSOR_H #define SANDBOXEDPROCESSOR_H #include +#include #include "pagedata.h" class SandboxedProcessor { private: QString filePath; + QMimeDatabase mimeDatabase; void enableSandbox(QString readablePath = ""); void printResults(const QVector &pageData);