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

Ask user before allowing kdbx files as key files #3625

Merged
Merged
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
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
24 changes: 23 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,26 @@ 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("Suspicious Key File"),
tr("The chosen key file looks like a password database file. A key file must be a "
"static file that never changes or you will lose access to your database "
"forever.\nAre you sure you want to continue with this file?"),
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