mirror of https://github.com/oxen-io/session-ios
More logging, scrub phone numbers from file logs (#1357)
Phone number are still logged in TTY for development. // FREEBIEpull/1/head
parent
1433ee2655
commit
2ab6955967
@ -0,0 +1,10 @@
|
||||
// Created by Michael Kirk on 9/27/16.
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface OWSScrubbingLogFormatter : DDLogFileFormatterDefault
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,27 @@
|
||||
// Created by Michael Kirk on 9/27/16.
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSScrubbingLogFormatter.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@implementation OWSScrubbingLogFormatter
|
||||
|
||||
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
|
||||
{
|
||||
NSString *string = [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 ]"];
|
||||
|
||||
return filteredString;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
@ -0,0 +1,63 @@
|
||||
// Created by Michael Kirk on 9/27/16.
|
||||
// Copyright © 2016 Open Whisper Systems. All rights reserved.
|
||||
|
||||
#import "OWSScrubbingLogFormatter.h"
|
||||
#import <XCTest/XCTest.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface OWSScrubbingLogFormatterTest : XCTestCase
|
||||
|
||||
@end
|
||||
|
||||
@implementation OWSScrubbingLogFormatterTest
|
||||
|
||||
- (DDLogMessage *)messageWithString:(NSString *)string
|
||||
{
|
||||
return [[DDLogMessage alloc] initWithMessage:string
|
||||
level:DDLogLevelInfo
|
||||
flag:0
|
||||
context:0
|
||||
file:nil
|
||||
function:nil
|
||||
line:0
|
||||
tag:nil
|
||||
options:0
|
||||
timestamp:[NSDate new]];
|
||||
}
|
||||
|
||||
- (void)testPhoneNumbersScrubbed
|
||||
{
|
||||
NSArray<NSString *> *phoneStrings = @[
|
||||
@"+13331231234 ",
|
||||
@"+4113331231234",
|
||||
@"+13331231234 something something +13331231234",
|
||||
];
|
||||
|
||||
for (NSString *phoneString in phoneStrings) {
|
||||
OWSScrubbingLogFormatter *formatter = [OWSScrubbingLogFormatter new];
|
||||
NSString *actual = [formatter
|
||||
formatLogMessage:[self
|
||||
messageWithString:[NSString stringWithFormat:@"My phone number is %@", phoneString]]];
|
||||
|
||||
NSRange redactedRange = [actual rangeOfString:@"My phone number is [ REDACTED_PHONE_NUMBER ]"];
|
||||
XCTAssertNotEqual(NSNotFound, redactedRange.location, "Failed to redact phone string: %@", phoneString);
|
||||
|
||||
NSRange phoneNumberRange = [actual rangeOfString:phoneString];
|
||||
XCTAssertEqual(NSNotFound, phoneNumberRange.location, "Failed to redact phone string: %@", phoneString);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)testNonPhonenumberNotScrubbed
|
||||
{
|
||||
OWSScrubbingLogFormatter *formatter = [OWSScrubbingLogFormatter new];
|
||||
NSString *actual =
|
||||
[formatter formatLogMessage:[self messageWithString:[NSString stringWithFormat:@"Some unfiltered string"]]];
|
||||
|
||||
NSRange redactedRange = [actual rangeOfString:@"Some unfiltered string"];
|
||||
XCTAssertNotEqual(NSNotFound, redactedRange.location, "Shouldn't touch non phone string.");
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
Loading…
Reference in New Issue