Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KeePass header check for testing remote download (#10910) #10915

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2332,10 +2332,6 @@ removed from the database.</source>
<source>Download failed with error: %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Download finished, but file %1 could not be found.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Download successful.</source>
<translation type="unfinished"></translation>
Expand Down Expand Up @@ -2368,6 +2364,14 @@ The command has to exit. In case of `sftp` as last command `exit` has to be sent
</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Command finished, but downloaded file does not exist.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Downloaded file is not a KeePass file or it&apos;s an unsupported version: %1</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DatabaseTabWidget</name>
Expand Down
22 changes: 19 additions & 3 deletions src/gui/remote/DatabaseSettingsWidgetRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "DatabaseSettingsWidgetRemote.h"
#include "ui_DatabaseSettingsWidgetRemote.h"

#include "core/Database.h"
#include "core/Global.h"
#include "core/Metadata.h"

Expand Down Expand Up @@ -177,12 +178,12 @@ void DatabaseSettingsWidgetRemote::testDownload()
params->downloadCommand = m_ui->downloadCommand->text();
params->downloadInput = m_ui->inputForDownload->toPlainText();

QScopedPointer<RemoteHandler> remoteHandler(new RemoteHandler(this));
if (params->downloadCommand.isEmpty()) {
m_ui->messageWidget->showMessage(tr("Download command cannot be empty."), MessageWidget::Warning);
return;
}

QScopedPointer<RemoteHandler> 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),
Expand All @@ -191,10 +192,25 @@ 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;
}

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::hasValidPublicHeader(QString& filePath, QString* error)
{
// Read public headers
QScopedPointer<Database> db(new Database());
return db->open(filePath, nullptr, error);
}
2 changes: 2 additions & 0 deletions src/gui/remote/DatabaseSettingsWidgetRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ private slots:
QListWidgetItem* findItemByName(const QString& name);
void clearFields();

bool hasValidPublicHeader(QString& filePath, QString* error);

QScopedPointer<RemoteSettings> m_remoteSettings;
const QScopedPointer<Ui::DatabaseSettingsWidgetRemote> m_ui;
bool m_modified = false;
Expand Down
Loading