Skip to content

Commit

Permalink
Implement SSH key file path env substitution
Browse files Browse the repository at this point in the history
Supports all platforms, including Windows with %FOO% syntax.

Fixes #3523
  • Loading branch information
hifi authored and droidmonkey committed Jan 27, 2020
1 parent c8ab3b5 commit eeb407a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/core/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,29 @@ namespace Tools
return QUuid::fromRfc4122(QByteArray::fromHex(uuid.toLatin1()));
}

QString envSubstitute(const QString& filepath, QProcessEnvironment environment)
{
QString subbed = filepath;

#if defined(Q_OS_WIN)
QRegularExpression varRe("\\%([A-Za-z][A-Za-z0-9_]*)\\%");
#else
QRegularExpression varRe("\\$([A-Za-z][A-Za-z0-9_]*)");
subbed.replace("~", environment.value("HOME"));
#endif

QRegularExpressionMatch match;

do {
match = varRe.match(subbed);
if (match.hasMatch()) {
subbed.replace(match.capturedStart(), match.capturedLength(), environment.value(match.captured(1)));
}
} while (match.hasMatch());

return subbed;
}

Buffer::Buffer()
: raw(nullptr)
, size(0)
Expand Down
3 changes: 3 additions & 0 deletions src/core/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "core/Global.h"

#include <QObject>
#include <QProcessEnvironment>
#include <QString>
#include <QUuid>

Expand All @@ -48,6 +49,8 @@ namespace Tools
bool useWildcards = false,
bool exactMatch = false,
bool caseSensitive = false);
QString envSubstitute(const QString& filepath,
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment());

template <typename RandomAccessIterator, typename T>
RandomAccessIterator binaryFind(RandomAccessIterator begin, RandomAccessIterator end, const T& value)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ bool EditEntryWidget::getOpenSSHKey(OpenSSHKey& key, bool decrypt)
fileName = m_sshAgentUi->attachmentComboBox->currentText();
privateKeyData = m_advancedUi->attachmentsWidget->getAttachment(fileName);
} else {
QFile localFile(m_sshAgentUi->externalFileEdit->text());
QFile localFile(Tools::envSubstitute(m_sshAgentUi->externalFileEdit->text()));
QFileInfo localFileInfo(localFile);
fileName = localFileInfo.fileName();

Expand Down
6 changes: 6 additions & 0 deletions src/sshagent/KeeAgentSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "KeeAgentSettings.h"
#include "core/Tools.h"

KeeAgentSettings::KeeAgentSettings()
: m_allowUseOfSshKey(false)
Expand Down Expand Up @@ -108,6 +109,11 @@ const QString KeeAgentSettings::fileName() const
return m_fileName;
}

const QString KeeAgentSettings::fileNameEnvSubst(QProcessEnvironment environment) const
{
return Tools::envSubstitute(m_fileName, environment);
}

void KeeAgentSettings::setAllowUseOfSshKey(bool allowUseOfSshKey)
{
m_allowUseOfSshKey = allowUseOfSshKey;
Expand Down
1 change: 1 addition & 0 deletions src/sshagent/KeeAgentSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class KeeAgentSettings
const QString attachmentName() const;
bool saveAttachmentToTempFile() const;
const QString fileName() const;
const QString fileNameEnvSubst(QProcessEnvironment environment = QProcessEnvironment::systemEnvironment()) const;

void setAllowUseOfSshKey(bool allowUseOfSshKey);
void setAddAtDatabaseOpen(bool addAtDatabaseOpen);
Expand Down
2 changes: 1 addition & 1 deletion src/sshagent/SSHAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void SSHAgent::databaseModeChanged()
fileName = settings.attachmentName();
keyData = e->attachments()->value(fileName);
} else if (!settings.fileName().isEmpty()) {
QFile file(settings.fileName());
QFile file(settings.fileNameEnvSubst());
QFileInfo fileInfo(file);

fileName = fileInfo.fileName();
Expand Down
21 changes: 21 additions & 0 deletions tests/TestTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ void TestTools::testIsBase64()
QVERIFY(not Tools::isBase64(QByteArray("abc_")));
QVERIFY(not Tools::isBase64(QByteArray("123")));
}

void TestTools::testEnvSubstitute()
{
QProcessEnvironment environment;

#if defined(Q_OS_WIN)
environment.insert("HOMEDRIVE", "C:");
environment.insert("HOMEPATH", "\\Users\\User");

QCOMPARE(Tools::envSubstitute("%HOMEDRIVE%%HOMEPATH%\\.ssh\\id_rsa", environment),
QString("C:\\Users\\User\\.ssh\\id_rsa"));
QCOMPARE(Tools::envSubstitute("start%EMPTY%%EMPTY%%%HOMEDRIVE%%end", environment), QString("start%C:%end"));
#else
environment.insert("HOME", QString("/home/user"));
environment.insert("USER", QString("user"));

QCOMPARE(Tools::envSubstitute("~/.ssh/id_rsa", environment), QString("/home/user/.ssh/id_rsa"));
QCOMPARE(Tools::envSubstitute("$HOME/.ssh/id_rsa", environment), QString("/home/user/.ssh/id_rsa"));
QCOMPARE(Tools::envSubstitute("start/$EMPTY$$EMPTY$HOME/end", environment), QString("start/$/home/user/end"));
#endif
}
1 change: 1 addition & 0 deletions tests/TestTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ private slots:
void testHumanReadableFileSize();
void testIsHex();
void testIsBase64();
void testEnvSubstitute();
};

#endif // KEEPASSX_TESTTOOLS_H

0 comments on commit eeb407a

Please sign in to comment.