asar_util.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/asar/asar_util.h"
  5. #include <map>
  6. #include <memory>
  7. #include <string>
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/logging.h"
  11. #include "base/no_destructor.h"
  12. #include "base/strings/string_number_conversions.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/synchronization/lock.h"
  15. #include "base/threading/thread_local.h"
  16. #include "crypto/secure_hash.h"
  17. #include "crypto/sha2.h"
  18. #include "shell/common/asar/archive.h"
  19. #include "shell/common/thread_restrictions.h"
  20. namespace asar {
  21. namespace {
  22. using ArchiveMap = std::map<base::FilePath, std::shared_ptr<Archive>>;
  23. const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
  24. bool IsDirectoryCached(const base::FilePath& path) {
  25. static base::NoDestructor<std::map<base::FilePath, bool>>
  26. s_is_directory_cache;
  27. static base::NoDestructor<base::Lock> lock;
  28. base::AutoLock auto_lock(*lock);
  29. auto& is_directory_cache = *s_is_directory_cache;
  30. auto it = is_directory_cache.find(path);
  31. if (it != is_directory_cache.end()) {
  32. return it->second;
  33. }
  34. electron::ScopedAllowBlockingForElectron allow_blocking;
  35. return is_directory_cache[path] = base::DirectoryExists(path);
  36. }
  37. ArchiveMap& GetArchiveCache() {
  38. static base::NoDestructor<ArchiveMap> s_archive_map;
  39. return *s_archive_map;
  40. }
  41. base::Lock& GetArchiveCacheLock() {
  42. static base::NoDestructor<base::Lock> lock;
  43. return *lock;
  44. }
  45. } // namespace
  46. std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
  47. base::AutoLock auto_lock(GetArchiveCacheLock());
  48. ArchiveMap& map = GetArchiveCache();
  49. // if we have it, return it
  50. const auto lower = map.lower_bound(path);
  51. if (lower != std::end(map) && !map.key_comp()(path, lower->first))
  52. return lower->second;
  53. // if we can create it, return it
  54. auto archive = std::make_shared<Archive>(path);
  55. if (archive->Init()) {
  56. map.try_emplace(lower, path, archive);
  57. return archive;
  58. }
  59. // didn't have it, couldn't create it
  60. return nullptr;
  61. }
  62. bool GetAsarArchivePath(const base::FilePath& full_path,
  63. base::FilePath* asar_path,
  64. base::FilePath* relative_path,
  65. bool allow_root) {
  66. base::FilePath iter = full_path;
  67. while (true) {
  68. base::FilePath dirname = iter.DirName();
  69. if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
  70. break;
  71. else if (iter == dirname)
  72. return false;
  73. iter = dirname;
  74. }
  75. base::FilePath tail;
  76. if (!((allow_root && iter == full_path) ||
  77. iter.AppendRelativePath(full_path, &tail)))
  78. return false;
  79. *asar_path = iter;
  80. *relative_path = tail;
  81. return true;
  82. }
  83. bool ReadFileToString(const base::FilePath& path, std::string* contents) {
  84. base::FilePath asar_path, relative_path;
  85. if (!GetAsarArchivePath(path, &asar_path, &relative_path))
  86. return base::ReadFileToString(path, contents);
  87. std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
  88. if (!archive)
  89. return false;
  90. Archive::FileInfo info;
  91. if (!archive->GetFileInfo(relative_path, &info))
  92. return false;
  93. if (info.unpacked) {
  94. base::FilePath real_path;
  95. // For unpacked file it will return the real path instead of doing the copy.
  96. archive->CopyFileOut(relative_path, &real_path);
  97. return base::ReadFileToString(real_path, contents);
  98. }
  99. base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
  100. if (!src.IsValid())
  101. return false;
  102. contents->resize(info.size);
  103. if (!src.ReadAndCheck(info.offset, base::as_writable_byte_span(*contents)))
  104. return false;
  105. if (info.integrity)
  106. ValidateIntegrityOrDie(base::as_byte_span(*contents), *info.integrity);
  107. return true;
  108. }
  109. void ValidateIntegrityOrDie(base::span<const uint8_t> input,
  110. const IntegrityPayload& integrity) {
  111. if (integrity.algorithm == HashAlgorithm::kSHA256) {
  112. const std::string hex_hash =
  113. base::ToLowerASCII(base::HexEncode(crypto::SHA256Hash(input)));
  114. if (integrity.hash != hex_hash) {
  115. LOG(FATAL) << "Integrity check failed for asar archive ("
  116. << integrity.hash << " vs " << hex_hash << ")";
  117. }
  118. } else {
  119. LOG(FATAL) << "Unsupported hashing algorithm in ValidateIntegrityOrDie";
  120. }
  121. }
  122. } // namespace asar