diff --git a/Implementation/Common/common_utils.cpp b/Implementation/Common/common_utils.cpp index 4bdbfcdf..b34ebe62 100644 --- a/Implementation/Common/common_utils.cpp +++ b/Implementation/Common/common_utils.cpp @@ -616,6 +616,35 @@ namespace AMCCommon { return true; } + bool CUtils::stringIsValidFileName(const std::string& sFileName) + { + if (sFileName.empty()) + return false; + + if (sFileName.find(":") != std::string::npos) + return false; + if (sFileName.find(">") != std::string::npos) + return false; + if (sFileName.find("<") != std::string::npos) + return false; + if (sFileName.find("\"") != std::string::npos) + return false; + if (sFileName.find("/") != std::string::npos) + return false; + if (sFileName.find("\\") != std::string::npos) + return false; + if (sFileName.find("|") != std::string::npos) + return false; + if (sFileName.find("?") != std::string::npos) + return false; + if (sFileName.find("*") != std::string::npos) + return false; + if (sFileName.find("..") != std::string::npos) + return false; + + return true; + } + bool CUtils::stringIsUUIDString(const std::string& sRawString) { std::string sTrimmedString = trimString(sRawString); diff --git a/Implementation/Common/common_utils.hpp b/Implementation/Common/common_utils.hpp index 460317c6..af4d85c2 100644 --- a/Implementation/Common/common_utils.hpp +++ b/Implementation/Common/common_utils.hpp @@ -106,6 +106,7 @@ namespace AMCCommon { static bool stringIsValidAlphanumericNameString(const std::string& sString); // Only alphanumeric characters and underscore, underscore not as first character! static bool stringIsValidAlphanumericPathString(const std::string& sString); // Only alphanumeric name strings separated by dots + static bool stringIsValidFileName(const std::string& sFileName); // No path delimiters or reserved characters static std::string getCurrentUserHomeDirectory(); static std::string getTempFolder(); diff --git a/Implementation/LibMCData/libmcdata_storage.cpp b/Implementation/LibMCData/libmcdata_storage.cpp index 7384337f..5f42c98d 100644 --- a/Implementation/LibMCData/libmcdata_storage.cpp +++ b/Implementation/LibMCData/libmcdata_storage.cpp @@ -409,7 +409,8 @@ void CStorage::CreateDownloadTicket(const std::string& sTicketUUID, const std::s if (sClientFileName.empty()) throw ELibMCDataInterfaceException(LIBMCDATA_ERROR_EMPTYCLIENTFILENAME); - // TODO: Check invalid file name? + if (!AMCCommon::CUtils::stringIsValidFileName(sClientFileName)) + throw ELibMCDataInterfaceException(LIBMCDATA_ERROR_INVALIDCLIENTFILENAME, "invalid client file name: " + sClientFileName); auto pTransaction = m_pSQLHandler->beginTransaction(); @@ -527,4 +528,3 @@ void CStorage::AttachStreamToJournal(const std::string& sStreamUUID, const std:: pTransaction->commit(); } - diff --git a/Implementation/LibMCEnv/libmcenv_uienvironment.cpp b/Implementation/LibMCEnv/libmcenv_uienvironment.cpp index 73ed521e..cc96ac58 100644 --- a/Implementation/LibMCEnv/libmcenv_uienvironment.cpp +++ b/Implementation/LibMCEnv/libmcenv_uienvironment.cpp @@ -50,6 +50,7 @@ Abstract: This is a stub class definition of CUIEnvironment #include "libmcenv_zipstreamwriter.hpp" #include "libmcenv_imageloader.hpp" #include "libmcenv_machineconfigurationhandler.hpp" +#include "libmcdata_interfaceexception.hpp" #include "amc_systemstate.hpp" #include "amc_accesscontrol.hpp" @@ -174,6 +175,9 @@ void CUIEnvironment::StartStreamDownload(const std::string& sUUID, const std::st if (sFilename.size () > DOWNLOADFILENAME_MAXLENGTH) throw ELibMCEnvInterfaceException(LIBMCENV_ERROR_INVALIDDOWNLOADSTREAMFILENAME); + if (!AMCCommon::CUtils::stringIsValidFileName(sFilename)) + throw ELibMCEnvInterfaceException(LIBMCENV_ERROR_INVALIDDOWNLOADSTREAMFILENAME, "invalid download stream filename: " + sFilename); + if (!m_pAPIAuth->userIsAuthorized ()) throw ELibMCEnvInterfaceException(LIBMCENV_ERROR_USERISNOTAUTHORIZED); @@ -182,7 +186,14 @@ void CUIEnvironment::StartStreamDownload(const std::string& sUUID, const std::st auto pGlobalChrono = m_pUISystemState->getGlobalChronoInstance(); std::string sTicketUUID = m_pAPIAuth->createStreamDownloadTicket (sNormalizedUUID, sFilename); - pStorage->CreateDownloadTicket (sTicketUUID, sNormalizedUUID, sFilename, m_pAPIAuth->getSessionUUID (), sUserUUID, pGlobalChrono->getUTCTimeStampInMicrosecondsSince1970 ()); + try { + pStorage->CreateDownloadTicket (sTicketUUID, sNormalizedUUID, sFilename, m_pAPIAuth->getSessionUUID (), sUserUUID, pGlobalChrono->getUTCTimeStampInMicrosecondsSince1970 ()); + } + catch (ELibMCDataInterfaceException & E) { + if (E.getErrorCode() == LIBMCDATA_ERROR_INVALIDCLIENTFILENAME) + throw ELibMCEnvInterfaceException(LIBMCENV_ERROR_INVALIDDOWNLOADSTREAMFILENAME, E.what()); + throw; + } m_ClientActions.push_back(std::make_shared(sTicketUUID, sFilename)); } @@ -1097,4 +1108,4 @@ IJSONObject* CUIEnvironment::ParseJSONData(const LibMCEnv_uint64 nJSONDataBuffer IMachineConfigurationHandler* CUIEnvironment::CreateMachineConfigurationHandler() { return new CMachineConfigurationHandler(m_pUISystemState->getDataModel()); -} \ No newline at end of file +}