Skip to content

Commit

Permalink
Raise error if we don't find exactly one root group.
Browse files Browse the repository at this point in the history
  • Loading branch information
debfx committed Apr 29, 2013
1 parent f1bebe9 commit 0ec29b2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/format/KeePass2XmlReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,30 @@ bool KeePass2XmlReader::parseKeePassFile()
{
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "KeePassFile");

bool rootParsed = false;
bool rootElementFound = false;
bool rootParsedSuccesfully = false;

while (!m_xml.error() && m_xml.readNextStartElement()) {
if (m_xml.name() == "Meta") {
parseMeta();
}
else if (m_xml.name() == "Root") {
rootParsed = parseRoot();
rootParsedSuccesfully = parseRoot();

if (rootElementFound) {
rootParsedSuccesfully = false;
raiseError(29);
}
else {
rootElementFound = true;
}
}
else {
skipCurrentElement();
}
}

return rootParsed;
return rootParsedSuccesfully;
}

void KeePass2XmlReader::parseMeta()
Expand Down Expand Up @@ -423,7 +432,8 @@ bool KeePass2XmlReader::parseRoot()
{
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Root");

bool groupParsed = false;
bool groupElementFound = false;
bool groupParsedSuccesfully = false;

while (!m_xml.error() && m_xml.readNextStartElement()) {
if (m_xml.name() == "Group") {
Expand All @@ -432,7 +442,15 @@ bool KeePass2XmlReader::parseRoot()
Group* oldRoot = m_db->rootGroup();
m_db->setRootGroup(rootGroup);
delete oldRoot;
groupParsed = true;
groupParsedSuccesfully = true;
}

if (groupElementFound) {
groupParsedSuccesfully = false;
raiseError(30);
}
else {
groupElementFound = true;
}
}
else if (m_xml.name() == "DeletedObjects") {
Expand All @@ -443,7 +461,7 @@ bool KeePass2XmlReader::parseRoot()
}
}

return groupParsed;
return groupParsedSuccesfully;
}

Group* KeePass2XmlReader::parseGroup()
Expand Down
2 changes: 2 additions & 0 deletions tests/TestKeePass2XmlReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ void TestKeePass2XmlReader::testBroken_data()
QTest::newRow("BrokenNoGroupUuid") << "BrokenNoGroupUuid";
QTest::newRow("BrokenNoEntryUuid") << "BrokenNoEntryUuid";
QTest::newRow("BrokenNoRootGroup") << "BrokenNoRootGroup";
QTest::newRow("BrokenTwoRoots") << "BrokenTwoRoots";
QTest::newRow("BrokenTwoRootGroups") << "BrokenTwoRootGroups";
}

void TestKeePass2XmlReader::cleanupTestCase()
Expand Down
13 changes: 13 additions & 0 deletions tests/data/BrokenTwoRootGroups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<KeePassFile>
<Root>
<Group>
<UUID>lmU+9n0aeESKZvcEze+bRg==</UUID>
<Name>Test</Name>
</Group>
<Group>
<UUID>AaUYVdXsI02h4T1RiAlgtg==</UUID>
<Name>Test</Name>
</Group>
</Root>
</KeePassFile>
15 changes: 15 additions & 0 deletions tests/data/BrokenTwoRoots.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<KeePassFile>
<Root>
<Group>
<UUID>lmU+9n0aeESKZvcEze+bRg==</UUID>
<Name>Test</Name>
</Group>
</Root>
<Root>
<Group>
<UUID>AaUYVdXsI02h4T1RiAlgtg==</UUID>
<Name>Test</Name>
</Group>
</Root>
</KeePassFile>

0 comments on commit 0ec29b2

Please sign in to comment.