Skip to content

Commit

Permalink
Add checks for valid UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Sep 8, 2021
1 parent 2be370f commit 8997118
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/browser/BrowserAction.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,6 +22,7 @@
#include "BrowserShared.h"
#include "config-keepassx.h"
#include "core/Global.h"
#include "core/Tools.h"

#include <QJsonArray>
#include <QJsonDocument>
Expand Down Expand Up @@ -52,7 +53,7 @@ namespace
ERROR_KEEPASS_NO_LOGINS_FOUND = 15,
ERROR_KEEPASS_NO_GROUPS_FOUND = 16,
ERROR_KEEPASS_CANNOT_CREATE_NEW_GROUP = 17,
ERROR_KEEPASS_NO_UUID_PROVIDED = 18
ERROR_KEEPASS_NO_VALID_UUID_PROVIDED = 18
};
}

Expand Down Expand Up @@ -363,6 +364,10 @@ QJsonObject BrowserAction::handleSetLogin(const QJsonObject& json, const QString
if (uuid.isEmpty()) {
browserService()->addEntry(id, login, password, url, submitUrl, realm, group, groupUuid);
} else {
if (!Tools::isValidUuid(uuid)) {
return getErrorReply(action, ERROR_KEEPASS_NO_VALID_UUID_PROVIDED);
}

result = browserService()->updateEntry(id, uuid, login, password, url, submitUrl);
}

Expand Down Expand Up @@ -493,6 +498,9 @@ QJsonObject BrowserAction::handleGetTotp(const QJsonObject& json, const QString&
}

const QString uuid = decrypted.value("uuid").toString();
if (!Tools::isValidUuid(uuid)) {
return getErrorReply(action, ERROR_KEEPASS_NO_VALID_UUID_PROVIDED);
}

// Get the current TOTP
const auto totp = browserService()->getCurrentTotp(uuid);
Expand Down Expand Up @@ -524,8 +532,8 @@ QJsonObject BrowserAction::handleDeleteEntry(const QJsonObject& json, const QStr
}

const auto uuid = decrypted.value("uuid").toString();
if (uuid.isEmpty()) {
return getErrorReply(action, ERROR_KEEPASS_NO_UUID_PROVIDED);
if (!Tools::isValidUuid(uuid)) {
return getErrorReply(action, ERROR_KEEPASS_NO_VALID_UUID_PROVIDED);
}

const auto result = browserService()->deleteEntry(uuid);
Expand Down Expand Up @@ -600,8 +608,8 @@ QString BrowserAction::getErrorMessage(const int errorCode) const
return QObject::tr("No groups found");
case ERROR_KEEPASS_CANNOT_CREATE_NEW_GROUP:
return QObject::tr("Cannot create new group");
case ERROR_KEEPASS_NO_UUID_PROVIDED:
return QObject::tr("No UUID provided");
case ERROR_KEEPASS_NO_VALID_UUID_PROVIDED:
return QObject::tr("No valid UUID provided");
default:
return QObject::tr("Unknown error");
}
Expand Down
2 changes: 1 addition & 1 deletion src/browser/BrowserAction.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2013 Francois Ferrand
* Copyright (C) 2017 Sami Vänttinen <sami.vanttinen@protonmail.com>
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/browser/BrowserService.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2013 Francois Ferrand
* Copyright (C) 2017 Sami Vänttinen <sami.vanttinen@protonmail.com>
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
16 changes: 15 additions & 1 deletion src/core/Tools.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 Lennart Glauer <mail@lennart-glauer.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -317,6 +317,20 @@ namespace Tools
return QUuid::fromRfc4122(QByteArray::fromHex(uuid.toLatin1()));
}

bool isValidUuid(const QString& uuidStr)
{
if (uuidStr.isEmpty() || uuidStr.length() != 32 || !isHex(uuidStr.toLatin1())) {
return false;
}

const auto uuid = hexToUuid(uuidStr);
if (uuid.isNull() || uuid.version() == QUuid::VerUnknown) {
return false;
}

return true;
}

QString envSubstitute(const QString& filepath, QProcessEnvironment environment)
{
QString subbed = filepath;
Expand Down
3 changes: 2 additions & 1 deletion src/core/Tools.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -40,6 +40,7 @@ namespace Tools
bool checkUrlValid(const QString& urlField);
QString uuidToHex(const QUuid& uuid);
QUuid hexToUuid(const QString& uuid);
bool isValidUuid(const QString& uuidStr);
QRegularExpression convertToRegex(const QString& string,
bool useWildcards = false,
bool exactMatch = false,
Expand Down
19 changes: 18 additions & 1 deletion tests/TestTools.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -88,3 +88,20 @@ void TestTools::testEnvSubstitute()
QCOMPARE(Tools::envSubstitute("start/$EMPTY$$EMPTY$HOME/end", environment), QString("start/$/home/user/end"));
#endif
}

void TestTools::testValidUuid()
{
auto validUuid = Tools::uuidToHex(QUuid::createUuid());
auto nonValidUuid = "1234567890abcdef1234567890abcdef";
auto emptyUuid = QString();
auto shortUuid = validUuid.left(10);
auto longUuid = validUuid + "baddata";
auto nonHexUuid = Tools::uuidToHex(QUuid::createUuid()).replace(0, 1, 'p');

QVERIFY(Tools::isValidUuid(validUuid));
QVERIFY(not Tools::isValidUuid(nonValidUuid));
QVERIFY(not Tools::isValidUuid(emptyUuid));
QVERIFY(not Tools::isValidUuid(shortUuid));
QVERIFY(not Tools::isValidUuid(longUuid));
QVERIFY(not Tools::isValidUuid(nonHexUuid));
}
3 changes: 2 additions & 1 deletion tests/TestTools.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2021 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -28,6 +28,7 @@ private slots:
void testIsHex();
void testIsBase64();
void testEnvSubstitute();
void testValidUuid();
};

#endif // KEEPASSX_TESTTOOLS_H

0 comments on commit 8997118

Please sign in to comment.