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 1 commit
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>Download finished, but file failed KeePass header check. File is not a KeePass file or it&apos;s an unsupported version</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>DatabaseTabWidget</name>
Expand Down
17 changes: 16 additions & 1 deletion 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 @@ -191,10 +192,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 "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the actual error from database open here. "KeePass header check" is quite a technical error message as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have adapted the error message

"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<Database> db(new Database());
return db->open(filePath, nullptr, &error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could inline this since you have only a single call for it and use the error string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if I should use the error string, as it can be quite long and may not fit in the message box. I added the error anyway, as it is better to have it rather than leave the user in the dark.

I did not inline the method though, as I prefer having a named method, rather than calling Database::open with a nullptr as second parameter, which implies reading the headers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are repeating this pattern we should introduce a Database::readHeaders function that takes care of the particulars.

}
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 hasValidPublicHeaders(QString& filePath);

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