1 |
efrain |
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 |
|
|
|
15 |
#import "FirebasePerformance/Sources/FPRURLFilter.h"
|
|
|
16 |
#import "FirebasePerformance/Sources/FPRURLFilter_Private.h"
|
|
|
17 |
|
|
|
18 |
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
|
|
|
19 |
|
|
|
20 |
#import <GoogleDataTransport/GoogleDataTransport.h>
|
|
|
21 |
|
|
|
22 |
/** The expected key of the domain allowlist array. */
|
|
|
23 |
static NSString *const kFPRAllowlistDomainsKey = @"FPRWhitelistedDomains";
|
|
|
24 |
|
|
|
25 |
/** Allowlist status enums. */
|
|
|
26 |
typedef NS_ENUM(NSInteger, FPRURLAllowlistStatus) {
|
|
|
27 |
|
|
|
28 |
/** No allowlist is present, so the URL will be allowed. */
|
|
|
29 |
FPRURLAllowlistStatusDoesNotExist = 1,
|
|
|
30 |
|
|
|
31 |
/** The URL is allowed. */
|
|
|
32 |
FPRURLAllowlistStatusAllowed = 2,
|
|
|
33 |
|
|
|
34 |
/** The URL is NOT allowed. */
|
|
|
35 |
FPRURLAllowlistStatusNotAllowed = 3
|
|
|
36 |
};
|
|
|
37 |
|
|
|
38 |
/** Returns the set of denied URL strings.
|
|
|
39 |
*
|
|
|
40 |
* @return the set of denied URL strings.
|
|
|
41 |
*/
|
|
|
42 |
NSSet<NSString *> *GetSystemDenyListURLStrings(void) {
|
|
|
43 |
// The denylist of URLs for uploading events to avoid cyclic generation of those network events.
|
|
|
44 |
static NSSet *denylist = nil;
|
|
|
45 |
static dispatch_once_t onceToken;
|
|
|
46 |
dispatch_once(&onceToken, ^{
|
|
|
47 |
denylist = [[NSSet alloc] initWithArray:@[
|
|
|
48 |
[[GDTCOREndpoints uploadURLForTarget:kGDTCORTargetCCT] absoluteString],
|
|
|
49 |
[[GDTCOREndpoints uploadURLForTarget:kGDTCORTargetFLL] absoluteString]
|
|
|
50 |
]];
|
|
|
51 |
});
|
|
|
52 |
return denylist;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
@implementation FPRURLFilter
|
|
|
56 |
|
|
|
57 |
+ (instancetype)sharedInstance {
|
|
|
58 |
static FPRURLFilter *sharedInstance;
|
|
|
59 |
static dispatch_once_t onceToken;
|
|
|
60 |
dispatch_once(&onceToken, ^{
|
|
|
61 |
sharedInstance = [[FPRURLFilter alloc] initWithBundle:[NSBundle mainBundle]];
|
|
|
62 |
});
|
|
|
63 |
return sharedInstance;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
- (instancetype)initWithBundle:(NSBundle *)bundle {
|
|
|
67 |
self = [super init];
|
|
|
68 |
if (self) {
|
|
|
69 |
_mainBundle = bundle;
|
|
|
70 |
_allowlistDomains = [self retrieveAllowlistFromPlist];
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
return self;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
- (BOOL)shouldInstrumentURL:(NSString *)URL {
|
|
|
77 |
if ([self isURLDeniedByTheSDK:URL]) {
|
|
|
78 |
return NO;
|
|
|
79 |
}
|
|
|
80 |
FPRURLAllowlistStatus allowlistStatus = [self isURLAllowed:URL];
|
|
|
81 |
if (allowlistStatus == FPRURLAllowlistStatusDoesNotExist) {
|
|
|
82 |
return YES;
|
|
|
83 |
}
|
|
|
84 |
return allowlistStatus == FPRURLAllowlistStatusAllowed;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
#pragma mark - Private helper methods
|
|
|
88 |
|
|
|
89 |
/** Determines if the URL is denied by the SDK.
|
|
|
90 |
*
|
|
|
91 |
* @param URL the URL string to check.
|
|
|
92 |
* @return YES if the URL is allowed by the SDK, NO otherwise.
|
|
|
93 |
*/
|
|
|
94 |
- (BOOL)isURLDeniedByTheSDK:(NSString *)URL {
|
|
|
95 |
BOOL shouldDenyURL = NO;
|
|
|
96 |
|
|
|
97 |
for (NSString *denyListURL in GetSystemDenyListURLStrings()) {
|
|
|
98 |
if ([URL hasPrefix:denyListURL]) {
|
|
|
99 |
shouldDenyURL = YES;
|
|
|
100 |
break;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
return shouldDenyURL;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/** Determines if the URL is allowed by the Developer.
|
|
|
108 |
*
|
|
|
109 |
* @param URL The URL string to check.
|
|
|
110 |
* @return FPRURLAllowlistStatusAllowed if the URL is allowed,
|
|
|
111 |
* FPRURLAllowlistStatusNotAllowed if the URL is not allowed, or
|
|
|
112 |
* FPRURLAllowlistStatusDoesNotExist if the allowlist does not exist.
|
|
|
113 |
*/
|
|
|
114 |
- (FPRURLAllowlistStatus)isURLAllowed:(NSString *)URL {
|
|
|
115 |
if (self.allowlistDomains && !self.disablePlist) {
|
|
|
116 |
for (NSString *allowlistDomain in self.allowlistDomains) {
|
|
|
117 |
NSURLComponents *components = [[NSURLComponents alloc] initWithString:URL];
|
|
|
118 |
if ([components.host containsString:allowlistDomain]) {
|
|
|
119 |
return FPRURLAllowlistStatusAllowed;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
return FPRURLAllowlistStatusNotAllowed;
|
|
|
123 |
}
|
|
|
124 |
return FPRURLAllowlistStatusDoesNotExist;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/** Retrieves the allowlist from an Info.plist.
|
|
|
128 |
*
|
|
|
129 |
* @return An array of the allowlist values, or nil if the allowlist key is not found.
|
|
|
130 |
*/
|
|
|
131 |
- (nullable NSArray<NSString *> *)retrieveAllowlistFromPlist {
|
|
|
132 |
NSArray<NSString *> *allowlist = nil;
|
|
|
133 |
id plistObject = [self.mainBundle objectForInfoDictionaryKey:kFPRAllowlistDomainsKey];
|
|
|
134 |
if (!plistObject) {
|
|
|
135 |
NSBundle *localBundle = [NSBundle bundleForClass:[self class]];
|
|
|
136 |
plistObject = [localBundle objectForInfoDictionaryKey:kFPRAllowlistDomainsKey];
|
|
|
137 |
}
|
|
|
138 |
if ([plistObject isKindOfClass:[NSArray class]]) {
|
|
|
139 |
FPRLogInfo(kFPRURLAllowlistingEnabled, @"A domain allowlist was detected. Domains not "
|
|
|
140 |
"explicitly allowlisted will not be instrumented.");
|
|
|
141 |
allowlist = plistObject;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
return allowlist;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
@end
|