diff --git a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp --- a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp +++ b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp @@ -24,6 +24,8 @@ const std::string NotificationsCryptoModule::initialEncryptedMessageContent = "{\"type\": \"init\"}"; const int NotificationsCryptoModule::olmEncryptedTypeMessage = 1; +// The choice for 36 was inspired by UUID having 36 characters +const int temporaryFilePathRandomSuffixLength = 36; crypto::CryptoModule NotificationsCryptoModule::deserializeCryptoModule( const std::string &path, @@ -88,13 +90,11 @@ folly::dynamic::object("account", account)("sessions", sessions); std::string pickledPersist = folly::toJson(persistJSON); - std::string temporaryPath = path + callingProcessName; - // This is for the case if any of the steps below failed/app was killed - // in a previous call to this method leaving temporary file unremoved. - // We supply `callingProcessName` as function argument in order to name - // temporary file in a deterministic way. Otherwise we would need to use - // directory search API to retrieve unremoved files paths. - remove(temporaryPath.c_str()); + std::string temporaryFilePathRandomSuffix = + crypto::Tools::generateRandomString(temporaryFilePathRandomSuffixLength); + std::string temporaryPath = + path + callingProcessName + temporaryFilePathRandomSuffix; + mode_t readWritePermissionsMode = 0666; int temporaryFD = open(temporaryPath.c_str(), O_CREAT | O_WRONLY, readWritePermissionsMode); @@ -129,6 +129,7 @@ "Details: " + std::string(strerror(errno))); } + remove(temporaryFD); } void NotificationsCryptoModule::callCryptoModule(