From e079762c8ed036c5ee66e782c615a59653a1bc53 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 17 Jun 2021 09:02:58 +0200 Subject: [PATCH] GetObjectPath: ensure use of escaped name in all cases and use TruncateUsingHash() 68a0079c26686363b6202a8abd2712d2bf96d9f2 introduced two problems that are fixed with this commit: 1. The new truncated/hashed name did not use EscapeName() 2. There was a possible collision of names when creating objects with a full name of format "[80 characters]...[40 hex digits]" (i.e. the same as the truncated/hashed variant but short enough that it isn't hashed) --- lib/remote/configobjectutility.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/remote/configobjectutility.cpp b/lib/remote/configobjectutility.cpp index 6ba0e0ecc..df9aa24cc 100644 --- a/lib/remote/configobjectutility.cpp +++ b/lib/remote/configobjectutility.cpp @@ -35,15 +35,17 @@ String ConfigObjectUtility::GetObjectConfigPath(const Type::Ptr& type, const Str boost::algorithm::to_lower(typeDir); /* This may throw an exception the caller above must handle. */ - String prefix = GetConfigDir(); + String prefix = GetConfigDir() + "/conf.d/" + type->GetPluralName().ToLower() + "/"; - auto old (prefix + "/conf.d/" + typeDir + "/" + EscapeName(fullName) + ".conf"); + String escapedName = EscapeName(fullName); - if (fullName.GetLength() <= 80u + 3u /* "..." */ + 40u /* hex SHA1 */ || Utility::PathExists(old)) { + String old = prefix + escapedName + ".conf"; + if (Utility::PathExists(old)) { return std::move(old); } - return prefix + "/conf.d/" + typeDir + "/" + fullName.SubStr(0, 80) + "..." + SHA1(fullName) + ".conf"; + /* Maximum length 80 bytes object name + 3 bytes "..." + 40 bytes SHA1 (hex-encoded) */ + return prefix + Utility::TruncateUsingHash<80+3+40>(escapedName) + ".conf"; } void ConfigObjectUtility::RepairPackage(const String& package)