Skip to content

Commit

Permalink
Add check for database files when selecting a key file
Browse files Browse the repository at this point in the history
Reject own database file as the key file. Prompt for other kdbx files as key files.

Also add a static warning message to the key file selection dialog
  • Loading branch information
qw3ry authored and droidmonkey committed Oct 18, 2019
1 parent f726d75 commit e04abd3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/gui/dbsettings/DatabaseSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ void DatabaseSettingsWidget::load(QSharedPointer<Database> db)
m_db = std::move(db);
initialize();
}

const QSharedPointer<Database> DatabaseSettingsWidget::getDatabase() const
{
return m_db;
}

2 changes: 2 additions & 0 deletions src/gui/dbsettings/DatabaseSettingsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class DatabaseSettingsWidget : public SettingsWidget

virtual void load(QSharedPointer<Database> db);

const QSharedPointer<Database> getDatabase() const;

signals:
/**
* Can be emitted to indicate size changes and allow parents widgets to adjust properly.
Expand Down
23 changes: 22 additions & 1 deletion src/gui/masterkey/KeyFileEditWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

#include "KeyFileEditWidget.h"
#include "ui_KeyFileEditWidget.h"
#include <gui/dbsettings/DatabaseSettingsWidget.h>

#include "gui/FileDialog.h"
#include "gui/MainWindow.h"
#include "gui/MessageBox.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"

KeyFileEditWidget::KeyFileEditWidget(QWidget* parent)
KeyFileEditWidget::KeyFileEditWidget(DatabaseSettingsWidget* parent)
: KeyComponentWidget(parent)
, m_compUi(new Ui::KeyFileEditWidget())
, m_parent(parent)
{
setComponentName(tr("Key File"));
setComponentDescription(tr("<p>You can add a key file containing random bytes for additional security.</p>"
Expand Down Expand Up @@ -120,6 +122,25 @@ void KeyFileEditWidget::browseKeyFile()
QString filters = QString("%1 (*.key);;%2 (*)").arg(tr("Key files"), tr("All files"));
QString fileName = fileDialog()->getOpenFileName(this, tr("Select a key file"), QString(), filters);

if (QFileInfo(fileName).canonicalFilePath() == m_parent->getDatabase()->canonicalFilePath()) {
MessageBox::critical(getMainWindow(),
tr("Invalid Key File"),
tr("You cannot use the current database as its own keyfile. Please choose a different "
"file or generate a new key file."));
return;
} else if (fileName.endsWith(".kdbx", Qt::CaseInsensitive)) {
auto response =
MessageBox::warning(getMainWindow(),
tr("Invalid Key File"),
tr("You have chosen a database file as your key file. This file will most likely "
"change and prevent you from unlocking your database. Do you want to continue?"),
MessageBox::Continue | MessageBox::Cancel,
MessageBox::Cancel);
if (response != MessageBox::Continue) {
return;
}
}

if (!fileName.isEmpty()) {
m_compUi->keyFileCombo->setEditText(fileName);
}
Expand Down
6 changes: 5 additions & 1 deletion src/gui/masterkey/KeyFileEditWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ namespace Ui
class KeyFileEditWidget;
}

class DatabaseSettingsWidget;

class KeyFileEditWidget : public KeyComponentWidget
{
Q_OBJECT


public:
explicit KeyFileEditWidget(QWidget* parent = nullptr);
explicit KeyFileEditWidget(DatabaseSettingsWidget* parent);
Q_DISABLE_COPY(KeyFileEditWidget);
~KeyFileEditWidget() override;

Expand All @@ -49,6 +52,7 @@ private slots:
private:
const QScopedPointer<Ui::KeyFileEditWidget> m_compUi;
QPointer<QWidget> m_compEditWidget;
const QPointer<DatabaseSettingsWidget> m_parent;
};

#endif // KEEPASSXC_KEYFILEEDITWIDGET_H
17 changes: 16 additions & 1 deletion src/gui/masterkey/KeyFileEditWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>364</width>
<width>370</width>
<height>76</height>
</rect>
</property>
Expand Down Expand Up @@ -72,6 +72,21 @@
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Note: Do not use a file that may change as that will prevent you from unlocking your database!</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down

0 comments on commit e04abd3

Please sign in to comment.