diff options
author | saturneric <[email protected]> | 2024-03-01 06:36:33 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-03-01 06:36:33 +0000 |
commit | a74841f70cfe985ca12af353ed289925085241c1 (patch) | |
tree | 1975edc8d37a6519aea02c5762025d85c639d9fa | |
parent | feat: add prefix GF to all sdk and module symbols (diff) | |
download | GpgFrontend-a74841f70cfe985ca12af353ed289925085241c1.tar.gz GpgFrontend-a74841f70cfe985ca12af353ed289925085241c1.zip |
feat: validate module id format and compatibility of sdk and qt env
6 files changed, 175 insertions, 86 deletions
diff --git a/src/core/module/Module.cpp b/src/core/module/Module.cpp index 0c45dcbe..39f4f4a9 100644 --- a/src/core/module/Module.cpp +++ b/src/core/module/Module.cpp @@ -32,6 +32,7 @@ #include "core/utils/CommonUtils.h" #include "core/utils/IOUtils.h" #include "module/sdk/GFSDKModule.h" +#include "utils/BuildInfoUtils.h" namespace GpgFrontend::Module { @@ -67,9 +68,63 @@ class Module::Impl { identifier_ = GFUnStrDup(get_id_api_()); version_ = GFUnStrDup(get_version_api_()); + gf_sdk_ver_ = GFUnStrDup(get_sdk_ver_api_()); + qt_env_ver_ = GFUnStrDup(get_qt_ver_api_()); + + if (!module_identifier_regex_exp_.match(identifier_).hasMatch()) { + GF_CORE_LOG_WARN( + "illegal module: {}, reasson invalid module id, abort...", + identifier_); + return; + } + + if (!module_version_regex_exp_.match(version_).hasMatch()) { + GF_CORE_LOG_WARN( + "illegal module: {}, reasson invalid version: {}, abort...", + identifier_, version_); + return; + } - GF_CORE_LOG_INFO("module loaded, id: {}, verison: {}, hash: {}, path: {}", - identifier_, version_, module_hash_, module_library_path_); + if (!module_version_regex_exp_.match(gf_sdk_ver_).hasMatch()) { + GF_CORE_LOG_WARN( + "illegal module: {}, reasson invalid sdk version: {}, abort...", + identifier_, gf_sdk_ver_); + return; + } + + if (GFCompareSoftwareVersion(gf_sdk_ver_, GetProjectVersion()) > 0) { + GF_CORE_LOG_WARN( + "uncompatible module: {}, sdk version: {} greater than " + "current sdk version: {}, abort...", + identifier_, gf_sdk_ver_, GetProjectVersion()); + return; + } + + auto qt_env_ver_regex_match = module_version_regex_exp_.match(qt_env_ver_); + if (!qt_env_ver_regex_match.hasMatch()) { + GF_CORE_LOG_WARN( + "illegal module: {}, reasson invalid qt env version: {}, abort...", + identifier_, qt_env_ver_); + return; + } + + auto qt_env_ver_major = qt_env_ver_regex_match.captured(1); + auto qt_env_ver_minor = qt_env_ver_regex_match.captured(2); + + if (qt_env_ver_major != QString::number(QT_VERSION_MAJOR) + "." || + qt_env_ver_minor != QString::number(QT_VERSION_MINOR) + ".") { + GF_CORE_LOG_WARN( + "uncompatible module: {}, qt version: {} is not binary uncompatible " + "with application's qt env version: {}, abort...", + identifier_, qt_env_ver_, QString::fromUtf8(QT_VERSION_STR)); + return; + } + + GF_CORE_LOG_INFO( + "module loaded, id: {}, verison: {}, " + "sdk version: {}, qt env version: {}, hash: {}, path: {}", + identifier_, version_, gf_sdk_ver_, qt_env_ver_, module_hash_, + module_library_path_); ::GFModuleMetaData* p_meta_data = get_metadata_api_(); ::GFModuleMetaData* l_meta_data; @@ -136,6 +191,14 @@ class Module::Impl { return version_; } + [[nodiscard]] auto GetModuleSDKVersion() const -> QString { + return gf_sdk_ver_; + } + + [[nodiscard]] auto GetModuleQtEnvVersion() const -> QString { + return qt_env_ver_; + } + [[nodiscard]] auto GetModuleMetaData() const -> ModuleMetaData { return meta_data_; } @@ -159,6 +222,11 @@ class Module::Impl { QString gf_sdk_ver_; QString qt_env_ver_; + QRegularExpression module_identifier_regex_exp_ = QRegularExpression( + R"(^([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*$)"); + QRegularExpression module_version_regex_exp_ = + QRegularExpression(R"(^(\d+\.)?(\d+\.)?(\*|\d+)$)"); + bool good_; GFModuleAPIGetModuleGFSDKVersion get_sdk_ver_api_; @@ -254,5 +322,13 @@ auto Module::GetModuleIdentifier() const -> ModuleIdentifier { return p_->GetModuleHash(); } +[[nodiscard]] auto Module::GetModuleSDKVersion() const -> QString { + return p_->GetModuleSDKVersion(); +} + +[[nodiscard]] auto Module::GetModuleQtEnvVersion() const -> QString { + return p_->GetModuleQtEnvVersion(); +} + void Module::SetGPC(GlobalModuleContext* gpc) { p_->SetGPC(gpc); } } // namespace GpgFrontend::Module
\ No newline at end of file diff --git a/src/core/module/Module.h b/src/core/module/Module.h index 3e51357d..16f55fb1 100644 --- a/src/core/module/Module.h +++ b/src/core/module/Module.h @@ -75,6 +75,10 @@ class GPGFRONTEND_CORE_EXPORT Module : public QObject { [[nodiscard]] auto GetModuleHash() const -> QString; + [[nodiscard]] auto GetModuleSDKVersion() const -> QString; + + [[nodiscard]] auto GetModuleQtEnvVersion() const -> QString; + void SetGPC(GlobalModuleContext *); protected: diff --git a/src/module/integrated/gnupg_info_gathering_module/GnuPGInfoGatheringModule.cpp b/src/module/integrated/gnupg_info_gathering_module/GnuPGInfoGatheringModule.cpp index 38bdde11..dd506134 100644 --- a/src/module/integrated/gnupg_info_gathering_module/GnuPGInfoGatheringModule.cpp +++ b/src/module/integrated/gnupg_info_gathering_module/GnuPGInfoGatheringModule.cpp @@ -59,7 +59,7 @@ auto GFGetModuleQtEnvVersion() -> const char * { auto GFGetModuleID() -> const char * { return GFModuleStrDup( - "com.bktus.gpgfrontend.module.integrated.gnupg-info-gathering"); + "com.bktus.gpgfrontend.module.integrated.gnupg_info_gathering"); } auto GFGetModuleVersion() -> const char * { return GFModuleStrDup("1.0.0"); } @@ -74,10 +74,10 @@ auto GFGetModuleMetaData() -> GFModuleMetaData * { p_meta->next = static_cast<GFModuleMetaData *>( GFAllocateMemory(sizeof(GFModuleMetaData))); p_meta = p_meta->next; + p_meta->key = "Author"; p_meta->value = "Saturneric"; p_meta->next = nullptr; - return h_meta; } diff --git a/src/module/integrated/version_checking_module/VersionCheckingModule.cpp b/src/module/integrated/version_checking_module/VersionCheckingModule.cpp index 4ec63792..0c97311d 100644 --- a/src/module/integrated/version_checking_module/VersionCheckingModule.cpp +++ b/src/module/integrated/version_checking_module/VersionCheckingModule.cpp @@ -51,7 +51,7 @@ auto GFGetModuleQtEnvVersion() -> const char* { auto GFGetModuleID() -> const char* { return GFModuleStrDup( - "com.bktus.gpgfrontend.module.integrated.version-checking"); + "com.bktus.gpgfrontend.module.integrated.version_checking"); } auto GFGetModuleVersion() -> const char* { return GFModuleStrDup("1.0.0"); } diff --git a/src/ui/dialog/controller/ModuleControllerDialog.cpp b/src/ui/dialog/controller/ModuleControllerDialog.cpp index 71e39502..309ad8e1 100644 --- a/src/ui/dialog/controller/ModuleControllerDialog.cpp +++ b/src/ui/dialog/controller/ModuleControllerDialog.cpp @@ -85,6 +85,10 @@ void ModuleControllerDialog::slot_load_module_details( info << "# BASIC INFO" << Qt::endl << Qt::endl; info << tr("Version") << ": " << module->GetModuleVersion() << Qt::endl; + info << tr("SDK Version") << ": " << module->GetModuleSDKVersion() + << Qt::endl; + info << tr("Qt ENV Version") << ": " << module->GetModuleQtEnvVersion() + << Qt::endl; info << tr("Hash") << ": " << module->GetModuleHash() << Qt::endl; info << tr("Path") << ": " << module->GetModulePath() << Qt::endl; diff --git a/src/ui/dialog/import_export/KeyServerImportDialog.cpp b/src/ui/dialog/import_export/KeyServerImportDialog.cpp index 296df539..f1636987 100644 --- a/src/ui/dialog/import_export/KeyServerImportDialog.cpp +++ b/src/ui/dialog/import_export/KeyServerImportDialog.cpp @@ -243,11 +243,14 @@ void KeyServerImportDialog::slot_search_finished( set_message("<h4>" + tr("Too many responses from keyserver!") + "</h4>", true); return; - } else if (text.contains("No keys found")) { + } + + if (text.contains("No keys found")) { + auto query = search_line_edit_->text(); + // if string looks like hex string, search again with 0x prepended - QRegExp rx("[0-9A-Fa-f]*"); - QString query = search_line_edit_->text(); - if (rx.exactMatch(query)) { + QRegularExpression rx("[0-9A-Fa-f]*"); + if (rx.match(query).hasMatch()) { set_message("<h4>" + tr("No keys found, input may be kexId, retrying search " "with 0x.") + @@ -257,102 +260,104 @@ void KeyServerImportDialog::slot_search_finished( this->slot_search(); return; } + set_message( "<h4>" + tr("No keys found containing the search string!") + "</h4>", true); return; + } - } else if (text.contains("Insufficiently specific words")) { + if (text.contains("Insufficiently specific words")) { set_message( "<h4>" + tr("Insufficiently specific search string!") + "</h4>", true); return; - } else { - set_message(text, true); - return; } - } else { - int row = 0; - bool strikeout = false; - - // read lines until end of steam - while (!stream.atEnd()) { - QStringList line = - QString::fromUtf8(QByteArray::fromPercentEncoding( - stream.readLine().trimmed().toUtf8())) - .split(":"); - - // TODO: have a look at two following pub lines - if (line[0] == "pub") { - strikeout = false; - - QString flags = line[line.size() - 1]; - keys_table_->setRowCount(row + 1); - - // flags can be "d" for disabled, "r" for revoked - // or "e" for expired - if (flags.contains("r") or flags.contains("d") or flags.contains("e")) { - strikeout = true; - if (flags.contains("e")) { - keys_table_->setItem(row, 3, - new QTableWidgetItem(QString("expired"))); - } - if (flags.contains("r")) { - keys_table_->setItem(row, 3, new QTableWidgetItem(tr("revoked"))); - } - if (flags.contains("d")) { - keys_table_->setItem(row, 3, new QTableWidgetItem(tr("disabled"))); - } - } - QStringList line2 = QString(QByteArray::fromPercentEncoding( - stream.readLine().trimmed().toUtf8())) - .split(":"); + set_message(text, true); + return; + } - auto* uid = new QTableWidgetItem(); - if (line2.size() > 1) { - uid->setText(line2[1]); - keys_table_->setItem(row, 0, uid); + int row = 0; + bool strikeout = false; + + // read lines until end of steam + while (!stream.atEnd()) { + QStringList line = + QString::fromUtf8(QByteArray::fromPercentEncoding( + stream.readLine().trimmed().toUtf8())) + .split(":"); + + // TODO: have a look at two following pub lines + if (line[0] == "pub") { + strikeout = false; + + QString flags = line[line.size() - 1]; + keys_table_->setRowCount(row + 1); + + // flags can be "d" for disabled, "r" for revoked + // or "e" for expired + if (flags.contains("r") or flags.contains("d") or flags.contains("e")) { + strikeout = true; + if (flags.contains("e")) { + keys_table_->setItem(row, 3, + new QTableWidgetItem(QString("expired"))); } - auto* creation_date = - new QTableWidgetItem(QDateTime::fromSecsSinceEpoch(line[4].toInt()) - .toString("dd. MMM. yyyy")); - keys_table_->setItem(row, 1, creation_date); - auto* keyid = new QTableWidgetItem(line[1]); - keys_table_->setItem(row, 2, keyid); + if (flags.contains("r")) { + keys_table_->setItem(row, 3, new QTableWidgetItem(tr("revoked"))); + } + if (flags.contains("d")) { + keys_table_->setItem(row, 3, new QTableWidgetItem(tr("disabled"))); + } + } + + QStringList line2 = QString(QByteArray::fromPercentEncoding( + stream.readLine().trimmed().toUtf8())) + .split(":"); + + auto* uid = new QTableWidgetItem(); + if (line2.size() > 1) { + uid->setText(line2[1]); + keys_table_->setItem(row, 0, uid); + } + auto* creation_date = + new QTableWidgetItem(QDateTime::fromSecsSinceEpoch(line[4].toInt()) + .toString("dd. MMM. yyyy")); + keys_table_->setItem(row, 1, creation_date); + auto* keyid = new QTableWidgetItem(line[1]); + keys_table_->setItem(row, 2, keyid); + if (strikeout) { + QFont strike = uid->font(); + strike.setStrikeOut(true); + uid->setFont(strike); + creation_date->setFont(strike); + keyid->setFont(strike); + } + row++; + } else { + if (line[0] == "uid") { + QStringList l; + int height = keys_table_->rowHeight(row - 1); + keys_table_->setRowHeight(row - 1, height + 16); + QString tmp = keys_table_->item(row - 1, 0)->text(); + tmp.append(QString("\n") + line[1]); + auto* tmp1 = new QTableWidgetItem(tmp); + keys_table_->setItem(row - 1, 0, tmp1); if (strikeout) { - QFont strike = uid->font(); + QFont strike = tmp1->font(); strike.setStrikeOut(true); - uid->setFont(strike); - creation_date->setFont(strike); - keyid->setFont(strike); - } - row++; - } else { - if (line[0] == "uid") { - QStringList l; - int height = keys_table_->rowHeight(row - 1); - keys_table_->setRowHeight(row - 1, height + 16); - QString tmp = keys_table_->item(row - 1, 0)->text(); - tmp.append(QString("\n") + line[1]); - auto* tmp1 = new QTableWidgetItem(tmp); - keys_table_->setItem(row - 1, 0, tmp1); - if (strikeout) { - QFont strike = tmp1->font(); - strike.setStrikeOut(true); - tmp1->setFont(strike); - } + tmp1->setFont(strike); } } - set_message( - QString("<h4>") + - tr("%1 keys found. Double click a key to import it.").arg(row) + - "</h4>", - false); } - keys_table_->resizeColumnsToContents(); - import_button_->setDisabled(keys_table_->size().isEmpty()); + set_message( + QString("<h4>") + + tr("%1 keys found. Double click a key to import it.").arg(row) + + "</h4>", + false); } + keys_table_->resizeColumnsToContents(); + import_button_->setDisabled(keys_table_->size().isEmpty()); } void KeyServerImportDialog::slot_import() { |