Skip to content
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
10 changes: 10 additions & 0 deletions src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,16 @@ bool DatabaseOpenWidget::browseKeyFile()
return false;
}

QFileInfo fileInfo(filename);
if (!fileInfo.isReadable()) {
MessageBox::warning(this,
tr("Cannot read key file"),
tr("The selected key file cannot be read.\n"
"Please check that the file exists and is readable."),
MessageBox::Button::Ok);
return false;
}

m_ui->keyFileLineEdit->setText(filename);
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions src/gui/databasekey/KeyFileEditWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ void KeyFileEditWidget::browseKeyFile()
}

if (!fileName.isEmpty()) {
QFileInfo fileInfo(fileName);
if (!fileInfo.isReadable()) {
MessageBox::warning(this,
tr("Cannot read key file"),
tr("The selected key file cannot be read.\n"
"Please check that the file exists and is readable."),
MessageBox::Button::Ok);
return;
}
m_compUi->keyFileLineEdit->setText(fileName);
}
}
23 changes: 23 additions & 0 deletions tests/TestKeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "TestKeys.h"

#include <QBuffer>
#include <QTemporaryFile>
#include <QTest>

#include "config-keepassx-tests.h"
Expand Down Expand Up @@ -221,6 +222,28 @@ void TestKeys::testFileKeyError()
errorMsg = "";
}

void TestKeys::testFileKeyUnreadable()
{
// Create a temporary file and remove read permissions
QTemporaryFile tempFile;
QVERIFY(tempFile.open());
QString fileName = tempFile.fileName();
tempFile.write("test key data", 12);
tempFile.close();

// Remove read permissions
QFile::setPermissions(fileName, QFileDevice::WriteOwner | QFileDevice::WriteUser);

FileKey fileKey;
QString errorMsg;
bool result = fileKey.load(fileName, &errorMsg);
QVERIFY(!result);
QVERIFY(!errorMsg.isEmpty());

// Restore permissions so the temp file can be cleaned up
QFile::setPermissions(fileName, QFileDevice::ReadOwner | QFileDevice::WriteOwner);
}

void TestKeys::benchmarkTransformKey()
{
QByteArray env = qgetenv("BENCHMARK");
Expand Down
1 change: 1 addition & 0 deletions tests/TestKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private slots:
void testCreateAndOpenFileKey();
void testFileKeyHash();
void testFileKeyError();
void testFileKeyUnreadable();
void testCompositeKeyComponents();
void benchmarkTransformKey();
};
Expand Down