mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-12-04 15:39:34 +01:00
Fix cross-database reference resolution to handle unresolvable references correctly
Co-authored-by: droidmonkey <2809491+droidmonkey@users.noreply.github.com>
This commit is contained in:
@@ -1423,21 +1423,33 @@ void Entry::resolveReferencesBeforeDatabaseMove()
|
|||||||
// Resolve references in all default attributes
|
// Resolve references in all default attributes
|
||||||
for (const QString& key : EntryAttributes::DefaultAttributes) {
|
for (const QString& key : EntryAttributes::DefaultAttributes) {
|
||||||
if (m_attributes->contains(key) && m_attributes->isReference(key)) {
|
if (m_attributes->contains(key) && m_attributes->isReference(key)) {
|
||||||
QString resolvedValue = resolveMultiplePlaceholdersRecursive(m_attributes->value(key), 10);
|
QString originalValue = m_attributes->value(key);
|
||||||
|
QString resolvedValue = resolveMultiplePlaceholdersRecursive(originalValue, 10);
|
||||||
|
|
||||||
|
// Only replace if the resolution produced a different value and it's not empty
|
||||||
|
// Empty resolution means the reference couldn't be resolved, so keep original
|
||||||
|
if (!resolvedValue.isEmpty() && resolvedValue != originalValue) {
|
||||||
bool isProtected = m_attributes->isProtected(key);
|
bool isProtected = m_attributes->isProtected(key);
|
||||||
m_attributes->set(key, resolvedValue, isProtected);
|
m_attributes->set(key, resolvedValue, isProtected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve references in custom attributes
|
// Resolve references in custom attributes
|
||||||
const QList<QString> customKeys = m_attributes->customKeys();
|
const QList<QString> customKeys = m_attributes->customKeys();
|
||||||
for (const QString& key : customKeys) {
|
for (const QString& key : customKeys) {
|
||||||
if (m_attributes->isReference(key)) {
|
if (m_attributes->isReference(key)) {
|
||||||
QString resolvedValue = resolveMultiplePlaceholdersRecursive(m_attributes->value(key), 10);
|
QString originalValue = m_attributes->value(key);
|
||||||
|
QString resolvedValue = resolveMultiplePlaceholdersRecursive(originalValue, 10);
|
||||||
|
|
||||||
|
// Only replace if the resolution produced a different value and it's not empty
|
||||||
|
// Empty resolution means the reference couldn't be resolved, so keep original
|
||||||
|
if (!resolvedValue.isEmpty() && resolvedValue != originalValue) {
|
||||||
bool isProtected = m_attributes->isProtected(key);
|
bool isProtected = m_attributes->isProtected(key);
|
||||||
m_attributes->set(key, resolvedValue, isProtected);
|
m_attributes->set(key, resolvedValue, isProtected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Entry::maskPasswordPlaceholders(const QString& str) const
|
QString Entry::maskPasswordPlaceholders(const QString& str) const
|
||||||
|
|||||||
@@ -712,7 +712,8 @@ void TestEntry::testCrossDatabaseReferences()
|
|||||||
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->password()), QString("OriginalPassword"));
|
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->password()), QString("OriginalPassword"));
|
||||||
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->url()), QString("http://original.com"));
|
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->url()), QString("http://original.com"));
|
||||||
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->notes()), QString("OriginalNotes"));
|
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->notes()), QString("OriginalNotes"));
|
||||||
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->attributes()->value("CustomRef")), QString("OriginalTitle"));
|
QCOMPARE(refEntry->resolveMultiplePlaceholders(refEntry->attributes()->value("CustomRef")),
|
||||||
|
QString("OriginalTitle"));
|
||||||
|
|
||||||
// Verify the attributes still contain references (not yet resolved)
|
// Verify the attributes still contain references (not yet resolved)
|
||||||
QVERIFY(refEntry->attributes()->isReference(EntryAttributes::TitleKey));
|
QVERIFY(refEntry->attributes()->isReference(EntryAttributes::TitleKey));
|
||||||
|
|||||||
Reference in New Issue
Block a user