Make allowsAuthorization apply to all of SUFileManager's methods

This commit is contained in:
Zorg committed 2015-10-09 17:49:46 -04:00
1 parent a7af6fb82d
commit 9b74309eb5
5 files changed
+38 -29

No files matched your search

+1 -1
View File
@@ -461,7 +461,7 @@
// We probably don't need to release the quarantine, but we'll do it just in case it's necessary.
// Perhaps in a sandboxed environment this matters more. Note that this may not be a fatal error.
NSError *quarantineError = nil;
if (![[[SUFileManager alloc] init] releaseItemFromQuarantineWithoutAuthenticationAtRootURL:[NSURL fileURLWithPath:targetPath] error:&quarantineError]) {
if (![[[SUFileManager alloc] initAllowingAuthorization:NO] releaseItemFromQuarantineAtRootURL:[NSURL fileURLWithPath:targetPath] error:&quarantineError]) {
SULog(@"Failed to release quarantine on %@ with error %@", targetPath, quarantineError);
}
self.relaunchPath = targetPath;
+11 -6
View File
@@ -9,12 +9,22 @@
#import <Foundation/Foundation.h>
/**
* A class used for performing file operations that may also perform authentication if permission is denied when trying to
* A class used for performing file operations that may also perform authentication if allowed and if permission is denied when trying to
* perform them normally as the running user. All operations on this class may be used on thread other than the main thread.
* This class provides basic file operations and stays away from including much application-level logic.
*/
@interface SUFileManager : NSObject
/**
* Initializes a file manager
* @param allowsAuthorization Specifies whether operations invoked on this instance are allowed to acquire authorization in order
* to perform file operations when read or write access is denied
* @return A new file manager instance
*
* This method just initializes the file manager. It doesn't acquire authorization immediately if allowsAuthorization is YES
*/
- (id)initAllowingAuthorization:(BOOL)allowsAuthorization;
/**
* Creates a temporary directory on the same volume as a provided URL
* @param preferredName A name that may be used when creating the temporary directory. Note that in the uncommon case this name is used, the temporary directory will be created inside the directory pointed by appropriateURL
@@ -110,9 +120,4 @@
*/
- (BOOL)releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL error:(NSError **)error;
/**
* Behaves as -releaseItemFromQuarantineAtRootURL:error: does, except without the capability of authenticating when permission is denied
*/
- (BOOL)releaseItemFromQuarantineWithoutAuthenticationAtRootURL:(NSURL *)rootURL error:(NSError **)error;
@end
+13 -15
View File
@@ -52,13 +52,15 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza
{
AuthorizationRef _auth;
NSFileManager *_fileManager;
BOOL _allowsAuthorization;
}
- (id)init
- (id)initAllowingAuthorization:(BOOL)allowsAuthorization
{
self = [super init];
if (self != nil) {
_fileManager = [[NSFileManager alloc] init];
_allowsAuthorization = allowsAuthorization;
}
return self;
}
@@ -71,6 +73,13 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza
return YES;
}
if (!_allowsAuthorization) {
if (error != NULL) {
*error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: @"Unable to grant authorization to perform action because it is explicitly turned off" }];
}
return NO;
}
OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &_auth);
if (status != errAuthorizationSuccess) {
if (error != NULL) {
@@ -177,7 +186,7 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza
return success;
}
- (BOOL)_releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL allowingAuthentication:(BOOL)allowsAuthentication withQuarantineRetrieval:(BOOL (^)(NSURL *))quarantineRetrieval quarantineRemoval:(BOOL (^)(NSURL *, NSError * __autoreleasing *))quarantineRemoval isAccessError:(BOOL (^)(NSError *))isAccessError error:(NSError * __autoreleasing *)error
- (BOOL)_releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL withQuarantineRetrieval:(BOOL (^)(NSURL *))quarantineRetrieval quarantineRemoval:(BOOL (^)(NSURL *, NSError * __autoreleasing *))quarantineRemoval isAccessError:(BOOL (^)(NSError *))isAccessError error:(NSError * __autoreleasing *)error
{
if (![self _itemExistsAtURL:rootURL]) {
if (error != NULL) {
@@ -195,7 +204,7 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza
if (quarantineRemoval(fileURL, &removalError)) {
removedQuarantine = YES;
} else {
if (allowsAuthentication && isAccessError(removalError)) {
if (isAccessError(removalError)) {
removedQuarantine = [self _removeXAttrWithAuthentication:SUAppleQuarantineIdentifier fromRootURL:rootURL error:error];
attemptedAuthentication = YES;
} else {
@@ -269,14 +278,13 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza
// We used to have code similar to the method below that used -[NSURL getResourceValue:forKey:error:] and -[NSURL setResourceValue:forKey:error:]
// However, those methods *really suck* - you can't rely on the return value from getting the resource value and if you set the resource value
// when the key isn't present, errors are spewed out to the console
- (BOOL)_releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL allowingAuthentication:(BOOL)allowsAuthentication error:(NSError *__autoreleasing *)error
- (BOOL)releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL error:(NSError *__autoreleasing *)error
{
static const int removeXAttrOptions = XATTR_NOFOLLOW;
return
[self
_releaseItemFromQuarantineAtRootURL:rootURL
allowingAuthentication:allowsAuthentication
withQuarantineRetrieval:^BOOL(NSURL *fileURL) {
return ([self _getXAttr:SUAppleQuarantineIdentifier fromFile:fileURL.path options:removeXAttrOptions] >= 0);
}
@@ -293,16 +301,6 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza
error:error];
}
- (BOOL)releaseItemFromQuarantineAtRootURL:(NSURL *)rootURL error:(NSError * __autoreleasing *)error
{
return [self _releaseItemFromQuarantineAtRootURL:rootURL allowingAuthentication:YES error:error];
}
- (BOOL)releaseItemFromQuarantineWithoutAuthenticationAtRootURL:(NSURL *)rootURL error:(NSError * __autoreleasing *)error
{
return [self _releaseItemFromQuarantineAtRootURL:rootURL allowingAuthentication:NO error:error];
}
- (BOOL)moveItemAtURL:(NSURL *)sourceURL toURL:(NSURL *)destinationURL error:(NSError *__autoreleasing *)error
{
if (![self _itemExistsAtURL:sourceURL]) {
+1 -1
View File
@@ -42,7 +42,7 @@
return NO;
}
SUFileManager *fileManager = [[SUFileManager alloc] init];
SUFileManager *fileManager = [[SUFileManager alloc] initAllowingAuthorization:YES];
// Create a temporary directory for our new app that resides on our destination's volume
NSURL *tempNewDirectoryURL = [fileManager makeTemporaryDirectoryWithPreferredName:[installationURL.lastPathComponent.stringByDeletingPathExtension stringByAppendingString:@" (Incomplete Update)"] appropriateForDirectoryURL:installationURL.URLByDeletingLastPathComponent error:error];
+12 -6
View File
@@ -12,7 +12,7 @@ class SUFileManagerTest: XCTestCase
{
func makeTempFiles(testBlock: (SUFileManager, NSURL, NSURL, NSURL, NSURL) -> Void)
{
let fileManager = SUFileManager()
let fileManager = SUFileManager(allowingAuthorization: false)
let tempDirectoryURL = try! fileManager.makeTemporaryDirectoryWithPreferredName("Sparkle Unit Test Data", appropriateForDirectoryURL: NSURL(fileURLWithPath: NSHomeDirectory()))
@@ -95,8 +95,8 @@ class SUFileManagerTest: XCTestCase
func testReleaseFilesFromQuarantine()
{
makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in
try! fileManager.releaseItemFromQuarantineWithoutAuthenticationAtRootURL(ordinaryFileURL)
try! fileManager.releaseItemFromQuarantineWithoutAuthenticationAtRootURL(directoryURL)
try! fileManager.releaseItemFromQuarantineAtRootURL(ordinaryFileURL)
try! fileManager.releaseItemFromQuarantineAtRootURL(directoryURL)
let quarantineData = "does not really matter what is here".cStringUsingEncoding(NSUTF8StringEncoding)!
let quarantineDataLength = Int(strlen(quarantineData))
@@ -104,7 +104,7 @@ class SUFileManagerTest: XCTestCase
XCTAssertEqual(0, setxattr(ordinaryFileURL.path!, SUAppleQuarantineIdentifier, quarantineData, quarantineDataLength, 0, XATTR_CREATE))
XCTAssertGreaterThan(getxattr(ordinaryFileURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW), 0)
try! fileManager.releaseItemFromQuarantineWithoutAuthenticationAtRootURL(ordinaryFileURL)
try! fileManager.releaseItemFromQuarantineAtRootURL(ordinaryFileURL)
XCTAssertEqual(-1, getxattr(ordinaryFileURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW))
XCTAssertEqual(0, setxattr(directoryURL.path!, SUAppleQuarantineIdentifier, quarantineData, quarantineDataLength, 0, XATTR_CREATE))
@@ -113,7 +113,7 @@ 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)
try! fileManager.releaseItemFromQuarantineWithoutAuthenticationAtRootURL(directoryURL)
try! fileManager.releaseItemFromQuarantineAtRootURL(directoryURL)
XCTAssertEqual(-1, getxattr(directoryURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW))
XCTAssertEqual(-1, getxattr(fileInDirectoryURL.path!, SUAppleQuarantineIdentifier, nil, 0, 0, XATTR_NOFOLLOW))
@@ -244,7 +244,13 @@ class SUFileManagerTest: XCTestCase
// This alone shouldn't prompt a password dialog and should always succeed
func testAcquireAuthorization()
{
let fileManager = SUFileManager()
let fileManager = SUFileManager(allowingAuthorization: true)
try! fileManager._acquireAuthorization()
}
func testAcquireBadAuthorization()
{
let fileManager = SUFileManager(allowingAuthorization: false)
XCTAssertNil(try? fileManager._acquireAuthorization())
}
}