Skip to content

Commit

Permalink
Transform SHA512s to lowercase for azurl. (#1574)
Browse files Browse the repository at this point in the history
Same as #1564 but for azurl as well.
  • Loading branch information
BillyONeal authored Jan 17, 2025
1 parent 89068dd commit f8052c7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions azure-pipelines/end-to-end-tests-dir/asset-caching.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ $expected = @(
"$"
) -join "`n"

$actual = Run-VcpkgAndCaptureOutput -TestArgs ($commonArgs + @("x-download", "$downloadsRoot/example3.html", "--sha512", "d06b93c883f8126a04589937a884032df031b05518eed9d433efb6447834df2596aebd500d69b8283e5702d988ed49655ae654c1683c7a4ae58bfa6b92f2b73a", "--url", "https://localhost:1234/foobar.html", "--url", "https://localhost:1235/baz.html"))
$actual = Run-VcpkgAndCaptureOutput -TestArgs ($commonArgs + @("x-download", "$downloadsRoot/example3.html", "--sha512", "D06B93C883F8126A04589937A884032DF031B05518EED9D433EFB6447834DF2596AEBD500D69B8283E5702D988ED49655AE654C1683C7A4AE58BFA6B92F2B73A", "--url", "https://localhost:1234/foobar.html", "--url", "https://localhost:1235/baz.html"))
Throw-IfNotFailed
if (-not ($actual -match $expected)) {
throw "Failure: azurl (no), x-block-origin (no), asset-cache (n/a), download (fail)"
}

#azurl (no), x-block-origin (no), asset-cache (n/a), download (sha-mismatch)
#Expected: Hash check failed message expected/actual sha
#Expected: Hash check failed message expected/actual sha. Note that the expected sha is changed to lowercase.
Refresh-TestRoot
$expected = @(
"^Downloading https://example\.com -> example3\.html",
Expand Down Expand Up @@ -230,7 +230,7 @@ Throw-IfNotFailed


# azurl (yes), x-block-origin (no), asset-cache (miss), download (fail)
# Expected: Download failure message, asset cache named, nothing about x-block-origin
# Expected: Download failure message, asset cache named, nothing about x-block-origin. Note that the expected SHA is changed to lowercase.
Refresh-TestRoot
$expected = @(
"^Trying to download example3\.html using asset cache file://$assetCacheRegex/[0-9a-z]+",
Expand All @@ -250,7 +250,7 @@ $expected = @(
"$"
) -join "`n"

$actual = Run-VcpkgAndCaptureOutput -TestArgs ($commonArgs + @("x-download", "$downloadsRoot/example3.html", "--sha512", "d06b93c883f8126a04589937a884032df031b05518eed9d433efb6447834df2596aebd500d69b8283e5702d988ed49655ae654c1683c7a4ae58bfa6b92f2b73a", "--url", "https://localhost:1234/foobar.html", "--x-asset-sources=x-azurl,file://$AssetCache,,readwrite"))
$actual = Run-VcpkgAndCaptureOutput -TestArgs ($commonArgs + @("x-download", "$downloadsRoot/example3.html", "--sha512", "D06B93C883F8126A04589937A884032DF031B05518EED9D433EFB6447834DF2596AEBD500D69B8283E5702D988ED49655AE654C1683C7A4AE58BFA6B92F2B73A", "--url", "https://localhost:1234/foobar.html", "--x-asset-sources=x-azurl,file://$AssetCache,,readwrite"))
Throw-IfNotFailed
if (-not ($actual -match $expected)) {
throw "Failure: azurl (yes), x-block-origin (no), asset-cache (miss), download (fail)"
Expand Down
1 change: 0 additions & 1 deletion include/vcpkg/base/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ namespace vcpkg::Strings
void inplace_ascii_to_lowercase(std::string& s);
[[nodiscard]] std::string ascii_to_lowercase(StringView s);
[[nodiscard]] std::string ascii_to_uppercase(StringView s);
void append_ascii_lowercase(std::string& target, StringView s);

bool case_insensitive_ascii_starts_with(StringView s, StringView pattern);
bool case_insensitive_ascii_ends_with(StringView s, StringView pattern);
Expand Down
13 changes: 10 additions & 3 deletions src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,16 @@ namespace vcpkg
const Path& downloaded_path,
StringView sha512)
{
if (!std::all_of(sha512.begin(), sha512.end(), ParserBase::is_hex_digit_lower))
{
Checks::unreachable(VCPKG_LINE_INFO);
}

auto maybe_actual_hash =
vcpkg::Hash::get_file_hash_required(context, fs, downloaded_path, Hash::Algorithm::Sha512);
if (auto actual_hash = maybe_actual_hash.get())
{
if (Strings::case_insensitive_ascii_equals(sha512, *actual_hash))
if (sha512 == *actual_hash)
{
return true;
}
Expand Down Expand Up @@ -1427,7 +1432,7 @@ namespace vcpkg
{
if (auto sha512 = maybe_sha512.get())
{
Strings::append_ascii_lowercase(out, *sha512);
out.append(*sha512);
return true;
}

Expand Down Expand Up @@ -1607,7 +1612,7 @@ namespace vcpkg
View<std::string> raw_urls,
View<std::string> headers,
const Path& download_path,
const Optional<std::string>& maybe_sha512)
const Optional<std::string>& maybe_sha512_mixed_case)
{
// Design goals:
// * We want it to be clear when asset cache(s) are used. This means not printing the authoritative URL in a
Expand All @@ -1627,6 +1632,8 @@ namespace vcpkg
//
// See examples of console output in asset-caching.ps1

auto maybe_sha512 =
maybe_sha512_mixed_case.map([](const std::string& sha512) { return Strings::ascii_to_lowercase(sha512); });
// Note: no secrets for the input URLs
std::vector<SanitizedUrl> sanitized_urls =
Util::fmap(raw_urls, [&](const std::string& url) { return SanitizedUrl{url, {}}; });
Expand Down
7 changes: 1 addition & 6 deletions src/vcpkg/base/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void Strings::inplace_ascii_to_lowercase(std::string& s)
std::string Strings::ascii_to_lowercase(StringView s)
{
std::string result;
append_ascii_lowercase(result, s);
std::transform(s.begin(), s.end(), std::back_inserter(result), tolower_char);
return result;
}

Expand All @@ -213,11 +213,6 @@ std::string Strings::ascii_to_uppercase(StringView s)
return result;
}

void Strings::append_ascii_lowercase(std::string& target, StringView s)
{
std::transform(s.begin(), s.end(), std::back_inserter(target), tolower_char);
}

bool Strings::case_insensitive_ascii_starts_with(StringView s, StringView pattern)
{
if (s.size() < pattern.size()) return false;
Expand Down

0 comments on commit f8052c7

Please sign in to comment.