mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2026-12-01 06:12:52 -08:00
Fix identifying nested reference placeholders (#13688)
This commit is contained in:
1 parent
5ac1b0c98d
commit
474da9b877
7 files changed
+46
-32
No files matched your search
+3
-3
@@ -396,7 +396,7 @@ QString Entry::resolveUrl() const
|
||||
return {};
|
||||
}
|
||||
|
||||
return EntryAttributes::matchReference(entryUrl).hasMatch() ? resolveMultiplePlaceholders(entryUrl) : entryUrl;
|
||||
return EntryPlaceholders::matchReference(entryUrl).hasMatch() ? resolveMultiplePlaceholders(entryUrl) : entryUrl;
|
||||
}
|
||||
|
||||
QStringList Entry::getAllUrls() const
|
||||
@@ -1329,7 +1329,7 @@ QString Entry::resolveReferencePlaceholderRecursive(const QString& placeholder,
|
||||
// resolving references in format: {REF:<WantedField>@<SearchIn>:<SearchText>}
|
||||
// using format from http://keepass.info/help/base/fieldrefs.html at the time of writing
|
||||
|
||||
const QRegularExpressionMatch match = EntryAttributes::matchReference(placeholder);
|
||||
const QRegularExpressionMatch match = EntryPlaceholders::matchReference(placeholder);
|
||||
if (!match.hasMatch() || !m_group || !m_group->database()) {
|
||||
return placeholder;
|
||||
}
|
||||
@@ -1461,7 +1461,7 @@ Database* Entry::database()
|
||||
|
||||
Entry* Entry::resolveReference(const QString& str) const
|
||||
{
|
||||
QRegularExpressionMatch match = EntryAttributes::matchReference(str);
|
||||
QRegularExpressionMatch match = EntryPlaceholders::matchReference(str);
|
||||
if (!match.hasMatch()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "EntryAttributes.h"
|
||||
#include "EntryPlaceholders.h"
|
||||
#include "core/Global.h"
|
||||
#include "core/Tools.h"
|
||||
|
||||
@@ -144,7 +145,7 @@ bool EntryAttributes::isReference(const QString& key) const
|
||||
}
|
||||
|
||||
const QString data = value(key);
|
||||
return matchReference(data).hasMatch();
|
||||
return EntryPlaceholders::matchReference(data).hasMatch();
|
||||
}
|
||||
|
||||
void EntryAttributes::set(const QString& key, const QString& value, bool protect)
|
||||
@@ -306,7 +307,7 @@ QUuid EntryAttributes::referenceUuid(const QString& key) const
|
||||
return {};
|
||||
}
|
||||
|
||||
auto match = matchReference(value(key));
|
||||
auto match = EntryPlaceholders::matchReference(value(key));
|
||||
if (match.hasMatch()) {
|
||||
const QString uuid = match.captured("SearchText");
|
||||
if (!uuid.isEmpty()) {
|
||||
@@ -327,16 +328,6 @@ bool EntryAttributes::operator!=(const EntryAttributes& other) const
|
||||
return (m_attributes != other.m_attributes || m_protectedAttributes != other.m_protectedAttributes);
|
||||
}
|
||||
|
||||
QRegularExpressionMatch EntryAttributes::matchReference(const QString& text)
|
||||
{
|
||||
// Updated regex to handle nested braces in SearchText (e.g., {UUID})
|
||||
static const QRegularExpression referenceRegExp(
|
||||
R"(\{REF:(?<WantedField>[TUPANI])@(?<SearchIn>[TUPANIO]):(?<SearchText>(?:[^{}]|\{[^}]*\})+)\})",
|
||||
QRegularExpression::CaseInsensitiveOption);
|
||||
|
||||
return referenceRegExp.match(text);
|
||||
}
|
||||
|
||||
void EntryAttributes::clear()
|
||||
{
|
||||
emit aboutToBeReset();
|
||||
|
||||
@@ -54,8 +54,6 @@ public:
|
||||
bool operator==(const EntryAttributes& other) const;
|
||||
bool operator!=(const EntryAttributes& other) const;
|
||||
|
||||
static QRegularExpressionMatch matchReference(const QString& text);
|
||||
|
||||
static const QString TitleKey;
|
||||
static const QString UserNameKey;
|
||||
static const QString PasswordKey;
|
||||
|
||||
@@ -186,6 +186,16 @@ namespace EntryPlaceholders
|
||||
return placeholderRegEx.globalMatch(str);
|
||||
}
|
||||
|
||||
QRegularExpressionMatch matchReference(const QString& text)
|
||||
{
|
||||
// Updated regex to handle nested braces in SearchText (e.g., {UUID})
|
||||
static const QRegularExpression referenceRegExp(
|
||||
R"(\{REF:(?<WantedField>[TUPANI])@(?<SearchIn>[TUPANIO]):(?<SearchText>(?:[^{}]|\{[^}]*\})+)\})",
|
||||
QRegularExpression::CaseInsensitiveOption);
|
||||
|
||||
return referenceRegExp.match(text);
|
||||
}
|
||||
|
||||
bool containsPlaceholder(const QString& str)
|
||||
{
|
||||
auto matches = placeholderMatches(str);
|
||||
@@ -201,10 +211,16 @@ namespace EntryPlaceholders
|
||||
captured.replace(captured.size() - 2, 2, "}");
|
||||
}
|
||||
|
||||
// Check for placeholder type
|
||||
const auto placeHolderType = placeholderType(captured);
|
||||
if (placeHolderType != PlaceholderType::NotPlaceholder && placeHolderType != PlaceholderType::Unknown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check for references. Deeply nested reference placeholders might return an Unknown type.
|
||||
if (matchReference(captured).hasMatch()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ namespace EntryPlaceholders
|
||||
QString resolveDateTimePlaceholder(PlaceholderType placeholderType);
|
||||
QString maskPasswordPlaceholders(const QString& str);
|
||||
QRegularExpressionMatchIterator placeholderMatches(const QString& str);
|
||||
QRegularExpressionMatch matchReference(const QString& text);
|
||||
bool containsPlaceholder(const QString& str);
|
||||
} // namespace EntryPlaceholders
|
||||
|
||||
|
||||
@@ -159,7 +159,7 @@ namespace FdoSecrets
|
||||
return QDBusError::InvalidArgs;
|
||||
}
|
||||
|
||||
if (EntryAttributes::matchReference(it.value()).hasMatch()) {
|
||||
if (EntryPlaceholders::matchReference(it.value()).hasMatch()) {
|
||||
return QDBusError::InvalidArgs;
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ namespace FdoSecrets
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (EntryAttributes::matchReference(label).hasMatch()) {
|
||||
if (EntryPlaceholders::matchReference(label).hasMatch()) {
|
||||
return QDBusError::InvalidArgs;
|
||||
}
|
||||
|
||||
@@ -309,7 +309,7 @@ namespace FdoSecrets
|
||||
auto decoded = secret.session->decode(secret);
|
||||
|
||||
// block references
|
||||
if (EntryAttributes::matchReference(decoded.value).hasMatch()) {
|
||||
if (EntryPlaceholders::matchReference(decoded.value).hasMatch()) {
|
||||
return QDBusError::InvalidArgs;
|
||||
}
|
||||
|
||||
@@ -431,7 +431,7 @@ namespace FdoSecrets
|
||||
}
|
||||
|
||||
if (!mimeType.isValid() || !mimeType.inherits(QStringLiteral("text/plain"))) {
|
||||
if (EntryAttributes::matchReference(contentType).hasMatch()) {
|
||||
if (EntryPlaceholders::matchReference(contentType).hasMatch()) {
|
||||
return QDBusError::InvalidArgs;
|
||||
}
|
||||
// we can't handle this content type, save the data as attachment, and clear the password field
|
||||
@@ -440,7 +440,7 @@ namespace FdoSecrets
|
||||
entry->attributes()->set(FDO_SECRETS_CONTENT_TYPE, contentType);
|
||||
} else {
|
||||
auto password = decoder.decode(data);
|
||||
if (EntryAttributes::matchReference(password).hasMatch()) {
|
||||
if (EntryPlaceholders::matchReference(password).hasMatch()) {
|
||||
return QDBusError::InvalidArgs;
|
||||
}
|
||||
// save the data to password field
|
||||
|
||||
+18
-10
@@ -500,6 +500,7 @@ void TestEntry::testResolveUuidPlaceholder()
|
||||
|
||||
// Test advanced attribute with {REF:U@I:{UUID}} - should resolve to the entry's own username
|
||||
entry->attributes()->set("SelfReference", "{REF:U@I:{UUID}}");
|
||||
// entry->attributes()->set("SelfReference", "{{{{{{{{{{{{{{REF:U@I:{UUID}}}}}}}}}}}}}}}");
|
||||
QString attributeValue = entry->attributes()->value("SelfReference");
|
||||
QString resolvedSelfRef = entry->resolveMultiplePlaceholders(attributeValue);
|
||||
|
||||
@@ -937,18 +938,29 @@ void TestEntry::testContainsPlaceholder()
|
||||
QVERIFY(!EntryPlaceholders::containsPlaceholder(""));
|
||||
QVERIFY(!EntryPlaceholders::containsPlaceholder("testString{REF:nothing")); // Placeholder is not finished
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("testString{REF:P@T:Other Entry}something"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("testString{ref:P@T:Other Entry}something")); // lowercase
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{URL:USERNAME}yes"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{URL:USERNAME}yes{REF:A@O:Attribute 1}"));
|
||||
QVERIFY(!EntryPlaceholders::containsPlaceholder("{NOTAREALPLACEHOLDER:USERNAME}yes")); // Unknown placeholder
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("yes{URL:PORT}"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("yes{S:KPEX_PASSKEYS_USER_ID}no"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{T-REPLACE-RX:/{REF:U@I:%1}/0202$/2/}"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{t-replace-rx:/{REF:U@I:%1}/0202$/2/}")); // lowercase
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{REF:U@A:https://url.com/ }")); // Space before }
|
||||
|
||||
// Escaped
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("url\\{REF:U@A:https://url.com/}"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("\\{REF:U@A:https://url.com/}"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("\\{URL:USERNAME\\}yes"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{URL:USERNAME\\}yes"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("\\{URL:USERNAME}yes"));
|
||||
|
||||
// Placeholder can be inside {} brackets, and must be identified
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{{REF:U@A:https://url.com/}}"));
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{{{REF:U@A:https://url.com/}}}"));
|
||||
|
||||
// This kind of mixup is not considered as a placeholder
|
||||
QVERIFY(!EntryPlaceholders::containsPlaceholder("{[{REF:U@A:https://url.com/}]}"));
|
||||
// This kind of mixup is also a placeholder
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{[{REF:U@A:https://url.com/}]}"));
|
||||
|
||||
// Static placeholdersi
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("{TITLE}"));
|
||||
@@ -964,13 +976,9 @@ void TestEntry::testContainsPlaceholder()
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder("test\\{TOTP\\}"));
|
||||
|
||||
// Max depth (10), and max depth exceeded
|
||||
for (auto i = 1; i <= EntryPlaceholders::ResolveMaximumDepth + 1; ++i) {
|
||||
const auto placeholder = QString("{").repeated(i) + QString("TOTP") + QString("}").repeated(i);
|
||||
if (i <= EntryPlaceholders::ResolveMaximumDepth) {
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder(placeholder));
|
||||
} else {
|
||||
// Max depth exceeded
|
||||
QVERIFY(!EntryPlaceholders::containsPlaceholder(placeholder));
|
||||
}
|
||||
for (auto i = 1; i <= EntryPlaceholders::ResolveMaximumDepth + 10; ++i) {
|
||||
const auto placeholder =
|
||||
QString("{").repeated(i) + QString("REF:U@A:https://url.com/") + QString("}").repeated(i);
|
||||
QVERIFY(EntryPlaceholders::containsPlaceholder(placeholder));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user