Merge branch 'mkirk/scrub-log-data'

This commit is contained in:
Michael Kirk 2017-07-12 16:13:49 -04:00
commit 3769ce8337
2 changed files with 49 additions and 6 deletions

View file

@ -10,17 +10,32 @@ NS_ASSUME_NONNULL_BEGIN
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
{
NSString *string = [super formatLogMessage:logMessage];
NSString *logString = [super formatLogMessage:logMessage];
NSRegularExpression *phoneRegex =
[NSRegularExpression regularExpressionWithPattern:@"\\+\\d{7,12}(\\d{3})"
options:NSRegularExpressionCaseInsensitive
error:nil];
NSString *filteredString = [phoneRegex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:@"[ REDACTED_PHONE_NUMBER:xxx$1 ]"];
return filteredString;
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 =
[NSRegularExpression regularExpressionWithPattern:@"<([\\da-f]{2})[\\da-f]{6}( [\\da-f]{8})*>"
options:NSRegularExpressionCaseInsensitive
error:nil];
logString = [dataRegex stringByReplacingMatchesInString:logString
options:0
range:NSMakeRange(0, [logString length])
withTemplate:@"[ REDACTED_DATA:$1... ]"];
return logString;
}
@end

View file

@ -27,6 +27,34 @@ NS_ASSUME_NONNULL_BEGIN
timestamp:[NSDate new]];
}
- (void)testDataScrubbed
{
NSDictionary<NSString *, NSString *> *expectedOutputs = @{
@"<01234567 89a23def 23234567 89ab1234>" : @"[ REDACTED_DATA:01... ]",
@"My data is: <01234567 89a23def 23234567 89ab1223>" : @"My data is: [ REDACTED_DATA:01... ]",
@"My data is <12345670 89a23def 23234567 89ab1223> their data is <87654321 89ab1234>" :
@"My data is [ REDACTED_DATA:12... ] their data is [ REDACTED_DATA:87... ]"
};
OWSScrubbingLogFormatter *formatter = [OWSScrubbingLogFormatter new];
// Other formatters add a dynamic date prefix to log lines. We truncate that when comparing our expected output.
NSUInteger datePrefixLength = [formatter formatLogMessage:[self messageWithString:@""]].length;
for (NSString *input in expectedOutputs) {
NSString *rawActual = [formatter formatLogMessage:[self messageWithString:input]];
// strip out dynamic date portion of log line
NSString *actual =
[rawActual substringWithRange:NSMakeRange(datePrefixLength, rawActual.length - datePrefixLength)];
NSString *expected = expectedOutputs[input];
XCTAssertEqualObjects(expected, actual);
}
}
- (void)testPhoneNumbersScrubbed
{
NSArray<NSString *> *phoneStrings = @[