You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SignalMessaging/utils/OWSScrubbingLogFormatter.m

94 lines
3.7 KiB
Matlab

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
#import "OWSScrubbingLogFormatter.h"
NS_ASSUME_NONNULL_BEGIN
@implementation OWSScrubbingLogFormatter
- (NSRegularExpression *)phoneRegex
{
static NSRegularExpression *regex = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSError *error;
regex = [NSRegularExpression regularExpressionWithPattern:@"\\+\\d{7,12}(\\d{3})"
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error || !regex) {
OWSFail(@"%@ could not compile regular expression: %@", self.logTag, error);
}
});
return regex;
}
- (NSRegularExpression *)dataRegex
{
static NSRegularExpression *regex = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSError *error;
regex = [NSRegularExpression regularExpressionWithPattern:@"<([\\da-f]{2})[\\da-f]{6}( [\\da-f]{8})*>"
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error || !regex) {
OWSFail(@"%@ could not compile regular expression: %@", self.logTag, error);
}
});
return regex;
}
- (NSRegularExpression *)ipAddressRegex
{
static NSRegularExpression *regex = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Match IPv4 and IPv6 addresses.
//
// NOTE: the second group matches the last "quad/hex?" of the IPv4/IPv6 address.
NSError *error;
regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+\\.\\d+\\.)?\\d+\\.\\d+\\.\\d+\\.(\\d+)"
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error || !regex) {
OWSFail(@"%@ could not compile regular expression: %@", self.logTag, error);
}
});
return regex;
}
- (NSString *__nullable)formatLogMessage:(DDLogMessage *)logMessage
{
NSString *logString = [super formatLogMessage:logMessage];
NSRegularExpression *phoneRegex = self.phoneRegex;
logString = [phoneRegex stringByReplacingMatchesInString:logString
options:0
range:NSMakeRange(0, [logString length])
withTemplate:@"[ REDACTED_PHONE_NUMBER:xxx$1 ]"];
// We capture only the first two characters of the hex string for logging.
// example log line: "Called someFunction with nsData: <01234567 89abcdef>"
// scrubbed output: "Called someFunction with nsData: [ REDACTED_DATA:01 ]"
NSRegularExpression *dataRegex = self.dataRegex;
logString = [dataRegex stringByReplacingMatchesInString:logString
options:0
range:NSMakeRange(0, [logString length])
withTemplate:@"[ REDACTED_DATA:$1... ]"];
NSRegularExpression *ipAddressRegex = self.ipAddressRegex;
logString = [ipAddressRegex stringByReplacingMatchesInString:logString
options:0
range:NSMakeRange(0, [logString length])
withTemplate:@"[ REDACTED_IP_ADDRESS:...$2 ]"];
return logString;
}
@end
NS_ASSUME_NONNULL_END