From b8fecf1f36515b100107dcc04a1754e80c41d0fa Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 12 Mar 2016 18:21:07 -0500 Subject: [PATCH 1/5] Fix issues with unit tests I added public DSA file, automatic update checking, disabled system profiling in the unit test's info.plist so that SUUpdater won't complain that DSA is missing, and it won't prompt for update permission the first time. I also made SUUpdater use the correct bundle in SUUpdaterTest --- Tests/SUCodeSigningVerifierTest.m | 31 ++++++++++++++++++++----------- Tests/SUUpdaterTest.m | 2 +- Tests/SparkleTests-Info.plist | 6 ++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Tests/SUCodeSigningVerifierTest.m b/Tests/SUCodeSigningVerifierTest.m index a2b7e764..43f91d7e 100644 --- a/Tests/SUCodeSigningVerifierTest.m +++ b/Tests/SUCodeSigningVerifierTest.m @@ -10,6 +10,7 @@ #import #import "NTSynchronousTask.h" #import "SUCodeSigningVerifier.h" +#import "SUFileManager.h" @interface SUCodeSigningVerifierTest : XCTestCase @@ -32,11 +33,24 @@ NSBundle *unitTestBundle = [NSBundle bundleForClass:[self class]]; NSString *unitTestBundleIdentifier = unitTestBundle.bundleIdentifier; NSString *zippedAppPath = [unitTestBundle pathForResource:@"SparkleTestCodeSignApp" ofType:@"zip"]; - NSString *tempDir = [NSTemporaryDirectory() stringByAppendingPathComponent:unitTestBundleIdentifier]; + + SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; + + NSError *tempError = nil; + NSURL *tempDir = [fileManager makeTemporaryDirectoryWithPreferredName:unitTestBundleIdentifier appropriateForDirectoryURL:[NSURL fileURLWithPath:zippedAppPath] error:&tempError]; + + if (tempDir == nil) { + XCTFail(@"Failed to create temporary directory with error: %@", tempError); + return; + } + + NSString *tempDirPath = tempDir.path; + XCTAssertNotNil(tempDirPath); + NSError *error = nil; - if ([[NSFileManager defaultManager] createDirectoryAtPath:tempDir withIntermediateDirectories:YES attributes:nil error:&error]) { - if ([self unzip:zippedAppPath toPath:tempDir]) { - self.notSignedAppPath = [tempDir stringByAppendingPathComponent:@"SparkleTestCodeSignApp.app"]; + if ([[NSFileManager defaultManager] createDirectoryAtPath:tempDirPath withIntermediateDirectories:YES attributes:nil error:&error]) { + if ([self unzip:zippedAppPath toPath:tempDirPath]) { + self.notSignedAppPath = [tempDirPath stringByAppendingPathComponent:@"SparkleTestCodeSignApp.app"]; [self setupValidSignedApp]; [self setupInvalidSignedApp]; } @@ -54,13 +68,8 @@ [super tearDown]; if (self.notSignedAppPath) { - [[NSFileManager defaultManager] removeItemAtPath:self.notSignedAppPath error:nil]; - } - if (self.validSignedAppPath) { - [[NSFileManager defaultManager] removeItemAtPath:self.validSignedAppPath error:nil]; - } - if (self.invalidSignedAppPath) { - [[NSFileManager defaultManager] removeItemAtPath:self.invalidSignedAppPath error:nil]; + NSString *tempDir = [self.notSignedAppPath stringByDeletingLastPathComponent]; + [[NSFileManager defaultManager] removeItemAtPath:tempDir error:nil]; } } diff --git a/Tests/SUUpdaterTest.m b/Tests/SUUpdaterTest.m index 0e7d6e76..0ff7e930 100644 --- a/Tests/SUUpdaterTest.m +++ b/Tests/SUUpdaterTest.m @@ -24,7 +24,7 @@ { [super setUp]; self.queue = [[NSOperationQueue alloc] init]; - self.updater = [[SUUpdater alloc] init]; + self.updater = [[SUUpdater alloc] initForBundle:[NSBundle bundleForClass:[self class]]]; self.updater.delegate = self; } diff --git a/Tests/SparkleTests-Info.plist b/Tests/SparkleTests-Info.plist index 460a7d93..e7e83b9d 100644 --- a/Tests/SparkleTests-Info.plist +++ b/Tests/SparkleTests-Info.plist @@ -16,5 +16,11 @@ ???? CFBundleVersion 1.0 + SUEnableAutomaticChecks + + SUEnableSystemProfiling + + SUPublicDSAKeyFile + test-pubkey.pem From d263bf4fd00f79a7643feefc8e16ced7f0e4dae2 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 9 Apr 2016 22:33:17 -0400 Subject: [PATCH 2/5] Don't follow symbolic links for file operations This fixes issues with updating modification/access time, and changing owner/group where symbolic links used to be followed. Symbolic link tests are included. --- Sparkle/SUFileManager.m | 31 ++++++++++++-- Tests/SUFileManagerTest.swift | 81 +++++++++++++++++++++++++++-------- 2 files changed, 91 insertions(+), 21 deletions(-) diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index f13e3ffd..4e85fcae 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -548,7 +548,19 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return NO; } - if (chown(path, ownerID.unsignedIntValue, groupID.unsignedIntValue) != 0) { + int fileDescriptor = open(path, O_RDONLY | O_SYMLINK); + if (fileDescriptor == -1) { + if (error != NULL) { + *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open file descriptor to %@", targetURL.path.lastPathComponent] }]; + } + return NO; + } + + // We use fchown instead of chown because the latter can follow symbolic links + BOOL success = fchown(fileDescriptor, ownerID.unsignedIntValue, groupID.unsignedIntValue) == 0; + close(fileDescriptor); + + if (!success) { if (errno == EPERM) { if (needsAuth != NULL) { *needsAuth = YES; @@ -701,7 +713,20 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return NO; } - if (utimes(path, NULL) == 0) { + int fileDescriptor = open(path, O_RDONLY | O_SYMLINK); + if (fileDescriptor == -1) { + if (error != NULL) { + *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to open file descriptor to %@", targetURL.path.lastPathComponent] }]; + } + return NO; + } + + // Using futimes() because utimes() follows symbolic links + BOOL updatedTime = (futimes(fileDescriptor, NULL) == 0); + + close(fileDescriptor); + + if (updatedTime) { return YES; } @@ -716,7 +741,7 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return NO; } - BOOL success = AuthorizationExecuteWithPrivilegesAndWait(_auth, "/usr/bin/touch", kAuthorizationFlagDefaults, (char *[]){ "--", path, NULL }); + BOOL success = AuthorizationExecuteWithPrivilegesAndWait(_auth, "/usr/bin/touch", kAuthorizationFlagDefaults, (char *[]){ "-h", "--", path, NULL }); if (!success && error != NULL) { NSString *errorMessage = [NSString stringWithFormat:@"Failed to update modification & access time on %@ with authentication.", targetURL.path.lastPathComponent]; *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: errorMessage }]; diff --git a/Tests/SUFileManagerTest.swift b/Tests/SUFileManagerTest.swift index 260ff2f1..30f6286f 100644 --- a/Tests/SUFileManagerTest.swift +++ b/Tests/SUFileManagerTest.swift @@ -10,7 +10,7 @@ import XCTest class SUFileManagerTest: XCTestCase { - func makeTempFiles(testBlock: (SUFileManager, NSURL, NSURL, NSURL, NSURL) -> Void) + func makeTempFiles(testBlock: (SUFileManager, NSURL, NSURL, NSURL, NSURL, NSURL, NSURL) -> Void) { let fileManager = SUFileManager(allowingAuthorization: false) @@ -29,12 +29,18 @@ class SUFileManagerTest: XCTestCase let fileInDirectoryURL = directoryURL.URLByAppendingPathComponent("a file inside a directory written by sparkles unit tests") try! "bar baz".dataUsingEncoding(NSUTF8StringEncoding)!.writeToURL(fileInDirectoryURL, options: .DataWritingAtomic) - testBlock(fileManager, tempDirectoryURL, ordinaryFileURL, directoryURL, fileInDirectoryURL) + let validSymlinkURL = tempDirectoryURL.URLByAppendingPathComponent("symlink test") + try! NSFileManager.defaultManager().createSymbolicLinkAtURL(validSymlinkURL, withDestinationURL: directoryURL) + + let invalidSymlinkURL = tempDirectoryURL.URLByAppendingPathComponent("symlink test 2") + try! NSFileManager.defaultManager().createSymbolicLinkAtURL(invalidSymlinkURL, withDestinationURL: tempDirectoryURL.URLByAppendingPathComponent("does not exist")) + + testBlock(fileManager, tempDirectoryURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL) } func testMoveFiles() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager.moveItemAtURL(ordinaryFileURL, toURL: directoryURL)) XCTAssertNil(try? fileManager.moveItemAtURL(ordinaryFileURL, toURL: directoryURL.URLByAppendingPathComponent("foo").URLByAppendingPathComponent("bar"))) XCTAssertNil(try? fileManager.moveItemAtURL(rootURL.URLByAppendingPathComponent("does not exist"), toURL: directoryURL)) @@ -44,6 +50,17 @@ class SUFileManagerTest: XCTestCase XCTAssertFalse(fileManager._itemExistsAtURL(ordinaryFileURL)) XCTAssertTrue(fileManager._itemExistsAtURL(newFileURL)) + let newValidSymlinkURL = (ordinaryFileURL.URLByDeletingLastPathComponent?.URLByAppendingPathComponent("new symlink"))! + try! fileManager.moveItemAtURL(validSymlinkURL, toURL: newValidSymlinkURL) + XCTAssertFalse(fileManager._itemExistsAtURL(validSymlinkURL)) + XCTAssertTrue(fileManager._itemExistsAtURL(newValidSymlinkURL)) + XCTAssertTrue(fileManager._itemExistsAtURL(directoryURL)) + + let newInvalidSymlinkURL = (ordinaryFileURL.URLByDeletingLastPathComponent?.URLByAppendingPathComponent("new invalid symlink"))! + try! fileManager.moveItemAtURL(invalidSymlinkURL, toURL: newInvalidSymlinkURL) + XCTAssertFalse(fileManager._itemExistsAtURL(invalidSymlinkURL)) + XCTAssertTrue(fileManager._itemExistsAtURL(newValidSymlinkURL)) + let newDirectoryURL = (ordinaryFileURL.URLByDeletingLastPathComponent?.URLByAppendingPathComponent("new directory"))! try! fileManager.moveItemAtURL(directoryURL, toURL: newDirectoryURL) XCTAssertFalse(fileManager._itemExistsAtURL(directoryURL)) @@ -55,7 +72,7 @@ class SUFileManagerTest: XCTestCase func testMoveFilesToTrash() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager.moveItemAtURLToTrash(rootURL.URLByAppendingPathComponent("does not exist"))) let trashURL = try! NSFileManager.defaultManager().URLForDirectory(.TrashDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) @@ -67,6 +84,12 @@ class SUFileManagerTest: XCTestCase XCTAssertTrue(fileManager._itemExistsAtURL(ordinaryFileTrashURL)) try! fileManager.removeItemAtURL(ordinaryFileTrashURL) + let validSymlinkTrashURL = trashURL.URLByAppendingPathComponent(validSymlinkURL.lastPathComponent!) + try! fileManager.moveItemAtURLToTrash(validSymlinkURL) + XCTAssertTrue(fileManager._itemExistsAtURL(validSymlinkTrashURL)) + XCTAssertTrue(fileManager._itemExistsAtURL(directoryURL)) + try! fileManager.removeItemAtURL(validSymlinkTrashURL) + try! fileManager.moveItemAtURLToTrash(directoryURL) XCTAssertFalse(fileManager._itemExistsAtURL(directoryURL)) XCTAssertFalse(fileManager._itemExistsAtURL(fileInDirectoryURL)) @@ -81,7 +104,7 @@ class SUFileManagerTest: XCTestCase func testCopyFiles() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager.copyItemAtURL(ordinaryFileURL, toURL: directoryURL)) XCTAssertNil(try? fileManager.copyItemAtURL(ordinaryFileURL, toURL: directoryURL.URLByAppendingPathComponent("foo").URLByAppendingPathComponent("bar"))) XCTAssertNil(try? fileManager.copyItemAtURL(rootURL.URLByAppendingPathComponent("does not exist"), toURL: directoryURL)) @@ -91,6 +114,10 @@ class SUFileManagerTest: XCTestCase XCTAssertTrue(fileManager._itemExistsAtURL(ordinaryFileURL)) XCTAssertTrue(fileManager._itemExistsAtURL(newFileURL)) + let newSymlinkURL = (ordinaryFileURL.URLByDeletingLastPathComponent?.URLByAppendingPathComponent("new symlink file"))! + try! fileManager.copyItemAtURL(invalidSymlinkURL, toURL: newSymlinkURL) + XCTAssertTrue(fileManager._itemExistsAtURL(newSymlinkURL)) + let newDirectoryURL = (ordinaryFileURL.URLByDeletingLastPathComponent?.URLByAppendingPathComponent("new directory"))! try! fileManager.copyItemAtURL(directoryURL, toURL: newDirectoryURL) XCTAssertTrue(fileManager._itemExistsAtURL(directoryURL)) @@ -102,12 +129,16 @@ class SUFileManagerTest: XCTestCase func testRemoveFiles() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager.removeItemAtURL(rootURL.URLByAppendingPathComponent("does not exist"))) try! fileManager.removeItemAtURL(ordinaryFileURL) XCTAssertFalse(fileManager._itemExistsAtURL(ordinaryFileURL)) + try! fileManager.removeItemAtURL(validSymlinkURL) + XCTAssertFalse(fileManager._itemExistsAtURL(validSymlinkURL)) + XCTAssertTrue(fileManager._itemExistsAtURL(directoryURL)) + try! fileManager.removeItemAtURL(directoryURL) XCTAssertFalse(fileManager._itemExistsAtURL(directoryURL)) XCTAssertFalse(fileManager._itemExistsAtURL(fileInDirectoryURL)) @@ -116,9 +147,10 @@ class SUFileManagerTest: XCTestCase func testReleaseFilesFromQuarantine() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in try! fileManager.releaseItemFromQuarantineAtRootURL(ordinaryFileURL) try! fileManager.releaseItemFromQuarantineAtRootURL(directoryURL) + try! fileManager.releaseItemFromQuarantineAtRootURL(validSymlinkURL) let quarantineData = "does not really matter what is here".cStringUsingEncoding(NSUTF8StringEncoding)! let quarantineDataLength = Int(strlen(quarantineData)) @@ -135,6 +167,11 @@ class SUFileManagerTest: XCTestCase XCTAssertEqual(0, setxattr(fileInDirectoryURL.path!, SUAppleQuarantineIdentifier, quarantineData, quarantineDataLength, 0, XATTR_CREATE)) XCTAssertGreaterThan(getxattr(fileInDirectoryURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW), 0) + // Extended attributes can't be set on symbolic links in OS X, currently + try! fileManager.releaseItemFromQuarantineAtRootURL(validSymlinkURL) + XCTAssertGreaterThan(getxattr(directoryURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW), 0) + XCTAssertEqual(-1, getxattr(validSymlinkURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW)) + try! fileManager.releaseItemFromQuarantineAtRootURL(directoryURL) XCTAssertEqual(-1, getxattr(directoryURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW)) @@ -153,7 +190,7 @@ class SUFileManagerTest: XCTestCase // Instead we try to change the group ID - we just have to be a member of that group func testAlterFilesGroupID() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager.changeOwnerAndGroupOfItemAtRootURL(ordinaryFileURL, toMatchURL: rootURL.URLByAppendingPathComponent("does not exist"))) XCTAssertNil(try? fileManager.changeOwnerAndGroupOfItemAtRootURL(rootURL.URLByAppendingPathComponent("does not exist"), toMatchURL: ordinaryFileURL)) @@ -169,12 +206,15 @@ class SUFileManagerTest: XCTestCase XCTAssertEqual(staffGroupID, self.groupIDAtPath(ordinaryFileURL.path!)) XCTAssertEqual(staffGroupID, self.groupIDAtPath(directoryURL.path!)) XCTAssertEqual(staffGroupID, self.groupIDAtPath(fileInDirectoryURL.path!)) - + XCTAssertEqual(staffGroupID, self.groupIDAtPath(validSymlinkURL.path!)) + try! fileManager.changeOwnerAndGroupOfItemAtRootURL(fileInDirectoryURL, toMatchURL: ordinaryFileURL) try! fileManager.changeOwnerAndGroupOfItemAtRootURL(ordinaryFileURL, toMatchURL: ordinaryFileURL) + try! fileManager.changeOwnerAndGroupOfItemAtRootURL(validSymlinkURL, toMatchURL: ordinaryFileURL) XCTAssertEqual(staffGroupID, self.groupIDAtPath(ordinaryFileURL.path!)) XCTAssertEqual(staffGroupID, self.groupIDAtPath(directoryURL.path!)) + XCTAssertEqual(staffGroupID, self.groupIDAtPath(validSymlinkURL.path!)) XCTAssertEqual(0, chown(ordinaryFileURL.path!, getuid(), everyoneGroupID)) XCTAssertEqual(everyoneGroupID, self.groupIDAtPath(ordinaryFileURL.path!)) @@ -185,6 +225,9 @@ class SUFileManagerTest: XCTestCase try! fileManager.changeOwnerAndGroupOfItemAtRootURL(fileInDirectoryURL, toMatchURL: directoryURL) XCTAssertEqual(staffGroupID, self.groupIDAtPath(fileInDirectoryURL.path!)) + try! fileManager.changeOwnerAndGroupOfItemAtRootURL(validSymlinkURL, toMatchURL: ordinaryFileURL) + XCTAssertEqual(everyoneGroupID, self.groupIDAtPath(validSymlinkURL.path!)) + try! fileManager.changeOwnerAndGroupOfItemAtRootURL(directoryURL, toMatchURL: ordinaryFileURL) XCTAssertEqual(everyoneGroupID, self.groupIDAtPath(directoryURL.path!)) XCTAssertEqual(everyoneGroupID, self.groupIDAtPath(fileInDirectoryURL.path!)) @@ -193,28 +236,33 @@ class SUFileManagerTest: XCTestCase func testUpdateFileModificationTime() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager.updateModificationAndAccessTimeOfItemAtURL(rootURL.URLByAppendingPathComponent("does not exist"))) let oldOrdinaryFileAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(ordinaryFileURL.path!) let oldDirectoryAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(directoryURL.path!) + let oldValidSymlinkAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(validSymlinkURL.path!) sleep(1); // wait for clock to advance try! fileManager.updateModificationAndAccessTimeOfItemAtURL(ordinaryFileURL) try! fileManager.updateModificationAndAccessTimeOfItemAtURL(directoryURL) + try! fileManager.updateModificationAndAccessTimeOfItemAtURL(validSymlinkURL) let newOrdinaryFileAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(ordinaryFileURL.path!) XCTAssertGreaterThan((newOrdinaryFileAttributes[NSFileModificationDate] as! NSDate).timeIntervalSinceDate(oldOrdinaryFileAttributes[NSFileModificationDate] as! NSDate), 0) let newDirectoryAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(directoryURL.path!) XCTAssertGreaterThan((newDirectoryAttributes[NSFileModificationDate] as! NSDate).timeIntervalSinceDate(oldDirectoryAttributes[NSFileModificationDate] as! NSDate), 0) + + let newSymlinkAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(validSymlinkURL.path!) + XCTAssertGreaterThan((newSymlinkAttributes[NSFileModificationDate] as! NSDate).timeIntervalSinceDate(oldValidSymlinkAttributes[NSFileModificationDate] as! NSDate), 0) } } func testFileExists() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertTrue(fileManager._itemExistsAtURL(ordinaryFileURL)) XCTAssertTrue(fileManager._itemExistsAtURL(directoryURL)) XCTAssertFalse(fileManager._itemExistsAtURL(rootURL.URLByAppendingPathComponent("does not exist"))) @@ -227,17 +275,11 @@ class SUFileManagerTest: XCTestCase XCTAssertFalse(fileManager._itemExistsAtURL(rootURL.URLByAppendingPathComponent("does not exist"), isDirectory: nil)) - let validSymlinkURL = rootURL.URLByAppendingPathComponent("symlink test") - try! NSFileManager.defaultManager().createSymbolicLinkAtURL(validSymlinkURL, withDestinationURL: directoryURL) - XCTAssertTrue(fileManager._itemExistsAtURL(validSymlinkURL)) var validSymlinkIsADirectory: ObjCBool = false XCTAssertTrue(fileManager._itemExistsAtURL(validSymlinkURL, isDirectory: &validSymlinkIsADirectory) && !validSymlinkIsADirectory) - let invalidSymlinkURL = rootURL.URLByAppendingPathComponent("symlink test 2") - try! NSFileManager.defaultManager().createSymbolicLinkAtURL(invalidSymlinkURL, withDestinationURL: rootURL.URLByAppendingPathComponent("does not exist")) - // Symlink should still exist even if it doesn't point to a file that exists XCTAssertTrue(fileManager._itemExistsAtURL(invalidSymlinkURL)) @@ -248,7 +290,7 @@ class SUFileManagerTest: XCTestCase func testMakeDirectory() { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in XCTAssertNil(try? fileManager._makeDirectoryAtURL(ordinaryFileURL)) XCTAssertNil(try? fileManager._makeDirectoryAtURL(directoryURL)) @@ -260,6 +302,9 @@ class SUFileManagerTest: XCTestCase var isDirectory: ObjCBool = false XCTAssertTrue(fileManager._itemExistsAtURL(newDirectoryURL, isDirectory: &isDirectory)) + + try! fileManager.removeItemAtURL(directoryURL) + XCTAssertNil(try? fileManager._makeDirectoryAtURL(validSymlinkURL)) } } From aa7c46ba4f6cfa8fff161d54a9ce7ec3d614ba01 Mon Sep 17 00:00:00 2001 From: Zorg Date: Mon, 11 Apr 2016 03:15:36 -0400 Subject: [PATCH 3/5] Check the bundle's parent directory for writability too This is to decide if we should allow automatic downloading & installing of updates or not. --- Sparkle/SUHost.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 00ba50d1..986c983d 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -71,7 +71,10 @@ } // Can we automatically update in the background without bugging the user (e.g, with a administrator password prompt)? - return [[NSFileManager defaultManager] isWritableFileAtPath:self.bundlePath]; + // Note it's very well possible to have the bundle be writable but not be able to write into the parent directory + // And if the bundle isn't writable, but we can write into the parent directory, we will still need to authorize to replace it + NSString *bundlePath = [self bundlePath]; + return [[NSFileManager defaultManager] isWritableFileAtPath:bundlePath.stringByDeletingLastPathComponent] && [[NSFileManager defaultManager] isWritableFileAtPath:bundlePath]; } - (NSString *)appCachePath From 99be33d83b37fb9dd5e93b12a5366ca6e4919dc7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 19 Apr 2016 02:31:02 -0400 Subject: [PATCH 4/5] Move Test App server directory data into subdirectory inside Caches We want to wipe the caches directory we create for the test app data on launch, and want to make sure no other data is being inserted into the same directory. Before, Sparkle was inserting data into the same directory when downloading an update. --- TestApplication/SUTestApplicationDelegate.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestApplication/SUTestApplicationDelegate.m b/TestApplication/SUTestApplicationDelegate.m index d360c607..dbf0f22a 100644 --- a/TestApplication/SUTestApplicationDelegate.m +++ b/TestApplication/SUTestApplicationDelegate.m @@ -54,7 +54,7 @@ static NSString * const UPDATED_VERSION = @"2.0"; assert(bundleIdentifier != nil); // Create a directory that'll be used for our web server listing - NSURL *serverDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; + NSURL *serverDirectoryURL = [[cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier] URLByAppendingPathComponent:@"ServerData"]; if ([serverDirectoryURL checkResourceIsReachableAndReturnError:nil]) { NSError *removeServerDirectoryError = nil; From a937fa0a1738bdc5f6f4c3371fb820a5606e9b71 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 19 Apr 2016 02:33:33 -0400 Subject: [PATCH 5/5] Clear update caches directory before downloading new update This aims to make it impossible to have download data accumulate inside the caches directory. We still try to deal with if an intermediate file is created at the path we want. appCachePath method was also moved away from SUHost into SUBasicUpdateDriver which is the only class that needs it. This method also has very little to do with host bundles. appCachePath is also called less when checking for the existence of intermediate file paths. Lastly, we now create a directory whose name is the sparkle bundle ID instead of just "Sparkle" This is to improve potential name collision issues. --- Sparkle/SUBasicUpdateDriver.m | 39 ++++++++++++++++++++++++++++++----- Sparkle/SUHost.h | 1 - Sparkle/SUHost.m | 22 -------------------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 2f1230a1..e40b3893 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -208,8 +208,36 @@ }]]; } +- (NSString *)appCachePath +{ + NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + NSString *cachePath = nil; + if ([cachePaths count]) { + cachePath = [cachePaths objectAtIndex:0]; + } + if (!cachePath) { + SULog(@"Failed to find user's cache directory! Using system default"); + cachePath = NSTemporaryDirectory(); + } + + NSString *name = [self.host.bundle bundleIdentifier]; + if (!name) { + name = [self.host name]; + } + + cachePath = [cachePath stringByAppendingPathComponent:name]; + cachePath = [cachePath stringByAppendingPathComponent:@SPARKLE_BUNDLE_IDENTIFIER]; + return cachePath; +} + - (void)downloadUpdate { + // Clear cache directory so that downloads can't possibly accumulate inside + NSString *appCachePath = [self appCachePath]; + if ([[NSFileManager defaultManager] fileExistsAtPath:appCachePath]) { + [[NSFileManager defaultManager] removeItemAtPath:appCachePath error:NULL]; + } + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self.updateItem fileURL]]; [request setValue:[self.updater userAgentString] forHTTPHeaderField:@"User-Agent"]; if ([[self.updater delegate] respondsToSelector:@selector(updater:willDownloadUpdate:withRequest:)]) { @@ -223,13 +251,14 @@ - (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name { NSString *downloadFileName = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; - - - self.tempDir = [self.host.appCachePath stringByAppendingPathComponent:downloadFileName]; + + NSString *appCachePath = [self appCachePath]; + + self.tempDir = [appCachePath stringByAppendingPathComponent:downloadFileName]; int cnt = 1; while ([[NSFileManager defaultManager] fileExistsAtPath:self.tempDir] && cnt <= 999) { - self.tempDir = [self.host.appCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", downloadFileName, cnt++]]; + self.tempDir = [appCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", downloadFileName, cnt++]]; } // Create the temporary directory if necessary. @@ -480,7 +509,7 @@ NSString *const relaunchToolName = @"" SPARKLE_RELAUNCH_TOOL_NAME; NSString *const relaunchPathToCopy = [sparkleBundle pathForResource:relaunchToolName ofType:@"app"]; if (relaunchPathToCopy != nil) { - NSString *targetPath = [self.host.appCachePath stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; + NSString *targetPath = [[self appCachePath] stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; NSError *error = nil; diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 5f73f955..6d058b90 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -26,7 +26,6 @@ typedef struct { - (instancetype)initWithBundle:(NSBundle *)aBundle; @property (readonly, copy) NSString *bundlePath; @property (readonly) BOOL allowsAutomaticUpdates; -@property (readonly, copy) NSString *appCachePath; @property (readonly, copy) NSString *installationPath; @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 00ba50d1..ffe8c915 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -74,28 +74,6 @@ return [[NSFileManager defaultManager] isWritableFileAtPath:self.bundlePath]; } -- (NSString *)appCachePath -{ - NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - NSString *cachePath = nil; - if ([cachePaths count]) { - cachePath = [cachePaths objectAtIndex:0]; - } - if (!cachePath) { - SULog(@"Failed to find user's cache directory! Using system default"); - cachePath = NSTemporaryDirectory(); - } - - NSString *name = [self.bundle bundleIdentifier]; - if (!name) { - name = [self name]; - } - - cachePath = [cachePath stringByAppendingPathComponent:name]; - cachePath = [cachePath stringByAppendingPathComponent:@"Sparkle"]; - return cachePath; -} - - (NSString *)installationPath { if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) {