diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2018-04-05 22:05:55 -0700 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2018-04-06 17:26:58 -0700 |
| commit | 1bfb01cfeeffc8c8161f64b1bd2986557b6ba207 (patch) | |
| tree | 2d68e5c87fb780da4716fd3d71570c614794941d | |
| parent | ec790eb1716fca367dccc2b5f7e96a8719ca74fa (diff) | |
| download | vcpkg-1bfb01cfeeffc8c8161f64b1bd2986557b6ba207.tar.gz vcpkg-1bfb01cfeeffc8c8161f64b1bd2986557b6ba207.zip | |
[vcpkg hash] Add sanity checking
| -rw-r--r-- | toolsrc/include/vcpkg/commands.h | 4 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/build.cpp | 8 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/commands.cpp | 2 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/commands.fetch.cpp | 2 | ||||
| -rw-r--r-- | toolsrc/src/vcpkg/commands.hash.cpp | 33 |
5 files changed, 31 insertions, 18 deletions
diff --git a/toolsrc/include/vcpkg/commands.h b/toolsrc/include/vcpkg/commands.h index d8d2abfa9..6d29b7960 100644 --- a/toolsrc/include/vcpkg/commands.h +++ b/toolsrc/include/vcpkg/commands.h @@ -132,9 +132,9 @@ namespace vcpkg::Commands namespace Hash { std::string get_string_hash(const std::string& s, const std::string& hash_type); - std::string get_file_hash(const fs::path& path, const std::string& hash_type); + std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type); - void perform_and_exit(const VcpkgCmdArguments& args); + void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths); } namespace Fetch diff --git a/toolsrc/src/vcpkg/build.cpp b/toolsrc/src/vcpkg/build.cpp index 16a85db52..b7055100b 100644 --- a/toolsrc/src/vcpkg/build.cpp +++ b/toolsrc/src/vcpkg/build.cpp @@ -457,9 +457,9 @@ namespace vcpkg::Build abi_tag_entries.insert(abi_tag_entries.end(), dependency_abis.begin(), dependency_abis.end()); abi_tag_entries.emplace_back( - AbiEntry{"portfile", Commands::Hash::get_file_hash(config.port_dir / "portfile.cmake", "SHA1")}); + AbiEntry{"portfile", Commands::Hash::get_file_hash(paths, config.port_dir / "portfile.cmake", "SHA1")}); abi_tag_entries.emplace_back( - AbiEntry{"control", Commands::Hash::get_file_hash(config.port_dir / "CONTROL", "SHA1")}); + AbiEntry{"control", Commands::Hash::get_file_hash(paths, config.port_dir / "CONTROL", "SHA1")}); abi_tag_entries.emplace_back(AbiEntry{"triplet", pre_build_info.triplet_abi_tag}); @@ -494,7 +494,7 @@ namespace vcpkg::Build auto abi_file_path = paths.buildtrees / name / (triplet.canonical_name() + ".vcpkg_abi_info.txt"); fs.write_contents(abi_file_path, full_abi_info); - return AbiTagAndFile{Commands::Hash::get_file_hash(abi_file_path, "SHA1"), abi_file_path}; + return AbiTagAndFile{Commands::Hash::get_file_hash(paths, abi_file_path, "SHA1"), abi_file_path}; } else { @@ -784,7 +784,7 @@ namespace vcpkg::Build { return it_hash->second; } - auto hash = Commands::Hash::get_file_hash(triplet_file_path, "SHA1"); + auto hash = Commands::Hash::get_file_hash(paths, triplet_file_path, "SHA1"); s_hash_cache.emplace(triplet_file_path, hash); return hash; } diff --git a/toolsrc/src/vcpkg/commands.cpp b/toolsrc/src/vcpkg/commands.cpp index 9041b37ff..8b6ffb3d7 100644 --- a/toolsrc/src/vcpkg/commands.cpp +++ b/toolsrc/src/vcpkg/commands.cpp @@ -42,6 +42,7 @@ namespace vcpkg::Commands {"cache", &Cache::perform_and_exit}, {"portsdiff", &PortsDiff::perform_and_exit}, {"autocomplete", &Autocomplete::perform_and_exit}, + {"hash", &Hash::perform_and_exit}, // {"fetch", &Fetch::perform_and_exit}, }; return t; @@ -52,7 +53,6 @@ namespace vcpkg::Commands static std::vector<PackageNameAndFunction<CommandTypeC>> t = { {"version", &Version::perform_and_exit}, {"contact", &Contact::perform_and_exit}, - {"hash", &Hash::perform_and_exit}, }; return t; } diff --git a/toolsrc/src/vcpkg/commands.fetch.cpp b/toolsrc/src/vcpkg/commands.fetch.cpp index 1ba043728..e369f44d2 100644 --- a/toolsrc/src/vcpkg/commands.fetch.cpp +++ b/toolsrc/src/vcpkg/commands.fetch.cpp @@ -240,7 +240,7 @@ namespace vcpkg::Commands::Fetch R"(curl -L '%s' --create-dirs --output '%s')", url, download_path_part)); Checks::check_exit(VCPKG_LINE_INFO, code == 0, "Could not download %s", url); - const std::string actual_hash = Hash::get_file_hash(download_path_part, "SHA512"); + const std::string actual_hash = Hash::get_file_hash(paths, download_path_part, "SHA512"); Checks::check_exit(VCPKG_LINE_INFO, sha512 == actual_hash, "File does not have the expected hash:\n" diff --git a/toolsrc/src/vcpkg/commands.hash.cpp b/toolsrc/src/vcpkg/commands.hash.cpp index a3ac2cbbb..3f90eeaca 100644 --- a/toolsrc/src/vcpkg/commands.hash.cpp +++ b/toolsrc/src/vcpkg/commands.hash.cpp @@ -7,6 +7,19 @@ #include <vcpkg/commands.h> #include <vcpkg/help.h> +namespace vcpkg::Commands::Hash +{ + static void verify_has_only_allowed_chars(const std::string& s) + { + static const std::regex ALLOWED_CHARS{"^[a-zA-Z0-9-]*$"}; + Checks::check_exit(VCPKG_LINE_INFO, + std::regex_match(s, ALLOWED_CHARS), + "Only alphanumeric chars and dashes are currently allowed. String was:\n" + " % s", + s); + } +} + #if defined(_WIN32) #include <bcrypt.h> @@ -138,13 +151,16 @@ namespace vcpkg::Commands::Hash }; } - std::string get_file_hash(const fs::path& path, const std::string& hash_type) + std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type) { + Checks::check_exit( + VCPKG_LINE_INFO, paths.get_filesystem().exists(path), "File %s does not exist", path.u8string()); return BCryptHasher{hash_type}.hash_file(path); } std::string get_string_hash(const std::string& s, const std::string& hash_type) { + verify_has_only_allowed_chars(s); return BCryptHasher{hash_type}.hash_string(s); } } @@ -174,9 +190,11 @@ namespace vcpkg::Commands::Hash return Strings::trim(std::string{ec_data.output}); } - std::string get_file_hash(const fs::path& path, const std::string& hash_type) + std::string get_file_hash(const VcpkgPaths& paths, const fs::path& path, const std::string& hash_type) { const std::string digest_size = get_digest_size(hash_type); + Checks::check_exit( + VCPKG_LINE_INFO, paths.get_filesystem().exists(path), "File %s does not exist", path.u8string()); const std::string cmd_line = Strings::format( R"(shasum -a %s "%s" | awk '{ print $1 }')", digest_size, path.u8string()); return run_shasum_and_post_process(cmd_line); @@ -185,12 +203,7 @@ namespace vcpkg::Commands::Hash std::string get_string_hash(const std::string& s, const std::string& hash_type) { const std::string digest_size = get_digest_size(hash_type); - - static const std::regex ALLOWED_CHARS{"^[a-zA-Z0-9:]*$"}; - - Checks::check_exit(VCPKG_LINE_INFO, - std::regex_match(s, ALLOWED_CHARS), - "Only alphanumeric chars and colons are currently allowed"); + verify_has_only_allowed_chars(s); const std::string cmd_line = Strings::format( R"(echo -n "%s" | shasum -a %s | awk '{ print $1 }')", s, digest_size); @@ -210,13 +223,13 @@ namespace vcpkg::Commands::Hash nullptr, }; - void perform_and_exit(const VcpkgCmdArguments& args) + void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths) { Util::unused(args.parse_arguments(COMMAND_STRUCTURE)); const fs::path file_to_hash = args.command_arguments[0]; const std::string algorithm = args.command_arguments.size() == 2 ? args.command_arguments[1] : "SHA512"; - const std::string hash = get_file_hash(file_to_hash, algorithm); + const std::string hash = get_file_hash(paths, file_to_hash, algorithm); System::println(hash); Checks::exit_success(VCPKG_LINE_INFO); } |
