From c90808142134a6e2282a0562752308b83df91c0f Mon Sep 17 00:00:00 2001 From: Stefan Forstenlechner Date: Mon, 17 Jun 2024 23:41:56 +0200 Subject: [PATCH 1/2] Add KeePass header check for testing remote download (#10910) --- share/translations/keepassxc_en.ts | 12 ++++++++---- src/gui/remote/DatabaseSettingsWidgetRemote.cpp | 17 ++++++++++++++++- src/gui/remote/DatabaseSettingsWidgetRemote.h | 2 ++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/share/translations/keepassxc_en.ts b/share/translations/keepassxc_en.ts index 0cf445a864..9c2a6f9f82 100644 --- a/share/translations/keepassxc_en.ts +++ b/share/translations/keepassxc_en.ts @@ -2409,10 +2409,6 @@ removed from the database. Download failed with error: %1 - - Download finished, but file %1 could not be found. - - Download successful. @@ -2445,6 +2441,14 @@ The command has to exit. In case of `sftp` as last command `exit` has to be sent + + Command finished, but downloaded file does not exist. + + + + Download finished, but file failed KeePass header check. File is not a KeePass file or it's an unsupported version + + Timeout: diff --git a/src/gui/remote/DatabaseSettingsWidgetRemote.cpp b/src/gui/remote/DatabaseSettingsWidgetRemote.cpp index f66edba7e7..31a019d3c7 100644 --- a/src/gui/remote/DatabaseSettingsWidgetRemote.cpp +++ b/src/gui/remote/DatabaseSettingsWidgetRemote.cpp @@ -18,6 +18,7 @@ #include "DatabaseSettingsWidgetRemote.h" #include "ui_DatabaseSettingsWidgetRemote.h" +#include "core/Database.h" #include "core/Global.h" #include "core/Metadata.h" @@ -200,10 +201,24 @@ void DatabaseSettingsWidgetRemote::testDownload() } if (!QFile::exists(result.filePath)) { - m_ui->messageWidget->showMessage(tr("Download finished, but file %1 could not be found.").arg(result.filePath), + m_ui->messageWidget->showMessage(tr("Command finished, but downloaded file does not exist."), + MessageWidget::Error); + return; + } + + if (!hasValidPublicHeaders(result.filePath)) { + m_ui->messageWidget->showMessage(tr("Download finished, but file failed KeePass header check. File is not a " + "KeePass file or it's an unsupported version"), MessageWidget::Error); return; } m_ui->messageWidget->showMessage(tr("Download successful."), MessageWidget::Positive); } + +bool DatabaseSettingsWidgetRemote::hasValidPublicHeaders(QString& filePath) { + // Read public headers + QString error; + QScopedPointer db(new Database()); + return db->open(filePath, nullptr, &error); +} diff --git a/src/gui/remote/DatabaseSettingsWidgetRemote.h b/src/gui/remote/DatabaseSettingsWidgetRemote.h index 6184e4bd91..44a27fd5a2 100644 --- a/src/gui/remote/DatabaseSettingsWidgetRemote.h +++ b/src/gui/remote/DatabaseSettingsWidgetRemote.h @@ -56,6 +56,8 @@ private slots: QListWidgetItem* findItemByName(const QString& name); void clearFields(); + bool hasValidPublicHeaders(QString& filePath); + QScopedPointer m_remoteSettings; const QScopedPointer m_ui; bool m_modified = false; From 85ed0e0f7000d32aed3558c02bd4e108c8c9ca6b Mon Sep 17 00:00:00 2001 From: Stefan Forstenlechner Date: Wed, 19 Jun 2024 13:59:55 +0200 Subject: [PATCH 2/2] Add error message from opening database --- share/translations/keepassxc_en.ts | 2 +- src/gui/remote/DatabaseSettingsWidgetRemote.cpp | 17 +++++++++-------- src/gui/remote/DatabaseSettingsWidgetRemote.h | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/share/translations/keepassxc_en.ts b/share/translations/keepassxc_en.ts index 9c2a6f9f82..fd45487f50 100644 --- a/share/translations/keepassxc_en.ts +++ b/share/translations/keepassxc_en.ts @@ -2446,7 +2446,7 @@ The command has to exit. In case of `sftp` as last command `exit` has to be sent - Download finished, but file failed KeePass header check. File is not a KeePass file or it's an unsupported version + Downloaded file is not a KeePass file or it's an unsupported version: %1 diff --git a/src/gui/remote/DatabaseSettingsWidgetRemote.cpp b/src/gui/remote/DatabaseSettingsWidgetRemote.cpp index 31a019d3c7..583b3cbca2 100644 --- a/src/gui/remote/DatabaseSettingsWidgetRemote.cpp +++ b/src/gui/remote/DatabaseSettingsWidgetRemote.cpp @@ -187,12 +187,12 @@ void DatabaseSettingsWidgetRemote::testDownload() params->downloadInput = m_ui->inputForDownload->toPlainText(); params->downloadTimeoutMsec = m_ui->downloadTimeoutSec->value() * 1000; - QScopedPointer remoteHandler(new RemoteHandler(this)); if (params->downloadCommand.isEmpty()) { m_ui->messageWidget->showMessage(tr("Download command cannot be empty."), MessageWidget::Warning); return; } + QScopedPointer remoteHandler(new RemoteHandler(this)); RemoteHandler::RemoteResult result = remoteHandler->download(params); if (!result.success) { m_ui->messageWidget->showMessage(tr("Download failed with error: %1").arg(result.errorMessage), @@ -206,19 +206,20 @@ void DatabaseSettingsWidgetRemote::testDownload() return; } - if (!hasValidPublicHeaders(result.filePath)) { - m_ui->messageWidget->showMessage(tr("Download finished, but file failed KeePass header check. File is not a " - "KeePass file or it's an unsupported version"), - MessageWidget::Error); + QString error; + if (!hasValidPublicHeader(result.filePath, &error)) { + m_ui->messageWidget->showMessage( + tr("Downloaded file is not a KeePass file or it's an unsupported version: %1").arg(error), + MessageWidget::Error); return; } m_ui->messageWidget->showMessage(tr("Download successful."), MessageWidget::Positive); } -bool DatabaseSettingsWidgetRemote::hasValidPublicHeaders(QString& filePath) { +bool DatabaseSettingsWidgetRemote::hasValidPublicHeader(QString& filePath, QString* error) +{ // Read public headers - QString error; QScopedPointer db(new Database()); - return db->open(filePath, nullptr, &error); + return db->open(filePath, nullptr, error); } diff --git a/src/gui/remote/DatabaseSettingsWidgetRemote.h b/src/gui/remote/DatabaseSettingsWidgetRemote.h index 44a27fd5a2..8c5893f4f9 100644 --- a/src/gui/remote/DatabaseSettingsWidgetRemote.h +++ b/src/gui/remote/DatabaseSettingsWidgetRemote.h @@ -56,7 +56,7 @@ private slots: QListWidgetItem* findItemByName(const QString& name); void clearFields(); - bool hasValidPublicHeaders(QString& filePath); + bool hasValidPublicHeader(QString& filePath, QString* error); QScopedPointer m_remoteSettings; const QScopedPointer m_ui;