asar_util.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/lazy_instance.h"
  11. #include "base/logging.h"
  12. #include "base/no_destructor.h"
  13. #include "base/stl_util.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/strings/string_util.h"
  16. #include "base/synchronization/lock.h"
  17. #include "base/threading/thread_local.h"
  18. #include "crypto/secure_hash.h"
  19. #include "crypto/sha2.h"
  20. #include "shell/common/asar/archive.h"
  21. #include "shell/common/thread_restrictions.h"
  22. namespace asar {
  23. namespace {
  24. typedef std::map<base::FilePath, std::shared_ptr<Archive>> ArchiveMap;
  25. const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
  26. bool IsDirectoryCached(const base::FilePath& path) {
  27. static base::NoDestructor<std::map<base::FilePath, bool>>
  28. s_is_directory_cache;
  29. static base::NoDestructor<base::Lock> lock;
  30. base::AutoLock auto_lock(*lock);
  31. auto& is_directory_cache = *s_is_directory_cache;
  32. auto it = is_directory_cache.find(path);
  33. if (it != is_directory_cache.end()) {
  34. return it->second;
  35. }
  36. electron::ScopedAllowBlockingForElectron allow_blocking;
  37. return is_directory_cache[path] = base::DirectoryExists(path);
  38. }
  39. } // namespace
  40. ArchiveMap& GetArchiveCache() {
  41. static base::NoDestructor<ArchiveMap> s_archive_map;
  42. return *s_archive_map;
  43. }
  44. base::Lock& GetArchiveCacheLock() {
  45. static base::NoDestructor<base::Lock> lock;
  46. return *lock;
  47. }
  48. std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
  49. base::AutoLock auto_lock(GetArchiveCacheLock());
  50. ArchiveMap& map = GetArchiveCache();
  51. // if we have it, return it
  52. const auto lower = map.lower_bound(path);
  53. if (lower != std::end(map) && !map.key_comp()(path, lower->first))
  54. return lower->second;
  55. // if we can create it, return it
  56. auto archive = std::make_shared<Archive>(path);
  57. if (archive->Init()) {
  58. map.try_emplace(lower, path, archive);
  59. return archive;
  60. }
  61. // didn't have it, couldn't create it
  62. return nullptr;
  63. }
  64. void ClearArchives() {
  65. base::AutoLock auto_lock(GetArchiveCacheLock());
  66. ArchiveMap& map = GetArchiveCache();
  67. map.clear();
  68. }
  69. bool GetAsarArchivePath(const base::FilePath& full_path,
  70. base::FilePath* asar_path,
  71. base::FilePath* relative_path,
  72. bool allow_root) {
  73. base::FilePath iter = full_path;
  74. while (true) {
  75. base::FilePath dirname = iter.DirName();
  76. if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
  77. break;
  78. else if (iter == dirname)
  79. return false;
  80. iter = dirname;
  81. }
  82. base::FilePath tail;
  83. if (!((allow_root && iter == full_path) ||
  84. iter.AppendRelativePath(full_path, &tail)))
  85. return false;
  86. *asar_path = iter;
  87. *relative_path = tail;
  88. return true;
  89. }
  90. bool ReadFileToString(const base::FilePath& path, std::string* contents) {
  91. base::FilePath asar_path, relative_path;
  92. if (!GetAsarArchivePath(path, &asar_path, &relative_path))
  93. return base::ReadFileToString(path, contents);
  94. std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
  95. if (!archive)
  96. return false;
  97. Archive::FileInfo info;
  98. if (!archive->GetFileInfo(relative_path, &info))
  99. return false;
  100. if (info.unpacked) {
  101. base::FilePath real_path;
  102. // For unpacked file it will return the real path instead of doing the copy.
  103. archive->CopyFileOut(relative_path, &real_path);
  104. return base::ReadFileToString(real_path, contents);
  105. }
  106. base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
  107. if (!src.IsValid())
  108. return false;
  109. contents->resize(info.size);
  110. if (static_cast<int>(info.size) !=
  111. src.Read(info.offset, const_cast<char*>(contents->data()),
  112. contents->size())) {
  113. return false;
  114. }
  115. if (info.integrity.has_value()) {
  116. ValidateIntegrityOrDie(contents->data(), contents->size(),
  117. info.integrity.value());
  118. }
  119. return true;
  120. }
  121. void ValidateIntegrityOrDie(const char* data,
  122. size_t size,
  123. const IntegrityPayload& integrity) {
  124. if (integrity.algorithm == HashAlgorithm::kSHA256) {
  125. uint8_t hash[crypto::kSHA256Length];
  126. auto hasher = crypto::SecureHash::Create(crypto::SecureHash::SHA256);
  127. hasher->Update(data, size);
  128. hasher->Finish(hash, sizeof(hash));
  129. const std::string hex_hash =
  130. base::ToLowerASCII(base::HexEncode(hash, sizeof(hash)));
  131. if (integrity.hash != hex_hash) {
  132. LOG(FATAL) << "Integrity check failed for asar archive ("
  133. << integrity.hash << " vs " << hex_hash << ")";
  134. }
  135. } else {
  136. LOG(FATAL) << "Unsupported hashing algorithm in ValidateIntegrityOrDie";
  137. }
  138. }
  139. } // namespace asar