Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable-3.14] Fix mac-crafter codesign executable detection #7542

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions admin/osx/mac-crafter/Sources/Utils/Codesign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ func isAppExtension(_ path: String) -> Bool {
path.hasSuffix(".appex")
}

func isExecutable(_ path: String) -> Bool {
let fm = FileManager.default
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: path, isDirectory: &isDir)
return fm.isExecutableFile(atPath: path) && !isDir.boolValue && exists
func isExecutable(_ path: String) throws -> Bool {
let outPipe = Pipe()
let errPipe = Pipe()
let task = Process()
task.standardOutput = outPipe
task.standardError = errPipe

let command = "file \"\(path)\""
guard run("/bin/zsh", ["-c", command], task: task) == 0 else {
throw CodeSigningError.failedToCodeSign("Failed to determine if \(path) is an executable.")
}

let outputData = outPipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: outputData, encoding: .utf8) ?? ""
return output.contains("Mach-O 64-bit executable")
}

func codesign(identity: String, path: String, options: String = defaultCodesignOptions) throws {
Expand All @@ -60,11 +70,11 @@ func recursivelyCodesign(
}

for case let enumeratedItem as String in pathEnumerator {
guard isLibrary(enumeratedItem) ||
isAppExtension(enumeratedItem) ||
isExecutable(enumeratedItem)
else { continue }
try codesign(identity: identity, path: "\(path)/\(enumeratedItem)")
let isExecutableFile = try isExecutable(fm.currentDirectoryPath + "/" + path + "/" + enumeratedItem)
guard isLibrary(enumeratedItem) || isAppExtension(enumeratedItem) || isExecutableFile else {
continue
}
try codesign(identity: identity, path: "\(path)/\(enumeratedItem)", options: options)
}
}

Expand Down Expand Up @@ -136,7 +146,4 @@ func codesignClientAppBundle(
// Now we do the final codesign bit
print("Code-signing Nextcloud Desktop Client binaries...")
try recursivelyCodesign(path: "\(clientContentsDir)/MacOS/", identity: codeSignIdentity)

print("Code-signing Nextcloud Desktop Client app bundle...")
try codesign(identity: codeSignIdentity, path: clientAppDir)
}
31 changes: 15 additions & 16 deletions admin/osx/mac-crafter/Sources/Utils/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,39 @@

import Foundation

var task: Process?
weak var globalTaskRef: Process?

@discardableResult
func run(
_ launchPath: String,
_ args: [String],
env: [String: String]? = nil,
quiet: Bool = false
quiet: Bool = false,
task: Process = Process()
) -> Int32 {
defer { task = nil }
task = Process()

globalTaskRef = task
signal(SIGINT) { _ in
task?.terminate() // Send terminate signal to the task
exit(0) // Exit the script after cleanup
globalTaskRef?.terminate() // Send terminate signal to the task
exit(0) // Exit the script after cleanup
}

task?.launchPath = launchPath
task?.arguments = args
task.launchPath = launchPath
task.arguments = args

if let env,
let combinedEnv = task?.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
let combinedEnv = task.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
{
task?.environment = combinedEnv
task.environment = combinedEnv
}

if quiet {
task?.standardOutput = nil
task?.standardError = nil
task.standardOutput = nil
task.standardError = nil
}

task?.launch()
task?.waitUntilExit()
return task?.terminationStatus ?? 1
task.launch()
task.waitUntilExit()
return task.terminationStatus
}

func run(
Expand Down
Loading