diff options
author | Tobias Markmann <tm@ayena.de> | 2018-02-26 16:27:04 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2018-02-26 16:27:04 (GMT) |
commit | 8ede4f21d6b81263b15487509e37e6df4553c18f (patch) | |
tree | 890780a61640d2a1fc2c4a2f31142069decd3d7c | |
parent | bd4115c0db3d898d7de0944d340a9a2f1de4938c (diff) | |
download | swift-8ede4f21d6b81263b15487509e37e6df4553c18f.zip swift-8ede4f21d6b81263b15487509e37e6df4553c18f.tar.bz2 |
Ignore invalid vCard avatar update notifications
Test-Information:
Tests pass on macOS 10.13.3 with clang-trunk and ASAN.
Change-Id: Ice68e93341693349ed5d95dfc062c0a7b07dc673
-rw-r--r-- | Swift/Controllers/Storages/AvatarFileStorage.cpp | 31 | ||||
-rw-r--r-- | Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp | 3 | ||||
-rw-r--r-- | Swiften/Avatars/VCardUpdateAvatarManager.cpp | 6 |
3 files changed, 27 insertions, 13 deletions
diff --git a/Swift/Controllers/Storages/AvatarFileStorage.cpp b/Swift/Controllers/Storages/AvatarFileStorage.cpp index a103920..9d9b9ea 100644 --- a/Swift/Controllers/Storages/AvatarFileStorage.cpp +++ b/Swift/Controllers/Storages/AvatarFileStorage.cpp @@ -1,3 +1,3 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. @@ -8,4 +8,2 @@ -#include <iostream> - #include <boost/filesystem.hpp> @@ -13,2 +11,3 @@ +#include <Swiften/Base/Log.h> #include <Swiften/Base/String.h> @@ -33,3 +32,3 @@ AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& avatarsDir, else if (!r.first.empty() || !r.second.empty()) { - std::cerr << "Invalid entry in avatars file: " << r.second << std::endl; + SWIFT_LOG(error) << "Invalid entry in avatars file: " << r.second << std::endl; } @@ -39,3 +38,3 @@ AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& avatarsDir, catch (...) { - std::cerr << "Error reading avatars file" << std::endl; + SWIFT_LOG(error) << "Error reading avatars file" << std::endl; } @@ -57,8 +56,13 @@ void AvatarFileStorage::addAvatar(const std::string& hash, const ByteArray& avat catch (const boost::filesystem::filesystem_error& e) { - std::cerr << "ERROR: " << e.what() << std::endl; + SWIFT_LOG(error) << "filesystem error: " << e.what() << std::endl; } } - boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); - file.write(reinterpret_cast<const char*>(vecptr(avatar)), static_cast<std::streamsize>(avatar.size())); - file.close(); + + try { + boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); + file.write(reinterpret_cast<const char*>(vecptr(avatar)), static_cast<std::streamsize>(avatar.size())); + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "filesystem error: " << e.what() << std::endl; + } } @@ -71,3 +75,8 @@ ByteArray AvatarFileStorage::getAvatar(const std::string& hash) const { ByteArray data; - readByteArrayFromFile(data, getAvatarPath(hash)); + try { + readByteArrayFromFile(data, getAvatarPath(hash)); + } + catch (const boost::filesystem::filesystem_error& e) { + SWIFT_LOG(error) << "filesystem error: " << e.what() << std::endl; + } return data; @@ -100,3 +109,3 @@ void AvatarFileStorage::saveJIDAvatars() { catch (...) { - std::cerr << "Error writing avatars file" << std::endl; + SWIFT_LOG(error) << "Error writing avatars file" << std::endl; } diff --git a/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp b/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp index 241f375..5a35410 100644 --- a/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp +++ b/Swiften/Avatars/UnitTest/AvatarManagerImplTest.cpp @@ -1,3 +1,3 @@ /* - * Copyright (c) 2014-2016 Isode Limited. + * Copyright (c) 2014-2018 Isode Limited. * All rights reserved. @@ -93,2 +93,3 @@ class AvatarManagerImplTest : public CppUnit::TestFixture { vcardUpdate = std::make_shared<VCardUpdate>(); + vcardUpdate->setPhotoHash("da39a3ee5e6b4b0d3255bfef95601890afd80709"); presence = std::make_shared<Presence>(); diff --git a/Swiften/Avatars/VCardUpdateAvatarManager.cpp b/Swiften/Avatars/VCardUpdateAvatarManager.cpp index 3e8d87b..349af2f 100644 --- a/Swiften/Avatars/VCardUpdateAvatarManager.cpp +++ b/Swiften/Avatars/VCardUpdateAvatarManager.cpp @@ -1,3 +1,3 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. @@ -34,2 +34,6 @@ void VCardUpdateAvatarManager::handlePresenceReceived(std::shared_ptr<Presence> JID from = getAvatarJID(presence->getFrom()); + if (update->getPhotoHash().size() != 40) { + SWIFT_LOG(debug) << "Invalid vCard avatar photo hash length. Must be hex-encoded SHA-1, i.e. 40 characters." << std::endl; + return; + } if (getAvatarHash(from) == update->getPhotoHash()) { |