From: Joel Rosdahl Date: Thu, 7 Jul 2022 10:25:57 +0000 (+0200) Subject: fix: Handle more cases of invalid secondary storage URLs X-Git-Tag: v4.6.2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d1eb8cf0e800dc426371413319ecc30b960e33c;p=thirdparty%2Fccache.git fix: Handle more cases of invalid secondary storage URLs (cherry picked from commit b41ac6a7bc42f56161f929dd1f2c8fdeb22493ad) --- diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index 0b28e8a2c..20f2a1131 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -121,12 +121,15 @@ parse_storage_config(const nonstd::string_view entry) } SecondaryStorageConfig result; - result.params.url = std::string(parts[0]); - // The Url class is parsing the URL object lazily; check if successful. + 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.host(); - } catch (const Url::parse_error& e) { - throw core::Error("Cannot parse URL: {}", e.what()); + std::ignore = result.params.url.str(); + } catch (const std::exception& e) { + throw core::Error("Cannot parse URL {}: {}", url_str, e.what()); } if (result.params.url.scheme().empty()) { @@ -145,7 +148,6 @@ parse_storage_config(const nonstd::string_view entry) if (key == "read-only") { result.read_only = (value == "true"); } else if (key == "shards") { - const auto url_str = result.params.url.str(); if (url_str.find('*') == std::string::npos) { throw core::Error(R"(Missing "*" in URL when using shards: "{}")", url_str); diff --git a/test/suites/secondary_url.bash b/test/suites/secondary_url.bash index 74fe08294..9c68645dd 100644 --- a/test/suites/secondary_url.bash +++ b/test/suites/secondary_url.bash @@ -30,4 +30,18 @@ SUITE_secondary_url() { export CCACHE_SECONDARY_STORAGE="/qwerty" $CCACHE_COMPILE -c test.c 2>stderr.log expect_contains stderr.log "URL scheme must not be empty" + + # ------------------------------------------------------------------------- + TEST "Reject user info defined but no host" + + export CCACHE_SECONDARY_STORAGE="http://foo@" + $CCACHE_COMPILE -c test.c 2>stderr.log + expect_contains stderr.log "User info defined, but host is empty" + + # ------------------------------------------------------------------------- + TEST "Reject relative path with colon in first part" + + export CCACHE_SECONDARY_STORAGE="file:foo:bar" + $CCACHE_COMPILE -c test.c 2>stderr.log + expect_contains stderr.log "The first segment of the relative path can't contain ':'" }