asar_util.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. typedef std::map<base::FilePath, std::shared_ptr<Archive>> ArchiveMap;
  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. } // namespace
  38. ArchiveMap& GetArchiveCache() {
  39. static base::NoDestructor<ArchiveMap> s_archive_map;
  40. return *s_archive_map;
  41. }
  42. base::Lock& GetArchiveCacheLock() {
  43. static base::NoDestructor<base::Lock> lock;
  44. return *lock;
  45. }
  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. void ClearArchives() {
  63. base::AutoLock auto_lock(GetArchiveCacheLock());
  64. ArchiveMap& map = GetArchiveCache();
  65. map.clear();
  66. }
  67. bool GetAsarArchivePath(const base::FilePath& full_path,
  68. base::FilePath* asar_path,
  69. base::FilePath* relative_path,
  70. bool allow_root) {
  71. base::FilePath iter = full_path;
  72. while (true) {
  73. base::FilePath dirname = iter.DirName();
  74. if (iter.MatchesExtension(kAsarExtension) && !IsDirectoryCached(iter))
  75. break;
  76. else if (iter == dirname)
  77. return false;
  78. iter = dirname;
  79. }
  80. base::FilePath tail;
  81. if (!((allow_root && iter == full_path) ||
  82. iter.AppendRelativePath(full_path, &tail)))
  83. return false;
  84. *asar_path = iter;
  85. *relative_path = tail;
  86. return true;
  87. }
  88. bool ReadFileToString(const base::FilePath& path, std::string* contents) {
  89. base::FilePath asar_path, relative_path;
  90. if (!GetAsarArchivePath(path, &asar_path, &relative_path))
  91. return base::ReadFileToString(path, contents);
  92. std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
  93. if (!archive)
  94. return false;
  95. Archive::FileInfo info;
  96. if (!archive->GetFileInfo(relative_path, &info))
  97. return false;
  98. if (info.unpacked) {
  99. base::FilePath real_path;
  100. // For unpacked file it will return the real path instead of doing the copy.
  101. archive->CopyFileOut(relative_path, &real_path);
  102. return base::ReadFileToString(real_path, contents);
  103. }
  104. base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
  105. if (!src.IsValid())
  106. return false;
  107. contents->resize(info.size);
  108. if (static_cast<int>(info.size) !=
  109. src.Read(info.offset, const_cast<char*>(contents->data()),
  110. contents->size())) {
  111. return false;
  112. }
  113. if (info.integrity.has_value()) {
  114. ValidateIntegrityOrDie(contents->data(), contents->size(),
  115. info.integrity.value());
  116. }
  117. return true;
  118. }
  119. void ValidateIntegrityOrDie(const char* data,
  120. size_t size,
  121. const IntegrityPayload& integrity) {
  122. if (integrity.algorithm == HashAlgorithm::kSHA256) {
  123. uint8_t hash[crypto::kSHA256Length];
  124. auto hasher = crypto::SecureHash::Create(crypto::SecureHash::SHA256);
  125. hasher->Update(data, size);
  126. hasher->Finish(hash, sizeof(hash));
  127. const std::string hex_hash =
  128. base::ToLowerASCII(base::HexEncode(hash, sizeof(hash)));
  129. if (integrity.hash != hex_hash) {
  130. LOG(FATAL) << "Integrity check failed for asar archive ("
  131. << integrity.hash << " vs " << hex_hash << ")";
  132. }
  133. } else {
  134. LOG(FATAL) << "Unsupported hashing algorithm in ValidateIntegrityOrDie";
  135. }
  136. }
  137. } // namespace asar