Просмотр исходного кода

Only log error domain and code

The error domain and code should provide enough information to
diagnose an error. So log these values instead of attempting to
redact any sensitive information from the localized description.
mirokuratczyk 5 лет назад
Родитель
Сommit
2b7325b68e

+ 0 - 4
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel.xcodeproj/project.pbxproj

@@ -47,7 +47,6 @@
 		CE4616C02539493600D1243E /* Reachability+HasNetworkConnectivity.m in Sources */ = {isa = PBXBuildFile; fileRef = CE4616BE2539493600D1243E /* Reachability+HasNetworkConnectivity.m */; };
 		CE9549F525C8AAEE00F9AF86 /* Redactor.h in Headers */ = {isa = PBXBuildFile; fileRef = CE9549F325C8AAEE00F9AF86 /* Redactor.h */; };
 		CE9549F625C8AAEE00F9AF86 /* Redactor.m in Sources */ = {isa = PBXBuildFile; fileRef = CE9549F425C8AAEE00F9AF86 /* Redactor.m */; };
-		CE9549F925C98CA100F9AF86 /* RedactorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CE9549F825C98CA100F9AF86 /* RedactorTests.m */; };
 		CEC229FC24F047E700534D04 /* PsiphonProviderNoticeHandlerShim.h in Headers */ = {isa = PBXBuildFile; fileRef = CEC229FA24F047E700534D04 /* PsiphonProviderNoticeHandlerShim.h */; };
 		CEC229FD24F047E700534D04 /* PsiphonProviderNoticeHandlerShim.m in Sources */ = {isa = PBXBuildFile; fileRef = CEC229FB24F047E700534D04 /* PsiphonProviderNoticeHandlerShim.m */; };
 		CECF01442538D34100CD3E5C /* IPv6Synthesizer.h in Headers */ = {isa = PBXBuildFile; fileRef = CECF01422538D34100CD3E5C /* IPv6Synthesizer.h */; };
@@ -131,7 +130,6 @@
 		CE4616BE2539493600D1243E /* Reachability+HasNetworkConnectivity.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "Reachability+HasNetworkConnectivity.m"; sourceTree = "<group>"; };
 		CE9549F325C8AAEE00F9AF86 /* Redactor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Redactor.h; sourceTree = "<group>"; };
 		CE9549F425C8AAEE00F9AF86 /* Redactor.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Redactor.m; sourceTree = "<group>"; };
-		CE9549F825C98CA100F9AF86 /* RedactorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RedactorTests.m; sourceTree = "<group>"; };
 		CEC229FA24F047E700534D04 /* PsiphonProviderNoticeHandlerShim.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PsiphonProviderNoticeHandlerShim.h; sourceTree = "<group>"; };
 		CEC229FB24F047E700534D04 /* PsiphonProviderNoticeHandlerShim.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PsiphonProviderNoticeHandlerShim.m; sourceTree = "<group>"; };
 		CECF01422538D34100CD3E5C /* IPv6Synthesizer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IPv6Synthesizer.h; sourceTree = "<group>"; };
@@ -250,7 +248,6 @@
 			isa = PBXGroup;
 			children = (
 				66BDB02E1DA6BFCC0079384C /* PsiphonTunnelTests.m */,
-				CE9549F825C98CA100F9AF86 /* RedactorTests.m */,
 				66BDB0301DA6BFCC0079384C /* Info.plist */,
 			);
 			path = PsiphonTunnelTests;
