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,16 +1,15 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Isode Limited. | 2 | * Copyright (c) 2010-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | #include <Swift/Controllers/Storages/AvatarFileStorage.h> | 7 | #include <Swift/Controllers/Storages/AvatarFileStorage.h> |
| 8 | 8 | ||
| 9 | #include <iostream> | ||
| 10 | |||
| 11 | #include <boost/filesystem.hpp> | 9 | #include <boost/filesystem.hpp> |
| 12 | #include <boost/filesystem/fstream.hpp> | 10 | #include <boost/filesystem/fstream.hpp> |
| 13 | 11 | ||
| 12 | #include <Swiften/Base/Log.h> | ||
| 14 | #include <Swiften/Base/String.h> | 13 | #include <Swiften/Base/String.h> |
| 15 | #include <Swiften/Crypto/CryptoProvider.h> | 14 | #include <Swiften/Crypto/CryptoProvider.h> |
| 16 | #include <Swiften/StringCodecs/Hexify.h> | 15 | #include <Swiften/StringCodecs/Hexify.h> |
| @@ -31,13 +30,13 @@ AvatarFileStorage::AvatarFileStorage(const boost::filesystem::path& avatarsDir, | |||
| 31 | jidAvatars.insert(std::make_pair(jid, r.first)); | 30 | jidAvatars.insert(std::make_pair(jid, r.first)); |
| 32 | } | 31 | } |
| 33 | else if (!r.first.empty() || !r.second.empty()) { | 32 | else if (!r.first.empty() || !r.second.empty()) { |
| 34 | std::cerr << "Invalid entry in avatars file: " << r.second << std::endl; | 33 | SWIFT_LOG(error) << "Invalid entry in avatars file: " << r.second << std::endl; |
| 35 | } | 34 | } |
| 36 | } | 35 | } |
| 37 | } | 36 | } |
| 38 | } | 37 | } |
| 39 | catch (...) { | 38 | catch (...) { |
| 40 | std::cerr << "Error reading avatars file" << std::endl; | 39 | SWIFT_LOG(error) << "Error reading avatars file" << std::endl; |
| 41 | } | 40 | } |
| 42 | } | 41 | } |
| 43 | } | 42 | } |
| @@ -55,12 +54,17 @@ void AvatarFileStorage::addAvatar(const std::string& hash, const ByteArray& avat | |||
| 55 | boost::filesystem::create_directories(avatarPath.parent_path()); | 54 | boost::filesystem::create_directories(avatarPath.parent_path()); |
| 56 | } | 55 | } |
| 57 | catch (const boost::filesystem::filesystem_error& e) { | 56 | catch (const boost::filesystem::filesystem_error& e) { |
| 58 | std::cerr << "ERROR: " << e.what() << std::endl; | 57 | SWIFT_LOG(error) << "filesystem error: " << e.what() << std::endl; |
| 59 | } | 58 | } |
| 60 | } | 59 | } |
| 61 | boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); | 60 | |
| 62 | file.write(reinterpret_cast<const char*>(vecptr(avatar)), static_cast<std::streamsize>(avatar.size())); | 61 | try { |
| 63 | file.close(); | 62 | boost::filesystem::ofstream file(avatarPath, boost::filesystem::ofstream::binary|boost::filesystem::ofstream::out); |
| 63 | file.write(reinterpret_cast<const char*>(vecptr(avatar)), static_cast<std::streamsize>(avatar.size())); | ||
| 64 | } | ||
| 65 | catch (const boost::filesystem::filesystem_error& e) { | ||
| 66 | SWIFT_LOG(error) << "filesystem error: " << e.what() << std::endl; | ||
| 67 | } | ||
| 64 | } | 68 | } |
| 65 | 69 | ||
| 66 | boost::filesystem::path AvatarFileStorage::getAvatarPath(const std::string& hash) const { | 70 | boost::filesystem::path AvatarFileStorage::getAvatarPath(const std::string& hash) const { |
| @@ -69,7 +73,12 @@ boost::filesystem::path AvatarFileStorage::getAvatarPath(const std::string& hash | |||
| 69 | 73 | ||
| 70 | ByteArray AvatarFileStorage::getAvatar(const std::string& hash) const { | 74 | ByteArray AvatarFileStorage::getAvatar(const std::string& hash) const { |
| 71 | ByteArray data; | 75 | ByteArray data; |
| 72 | readByteArrayFromFile(data, getAvatarPath(hash)); | 76 | try { |
| 77 | readByteArrayFromFile(data, getAvatarPath(hash)); | ||
| 78 | } | ||
| 79 | catch (const boost::filesystem::filesystem_error& e) { | ||
| 80 | SWIFT_LOG(error) << "filesystem error: " << e.what() << std::endl; | ||
| 81 | } | ||
| 73 | return data; | 82 | return data; |
| 74 | } | 83 | } |
| 75 | 84 | ||
| @@ -98,7 +107,7 @@ void AvatarFileStorage::saveJIDAvatars() { | |||
| 98 | file.close(); | 107 | file.close(); |
| 99 | } | 108 | } |
| 100 | catch (...) { | 109 | catch (...) { |
| 101 | std::cerr << "Error writing avatars file" << std::endl; | 110 | SWIFT_LOG(error) << "Error writing avatars file" << std::endl; |
| 102 | } | 111 | } |
| 103 | } | 112 | } |
| 104 | 113 | ||
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,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2014-2016 Isode Limited. | 2 | * Copyright (c) 2014-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -91,6 +91,7 @@ class AvatarManagerImplTest : public CppUnit::TestFixture { | |||
| 91 | /* send new presence to notify of blank avatar */ | 91 | /* send new presence to notify of blank avatar */ |
| 92 | 92 | ||
| 93 | vcardUpdate = std::make_shared<VCardUpdate>(); | 93 | vcardUpdate = std::make_shared<VCardUpdate>(); |
| 94 | vcardUpdate->setPhotoHash("da39a3ee5e6b4b0d3255bfef95601890afd80709"); | ||
| 94 | presence = std::make_shared<Presence>(); | 95 | presence = std::make_shared<Presence>(); |
| 95 | presence->setTo(ownerJID); | 96 | presence->setTo(ownerJID); |
| 96 | presence->setFrom(personJID); | 97 | presence->setFrom(personJID); |
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,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Isode Limited. | 2 | * Copyright (c) 2010-2018 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -32,6 +32,10 @@ void VCardUpdateAvatarManager::handlePresenceReceived(std::shared_ptr<Presence> | |||
| 32 | return; | 32 | return; |
| 33 | } | 33 | } |
| 34 | JID from = getAvatarJID(presence->getFrom()); | 34 | JID from = getAvatarJID(presence->getFrom()); |
| 35 | if (update->getPhotoHash().size() != 40) { | ||
| 36 | SWIFT_LOG(debug) << "Invalid vCard avatar photo hash length. Must be hex-encoded SHA-1, i.e. 40 characters." << std::endl; | ||
| 37 | return; | ||
| 38 | } | ||
| 35 | if (getAvatarHash(from) == update->getPhotoHash()) { | 39 | if (getAvatarHash(from) == update->getPhotoHash()) { |
| 36 | return; | 40 | return; |
| 37 | } | 41 | } |
Swift