trilemma-dev / SecureXPC

A simple and secure XPC framework for Swift
MIT License
77 stars 15 forks source link

Fixes #128 - consistently determines location of CLI or app bundle #132

Closed jakaplan closed 1 year ago

jakaplan commented 1 year ago

This a rather complex fix as it turns out Bundle.main.bundleURL behaves differently for helper tools located within an .app bundle if they're located in Contents/MacOS/ vs Contents/Resources/. In the case of Contents/MacOS/ which I had previously never tested, the bundleURL returned is for the entire bundle while for the Contents/Resources/ case it returns a path to Resources.

This by itself wouldn't be too complicated because in these cases Bundle.main.executableURL will return a path to itself. However it's valid for a SMAppService agent or daemon to be an app bundle (and not just a command line tool) and we need to differentiate this situation otherwise it wouldn't be possible to identify the parent bundle.

Additionally it's valid for the case where the helper tool isn't in a bundle at all in which case Bundle.main.executableURL isn't applicable. As such _NSGetExecutablePath was used to consistently derive a path.