FPRURLFilter.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebasePerformance/Sources/FPRURLFilter.h"
  15. #import "FirebasePerformance/Sources/FPRURLFilter_Private.h"
  16. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  17. #import "GoogleDataTransport/GDTCORLibrary/Internal/GoogleDataTransportInternal.h"
  18. /** The expected key of the domain allowlist array. */
  19. static NSString *const kFPRAllowlistDomainsKey = @"FPRWhitelistedDomains";
  20. /** Allowlist status enums. */
  21. typedef NS_ENUM(NSInteger, FPRURLAllowlistStatus) {
  22. /** No allowlist is present, so the URL will be allowed. */
  23. FPRURLAllowlistStatusDoesNotExist = 1,
  24. /** The URL is allowed. */
  25. FPRURLAllowlistStatusAllowed = 2,
  26. /** The URL is NOT allowed. */
  27. FPRURLAllowlistStatusNotAllowed = 3
  28. };
  29. /** Returns the set of denied URL strings.
  30. *
  31. * @return the set of denied URL strings.
  32. */
  33. NSSet<NSString *> *GetSystemDenyListURLStrings() {
  34. // The denylist of URLs for uploading events to avoid cyclic generation of those network events.
  35. static NSSet *denylist = nil;
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. denylist = [[NSSet alloc] initWithArray:@[
  39. [[GDTCOREndpoints uploadURLForTarget:kGDTCORTargetCCT] absoluteString],
  40. [[GDTCOREndpoints uploadURLForTarget:kGDTCORTargetFLL] absoluteString]
  41. ]];
  42. });
  43. return denylist;
  44. }
  45. @implementation FPRURLFilter
  46. + (instancetype)sharedInstance {
  47. static FPRURLFilter *sharedInstance;
  48. static dispatch_once_t onceToken;
  49. dispatch_once(&onceToken, ^{
  50. sharedInstance = [[FPRURLFilter alloc] initWithBundle:[NSBundle mainBundle]];
  51. });
  52. return sharedInstance;
  53. }
  54. - (instancetype)initWithBundle:(NSBundle *)bundle {
  55. self = [super init];
  56. if (self) {
  57. _mainBundle = bundle;
  58. _allowlistDomains = [self retrieveAllowlistFromPlist];
  59. }
  60. return self;
  61. }
  62. - (BOOL)shouldInstrumentURL:(NSString *)URL {
  63. if ([self isURLDeniedByTheSDK:URL]) {
  64. return NO;
  65. }
  66. FPRURLAllowlistStatus allowlistStatus = [self isURLAllowed:URL];
  67. if (allowlistStatus == FPRURLAllowlistStatusDoesNotExist) {
  68. return YES;
  69. }
  70. return allowlistStatus == FPRURLAllowlistStatusAllowed;
  71. }
  72. #pragma mark - Private helper methods
  73. /** Determines if the URL is denied by the SDK.
  74. *
  75. * @param URL the URL string to check.
  76. * @return YES if the URL is allowed by the SDK, NO otherwise.
  77. */
  78. - (BOOL)isURLDeniedByTheSDK:(NSString *)URL {
  79. BOOL shouldDenyURL = NO;
  80. for (NSString *denyListURL in GetSystemDenyListURLStrings()) {
  81. if ([URL hasPrefix:denyListURL]) {
  82. shouldDenyURL = YES;
  83. break;
  84. }
  85. }
  86. return shouldDenyURL;
  87. }
  88. /** Determines if the URL is allowed by the Developer.
  89. *
  90. * @param URL The URL string to check.
  91. * @return FPRURLAllowlistStatusAllowed if the URL is allowed,
  92. * FPRURLAllowlistStatusNotAllowed if the URL is not allowed, or
  93. * FPRURLAllowlistStatusDoesNotExist if the allowlist does not exist.
  94. */
  95. - (FPRURLAllowlistStatus)isURLAllowed:(NSString *)URL {
  96. if (self.allowlistDomains && !self.disablePlist) {
  97. for (NSString *allowlistDomain in self.allowlistDomains) {
  98. NSURLComponents *components = [[NSURLComponents alloc] initWithString:URL];
  99. if ([components.host containsString:allowlistDomain]) {
  100. return FPRURLAllowlistStatusAllowed;
  101. }
  102. }
  103. return FPRURLAllowlistStatusNotAllowed;
  104. }
  105. return FPRURLAllowlistStatusDoesNotExist;
  106. }
  107. /** Retrieves the allowlist from an Info.plist.
  108. *
  109. * @return An array of the allowlist values, or nil if the allowlist key is not found.
  110. */
  111. - (nullable NSArray<NSString *> *)retrieveAllowlistFromPlist {
  112. NSArray<NSString *> *allowlist = nil;
  113. id plistObject = [self.mainBundle objectForInfoDictionaryKey:kFPRAllowlistDomainsKey];
  114. if (!plistObject) {
  115. NSBundle *localBundle = [NSBundle bundleForClass:[self class]];
  116. plistObject = [localBundle objectForInfoDictionaryKey:kFPRAllowlistDomainsKey];
  117. }
  118. if ([plistObject isKindOfClass:[NSArray class]]) {
  119. FPRLogInfo(kFPRURLAllowlistingEnabled, @"A domain allowlist was detected. Domains not "
  120. "explicitly allowlisted will not be instrumented.");
  121. allowlist = plistObject;
  122. }
  123. return allowlist;
  124. }
  125. @end