diff --git a/SUCodeSigningVerifier.h b/SUCodeSigningVerifier.h index 1f01d1b5..637355bc 100644 --- a/SUCodeSigningVerifier.h +++ b/SUCodeSigningVerifier.h @@ -14,6 +14,7 @@ @interface SUCodeSigningVerifier : NSObject + (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error; + (BOOL)hostApplicationIsCodeSigned; ++ (BOOL)hostApplicationIsSandboxed; @end #endif diff --git a/SUCodeSigningVerifier.m b/SUCodeSigningVerifier.m index 0dc029f0..e2a6374f 100644 --- a/SUCodeSigningVerifier.m +++ b/SUCodeSigningVerifier.m @@ -13,13 +13,11 @@ @implementation SUCodeSigningVerifier extern OSStatus SecCodeCopySelf(SecCSFlags flags, SecCodeRef *self) __attribute__((weak_import)); - extern OSStatus SecCodeCopyDesignatedRequirement(SecStaticCodeRef code, SecCSFlags flags, SecRequirementRef *requirement) __attribute__((weak_import)); - extern OSStatus SecStaticCodeCreateWithPath(CFURLRef path, SecCSFlags flags, SecStaticCodeRef *staticCode) __attribute__((weak_import)); - extern OSStatus SecStaticCodeCheckValidityWithErrors(SecStaticCodeRef staticCode, SecCSFlags flags, SecRequirementRef requirement, CFErrorRef *errors) __attribute__((weak_import)); - +extern OSStatus SecRequirementCreateWithString(CFStringRef text, SecCSFlags flags, SecRequirementRef *requirement) __attribute__((weak_import)); +extern OSStatus SecCodeCheckValidity(SecCodeRef code, SecCSFlags flags, SecRequirementRef requirement) __attribute__((weak_import)); + (BOOL)codeSignatureIsValidAtPath:(NSString *)destinationPath error:(NSError **)error { @@ -83,4 +81,49 @@ finally: return (result == 0); } ++ (BOOL)hostApplicationIsSandboxed +{ + // This API didn't exist prior to 10.6 + if (SecCodeCopySelf == NULL) return NO; + + BOOL isSandboxed = NO; + do + { + SecCodeRef hostCode = NULL; + SecRequirementRef hostRequirement = NULL; + + #define Cleanup() \ + { \ + if (hostCode) CFRelease(hostCode); \ + if (hostRequirement) CFRelease(hostRequirement); \ + } + + OSStatus status = SecCodeCopySelf(kSecCSDefaultFlags, &hostCode); + + if (status != noErr || hostCode == NULL) + { + Cleanup(); + break; + } + + CFStringRef requirementString = CFSTR("entitlement[\"com.apple.security.app-sandbox\"] exists"); + status = SecRequirementCreateWithString(requirementString, kSecCSDefaultFlags, &hostRequirement); + + if (status != noErr || hostRequirement == NULL) + { + Cleanup(); + break; + } + + status = SecCodeCheckValidity(hostCode, kSecCSDefaultFlags, hostRequirement); + if (noErr == status) + isSandboxed = YES; + + Cleanup(); + } + while (NO); + + return isSandboxed; +} + @end diff --git a/SUUpdater.m b/SUUpdater.m index 165aa18b..c0f99353 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -556,10 +556,12 @@ static NSString * const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefault + (BOOL)shouldUseXPC { - BOOL running10_7 = floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6; - BOOL useXPC = running10_7 && [[NSFileManager defaultManager] fileExistsAtPath: - [[[[SUUpdater sharedUpdater] hostBundle] bundlePath] stringByAppendingPathComponent:@"Contents/XPCServices/com.andymatuschak.Sparkle.install-service.xpc"]]; - return useXPC; + if (![SUCodeSigningVerifier hostApplicationIsSandboxed]) + return NO; + + NSString *xpcServicePrefixPath = [[[SUUpdater sharedUpdater] hostBundle] bundlePath]; + NSString *xpcServiceSuffixPath = @"Contents/XPCServices/com.andymatuschak.Sparkle.install-service.xpc"; + return [[NSFileManager defaultManager] fileExistsAtPath:[xpcServicePrefixPath stringByAppendingPathComponent:xpcServiceSuffixPath]]; } @end