From eea87561ab13620cc1589fa1e7b2a63a0f818237 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 11 Apr 2021 21:52:09 -0700 Subject: [PATCH] Update XPC Service signing script Add entitlements to InstallerConnection and InstallerStatus services and add option to specify bundle id in the now renamed codesign_xpc_service.py script. --- Configurations/codesign_xpc_service.py | 93 +++++++++++++++++++ Configurations/make-release-package.sh | 4 + ...e-project.InstallerConnection.entitlements | 12 +++ ...arkle-project.InstallerStatus.entitlements | 12 +++ Sparkle.xcodeproj/project.pbxproj | 6 ++ bin/codesign_embedded_executable | 46 --------- 6 files changed, 127 insertions(+), 46 deletions(-) create mode 100644 Configurations/codesign_xpc_service.py create mode 100644 InstallerConnection/org.sparkle-project.InstallerConnection.entitlements create mode 100644 InstallerStatus/org.sparkle-project.InstallerStatus.entitlements delete mode 100755 bin/codesign_embedded_executable diff --git a/Configurations/codesign_xpc_service.py b/Configurations/codesign_xpc_service.py new file mode 100644 index 00000000..6c3836db --- /dev/null +++ b/Configurations/codesign_xpc_service.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +import os, sys, subprocess, tempfile, pathlib + +PRODUCT_BUNDLE_ID_MACRO = "$(PRODUCT_BUNDLE_IDENTIFIER)" + +def log_message(message): + sys.stderr.write(message + "\n") + +#Surround a token with double quotes if it has a space in it +def sanitize(argument): + return ('"' + argument + '"' if ' ' in argument else argument) + +def _codesign_service(identity, path, entitlements_path=None): + command = ["codesign", "-o", "runtime", "-fs", identity, path] + ([] if entitlements_path is None else ["--entitlements", entitlements_path]) + log_message(" ".join(map(sanitize, command))) + + process = subprocess.Popen(command, stdout=subprocess.PIPE) + process.communicate() + if process.returncode != 0: + log_message("Error: Failed to codesign %s" % (path)) + sys.exit(1) + +def codesign_service(service_name, identity, path, entitlements_path=None): + #Sign the auxiliary executables (if any) + executables_path = os.path.join(path, "Contents/MacOS/") + if os.path.exists(executables_path): + for executable in os.listdir(executables_path): + if executable != service_name: + _codesign_service(identity, os.path.join(executables_path, executable), entitlements_path) + + #Sign the service bundle + _codesign_service(identity, path, entitlements_path) + +# Returns entitlements back with a bundle id applied if needed +def applied_entitlements_path(entitlements_path, bundle_id, temp_dir): + entitlements_str = pathlib.Path(entitlements_path).read_text() + if PRODUCT_BUNDLE_ID_MACRO in entitlements_str: + if bundle_id is None: + return None + else: + new_entitlements_str = entitlements_str.replace(PRODUCT_BUNDLE_ID_MACRO, bundle_id) + new_entitlements_path = os.path.join(temp_dir, os.path.basename(entitlements_path)) + pathlib.Path(new_entitlements_path).write_text(new_entitlements_str) + return new_entitlements_path + else: + return entitlements_path + +def print_usage(): + log_message("Usage:\n\t%s code-signing-identity [-b app-bundle-id] service1 service2 ..." % (sys.argv[0])) + log_message("Example:\n\t%s \"Developer ID Application\" ./XPCServices/org.sparkle-project.InstallerLauncher.xpc" % (sys.argv[0])) + log_message("") + log_message("Use this script to sign XPC Services to include in your Sandboxed application.") + log_message("") + log_message("bundle-id is optional and only needs to be provided when signing InstallerConnection and InstallerStatus services. Most applications do not need to use these services and can provide additional entitlements to their application instead.") + log_message("") + log_message("This script may not be needed for common development workflows. Please visit https://sparkle-project.org/documentation/sandboxing/ for more details and instructions.") + +if __name__ == "__main__": + if len(sys.argv) < 3: + print_usage() + sys.exit(1) + + signing_identity = sys.argv[1] + remaining_args = sys.argv[2:] + bundle_id = None + if remaining_args[0] == '-b': + if len(remaining_args) < 3: + # We need the bundle-id and at least one XPC Service to sign + print_usage() + sys.exit(1) + else: + bundle_id = remaining_args[1] + services = remaining_args[2:] + else: + services = remaining_args + + with tempfile.TemporaryDirectory() as temp_dir: + for service in services: + parent, child = os.path.split(service) + service_name = os.path.splitext(child)[0] + entitlements_path = os.path.join(parent, service_name + ".entitlements") + if os.path.exists(entitlements_path): + service_entitlements_path = applied_entitlements_path(entitlements_path, bundle_id, temp_dir) + if service_entitlements_path is None: + print_usage() + log_message("") + log_message("Error: Bundle ID must be specified for signing %s. As noted in the usage above, you probably don't need to embed this service." % (service_name)) + sys.exit(1) + else: + service_entitlements_path = None + + codesign_service(service_name, signing_identity, service, service_entitlements_path) diff --git a/Configurations/make-release-package.sh b/Configurations/make-release-package.sh index ad52a215..6f28327b 100755 --- a/Configurations/make-release-package.sh +++ b/Configurations/make-release-package.sh @@ -26,6 +26,8 @@ if [ "$ACTION" = "" ] ; then cp "$CONFIGURATION_BUILD_DIR/generate_appcast" "$CONFIGURATION_BUILD_DIR/staging/bin" cp "$CONFIGURATION_BUILD_DIR/generate_keys" "$CONFIGURATION_BUILD_DIR/staging/bin" cp "$CONFIGURATION_BUILD_DIR/sign_update" "$CONFIGURATION_BUILD_DIR/staging/bin" + cp "$SRCROOT/Configurations/codesign_xpc_service.py" "$CONFIGURATION_BUILD_DIR/staging/bin/codesign_xpc_service" + chmod 0755 "$CONFIGURATION_BUILD_DIR/staging/bin/codesign_xpc_service" cp -R "$CONFIGURATION_BUILD_DIR/Sparkle Test App.app" "$CONFIGURATION_BUILD_DIR/staging" cp -R "$CONFIGURATION_BUILD_DIR/Sparkle Test App.app" "$CONFIGURATION_BUILD_DIR/staging-spm" cp -R "$CONFIGURATION_BUILD_DIR/sparkle.app" "$CONFIGURATION_BUILD_DIR/staging" @@ -43,6 +45,8 @@ if [ "$ACTION" = "" ] ; then cp -R "$CONFIGURATION_BUILD_DIR/$INSTALLER_STATUS_BUNDLE_ID.xpc" "$CONFIGURATION_BUILD_DIR/staging/XPCServices" cp "$SRCROOT/Downloader/$DOWNLOADER_BUNDLE_ID.entitlements" "$CONFIGURATION_BUILD_DIR/staging/XPCServices" + cp "$SRCROOT/InstallerConnection/$INSTALLER_CONNECTION_BUNDLE_ID.entitlements" "$CONFIGURATION_BUILD_DIR/staging/XPCServices" + cp "$SRCROOT/InstallerStatus/$INSTALLER_STATUS_BUNDLE_ID.entitlements" "$CONFIGURATION_BUILD_DIR/staging/XPCServices" # Only copy dSYMs for Release builds, but don't check for the presence of the actual files # because missing dSYMs in a release build SHOULD trigger a build failure diff --git a/InstallerConnection/org.sparkle-project.InstallerConnection.entitlements b/InstallerConnection/org.sparkle-project.InstallerConnection.entitlements new file mode 100644 index 00000000..fa7d47e6 --- /dev/null +++ b/InstallerConnection/org.sparkle-project.InstallerConnection.entitlements @@ -0,0 +1,12 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.temporary-exception.mach-lookup.global-name + + $(PRODUCT_BUNDLE_IDENTIFIER)-spki + + + diff --git a/InstallerStatus/org.sparkle-project.InstallerStatus.entitlements b/InstallerStatus/org.sparkle-project.InstallerStatus.entitlements new file mode 100644 index 00000000..0cceb6d0 --- /dev/null +++ b/InstallerStatus/org.sparkle-project.InstallerStatus.entitlements @@ -0,0 +1,12 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.temporary-exception.mach-lookup.global-name + + $(PRODUCT_BUNDLE_IDENTIFIER)-spks + + + diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index cd7b9deb..069ab392 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -1020,6 +1020,9 @@ 7214B8851D45AD9A00CB5CED /* SPUInstallationType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUInstallationType.h; sourceTree = ""; }; 72162B071C82C9600013C1C5 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = ""; }; 721652671D3C8FED00FD13D8 /* SUInstallerLauncherStatus.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SUInstallerLauncherStatus.h; path = InstallerLauncher/SUInstallerLauncherStatus.h; sourceTree = SOURCE_ROOT; }; + 7218EC362623F31C008FECF3 /* org.sparkle-project.InstallerConnection.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "org.sparkle-project.InstallerConnection.entitlements"; sourceTree = ""; }; + 7218EC372623F32E008FECF3 /* org.sparkle-project.InstallerStatus.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "org.sparkle-project.InstallerStatus.entitlements"; sourceTree = ""; }; + 7218EC4B2623F51C008FECF3 /* codesign_xpc_service.py */ = {isa = PBXFileReference; lastKnownFileType = text.script.python; path = codesign_xpc_service.py; sourceTree = ""; }; 721BC2061D17A532002BC71E /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; }; 721BC2081D17A553002BC71E /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; }; 721BC20A1D17A5AD002BC71E /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; }; @@ -2107,6 +2110,7 @@ 724BB36E1D31D0B7005D534A /* SUInstallerConnectionProtocol.h */, 724BB3851D32A167005D534A /* SUXPCInstallerConnection.h */, 724BB3861D32A167005D534A /* SUXPCInstallerConnection.m */, + 7218EC362623F31C008FECF3 /* org.sparkle-project.InstallerConnection.entitlements */, ); name = InstallerConnection; path = ../InstallerConnection; @@ -2123,6 +2127,7 @@ 7267E5DD1D3D8F5A00D1BF90 /* SUStatusInfoProtocol.h */, 724BB3A61D33461B005D534A /* SUXPCInstallerStatus.h */, 724BB3A71D33461B005D534A /* SUXPCInstallerStatus.m */, + 7218EC372623F32E008FECF3 /* org.sparkle-project.InstallerStatus.entitlements */, ); name = InstallerStatus; path = ../InstallerStatus; @@ -2372,6 +2377,7 @@ EA1E285722B6622E004AA304 /* ed25519-Release.xcconfig */, EA1E285922B6622E004AA304 /* ed25519-Shared.xcconfig */, 14732BC91960F70A00593899 /* make-release-package.sh */, + 7218EC4B2623F51C008FECF3 /* codesign_xpc_service.py */, 14652F7919A93E5F00959E44 /* set-git-version-info.sh */, 720B4C2325EBFAFD005A0592 /* link-tools.sh */, 146EC84E19A68CF8004A50C5 /* Sparkle.podspec */, diff --git a/bin/codesign_embedded_executable b/bin/codesign_embedded_executable deleted file mode 100755 index 2337949a..00000000 --- a/bin/codesign_embedded_executable +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python - -import os, sys, subprocess - -def log_message(message): - sys.stderr.write(message + "\n") - -#Surround a token with double quotes if it has a space in it -def sanitize(argument): - return ('"' + argument + '"' if ' ' in argument else argument) - -def _codesign_service(identity, path, entitlements_path=None): - command = ["codesign", "-o", "runtime", "-fs", identity, path] + ([] if entitlements_path is None else ["--entitlements", entitlements_path]) - log_message(" ".join(map(sanitize, command))) - - process = subprocess.Popen(command, stdout=subprocess.PIPE) - process.communicate() - if process.returncode != 0: - log_message("Error: Failed to codesign %s" % (path)) - sys.exit(1) - -def codesign_service(service_name, identity, path, entitlements_path=None): - #Sign the auxiliary executables (if any) - executables_path = os.path.join(path, "Contents/MacOS/") - if os.path.exists(executables_path): - for executable in os.listdir(executables_path): - if executable != service_name: - _codesign_service(identity, os.path.join(executables_path, executable), entitlements_path) - - #Sign the service bundle - _codesign_service(identity, path, entitlements_path) - -if __name__ == "__main__": - if len(sys.argv) < 3: - log_message("Usage:\n\t%s code-signing-identity executable1 executable1 ..." % (sys.argv[0])) - log_message("Example:\n\t%s \"Developer ID Application\" ./XPCServices/*.xpc" % (sys.argv[0])) - sys.exit(1) - - signing_identity = sys.argv[1] - executables = sys.argv[2:] - - for executable in executables: - parent, child = os.path.split(executable) - service_name = os.path.splitext(child)[0] - entitlements_path = os.path.join(parent, service_name + ".entitlements") - codesign_service(service_name, signing_identity, executable, None if not os.path.exists(entitlements_path) else entitlements_path)