Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if my app is sandboxed?

I have an application that is targeted to run in both sandboxed and non-sandboxed MacOS. If a user upgrades from MacOS 10.6 to a later OS I need the user to re-pick folders so I can bookmark them with secure bookmarks.

How do I detect that my application is on an OS that supports sandboxing?

like image 238
NPAssoc Avatar asked Aug 29 '12 12:08

NPAssoc


People also ask

Are all macOS apps sandboxed?

The macOS operating system and its file system operate differently and are structured differently. The idea is similar, though. Every application is given a sandbox, a directory it can use to store data in.

Are apps sandboxed?

Android sandboxThe Android platform isolates apps from each other and protects them -- and the overall system -- from malicious apps and intruders. Android assigns a unique user ID (UID) to each application to create a kernel-level sandbox. This kernel ensures security between apps and the system at the process level.


2 Answers

The only way I know of is to look for APP_SANDBOX_CONTAINER_ID environment variable. It is present when the app is running inside a sandbox container.

NSDictionary* environ = [[NSProcessInfo processInfo] environment];
BOOL inSandbox = (nil != [environ objectForKey:@"APP_SANDBOX_CONTAINER_ID"]);
like image 143
hamstergene Avatar answered Sep 28 '22 11:09

hamstergene


BOOL isSandboxed = NO;

SecStaticCodeRef staticCode = NULL;
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];

if (SecStaticCodeCreateWithPath((__bridge CFURLRef)bundleURL, kSecCSDefaultFlags, &staticCode) == errSecSuccess) {
    if (SecStaticCodeCheckValidityWithErrors(staticCode, kSecCSBasicValidateOnly, NULL, NULL) == errSecSuccess) {
        SecRequirementRef sandboxRequirement;
        if (SecRequirementCreateWithString(CFSTR("entitlement[\"com.apple.security.app-sandbox\"] exists"), kSecCSDefaultFlags,
                                       &sandboxRequirement) == errSecSuccess)
        {
            OSStatus codeCheckResult = SecStaticCodeCheckValidityWithErrors(staticCode, kSecCSBasicValidateOnly, sandboxRequirement, NULL);
            if (codeCheckResult == errSecSuccess) {
                isSandboxed = YES;
            }
        }
    }
    CFRelease(staticCode);
}
like image 33
Oleksii Avatar answered Sep 28 '22 12:09

Oleksii