archive_mac.mm 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2021 Slack Technologies, 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/archive.h"
  5. #include <CommonCrypto/CommonDigest.h>
  6. #include <CoreFoundation/CoreFoundation.h>
  7. #include <Foundation/Foundation.h>
  8. #include <iomanip>
  9. #include <string>
  10. #include "base/files/file_util.h"
  11. #include "base/logging.h"
  12. #include "base/mac/bundle_locations.h"
  13. #include "base/mac/foundation_util.h"
  14. #include "base/mac/scoped_cftyperef.h"
  15. #include "base/strings/sys_string_conversions.h"
  16. #include "shell/common/asar/asar_util.h"
  17. namespace asar {
  18. absl::optional<base::FilePath> Archive::RelativePath() const {
  19. base::FilePath bundle_path = base::MakeAbsoluteFilePath(
  20. base::mac::MainBundlePath().Append("Contents"));
  21. base::FilePath relative_path;
  22. if (!bundle_path.AppendRelativePath(path_, &relative_path))
  23. return absl::nullopt;
  24. return relative_path;
  25. }
  26. absl::optional<IntegrityPayload> Archive::HeaderIntegrity() const {
  27. absl::optional<base::FilePath> relative_path = RelativePath();
  28. // Callers should have already asserted this
  29. CHECK(relative_path.has_value());
  30. NSDictionary* integrity = [[NSBundle mainBundle]
  31. objectForInfoDictionaryKey:@"ElectronAsarIntegrity"];
  32. // Integrity not provided
  33. if (!integrity)
  34. return absl::nullopt;
  35. NSString* ns_relative_path =
  36. base::mac::FilePathToNSString(relative_path.value());
  37. NSDictionary* integrity_payload = [integrity objectForKey:ns_relative_path];
  38. if (!integrity_payload)
  39. return absl::nullopt;
  40. NSString* algorithm = [integrity_payload objectForKey:@"algorithm"];
  41. NSString* hash = [integrity_payload objectForKey:@"hash"];
  42. if (algorithm && hash && [algorithm isEqualToString:@"SHA256"]) {
  43. IntegrityPayload header_integrity;
  44. header_integrity.algorithm = HashAlgorithm::SHA256;
  45. header_integrity.hash = base::SysNSStringToUTF8(hash);
  46. return header_integrity;
  47. }
  48. return absl::nullopt;
  49. }
  50. } // namespace asar