What non-user directories can sandboxed mac app read/write to? - macos

I have an app which needs to preserve data between times it runs. I had been using NSUserDefaults for this, but I've had a few users point this out to me: this causes different users to end up with different data, which isn't the way the app should work. It needs a single directory that it can read/write from regardless of which user is running it.
So, I need a non-user specific directory that a sandboxed mac app can read and write to.
Thanks!
(Oh, and if this directory is persistent between updates of my app, that'd be helpful, too!)
Quick barely related question: Is there a way to have a user modifiable resource file in a Mac App Store approved app? I don't want it to be modifiable via my app; I just want to make sure that users modifying it won't cause the system to kill the app for not matching a code signature hash or something.

I don't think that you will be able to read and write in a directory outside of the App Sandbox container without prompting the user to select it using Powerbox and saving a security-scoped bookmark (see App Sandbox Container Directory). From what I've gathered about App reviews lately, you won't even be able to specify a default in the open dialog if you elect to have the user choose the directory.
As for the second question, as I understand it any verification of code signatures is left to the developer. So while MAS apps have a _CodeSignature folder containing a plist with all the hashes of the resource, in my experiments changing them had no effect on app launch.

Related

OSX: Avoid userprompt when opening embedded binary

I have a sandboxed app that uses an embedded binary to show it's status item.
On first launch of the main app (where it launches the embedded binary like this:
NSWorkspace.sharedWorkspace().launchApplication(statusItemPath)
) OSX displays a user prompt, if the user really wants to start the embedded app:
I find this really confusing for the user - I understand that it is for security reasons but I want to distribute via MAS and so both binaries needs to pass review.
Is there a way to avoid this user prompt (maybe a singing option or entitlement key?)
When an application is downloaded from the internet, or run via another program for the first time, OS X is protecting the user with a mechanism known as 'quarantine'.
Once the user accepts running the application, the quarantine extended attribute on the app is removed.
Removing the quarantine attribute can be done with the following command:
xattr -d com.apple.quarantine /PATH/TO/APPLICATION
So you could call out to the system to run this from your initial application on the embedded binary. However I'm not sure this would be acceptable to Apple for the App Store.
The preferred method would be to use XPC and create a helper app which is launched automatically by launchd. You can read about that here.

Reading desktop pictures in sandboxed app

I'm having crashes in my sandboxed app related to reading desktops pictures which I can not reproduce on my Mac but I got rejected on the App Store for it crashing and other users have reported crashing.
The desktop pictures are being read from "/Library/Desktop Pictures" or using NSWorkspace's desktopImageURLForScreen method. These work just fine on my Mac but I'm a little confused because I don't know how I'm getting access to "/Library/Desktop Pictures" without the directory being chosen manually in an NSOpenPanel. As for desktopImageURLForScreen I'm not sure if this is sandbox safe depending on where the file is stored on that users screen.
Can anyone verify these 2 methods for getting the desktop picture are safe in all cases or do I need to request the user select the directory where the desktop pictures are stored (even from desktopImageURLForScreen) in a NSOpenPanel?
First, having the sandbox deny access to a URL does not cause a crash - methods will return error indications. So if your app is crashing it is either because you cannot access the URLs, or because you are not testing correctly for failure.
If you wish to quietly access desktop images you can specify the com.apple.security.assets.pictures.read-only entitlement - this will give your app access to common image locations. It does not guarantee that your app can access the URL of the desktop image, just increases the probability. So you must still test for success or failure.
If your program requires access and if silent access fails you can then put up a standard file dialog; with suitably customised prompts, buttons, etc; to request the user grants access to the URL.
HTH

Write folder in Mac OS X with sandbox active

I created a small application in Xcode with Cocoa Desktop and this application has to create a folder in the current user's desktop Mac OS X
When I run the application without using the app creates a sandbox folder properly on desktop
If I use the sandbox putting right files for read / write I can not create a folder on the desktop
Does anyone know how to solve this as to send the application to the AppStore is necessary to use sandbox?
You shouldn't just create a folder on the desktop, regardless of whether you are using the sandbox or not. Instead use NSOpenPanel configured to select folders and ask your user to provide you either a folder to use. That is compatible with both open and sandboxed apps.
In the sandbox world once you've asked the user for a folder you can create a security scoped bookmark and save it in your apps preferences; on subsequent runs you can use that bookmark to re-establish rights to access the folder without user intervention. E.g. a browser might ask once for access to a folder to store downloads and then save a security scoped bookmark to that folder.
HTH
For some applications it just better to use some directory as default directory (Eg. Mail and Firefox uses Downloads as default directory). I guess this is your case. But, for better user experience (and for higher chances of your App's acceptance in AppStore) follow best practices - like avoiding direct Desktop access. And, mostly you will find the answer yourself if you go through these guides:
App Sandbox Design Guide (https://developer.apple.com/library/mac/documentation/Security/Conceptual/AppSandboxDesignGuide/AboutAppSandbox/AboutAppSandbox.html)
Entitlement Key Reference (https://developer.apple.com/library/ios/DOCUMENTATION/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html)

Sandbox mac osx application

I have developed a mac application without using sandbox.
now i want to send it to the mac appSore, i buy my certificate, all is good.
I read this: Apple doc and in xcode i set the user selected file,music folder... to read/write access.
My application let the user chose from a folder any type of file, do some treatment and let the user chose where to save the new file.
To chose files, user can open folder with NSOpenPanel or with the open with functionality or drag-and-drop files.
Also the application can delete files.
I dont know if there's other things to do to sandbox the application (or a tool to test if it is sandboxed)
I come from ios this is why i'm asking :)
You can look in the Console when the application is running to view if sandboxd or pboxd logs messages referring to your application.
The format is: sandboxd[53037] ([53035]): AppName(53035)
I don't know if there are other processes who may log sandbox violation, but you can see them, if they are referring to your application.

Can Mac app store apps access local file system?

I am wondering what limitations are imposed on the Mac app store. Can someone point me in the right direction? For instance, lets say I wanted to write an app that does incremental auto-backups of files on the Mac file system, is that possible with an app in the Mac app store, or would my only option be a standalone mac app?
On the iPhone, apps are self contained in their own "sandbox"? Does this same principal apply to mac app store apps?
Fellows, what the OP is really referring to is described here:
http://developer.apple.com/library/mac/#documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html
It is all about
Containers
Entitlements
PowerBox (daemon)
Once an application is "sandboxed" its view of the ~ "home" directory is that of a Container (which happens to be created upon first start in $HOME/Library/Containers/appBundleID/Data). Therein it finds its "private copy" of config, cache and data files. And off course it can't read anything else, unless...
... you give the application the appropriate Entitlements in the form of a codesign-ed property file (in fact, codesigning is integral part of the sandboxing concept).
One of these Entitlements is the "com.apple.security.files.user-selected.read-write" which allows the application to read and write exactly those files which the user has explicitly chosen via the standard save and open dialog respectively.
The PowerBox (pboxd) daemon then renders the appropriate file dialog in its own process space and adds the selected file paths to the list of allowed files. This is transparent to the application, that is no code changes are required - as long as the application uses the standard NSOpenPanel or NSSavePanel dialogs.
"To facilitate application sandboxing, Mac OS X v10.7 provides a trusted system daemon that is tasked with presenting open and save panels on behalf of applications running in an application sandbox. That daemon is called Powerbox (its process name is pboxd). These Powerbox-presented remote panels appear fully indistinguishable from in-process panels in terms of user experience.
Any time an application running inside a sandbox invokes an NSOpenPanel or NSSavePanel dialog, rather than showing the panels directly, AppKit automatically asks the Powerbox to present the dialog. From a developer perspective, there are no code changes required in terms of how these panels are used; this process is fully transparent." [end quote from Apple docs]
Given all that, what the OP would need (for a backup solution) are "unmediated read/writes":
"If an application chooses to derive the user's home directory in a way that bypasses Cocoa APIs (by directly invoking getpwent, for example), the application sandbox prohibits it from writing to the paths it receives (unless the application has the unmediated write entitlement, which is strongly discouraged for obvious security reasons." [end quote from Apple docs]
However the closest Entitlements which would give "free access to the file system" I could fine would be:
"
Absolute file read-only—The ability to read the files or directories at the specified absolute paths. (com.apple.security.temporary-exception.files.absolute-path.read-only)
Absolute file read/write—The ability to read or write the files or directories at the specified absolute paths. (com.apple.security.temporary-exception.files.absolute-path.read-write)
" [end quote from Apple docs]
And I am not sure whether an application could simply provide the root directory "/"
Anyway, carefully note that these entitlements are marked "temporary": Apple might remove/deprecate those entitlements at seen fit!
I believe that starting in November, Mac App Store submissions must adopt the App Sandbox. There are specific entitlements that you can request when your app is submitted to the app store, along with an explanation of why you need those entitlements. More information can be found in WWDC Session 204 video on the Developer site.
Technically, there is sandboxing on the OS X. However, applicants started manually by the user bypass those sandboxing restrictions (sometimes requiring escalation / admin verification screens).
Take a look at:
http://techjournal.318.com/security/a-brief-introduction-to-mac-os-x-sandbox-technology/
The are changing or have changed with the Lion release. Mac Store apps used to be able to do anything within the filesystem that the logged in user privs could allow but I think you are now required to use the entitlements system and hence sandbox your App.
Read this for more ideas
https://developer.apple.com/library/mac/#releasenotes/General/SubmittingToMacAppStore/_index.html#//apple_ref/doc/uid/TP40010572
This will help. Take a good look at it.
http://developer.apple.com/library/mac/#documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW16
You can currently find Hype or Pixelmator on the Mac App Store.
This proves evidently that you can save to disk and read from disk, which seems a basic feature of any serious application. Moreover, Apple is pushing developers to start using incremental auto-backups of files, it would therefore be very surprising if they forbade that in the App Store, wouldn't it?

Resources