From: Joel Rosdahl Date: Tue, 29 Aug 2023 19:06:23 +0000 (+0200) Subject: fix: Exit more gracefully on invalid sharded remote storage URL X-Git-Tag: v4.9~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d47ee7e5d289bd92102f082002631ad70a5046d;p=thirdparty%2Fccache.git fix: Exit more gracefully on invalid sharded remote storage URL As mentioned in #1321. --- diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index 189e21e27..65ce3a70f 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -113,6 +113,20 @@ to_string(const RemoteStorageConfig& entry) return result; } +static Url +url_from_string(const std::string& url_string) +{ + // The Url class is parsing the URL object lazily. Check if the URL is valid + // now to avoid exceptions later. + try { + Url url(url_string); + std::ignore = url.str(); + return url; + } catch (const std::exception& e) { + throw core::Error(FMT("Cannot parse URL {}: {}", url_string, e.what())); + } +} + static RemoteStorageConfig parse_storage_config(const std::string_view entry) { @@ -126,15 +140,7 @@ parse_storage_config(const std::string_view entry) RemoteStorageConfig result; const auto url_str = std::string(parts[0]); - result.params.url = url_str; - - // The Url class is parsing the URL object lazily. Check if the URL is valid - // now to avoid exceptions later. - try { - std::ignore = result.params.url.str(); - } catch (const std::exception& e) { - throw core::Error(FMT("Cannot parse URL {}: {}", url_str, e.what())); - } + result.params.url = url_from_string(url_str); if (result.params.url.scheme().empty()) { throw core::Error(FMT("URL scheme must not be empty: {}", entry)); @@ -365,7 +371,7 @@ get_shard_url(const Hash::Digest& key, } } - return util::replace_first(url, "*", best_shard); + return url_from_string(util::replace_first(url, "*", best_shard)); } RemoteStorageBackendEntry*