| 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/FPRDataUtils.h"
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           #import "FirebasePerformance/Sources/Common/FPRConstants.h"
  | 
        
        
            | 
            | 
           18 | 
           #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  | 
        
        
            | 
            | 
           19 | 
              | 
        
        
            | 
            | 
           20 | 
           #pragma mark - Public functions
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           NSString *FPRReservableName(NSString *name) {
  | 
        
        
            | 
            | 
           23 | 
             NSString *reservableName =
  | 
        
        
            | 
            | 
           24 | 
                 [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  | 
        
        
            | 
            | 
           25 | 
             if ([reservableName hasPrefix:kFPRInternalNamePrefix]) {
  | 
        
        
            | 
            | 
           26 | 
               FPRLogError(kFPRClientNameReserved, @"%@ prefix is reserved. Dropped %@.",
  | 
        
        
            | 
            | 
           27 | 
                           kFPRInternalNamePrefix, reservableName);
  | 
        
        
            | 
            | 
           28 | 
               return nil;
  | 
        
        
            | 
            | 
           29 | 
             }
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
             if (reservableName.length == 0) {
  | 
        
        
            | 
            | 
           32 | 
               FPRLogError(kFPRClientNameLengthCheckFailed, @"Given name is empty.");
  | 
        
        
            | 
            | 
           33 | 
               return nil;
  | 
        
        
            | 
            | 
           34 | 
             }
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
             if (reservableName.length > kFPRMaxNameLength) {
  | 
        
        
            | 
            | 
           37 | 
               FPRLogError(kFPRClientNameLengthCheckFailed, @"%@ is greater than %d characters, dropping it.",
  | 
        
        
            | 
            | 
           38 | 
                           reservableName, kFPRMaxNameLength);
  | 
        
        
            | 
            | 
           39 | 
               return nil;
  | 
        
        
            | 
            | 
           40 | 
             }
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
             return reservableName;
  | 
        
        
            | 
            | 
           43 | 
           }
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           NSString *FPRReservableAttributeName(NSString *name) {
  | 
        
        
            | 
            | 
           46 | 
             NSString *reservableName =
  | 
        
        
            | 
            | 
           47 | 
                 [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
             static NSArray<NSString *> *reservedPrefix = nil;
  | 
        
        
            | 
            | 
           50 | 
             static NSPredicate *characterCheck = nil;
  | 
        
        
            | 
            | 
           51 | 
             static dispatch_once_t onceToken;
  | 
        
        
            | 
            | 
           52 | 
             dispatch_once(&onceToken, ^{
  | 
        
        
            | 
            | 
           53 | 
               reservedPrefix = @[ @"firebase_", @"google_", @"ga_" ];
  | 
        
        
            | 
            | 
           54 | 
               NSString *characterRegex = @"[A-Z0-9a-z_]*";
  | 
        
        
            | 
            | 
           55 | 
               characterCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", characterRegex];
  | 
        
        
            | 
            | 
           56 | 
             });
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
             __block BOOL containsReservedPrefix = NO;
  | 
        
        
            | 
            | 
           59 | 
             [reservedPrefix enumerateObjectsUsingBlock:^(NSString *prefix, NSUInteger idx, BOOL *stop) {
  | 
        
        
            | 
            | 
           60 | 
               if ([reservableName hasPrefix:prefix]) {
  | 
        
        
            | 
            | 
           61 | 
                 FPRLogError(kFPRClientNameReserved, @"%@ prefix is reserved. Dropped %@.", prefix,
  | 
        
        
            | 
            | 
           62 | 
                             reservableName);
  | 
        
        
            | 
            | 
           63 | 
                 *stop = YES;
  | 
        
        
            | 
            | 
           64 | 
                 containsReservedPrefix = YES;
  | 
        
        
            | 
            | 
           65 | 
               }
  | 
        
        
            | 
            | 
           66 | 
             }];
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
             if (containsReservedPrefix) {
  | 
        
        
            | 
            | 
           69 | 
               return nil;
  | 
        
        
            | 
            | 
           70 | 
             }
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
             if (reservableName.length == 0) {
  | 
        
        
            | 
            | 
           73 | 
               FPRLogError(kFPRClientNameLengthCheckFailed, @"Given name is empty.");
  | 
        
        
            | 
            | 
           74 | 
               return nil;
  | 
        
        
            | 
            | 
           75 | 
             }
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
             if ([characterCheck evaluateWithObject:reservableName] == NO) {
  | 
        
        
            | 
            | 
           78 | 
               FPRLogError(kFPRAttributeNameIllegalCharacters,
  | 
        
        
            | 
            | 
           79 | 
                           @"Illegal characters used for attribute name, "
  | 
        
        
            | 
            | 
           80 | 
                            "characters allowed are alphanumeric or underscore.");
  | 
        
        
            | 
            | 
           81 | 
               return nil;
  | 
        
        
            | 
            | 
           82 | 
             }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
             if (reservableName.length > kFPRMaxAttributeNameLength) {
  | 
        
        
            | 
            | 
           85 | 
               FPRLogError(kFPRClientNameLengthCheckFailed, @"%@ is greater than %d characters, dropping it.",
  | 
        
        
            | 
            | 
           86 | 
                           reservableName, kFPRMaxAttributeNameLength);
  | 
        
        
            | 
            | 
           87 | 
               return nil;
  | 
        
        
            | 
            | 
           88 | 
             }
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
             return reservableName;
  | 
        
        
            | 
            | 
           91 | 
           }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
           NSString *FPRValidatedAttributeValue(NSString *value) {
  | 
        
        
            | 
            | 
           94 | 
             if (value.length == 0) {
  | 
        
        
            | 
            | 
           95 | 
               FPRLogError(kFPRClientNameLengthCheckFailed, @"Given value is empty.");
  | 
        
        
            | 
            | 
           96 | 
               return nil;
  | 
        
        
            | 
            | 
           97 | 
             }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
             if (value.length > kFPRMaxAttributeValueLength) {
  | 
        
        
            | 
            | 
           100 | 
               FPRLogError(kFPRClientNameLengthCheckFailed, @"%@ is greater than %d characters, dropping it.",
  | 
        
        
            | 
            | 
           101 | 
                           value, kFPRMaxAttributeValueLength);
  | 
        
        
            | 
            | 
           102 | 
               return nil;
  | 
        
        
            | 
            | 
           103 | 
             }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
             return value;
  | 
        
        
            | 
            | 
           106 | 
           }
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
           NSString *FPRTruncatedURLString(NSString *URLString) {
  | 
        
        
            | 
            | 
           109 | 
             NSString *truncatedURLString = URLString;
  | 
        
        
            | 
            | 
           110 | 
             NSString *pathSeparator = @"/";
  | 
        
        
            | 
            | 
           111 | 
             if (truncatedURLString.length > kFPRMaxURLLength) {
  | 
        
        
            | 
            | 
           112 | 
               NSString *truncationCharacter =
  | 
        
        
            | 
            | 
           113 | 
                   [truncatedURLString substringWithRange:NSMakeRange(kFPRMaxURLLength, 1)];
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
               truncatedURLString = [URLString substringToIndex:kFPRMaxURLLength];
  | 
        
        
            | 
            | 
           116 | 
               if (![pathSeparator isEqual:truncationCharacter]) {
  | 
        
        
            | 
            | 
           117 | 
                 NSRange rangeOfTruncation = [truncatedURLString rangeOfString:pathSeparator
  | 
        
        
            | 
            | 
           118 | 
                                                                       options:NSBackwardsSearch];
  | 
        
        
            | 
            | 
           119 | 
                 truncatedURLString = [URLString substringToIndex:rangeOfTruncation.location];
  | 
        
        
            | 
            | 
           120 | 
               }
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
               FPRLogWarning(kFPRClientNameTruncated, @"URL exceeds %d characters. Truncated url: %@",
  | 
        
        
            | 
            | 
           123 | 
                             kFPRMaxURLLength, truncatedURLString);
  | 
        
        
            | 
            | 
           124 | 
             }
  | 
        
        
            | 
            | 
           125 | 
             return truncatedURLString;
  | 
        
        
            | 
            | 
           126 | 
           }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
           NSString *FPRValidatedMccMnc(NSString *mcc, NSString *mnc) {
  | 
        
        
            | 
            | 
           129 | 
             if ([mcc length] != 3 || [mnc length] < 2 || [mnc length] > 3) return nil;
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
             static NSCharacterSet *notDigits;
  | 
        
        
            | 
            | 
           132 | 
             static dispatch_once_t token;
  | 
        
        
            | 
            | 
           133 | 
             dispatch_once(&token, ^{
  | 
        
        
            | 
            | 
           134 | 
               notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
  | 
        
        
            | 
            | 
           135 | 
             });
  | 
        
        
            | 
            | 
           136 | 
             NSString *mccMnc = [mcc stringByAppendingString:mnc];
  | 
        
        
            | 
            | 
           137 | 
             if ([mccMnc rangeOfCharacterFromSet:notDigits].location != NSNotFound) return nil;
  | 
        
        
            | 
            | 
           138 | 
             return mccMnc;
  | 
        
        
            | 
            | 
           139 | 
           }
  |