test_support_mac.mm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/services/quarantine/test_support.h"
  5. #import <ApplicationServices/ApplicationServices.h>
  6. #import <Foundation/Foundation.h>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/mac/scoped_nsobject.h"
  10. #include "base/strings/sys_string_conversions.h"
  11. #include "base/threading/scoped_blocking_call.h"
  12. #include "components/services/quarantine/common.h"
  13. #include "components/services/quarantine/common_mac.h"
  14. #include "url/gurl.h"
  15. namespace quarantine {
  16. // On macOS 12.4+ the LSQuarantineDataURL and LSQuarantineOriginURL keys are
  17. // ignored by LaunchServices. This function will ensure the file has quarantine
  18. // data set with kLSQuarantineAgentBundleIdentifierKey mapping to any string.
  19. // The LSQuarantineDataURL and LSQuarantineOriginURL are treated as optional. If
  20. // the file has them set, the function will try to match the expected values.
  21. // Otherwise the URL matching check will be skipped.
  22. bool IsFileQuarantined(const base::FilePath& file,
  23. const GURL& expected_source_url_unsafe,
  24. const GURL& expected_referrer_url_unsafe) {
  25. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  26. base::BlockingType::MAY_BLOCK);
  27. if (!base::PathExists(file)) {
  28. return false;
  29. }
  30. base::scoped_nsobject<NSMutableDictionary> properties;
  31. bool success = GetQuarantineProperties(file, &properties);
  32. if (!success || !properties) {
  33. return false;
  34. }
  35. // The agent bundle id must always be set.
  36. NSString* bundle_id =
  37. [properties valueForKey:(NSString*)kLSQuarantineAgentBundleIdentifierKey];
  38. if (!bundle_id.length) {
  39. return false;
  40. }
  41. // The source and referrer URLs are optional.
  42. GURL expected_source_url =
  43. SanitizeUrlForQuarantine(expected_source_url_unsafe);
  44. NSString* source_url =
  45. [[properties valueForKey:(NSString*)kLSQuarantineDataURLKey] description];
  46. if (expected_source_url.is_valid() && source_url.length) {
  47. if (![source_url isEqualToString:base::SysUTF8ToNSString(
  48. expected_source_url.spec())]) {
  49. return false;
  50. }
  51. }
  52. GURL expected_referrer_url =
  53. SanitizeUrlForQuarantine(expected_referrer_url_unsafe);
  54. NSString* referrer_url = [[properties
  55. valueForKey:(NSString*)kLSQuarantineOriginURLKey] description];
  56. if (expected_referrer_url.is_valid() && referrer_url.length) {
  57. if (![referrer_url isEqualToString:base::SysUTF8ToNSString(
  58. expected_referrer_url.spec())]) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. } // namespace quarantine