Unable to Write Files Within App Bundle After Codesigning and Notarization

I have already posted asking about this:

Codesigned and notarized app cannot directly write files inside the app bundle...

But there are still some doubts that have not been answered.


We use Qt to develop an application on the macOS platform, and we are attempting to perform code signing and notarization to ensure our the application is trusted by Apple.

However, there are a few things that seem weird regarding this statement: "App bundles are read-only by design."

Let me provide more details.


Currently, when our application starts, it needs to create folder (e.g. Temp) in the root directory of the executable

For example: Myapp.app/Contents/MacOS/Myapp ---> Myapp.app/Contents/MacOS/Temp

The folder is designed for storing runtime logs or config files for our application. In the past, users may also modify the settings inside target folder if needed.

However, the strange thing is that after the application is codesigned and notarized.

When we double-click the application Myapp (a.k.a Myapp.app) in Finder, it could successfully launch and create the Temp folder inside the Myapp.app/Contents/MacOS folder.

However, when we navigate and attempt to run the main application executable in command line mode (as our application supports this command line execution)

$ cd Myapp.app/Contents/MacOS
$ ./Myapp -h

As our application will check if the root folder has write permission before starting (i.e., check if Myapp.app/Contents/MacOS is writable because we require to create Temp folder in the following steps)

It pop up the error that folder does not have write permission.

The aforementioned scenarios seems to conflict with this statement: "App bundles are read-only by design" (because when the application is launched directly by clicking in Finder, the Temp folder can be created successfully, but via the console command line, it cannot).


I would like to confirm again if writing files in the notarized application MacOS directory is not allowed?

If not, have any recommended approaches? (e.g., changing the folder to another directory). What causes the different results in these running scenarios?

We are not concerned about breaking the signature after application launched, as it seems that macOS will add it to system trust list after first time successfully launch. (Download the app from internet --> System: it is an app downloaded from the internet. Are you sure want to open it...? OK --> Although our application creates the Temp folder after first launch, when we click the application second time, it could directly open the app)

Answered by DTS Engineer in 815386022
I would like to confirm again if writing files in the notarized application MacOS directory is not allowed?

It depends on what you mean by “allowed”:

  • If you mean “Is this supported?” then the answer is “No.”

  • If you mean “Will the system block it in all cases?” then you already know the answer to that.

Lemme quote Embedding nonstandard code structures in a bundle:

A bundle is a read-only structure. All Apple platforms except the Mac enforce this requirement at runtime. On iOS, for example, any attempt to modify your app’s bundle at runtime will fail with an error. The Mac may or may not enforce this requirement at runtime, depending on the context, but modifying your app’s bundle isn’t supported because it breaks the seal on the app’s code signature.

You are doing something that’s unsupported and encountering weird problems. That’s kinda the nature of doing unsupported things. Sometimes it works, sometimes it doesn’t, and that can change from Mac to Mac, from release to release, and so on.

If you continue down this path:

  • You will have problems in some existing scenarios.

  • You are likely to have more problems on future versions of macOS as Apple continues to tighten platform security.

Stop now and rethink your approach.

If your primary concern is temporary items, macOS has a well-defined location for temporary items. You can locate this using various APIs, including:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer
I would like to confirm again if writing files in the notarized application MacOS directory is not allowed?

It depends on what you mean by “allowed”:

  • If you mean “Is this supported?” then the answer is “No.”

  • If you mean “Will the system block it in all cases?” then you already know the answer to that.

Lemme quote Embedding nonstandard code structures in a bundle:

A bundle is a read-only structure. All Apple platforms except the Mac enforce this requirement at runtime. On iOS, for example, any attempt to modify your app’s bundle at runtime will fail with an error. The Mac may or may not enforce this requirement at runtime, depending on the context, but modifying your app’s bundle isn’t supported because it breaks the seal on the app’s code signature.

You are doing something that’s unsupported and encountering weird problems. That’s kinda the nature of doing unsupported things. Sometimes it works, sometimes it doesn’t, and that can change from Mac to Mac, from release to release, and so on.

If you continue down this path:

  • You will have problems in some existing scenarios.

  • You are likely to have more problems on future versions of macOS as Apple continues to tighten platform security.

Stop now and rethink your approach.

If your primary concern is temporary items, macOS has a well-defined location for temporary items. You can locate this using various APIs, including:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for providing the information, we now understand that modifying the app bundle after codesign is not supported. Indeed, our situation has not been completely blocked, but since Apple does not support it, we will discuss internally how to handle this, thank you.

Unable to Write Files Within App Bundle After Codesigning and Notarization
 
 
Q