@@ -517,7 +514,6 @@
 			buildActionMask = 2147483647;
 			files = (
 				66BDB02F1DA6BFCC0079384C /* PsiphonTunnelTests.m in Sources */,
-				CE9549F925C98CA100F9AF86 /* RedactorTests.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 4 - 10
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m

@@ -633,9 +633,8 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
     // Note: this deprecates the "DataStoreDirectory" config field.
     NSURL *defaultDataRootDirectoryURL = [PsiphonTunnel defaultDataRootDirectoryWithError:&err];
     if (err != nil) {
-        NSString *redactedErr = [Redactor stripFilePaths:err.localizedDescription];
         NSString *s = [NSString stringWithFormat:@"Unable to get defaultDataRootDirectoryURL: %@",
-                       redactedErr];
+                       [Redactor errorDescription:err]];
         *outError = [NSError errorWithDomain:PsiphonTunnelErrorDomain
                                         code:PsiphonTunnelErrorCodeConfigError
                                     userInfo:@{NSLocalizedDescriptionKey:s}];
@@ -651,10 +650,8 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
                                attributes:nil
                                     error:&err];
         if (err != nil) {
-            NSString *redactedErr = [Redactor stripFilePaths:err.localizedDescription
-                                               withFilePaths:@[defaultDataRootDirectoryURL.path]];
             NSString *s = [NSString stringWithFormat:@"Unable to create defaultRootDirectoryURL: %@",
-                           redactedErr];
+                           [Redactor errorDescription:err]];
             *outError = [NSError errorWithDomain:PsiphonTunnelErrorDomain
                                             code:PsiphonTunnelErrorCodeConfigError
                                         userInfo:@{NSLocalizedDescriptionKey:s}];
@@ -672,9 +669,7 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
 
     BOOL succeeded = [Backups excludeFileFromBackup:dataRootDirectory.path err:&err];
     if (!succeeded) {
-        NSString *redactedErr = [Redactor stripFilePaths:err.localizedDescription
-                                           withFilePaths:@[dataRootDirectory.path]];
-        logMessage([NSString stringWithFormat:@"Failed to exclude data root directory from backup: %@", redactedErr]);
+        logMessage([NSString stringWithFormat:@"Failed to exclude data root directory from backup: %@", [Redactor errorDescription:err]]);
     } else {
         logMessage(@"Excluded data root directory from backup");
     }
@@ -685,8 +680,7 @@ typedef NS_ERROR_ENUM(PsiphonTunnelErrorDomain, PsiphonTunnelErrorCode) {
 
     NSURL *libraryURL = [PsiphonTunnel libraryURLWithError:&err];
     if (err != nil) {
-        NSString *redactedErr = [Redactor stripFilePaths:err.localizedDescription];
-        NSString *s = [NSString stringWithFormat:@"Unable to get Library URL: %@", redactedErr];
+        NSString *s = [NSString stringWithFormat:@"Unable to get Library URL: %@", [Redactor errorDescription:err]];
         *outError = [NSError errorWithDomain:PsiphonTunnelErrorDomain
                                         code:PsiphonTunnelErrorCodeConfigError
                                     userInfo:@{NSLocalizedDescriptionKey:s}];

+ 3 - 16
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Utils/Redactor.h

@@ -24,22 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
 /// Redactor implements a set of functions to redact sensitive values from data.
 @interface Redactor : NSObject
 
-/// Returns a redacted copy of the provided string where any file paths are replaced with "[redacted]". For example,
-/// the input "prefix /a/b/c suffix" will result in the return value "prefix [redacted] suffix".
-/// @warning An attempt is made to redact file paths, but there is no guarantee that all file path schemes on the
-/// given system will be caught.
-/// @param s The string to redact.
-/// @return A copy of the provided string with any file paths redacted.
-+ (NSString*)stripFilePaths:(NSString*)s;
-
-/// Version of stripFilePaths which first replaces any occurrences of the provided file paths in the input string with
-/// "[redacted]".
-/// @warning An attempt is made to redact file paths, but there is no guarantee that all file path schemes on the given
-/// system will be caught.
-/// @param s The string to redact.
-/// @param filePaths File paths to redact directly.
-/// @return A copy of the provided string with any file paths redacted.
-+ (NSString*)stripFilePaths:(NSString*)s withFilePaths:(NSArray<NSString*>*_Nullable)filePaths;
+/// Returns a formatted string containing the error's domain and code. This includes the domain and code
+/// of any underlying errors.
++ (NSString *)errorDescription:(NSError *)error;
 
 @end
 

+ 12 - 36
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/Utils/Redactor.m

@@ -21,43 +21,19 @@
 
 @implementation Redactor
 
-+ (NSString*)stripFilePaths:(NSString*)s {
-    return [Redactor stripFilePaths:s withFilePaths:nil];
-}
-
-+ (NSString*)stripFilePaths:(NSString*)s withFilePaths:(NSArray<NSString*>*)filePaths {
-
-    NSMutableString *ret = [s mutableCopy];
-    NSRange replaceRange = NSMakeRange(0, [ret length]);
-
-    for (NSString *filePath in filePaths) {
-        [ret replaceOccurrencesOfString:filePath withString:@"[redacted]"
-                                options:kNilOptions range:replaceRange];
++ (NSString *)errorDescription:(NSError *)error {
+    NSError *_Nullable underlyingError = error.userInfo[NSUnderlyingErrorKey];
+
+    if (underlyingError != nil) {
+        return [NSString stringWithFormat:@"NSError(domain:%@, code:%ld, underlyingError:%@)",
+                error.domain,
+                (long)error.code,
+                [Redactor errorDescription:underlyingError]];
+    } else {
+        return [NSString stringWithFormat:@"NSError(domain:%@, code:%ld)",
+                error.domain,
+                (long)error.code];
     }
-
-    NSString *filePathRegex =
-        // File path
-        @"("
-            // Leading characters
-            @"[^ ]*"
-            // At least one path separator
-            @"/"
-            // Path component; take until next space
-            @"[^ ]*"
-        @")+";
-
-    NSError *err = nil;
-    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:filePathRegex
-                                                                           options:kNilOptions
-                                                                             error:&err];
-    if (err != nil) {
-        [NSException raise:@"Regex compile failed" format:@"failed to compile %@", filePathRegex];
-    }
-
-    NSRange searchRange = NSMakeRange(0, [ret length]);
-    [regex replaceMatchesInString:ret options:kNilOptions range:searchRange withTemplate:@"[redacted]"];
-
-    return ret;
 }
 
 @end

+ 0 - 59
MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnelTests/RedactorTests.m

@@ -1,59 +0,0 @@
-/*
- * Copyright (c) 2021, Psiphon Inc.
- * All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-
-#import <XCTest/XCTest.h>
-
-#import "Redactor.h"
-
-@interface RedactorTests : XCTestCase
-
-@end
-
-@implementation RedactorTests
-
-- (void)testRedactor {
-    [self run:@"prefix /a suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-    [self run:@"prefix /a/b/c/d suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-    [self run:@"prefix ./a/b/c/d suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-    [self run:@"prefix a/b/c/d suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-    [self run:@"prefix ../a/b/c/d/../ suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-    [self run:@"prefix ~/a/b/c/d suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-    [self run:@"prefix /a/b c/d suffix" filePaths:nil expect:@"prefix [redacted] [redacted] suffix"];
-    [self run:@"prefix /a/b%20c/d suffix" filePaths:nil expect:@"prefix [redacted] suffix"];
-
-    // Unhandled case
-    [self run:@"prefix /a/file name with spaces /e/f/g/ suffix"
-    filePaths:nil
-       expect:@"prefix [redacted] name with spaces [redacted] suffix"];
-
-    // Handle unhandled case
-    [self run:@"prefix /a/file name with spaces /e/f/g/ suffix"
-    filePaths:@[@"/a/file name with spaces"]
-       expect:@"prefix [redacted] [redacted] suffix"];
-}
-
-- (void)run:(NSString*)input filePaths:(NSArray<NSString*>*)filePaths expect:(NSString*)expect {
-    NSString *redacted = [Redactor stripFilePaths:input withFilePaths:filePaths];
-    if ([redacted isEqualToString:expect] == false) {
-        XCTFail(@"Error: \"%@\" not equal to expect value \"%@\"", redacted, expect);
-    }
-}
-
-@end