mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-01-26 06:26:11 -05:00
Implement SSH key file path env substitution
Supports all platforms, including Windows with %FOO% syntax. Fixes #3523
This commit is contained in:
parent
47ce81c9a6
commit
6fc7be78ea
@ -324,6 +324,29 @@ namespace Tools
|
|||||||
return QUuid::fromRfc4122(QByteArray::fromHex(uuid.toLatin1()));
|
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()
|
Buffer::Buffer()
|
||||||
: raw(nullptr)
|
: raw(nullptr)
|
||||||
, size(0)
|
, size(0)
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
#include "core/Global.h"
|
#include "core/Global.h"
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QProcessEnvironment>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
|
|
||||||
@ -48,6 +49,8 @@ namespace Tools
|
|||||||
bool useWildcards = false,
|
bool useWildcards = false,
|
||||||
bool exactMatch = false,
|
bool exactMatch = false,
|
||||||
bool caseSensitive = false);
|
bool caseSensitive = false);
|
||||||
|
QString envSubstitute(const QString& filepath,
|
||||||
|
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment());
|
||||||
|
|
||||||
template <typename RandomAccessIterator, typename T>
|
template <typename RandomAccessIterator, typename T>
|
||||||
RandomAccessIterator binaryFind(RandomAccessIterator begin, RandomAccessIterator end, const T& value)
|
RandomAccessIterator binaryFind(RandomAccessIterator begin, RandomAccessIterator end, const T& value)
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "KeeAgentSettings.h"
|
#include "KeeAgentSettings.h"
|
||||||
|
#include "core/Tools.h"
|
||||||
|
|
||||||
KeeAgentSettings::KeeAgentSettings()
|
KeeAgentSettings::KeeAgentSettings()
|
||||||
: m_lifetimeConstraintDuration(600)
|
: m_lifetimeConstraintDuration(600)
|
||||||
@ -115,6 +116,11 @@ const QString KeeAgentSettings::fileName() const
|
|||||||
return m_fileName;
|
return m_fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QString KeeAgentSettings::fileNameEnvSubst(QProcessEnvironment environment) const
|
||||||
|
{
|
||||||
|
return Tools::envSubstitute(m_fileName, environment);
|
||||||
|
}
|
||||||
|
|
||||||
void KeeAgentSettings::setAllowUseOfSshKey(bool allowUseOfSshKey)
|
void KeeAgentSettings::setAllowUseOfSshKey(bool allowUseOfSshKey)
|
||||||
{
|
{
|
||||||
m_allowUseOfSshKey = allowUseOfSshKey;
|
m_allowUseOfSshKey = allowUseOfSshKey;
|
||||||
@ -361,7 +367,7 @@ bool KeeAgentSettings::toOpenSSHKey(const Entry* entry, OpenSSHKey& key, bool de
|
|||||||
fileName = m_attachmentName;
|
fileName = m_attachmentName;
|
||||||
privateKeyData = entry->attachments()->value(fileName);
|
privateKeyData = entry->attachments()->value(fileName);
|
||||||
} else {
|
} else {
|
||||||
QFile localFile(m_fileName);
|
QFile localFile(fileNameEnvSubst());
|
||||||
QFileInfo localFileInfo(localFile);
|
QFileInfo localFileInfo(localFile);
|
||||||
fileName = localFileInfo.fileName();
|
fileName = localFileInfo.fileName();
|
||||||
|
|
||||||
|
@ -54,6 +54,7 @@ public:
|
|||||||
const QString attachmentName() const;
|
const QString attachmentName() const;
|
||||||
bool saveAttachmentToTempFile() const;
|
bool saveAttachmentToTempFile() const;
|
||||||
const QString fileName() const;
|
const QString fileName() const;
|
||||||
|
const QString fileNameEnvSubst(QProcessEnvironment environment = QProcessEnvironment::systemEnvironment()) const;
|
||||||
|
|
||||||
void setAllowUseOfSshKey(bool allowUseOfSshKey);
|
void setAllowUseOfSshKey(bool allowUseOfSshKey);
|
||||||
void setAddAtDatabaseOpen(bool addAtDatabaseOpen);
|
void setAddAtDatabaseOpen(bool addAtDatabaseOpen);
|
||||||
|
@ -64,3 +64,24 @@ void TestTools::testIsBase64()
|
|||||||
QVERIFY(not Tools::isBase64(QByteArray("abc_")));
|
QVERIFY(not Tools::isBase64(QByteArray("abc_")));
|
||||||
QVERIFY(not Tools::isBase64(QByteArray("123")));
|
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
|
||||||
|
}
|
||||||
|
@ -27,6 +27,7 @@ private slots:
|
|||||||
void testHumanReadableFileSize();
|
void testHumanReadableFileSize();
|
||||||
void testIsHex();
|
void testIsHex();
|
||||||
void testIsBase64();
|
void testIsBase64();
|
||||||
|
void testEnvSubstitute();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // KEEPASSX_TESTTOOLS_H
|
#endif // KEEPASSX_TESTTOOLS_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user