When parsing make sure that databases contain at least the root group.

This commit is contained in:
Felix Geyer 2013-04-20 19:01:43 +02:00
parent 3be7fa6b3b
commit e16720d06d
2 changed files with 21 additions and 6 deletions

View File

@ -51,12 +51,18 @@ void KeePass2XmlReader::readDatabase(QIODevice* device, Database* db, KeePass2Ra
m_tmpParent = new Group(); m_tmpParent = new Group();
bool rootGroupParsed = false;
if (!m_xml.error() && m_xml.readNextStartElement()) { if (!m_xml.error() && m_xml.readNextStartElement()) {
if (m_xml.name() == "KeePassFile") { if (m_xml.name() == "KeePassFile") {
parseKeePassFile(); rootGroupParsed = parseKeePassFile();
} }
} }
if (!m_xml.error() && !rootGroupParsed) {
raiseError(28);
}
if (!m_xml.error()) { if (!m_xml.error()) {
if (!m_tmpParent->children().isEmpty()) { if (!m_tmpParent->children().isEmpty()) {
qWarning("KeePass2XmlReader::readDatabase: found %d invalid group reference(s)", qWarning("KeePass2XmlReader::readDatabase: found %d invalid group reference(s)",
@ -141,21 +147,25 @@ QByteArray KeePass2XmlReader::headerHash()
return m_headerHash; return m_headerHash;
} }
void KeePass2XmlReader::parseKeePassFile() bool KeePass2XmlReader::parseKeePassFile()
{ {
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "KeePassFile"); Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "KeePassFile");
bool rootParsed = false;
while (!m_xml.error() && m_xml.readNextStartElement()) { while (!m_xml.error() && m_xml.readNextStartElement()) {
if (m_xml.name() == "Meta") { if (m_xml.name() == "Meta") {
parseMeta(); parseMeta();
} }
else if (m_xml.name() == "Root") { else if (m_xml.name() == "Root") {
parseRoot(); rootParsed = parseRoot();
} }
else { else {
skipCurrentElement(); skipCurrentElement();
} }
} }
return rootParsed;
} }
void KeePass2XmlReader::parseMeta() void KeePass2XmlReader::parseMeta()
@ -409,10 +419,12 @@ void KeePass2XmlReader::parseCustomDataItem()
} }
} }
void KeePass2XmlReader::parseRoot() bool KeePass2XmlReader::parseRoot()
{ {
Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Root"); Q_ASSERT(m_xml.isStartElement() && m_xml.name() == "Root");
bool groupParsed = false;
while (!m_xml.error() && m_xml.readNextStartElement()) { while (!m_xml.error() && m_xml.readNextStartElement()) {
if (m_xml.name() == "Group") { if (m_xml.name() == "Group") {
Group* rootGroup = parseGroup(); Group* rootGroup = parseGroup();
@ -420,6 +432,7 @@ void KeePass2XmlReader::parseRoot()
Group* oldRoot = m_db->rootGroup(); Group* oldRoot = m_db->rootGroup();
m_db->setRootGroup(rootGroup); m_db->setRootGroup(rootGroup);
delete oldRoot; delete oldRoot;
groupParsed = true;
} }
} }
else if (m_xml.name() == "DeletedObjects") { else if (m_xml.name() == "DeletedObjects") {
@ -429,6 +442,8 @@ void KeePass2XmlReader::parseRoot()
skipCurrentElement(); skipCurrentElement();
} }
} }
return groupParsed;
} }
Group* KeePass2XmlReader::parseGroup() Group* KeePass2XmlReader::parseGroup()

View File

@ -49,7 +49,7 @@ public:
QByteArray headerHash(); QByteArray headerHash();
private: private:
void parseKeePassFile(); bool parseKeePassFile();
void parseMeta(); void parseMeta();
void parseMemoryProtection(); void parseMemoryProtection();
void parseCustomIcons(); void parseCustomIcons();
@ -57,7 +57,7 @@ private:
void parseBinaries(); void parseBinaries();
void parseCustomData(); void parseCustomData();
void parseCustomDataItem(); void parseCustomDataItem();
void parseRoot(); bool parseRoot();
Group* parseGroup(); Group* parseGroup();
void parseDeletedObjects(); void parseDeletedObjects();
void parseDeletedObject(); void parseDeletedObject();