okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1#!/usr/bin/env bash 2# Sign the Strata Mac app (dmg or .app) and, optionally, the Mac Terminal archive with the owner's Developer ID, then notarise 3# them with Apple, so macOS opens them without the "damaged" / "unidentified developer" warning. 4# 5# bash sign-and-notarize.sh <Strata-tester-mac.dmg | Strata.app> [--terminal <Strata-Terminal-Mac.zip>] [--engine <engine-folder.tar.gz>] [--updater-archive] [--out DIR] [--dry-run] 6# bash sign-and-notarize.sh --terminal <Strata-Terminal-Mac.zip> # the Terminal archive alone 7# bash sign-and-notarize.sh --terminal <Terminal.tar.gz> --engine <strata-...-unsigned-folder.tar.gz> # both in one run 8# 9# This script runs on the OWNER'S Mac, where the certificate "Developer ID Application: Niko Veit (79LR4R7RSC)" is in the Keychain and the 10# notarytool keychain profile "strata-notary" exists. It never asks for, reads or stores a password or a key: codesign and notarytool 11# take them from the Keychain themselves. Nothing is uploaded except what notarytool sends to Apple's notary service. 12# 13# Environment overrides (the defaults are the owner's values): 14# SIGN_ID the signing identity (default: Developer ID Application: Niko Veit (79LR4R7RSC)) 15# NOTARY_PROFILE the notarytool keychain profile (default: strata-notary) 16# TMPDIR where the work folder is made (needs about 4 x the size of the input free) 17# 18# --dry-run: no Apple account is touched. Signs with the ad-hoc identity (SIGN_ID=-), skips notarytool/stapler/spctl-accept, and is 19# how this script is tested on a Mac that has no certificate. An ad-hoc signature has no Team ID, so in a dry run the hardened-runtime 20# "library validation" cannot be satisfied by libraries signed with the same identity; the dry run therefore adds 21# com.apple.security.cs.disable-library-validation to node and python ONLY so that the smoke tests can run. A real run never does. 22# 23# What it does, in order (each step prints OK or FAIL; the first FAIL stops the run and the summary says which): 24# 1. copies the .app out of the dmg into a temporary folder; 25# 2. signs every nested Mach-O inside-out (deepest first): hardened runtime, secure timestamp, entitlements only where needed; 26# 3. rewrites the SHA-256 and size of every engine file it signed in the engine package's engine-component.json (Control refuses an 27# engine whose files do not match that manifest), then signs the .app itself; 28# 4. codesign --verify --deep --strict, spctl -a -vvv; 29# 5. smoke tests of the SIGNED copy (the app's own --self-test, node loading every native addon, python imports, the engine's --version, 30# and Control's own engine manifest check), so a signing mistake shows up here and not on a tester's Mac; 31# 6. notarises the .app, staples it, rebuilds the dmg (same layout as apps/desktop/scripts/make-dmg.sh), signs the dmg, notarises the dmg, 32# staples it, spctl -a -t open --context context:primary-signature -vv; 33# 7. Terminal archive: signs every Mach-O inside, re-zips, notarises the zip (a zip cannot be stapled: see the note at the end). 34# 7b. --engine FOLDER.tar.gz (an engine package of the Mac that Control installs from the feed, a tar of one folder: its program, its 35# metal/ sources and engine-component.json): signs every Mach-O in it like the engine inside the app (hardened runtime, timestamp, no entitlements), 36# writes the signed files' SHA-256 and size into engine-component.json, codesign --verify --strict on each, checks that the signed 37# program starts, notarises a zip of the folder, and packs it again as <component>-<version>-<platform>-<backend>-folder.tar.gz (modes 38# kept). Sign the folder with the feed key AFTER this step, on the PC: signing changes the program, so the manifest changes with it. 39# 8. --updater-archive (opt-in, needs the app input): packs the signed, notarised, stapled .app into Strata.app.tar.gz, the file Tauri's in-app 40# updater downloads on a Mac (Strata.app at the root of the archive), and checks it by unpacking it again. The archive is NOT signed with the 41# Tauri updater key here: this script never touches that key. Sign it on the PC (npx tauri signer sign) and put it in the update file with 42# packaging/feed_tool.py set-app-platform (see packaging/UPDATES.md). Any Strata.app.tar.gz / .sig that `tauri build` made earlier holds the 43# UNSIGNED app and must not be published. 44set -u 45set -o pipefail 46 47SIGN_ID =" ${ SIGN_ID :-Developer ID Application: Niko Veit (79LR4R7RSC)} " 48NOTARY_PROFILE =" ${ NOTARY_PROFILE :- strata -notary} " 49HERE =" $( cd " $( dirname " ${ BASH_SOURCE [0]} " ) " && pwd ) " 50ENT_NODE_SRC =" $ HERE /entitlements-node.plist" 51 52INPUT ="" ;TERMINAL ="" ;ENGINE ="" ;OUT ="" ;DRY =0;KEEP =0;SIGN_NESTED =0;UPDATER_ARCHIVE =0 53WORK ="" 54LOG ="" 55declare -aRESULTS =() 56declare -aMOUNTS =() 57declare -aFINAL =() 58NOTARY_ID ="" 59 60usage () { 61sed -n '2,/^set -u$/p' " ${ BASH_SOURCE [0]} " | sed '$d' | sed 's/^# \{0,1\}//' 62exit " ${ 1 :-2} " 63} 64 65while [$ # -gt 0 ];do 66case " $ 1 " in 67 --terminal) [$ # -ge 2 ] ||usage ;TERMINAL =" $ 2 " ;shift 2 ;; 68 --engine) [$ # -ge 2 ] ||usage ;ENGINE =" $ 2 " ;shift 2 ;; 69 --out) [$ # -ge 2 ] ||usage ;OUT =" $ 2 " ;shift 2 ;; 70 --dry-run)DRY =1;shift ;; 71 --keep-work)KEEP =1;shift ;; 72 --sign-nested)SIGN_NESTED =1;shift ;; 73 --updater-archive)UPDATER_ARCHIVE =1;shift ;; 74 -h| --help)usage 0 ;; 75 -*)echo "unknown option: $ 1 " >&2;usage ;; 76 *)if [ -z" $ INPUT " ];then INPUT =" $ 1 " ;shift ;else echo "unexpected argument: $ 1 " >&2;usage ;fi ;; 77esac 78done 79[ -n" $ INPUT " ] || [ -n" $ TERMINAL " ] || [ -n" $ ENGINE " ] ||usage 80if [" $ UPDATER_ARCHIVE " -eq 1 ]&& [ -z" $ INPUT " ];then echo "--updater-archive needs the app (a .dmg or .app) as the input" >&2;usage ;fi 81 82# ----------------------------------------------------------------------------------------------------------------- output helpers 83say () {printf '%s\n' " $ *" ; [ -z" $ LOG " ] ||printf '%s\n' " $ *" >> " $ LOG " ; } 84rec () {RESULTS +=(" $ 1 | $ 2 | ${ 3 :-} " );say " [ $ 1 ] $ 2 ${ 3 :+: $ 3 } " ; } 85 86summary () { 87 localrc =$ 1 line st name detail f sum 88say "" 89say "================================ SUMMARY ================================" 90if [" ${# RESULTS [@]} " -gt 0 ];then 91for line in " ${ RESULTS [@]} " ;do 92st =" ${ line %%|*} " ;line =" ${ line #*|} " ;name =" ${ line %%|*} " ;detail =" ${ line #*|} " 93printf ' %-5s %s%s\n' " $ st " " $ name " " ${ detail :+ ( $ detail )} " 94done 95fi 96if [" ${# FINAL [@]} " -gt 0 ];then 97say "" 98say "Final files:" 99for f in " ${ FINAL [@]} " ;do 100sum =" $( shasum -a 256 " $ f " | awk '{print $1}' ) " 101say " $ f " 102say " sha256 $ sum " 103 [ -z" $ OUT " ] ||printf '%s %s\n' " $ sum " " $( basename " $ f " ) " >> " $ OUT /SHA256SUMS.txt" 104done 105fi 106say "" 107if [" $ rc " -eq 0 ];then 108if [" $ DRY " -eq 1 ];then say "RESULT: DRY RUN finished. Nothing was sent to Apple; these files are ad-hoc signed and are NOT for release." 109else say "RESULT: all steps OK." ;fi 110else 111say "RESULT: FAILED. Read the FAIL line above. Full log: ${ LOG :-none} " 112fi 113if [ -n" $ TERMINAL " ] || [ -n" $ ENGINE " ];then 114say "" 115say "Note on the Terminal zip and the engine folder: Apple cannot staple a ticket to bare command-line programs in a zip, so the zip carries no ticket. Notarisation is" 116say "still recorded at Apple by the programs' hashes, and macOS checks that online the first time a quarantined (browser-downloaded) copy is opened." 117say "Offline, the first open of such a copy needs a connection. Installs done with 'curl | sh' are not quarantined at all, so this mainly helps zips" 118say "downloaded in a browser. The zip's sha256 changed: any feed or checksum list that names the old zip must be rebuilt from this one." 119fi 120} 121 122finish () {summary " $ 1 " ;exit " $ 1 " ; } 123fail () {rec FAIL" $ 1 " " ${ 2 :-} " ;finish 1; } 124 125# shellcheck disable=SC2329 # run by the EXIT trap below 126cleanup () { 127 localm 128if [" ${# MOUNTS [@]} " -gt 0 ];then 129for m in " ${ MOUNTS [@]} " ;do hdiutil detach" $ m " -force > /dev/null2 >&1 ||true ;done 130fi 131if [ -n" $ WORK " ]&& [ -d" $ WORK " ];then 132if [" $ KEEP " -eq 1 ];then echo "work folder kept: $ WORK " >&2;else rm -rf " $ WORK " ;fi 133fi 134} 135trap cleanup EXIT 136trap 'echo "interrupted" >&2; exit 130' INT TERM HUP 137 138# run a command, appending all its output to the log; on failure show the last lines of it 139logged () { 140 localtmp rc 141tmp =" $( mktemp " ${ WORK :-${ TMPBASE :-/tmp}} /cmd.XXXXXX" ) " 142" $ @" > " $ tmp " 2 >&1;rc =$ ? 143cat " $ tmp " >> " ${ LOG :-/dev/null} " 144 [" $ rc " -eq 0 ] || {tail -n 12" $ tmp " | sed 's/^/ /' >&2; } 145rm -f " $ tmp " 146return " $ rc " 147} 148 149# run a command with a time limit (macOS has no timeout(1)); 124 = timed out 150run_timeout () { 151 localsecs =$ 1 i =0pid 152shift 153" $ @" & 154pid =$ ! 155while kill -0 " $ pid " 2 > /dev/null;do 156if [" $ i " -ge $((secs * 2)) ];then 157pkill -P " $ pid " 2 > /dev/null;kill " $ pid " 2 > /dev/null;sleep 1;kill -9 " $ pid " 2 > /dev/null 158wait " $ pid " 2 > /dev/null 159return 124 160fi 161sleep 0.5;i =$((i + 1)) 162done 163wait " $ pid " 164} 165 166# ----------------------------------------------------------------------------------------------------------------- preflight 167[" $( uname -s ) " = Darwin ] || {echo "This script runs on a Mac (it needs codesign, hdiutil, notarytool)." >&2;exit 2; } 168for tool in codesign hdiutil ditto shasum file xattr perl PlistBuddy;do 169if [" $ tool " = PlistBuddy ];then [ -x /usr/libexec/PlistBuddy ] || {echo "missing /usr/libexec/PlistBuddy" >&2;exit 2; } 170else command -v " $ tool " > /dev/null2 >&1 || {echo "missing tool: $ tool " >&2;exit 2; };fi 171done 172 173if [" $ DRY " -eq 1 ];then 174SIGN_ID ="-" 175else 176command -v xcrun> /dev/null2 >&1 || {echo "missing xcrun (install the Xcode command line tools: xcode-select --install)" >&2;exit 2; } 177fi 178if [" $ DRY " -eq 1 ];then : 179elif [" $ SIGN_ID " ="-" ];then 180echo "SIGN_ID=- is an ad-hoc signature, which Apple cannot notarise. Add --dry-run to test with it." >&2;exit 2 181fi 182 183[ -z" $ INPUT " ] || [ -e" $ INPUT " ] || {echo "not found: $ INPUT " >&2;exit 2; } 184[ -z" $ TERMINAL " ] || [ -f" $ TERMINAL " ] || {echo "not found: $ TERMINAL " >&2;exit 2; } 185[ -z" $ ENGINE " ] || [ -f" $ ENGINE " ] || {echo "not found: $ ENGINE " >&2;exit 2; } 186case " $ ENGINE " in "" | *.tar.gz| *.tgz) ;; *)echo "--engine takes the engine folder as a .tar.gz: $ ENGINE " >&2;exit 2 ;;esac 187 188if [ -z" $ OUT " ];then 189first =" ${ INPUT :-${ TERMINAL :- $ ENGINE }} " 190OUT =" $( cd " $( dirname " $ first " ) " && pwd ) /signed" 191fi 192mkdir -p " $ OUT " || {echo "cannot create $ OUT " >&2;exit 2; } 193OUT =" $( cd " $ OUT " && pwd ) " 194TMPBASE =" ${ TMPDIR :-/tmp} " ;TMPBASE =" ${ TMPBASE %/} " # macOS TMPDIR ends in "/": a "//" in the path broke the app self-test 195WORK =" $( mktemp -d " $ TMPBASE /strata-sign.XXXXXX" ) " || {echo "cannot make a work folder" >&2;exit 2; } 196LOG =" $ OUT /sign-and-notarize.log" 197: > " $ LOG " 198: > " $ OUT /SHA256SUMS.txt" 199say "Strata sign-and-notarize, $( date '+%Y-%m-%d %H:%M:%S' ) " 200say " identity : $ SIGN_ID " 201if [" $ DRY " -eq 1 ];then say " mode : DRY RUN (ad-hoc, no notarisation)" ;else say " profile : $ NOTARY_PROFILE " ;fi 202say " work : $ WORK " 203say " output : $ OUT " 204say "" 205 206need_kb =0 207for f in " $ INPUT " " $ TERMINAL " " $ ENGINE " ;do 208 [ -n" $ f " ] ||continue 209kb =" $( du -sk " $ f " | awk '{print $1}' ) " ;need_kb =$((need_kb +kb * 5)) 210done 211free_kb =" $( df -k " $ WORK " | awk 'NR==2 {print $4}' ) " 212if [" $ free_kb " -lt" $ need_kb " ];then 213fail "free space" "about $(( need_kb / 1048576)) GB needed in $ WORK , $(( free_kb / 1048576)) GB free; set TMPDIR to a bigger disk" 214fi 215 216if [" $ DRY " -eq 0 ];then 217if !security find-identity-v -p codesigning2 > /dev/null| grep -qF "\" $ SIGN_ID \"" ;then 218fail "signing identity" "\" $ SIGN_ID \" is not in your Keychain (security find-identity -v -p codesigning lists what is)" 219fi 220if !xcrun notarytool history--keychain-profile " $ NOTARY_PROFILE " > /dev/null2 >&1;then 221fail "notary profile" "profile \" $ NOTARY_PROFILE \" does not work (create it once: xcrun notarytool store-credentials $ NOTARY_PROFILE ...), or there is no internet" 222fi 223rec OK"preflight" "identity and notary profile found" 224else 225rec OK"preflight" "dry run, ad-hoc identity" 226fi 227 228# ----------------------------------------------------------------------------------------------------------------- entitlements 229# Every entitlement is a hole in the hardened runtime, so there are as few as work (each one was tested; see entitlements-node.plist). 230# node: com.apple.security.cs.allow-jit (V8 writes machine code into MAP_JIT memory; without it node dies at start with 231# "Failed to reserve virtual memory for CodeRange"). Nothing else: WebAssembly, regexp JIT and the native addons run with just this. 232# python, the engine, the app shell, rg, spawn-helper, every .dylib/.so/.node: none. (Python's ctypes callbacks and numpy run without any; 233# Metal compiles the engine's kernel sources through the system compiler service, which needs nothing from the engine process.) 234# disable-library-validation: NOT used. It would only be needed if a hardened program loaded a library signed by somebody else; every 235# library in these bundles is re-signed here with the one identity, so they share a Team ID. (Dry run only: see the header.) 236ENT_NODE =" $ ENT_NODE_SRC " 237ENT_DLV ="" 238if [" $ DRY " -eq 1 ];then 239ENT_DLV =" $ WORK /dlv.plist" 240ENT_NODE =" $ WORK /node-dryrun.plist" 241cp " $ ENT_NODE_SRC " " $ ENT_NODE " 242/usr/libexec/PlistBuddy -c "Add :com.apple.security.cs.disable-library-validation bool true" " $ ENT_NODE " > /dev/null 243printf '<?xml version="1.0" encoding="UTF-8"?>\n<plist version="1.0"><dict><key>com.apple.security.cs.disable-library-validation</key><true/></dict></plist>\n' > " $ ENT_DLV " 244fi 245[ -f" $ ENT_NODE_SRC " ] ||fail "entitlements" "missing $ ENT_NODE_SRC (keep it next to this script)" 246 247ent_for () { 248case " $( basename " $ 1 " ) " in 249 node)printf '%s' " $ ENT_NODE " ;; 250 python| python3| python3.[0-9]*)printf '%s' " $ ENT_DLV " ;;# empty in a real run 251 *)printf '' ;; 252esac 253} 254 255# ----------------------------------------------------------------------------------------------------------------- signing helpers 256# Every Mach-O (programs, .dylib, .so, .node, universal binaries), deepest path first. Object files cannot be signed and are skipped. 257find_macho () { 258find " $ 1 " -type f-print0 2 > /dev/null| xargs -0 file--mime-type 2 > /dev/null \ 259| sed -n 's/:[[:space:]]*application\/x-mach-binary$//p' | sed 's/ (for architecture [^)]*)$//' | sort -u \ 260| while IFS =read -r f;do 261case " $( file -b " $ f " ) " in *object*| *"core file" *)continue ;;esac 262printf '%s\n' " $ f " 263done \ 264| awk '{ n = gsub("/", "/"); print n "\t" $0 }' | sort -rn | cut -f2- 265} 266 267codesign_item () {# path [extra codesign args...] 268 localf =$ 1 ent try =1rc =1 269shift 270 local -aargs =(--force --sign" $ SIGN_ID " --options runtime) 271if [" $ SIGN_ID " ="-" ];then args +=(--timestamp=none);else args +=(--timestamp);fi 272ent =" $( ent_for " $ f " ) " 273 [ -z" $ ent " ] ||args +=(--entitlements" $ ent " ) 274while [" $ try " -le 3 ];do 275logged codesign" ${ args [@]} " " $ @" " $ f " && {rc =0;break ; } 276say " (attempt $ try of 3 failed; the timestamp server is sometimes slow)" 277try =$((try + 1));sleep 3 278done 279return " $ rc " 280} 281 282codesign_plain () {# a disk image: signature and secure timestamp, no hardened-runtime flag (that flag is for programs) 283 localf =$ 1 try =1 284 local -aargs =(--force --sign" $ SIGN_ID " ) 285if [" $ SIGN_ID " ="-" ];then args +=(--timestamp=none);else args +=(--timestamp);fi 286while [" $ try " -le 3 ];do 287logged codesign" ${ args [@]} " " $ f " && return 0 288try =$((try + 1));sleep 3 289done 290return 1 291} 292 293sign_tree () {# folder label: all Mach-O inside-out, then nested bundles 294 localdir =$ 1 label =$ 2 f n =0bad =0d 295while IFS =read -r f;do 296 [ -n" $ f " ] ||continue 297if codesign_item " $ f " ;then n =$((n + 1));else say " could not sign: $ f " ;bad =$((bad + 1));fi 298done < <( find_macho " $ dir " ) 299 [" $ bad " -eq 0 ] ||fail "sign $ label " " $ bad file(s) could not be signed (see the log)" 300# nested bundles (frameworks, helper apps, XPC services), deepest first; there are none in today's app 301while IFS =read -r d;do 302 [ -n" $ d " ] ||continue 303 [" $ d " !=" $ dir " ] ||continue 304if [ -f" $ d /Contents/Info.plist" ] || [ -f" $ d /Resources/Info.plist" ] || [ -d" $ d /Versions" ];then 305codesign_item " $ d " ||fail "sign $ label " "nested bundle $ d " 306fi 307done < <( find " $ dir " -type d \( -name '*.app' -o -name '*.framework' -o -name '*.xpc' -o -name '*.appex' \) 2 > /dev/null | awk '{ n = gsub("/", "/"); print n "\t" $0 }' | sort -rn | cut -f2- ) 308rec OK"sign $ label " " $ n Mach-O file(s) signed" 309} 310 311# The engine packages inside the bundle carry engine-component.json with the SHA-256 and size of every file; Control checks them before it 312# starts an engine. Signing changes the engine binary, so the manifest must be brought up to date, or the signed app would refuse its own engine. 313update_engine_manifests () {# folder label 314 localdir =$ 1 label =$ 2 m out rc n =0 315while IFS =read -r m;do 316 [ -n" $ m " ] ||continue 317out =" $( perl -MJSON::PP -MDigest::SHA -e ' 318my ($file) = @ARGV; local $/; open my $fh, "<", $file or die "cannot read $file\n"; my $raw = <$fh>; close $fh; 319$raw =~ s/^\xEF\xBB\xBF//; 320my $json = JSON::PP->new->utf8->canonical->pretty; my $d = $json->decode($raw); 321(my $dir = $file) =~ s{/[^/]+$}{}; 322if (defined $d->{signature}) { print "signed-manifest\n"; exit 3; } 323my $changed = 0; 324for my $e (@{ $d->{files} || [] }) { 325my $p = "$dir/$e->{path}"; next unless -f $p; 326open my $f, "<:raw", $p or die "cannot read $p\n"; my $sha = Digest::SHA->new(256); $sha->addfile($f); close $f; 327my $hex = $sha->hexdigest; my $size = -s $p; 328if (lc($e->{sha256}) ne $hex or $e->{bytes} != $size) { $e->{sha256} = $hex; $e->{bytes} = $size + 0; $changed++; } 329} 330if ($changed) { open my $o, ">", $file or die "cannot write $file\n"; print $o $json->encode($d); close $o; } 331print "$changed\n"; 332' " $ m " 2 >&1) " ;rc =$ ? 333if [" $ rc " -eq 3 ];then 334fail "engine manifest $ label " " $ m carries a release signature; changing a file would break it. Sign this engine package at its source, or re-sign its manifest with the release key" 335elif [" $ rc " -ne 0 ];then 336fail "engine manifest $ label " " $ m : $ out " 337fi 338n =$((n + 1));say " $ m : $ out entry(ies) updated" 339done < <( find " $ dir " -name engine-component.json -type f 2 > /dev/null) 340rec OK"engine manifests $ label " " $ n manifest(s) checked" 341} 342 343verify_signed () {# path label 344 localp =$ 1 label =$ 2 345if logged codesign--verify --deep --strict --verbose=2 " $ p " ;then rec OK"codesign --verify --deep --strict $ label " 346else fail "codesign --verify --deep --strict $ label " "see the log" ;fi 347} 348 349# notarytool submit --wait; sets NOTARY_ID. On anything but Accepted, fetches Apple's log and fails. 350notarize () {# file label 351 localf =$ 1 label =$ 2 out status 352out =" $ WORK /notary- $ label .txt" 353say " sending $( basename " $ f " ) to Apple ( $( du -h " $ f " | awk '{print $1}' ) ); this waits for the result, usually 2 to 15 minutes ..." 354xcrun notarytool submit" $ f " --keychain-profile " $ NOTARY_PROFILE " --wait --timeout 60m> " $ out " 2 >&1 355cat " $ out " >> " $ LOG " 356NOTARY_ID =" $( awk '/^ *id:/ {print $2; exit}' " $ out " ) " 357status =" $( awk '/^ *status:/ {s = $2} END {print s}' " $ out " ) " 358if [" $ status " ="Accepted" ];then 359rec OK"notarise $ label " "Accepted, submission $ NOTARY_ID " 360return 0 361fi 362if [ -n" $ NOTARY_ID " ];then 363xcrun notarytool log--keychain-profile " $ NOTARY_PROFILE " " $ NOTARY_ID " " $ OUT /notary-log- $ label .json" >> " $ LOG " 2 >&1 364say "" 365say "---- Apple's notarisation log for $ label (submission $ NOTARY_ID ), also saved to $ OUT /notary-log- $ label .json ----" 366cat " $ OUT /notary-log- $ label .json" 2 > /dev/null| tee -a " $ LOG " 367say "-------------------------------------------------------------------------------" 368else 369tail -n 15" $ out " | sed 's/^/ /' 370fi 371fail "notarise $ label " "status: ${ status :-no answer from notarytool} " 372} 373 374# a finished file goes to the output folder only when every step for it has passed, so a half-done file is never mistaken for a release 375deliver () {# file original 376 localf =$ 1 orig =$ 2 dest 377dest =" $ OUT / $( basename " $ f " ) " 378if [ -e" $ dest " ]&& [" $ dest " -ef" $ orig " ];then fail "write the result" " $ dest is the input file itself; choose another --out folder" ;fi 379cp -p " $ f " " $ dest " ||fail "write the result" "cannot write $ dest " 380FINAL +=(" $ dest " ) 381} 382 383zip_dir () {# src_dir out.zip (keeps symlinks and permissions, no AppleDouble clutter) 384ditto -c -k --norsrc --noextattr --noqtn --noacl --keepParent " $ 1 " " $ 2 " 385} 386 387# ----------------------------------------------------------------------------------------------------------------- smoke tests (signed copy) 388smoke_app () {# app 389 localapp =$ 1 res =" $ 1 /Contents/Resources" home =" $ WORK /smokehome" rep =" $ WORK /self-test.json" node py ctl rc f fails =0out r engines =0 390mkdir -p " $ home " 391node =" $ res /node/node" ;ctl =" $ res /control" ;py =" $ ctl /python/bin/python3" 392export PYTHONDONTWRITEBYTECODE =1PYTHONNOUSERSITE =1# a smoke test must not write into the sealed bundle 393 394# 1. the app's own headless self-test: private node, host, UI, Control on the bundled python 395run_timeout 150 env HOME=" $ home " " $ app /Contents/MacOS/strata-desktop" --self-test= " $ rep " > " $ WORK /selftest.out" 2 >&1;rc =$ ? 396cat " $ WORK /selftest.out" >> " $ LOG " 397if [" $ rc " -eq 0 ];then rec OK"smoke: app --self-test (host, UI, Control)" 398else 399grep -E '"(name|ok|detail)"' " $ rep " 2 > /dev/null| paste - - - | grep 'false' | sed 's/^/ /' 400fail "smoke: app --self-test" "exit $ rc ; the signed app cannot start its host (see the log)" 401fi 402 403# 2. every native addon loads in the signed, hardened node (library validation, JIT) 404if [ -x" $ node " ];then 405out =" $ WORK /addons.txt" ;: > " $ out " 406while IFS =read -r f;do 407case " $( file -b " $ f " ) " in *arm64*) ;; *)continue ;;esac 408r =" $( " $ node " -e 'try { process.dlopen({ exports: {} }, process.argv[1]); console.log("OK"); } catch (e) { console.log("ERR " + String(e.message).split("\n")[0]); }' " $ f " 2 >&1 | head -n 1) " 409 [ -n" $ r " ] ||r ="ERR killed by the system (code signature)" 410printf '%s: %s\n' " $ f " " $ r " >> " $ out " 411done < <( find " $ res " -name '*.node' -type f 2 > /dev/null) 412cat " $ out " >> " $ LOG " 413if grep -qiE 'code signature|team id|library validation|not valid for use|killed' " $ out " ;then 414grep -iE 'code signature|team id|library validation|not valid for use' " $ out " | sed 's/^/ /' 415fail "smoke: node loads its native addons" "library validation refuses an addon (a library with a different signature)" 416fi 417if !" $ node " -e 'WebAssembly.instantiate(new Uint8Array([0,97,115,109,1,0,0,0])).then(()=>{let s=0;for(let i=0;i<2e6;i++)s+=i%7;if(!/a+b/.test("aab"))process.exit(3);console.log("jit ok")})' >> " $ LOG " 2 >&1;then 418fail "smoke: node JIT and WebAssembly" "the hardened node cannot run JIT code (entitlement missing?)" 419fi 420rec OK"smoke: node JIT, WebAssembly and native addons" " $( grep -c ': OK' " $ out " ) addon(s) loaded" 421else 422rec SKIP"smoke: node" "no bundled node" 423fi 424 425# 3. Control's python imports its compiled modules 426if [ -x" $ py " ];then 427if (cd " $ ctl " && env HOME=" $ home " " $ py " -c ' 428import ctypes, importlib, sys 429for m in ("numpy", "PIL.Image", "psutil", "yaml", "regex", "charset_normalizer", "markupsafe", "ssl", "sqlite3", "zlib", "lzma", "hashlib"): 430try: importlib.import_module(m) 431except ModuleNotFoundError: pass 432cb = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)(lambda a, b: 0) 433arr = (ctypes.c_int * 4)(4, 3, 2, 1); ctypes.CDLL(None).qsort(arr, 4, 4, cb) 434print("python ok", sys.version.split()[0])' >> " $ LOG " 2 >&1);then rec OK"smoke: bundled python imports and ctypes callbacks" 435else fail "smoke: bundled python" "an import or a ctypes callback failed under the hardened runtime (see the log)" ;fi 436fi 437 438# 4. every bundled engine starts, and Control accepts its manifest (hashes of the signed files) 439while IFS =read -r f;do 440 [ -n" $ f " ] ||continue 441engines =$((engines + 1)) 442if run_timeout 30" $ f " --version > " $ WORK /engine-version.txt" 2 >&1;then say " $( head -n 1 " $ WORK /engine-version.txt" ) " ;else 443cat " $ WORK /engine-version.txt" >> " $ LOG " ;fails =$((fails + 1));say " engine did not start: $ f " 444fi 445done < <( find " $ res /engine-bundle" -mindepth 2 -maxdepth 2 -type f \( -name strata -o -name strata-device -o -name strata-vision \) 2 > /dev/null) 446 [" $ fails " -eq 0 ] ||fail "smoke: bundled engine starts" " $ fails engine(s) did not start under the hardened runtime" 447if [ -x" $ py " ]&& [ -d" $ res /engine-bundle" ];then 448out =" $( cd " $ ctl " && env HOME= " $ home " " $ py " -c ' 449import sys, pathlib 450from strata_control import engine_component as ec 451bad = 0; n = 0 452for m in sorted(pathlib.Path(sys.argv[1]).glob("*/engine-component.json")): 453n += 1 454try: 455c = ec.read_manifest(m.parent); ec.verify(c, full=True); print("manifest ok", c.version, c.platform, c.backend) 456except Exception as e: 457bad += 1; print("MANIFEST PROBLEM", m, type(e).__name__, e) 458sys.exit(1 if bad or not n else 0)' " $ res /engine-bundle" 2 >&1) " ;rc =$ ? 459printf '%s\n' " $ out " >> " $ LOG " 460if [" $ rc " -eq 0 ];then rec OK"smoke: engine manifest matches the signed files (Control's own check)" 461else printf '%s\n' " $ out " | sed 's/^/ /' ;fail "smoke: engine manifest" "Control would refuse the signed engine" ;fi 462fi 463if [" $ engines " -gt 0 ];then rec OK"smoke: bundled engine --version" " $ engines engine(s), no model loaded" ;else rec SKIP"smoke: bundled engine" "this app carries no engine" ;fi 464} 465 466smoke_terminal () {# extracted terminal folder 467 localroot =$ 1 py f strata home =" $ WORK /termhome" out 468mkdir -p " $ home " 469export PYTHONDONTWRITEBYTECODE =1PYTHONNOUSERSITE =1 470py =" $( find " $ root " -maxdepth 4 -path '*/python/bin/python3' \( -type f -o -type l \) 2 > /dev/null | head -n 1) " 471if [ -n" $ py " ];then 472if env HOME=" $ home " " $ py " -c 'import ctypes, ssl, sqlite3, zlib; print("python ok")' >> " $ LOG " 2 >&1;then rec OK"smoke: terminal python starts" 473else fail "smoke: terminal python" "does not start under the hardened runtime" ;fi 474strata =" $( dirname " $ py " ) /../../bin/strata" 475if [ -x" $ strata " ];then 476if out =" $( env HOME= " $ home " STRATA_NO_ACQUIRE=1 " $ strata " --version 2 >&1) " ;then rec OK"smoke: strata --version" " $( printf '%s' " $ out " | head -n 1) " 477else printf '%s\n' " $ out " | tail -n 5| sed 's/^/ /' ;fail "smoke: strata --version" "the Terminal launcher does not start" ;fi 478fi 479else 480rec SKIP"smoke: terminal python" "no bundled python in this archive" 481fi 482while IFS =read -r f;do 483 [ -n" $ f " ] ||continue 484run_timeout 30" $ f " --version >> " $ LOG " 2 >&1 ||fail "smoke: engine in the Terminal archive" " $ f does not start" 485done < <( find " $ root " -type f \( -name strata -o -name strata-device -o -name strata-vision \) -perm -u+x 2 > /dev/null | while IFS = read -r c; do file " $ c " | grep -q 'Mach-O' && printf '%s\n' " $ c " ; done ) 486} 487 488# ----------------------------------------------------------------------------------------------------------------- the app 489process_app () { 490 localapp_src ="" appdir =" $ WORK /app" app version vol dmg_name zip out_dmg mnt bundle_real 491 492mkdir -p " $ appdir " 493if [ -d" $ INPUT " ]&& { [[" $ INPUT " == *.app ]] || [[" $ INPUT " == *.app/ ]]; };then 494app_src =" ${ INPUT %/} " 495ditto --norsrc --noextattr --noqtn " $ app_src " " $ appdir / $( basename " $ app_src " ) " ||fail "copy the app" "ditto failed" 496app =" $ appdir / $( basename " $ app_src " ) " 497dmg_name ="Strata.dmg" 498rec OK"copy the app" "from $ INPUT " 499elif [ -f" $ INPUT " ]&& [[" $ INPUT " == *.dmg ]];then 500mnt =" $ WORK /mnt-in" ;mkdir -p " $ mnt " 501logged hdiutil attach-nobrowse -readonly -noautoopen -mountpoint " $ mnt " " $ INPUT " ||fail "open the dmg" "hdiutil attach failed (is it already mounted?)" 502MOUNTS +=(" $ mnt " ) 503app_src =" $( find " $ mnt " -maxdepth 1 -name '*.app' -type d | head -n 1) " 504 [ -n" $ app_src " ] ||fail "open the dmg" "no .app at the top of $ INPUT " 505ditto --norsrc --noextattr --noqtn " $ app_src " " $ appdir / $( basename " $ app_src " ) " ||fail "copy the app" "ditto failed" 506hdiutil detach" $ mnt " > /dev/null2 >&1 ||hdiutil detach" $ mnt " -force > /dev/null2 >&1 507app =" $ appdir / $( basename " $ app_src " ) " 508dmg_name =" $( basename " $ INPUT " ) " 509rec OK"copy the app out of the dmg" " $( basename " $ app " ) ( $( du -sh " $ app " | awk '{print $1}' ) )" 510else 511fail "input" " $ INPUT is neither a .dmg nor a .app" 512fi 513 [ -f" $ app /Contents/Info.plist" ] ||fail "input" " $ app has no Contents/Info.plist" 514xattr -cr " $ app " 2 > /dev/null 515version =" $( /usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' " $ app /Contents/Info.plist" 2 > /dev/null || echo "" ) " 516 517sign_tree " $ app " "app contents" 518update_engine_manifests " $ app " "app" 519if codesign_item " $ app " ;then rec OK"sign the .app itself" ;else fail "sign the .app itself" "see the log" ;fi 520 521verify_signed " $ app " "app" 522if spctl -a -vvv -t exec" $ app " >> " $ LOG " 2 >&1;then rec OK"spctl -a -vvv app" "accepted" 523elif [" $ DRY " -eq 1 ];then rec INFO"spctl -a -vvv app" "rejected, as expected for an ad-hoc dry run" 524else rec INFO"spctl -a -vvv app" "rejected before notarisation (source=Unnotarized Developer ID); it is checked again after stapling" ;fi 525 526smoke_app " $ app " 527verify_signed " $ app " "app after the smoke tests (nothing in the sealed bundle changed)" 528 529bundle_real =" $ app " 530if [" $ DRY " -eq 0 ];then 531zip =" $ WORK / $( basename " $ app " .app) -notarize.zip" 532zip_dir " $ app " " $ zip " ||fail "zip the app for Apple" "ditto failed" 533notarize " $ zip " "app" 534logged xcrun stapler staple" $ app " ||fail "staple the app" "stapler failed" 535logged xcrun stapler validate" $ app " ||fail "staple the app" "stapler validate failed" 536rec OK"staple the app" 537if spctl -a -vvv -t exec" $ app " >> " $ LOG " 2 >&1;then rec OK"spctl -a -vvv app (after notarisation)" "accepted, Notarized Developer ID" 538else fail "spctl -a -vvv app (after notarisation)" "Gatekeeper still rejects the stapled app" ;fi 539rm -f " $ zip " 540fi 541 542# the dmg, same layout as apps/desktop/scripts/make-dmg.sh: the app, an Applications link, the volume icon, no Finder arrangement 543vol ="Strata ${ version :-app} " 544mkdir -p " $ WORK /final" 545out_dmg =" $ WORK /final/ $ dmg_name " 546build_dmg " $ bundle_real " " $ out_dmg " " $ vol " ||fail "build the dmg" "hdiutil failed (see the log)" 547rec OK"build the dmg" " $ vol " 548codesign_plain " $ out_dmg " ||fail "sign the dmg" "see the log" 549rec OK"sign the dmg" 550if [" $ DRY " -eq 0 ];then 551notarize " $ out_dmg " "dmg" 552logged xcrun stapler staple" $ out_dmg " ||fail "staple the dmg" "stapler failed" 553logged xcrun stapler validate" $ out_dmg " ||fail "staple the dmg" "stapler validate failed" 554rec OK"staple the dmg" 555if spctl -a -t open--context context:primary-signature-vv " $ out_dmg " >> " $ LOG " 2 >&1;then rec OK"spctl -a -t open --context context:primary-signature -vv dmg" "accepted" 556else fail "spctl on the dmg" "Gatekeeper rejects the notarised dmg" ;fi 557else 558rec INFO"notarise, staple and spctl on the dmg" "skipped in a dry run" 559fi 560logged hdiutil verify" $ out_dmg " ||fail "hdiutil verify" "the dmg is damaged" 561rec OK"hdiutil verify the dmg" 562 563# the app inside the finished dmg is still the signed one 564mnt =" $ WORK /mnt-out" ;mkdir -p " $ mnt " 565if logged hdiutil attach-nobrowse -readonly -noautoopen -mountpoint " $ mnt " " $ out_dmg " ;then 566MOUNTS +=(" $ mnt " ) 567if logged codesign--verify --deep --strict " $ mnt / $( basename " $ app " ) " ;then rec OK"the app inside the final dmg verifies" 568else hdiutil detach" $ mnt " -force > /dev/null2 >&1;fail "the app inside the final dmg" "codesign --verify failed" ;fi 569hdiutil detach" $ mnt " > /dev/null2 >&1 ||hdiutil detach" $ mnt " -force > /dev/null2 >&1 570fi 571deliver " $ out_dmg " " $ INPUT " 572if [" $ UPDATER_ARCHIVE " -eq 1 ];then make_updater_archive " $ bundle_real " ;fi 573} 574 575make_updater_archive () {# the finished (signed, stapled) .app 576 localapp =$ 1 name chk top out 577name =" $( basename " $ app " .app) .app.tar.gz" 578out =" $ WORK /final/ $ name " 579rm -f " $ out " 580# the layout Tauri's bundler makes and its updater unpacks: the .app folder at the root of a gzip tar, nothing else 581# (COPYFILE_DISABLE keeps AppleDouble "._" files out; tar keeps symlinks and permissions) 582 (cd " $( dirname " $ app " ) " && COPYFILE_DISABLE =1tar -czf " $ out " " $( basename " $ app " ) " ) ||fail "pack the updater archive" "tar failed" 583chk =" $ WORK /updater-check" ;rm -rf " $ chk " ;mkdir -p " $ chk " 584tar -xzf " $ out " -C " $ chk " ||fail "check the updater archive" "tar cannot unpack it" 585top =" $( ls -A " $ chk " ) " 586 [" $ top " =" $( basename " $ app " ) " ] ||fail "check the updater archive" "the archive root holds ' $ top ', not just $( basename " $ app " ) " 587find " $ chk " -name '._*' | grep -q .&& fail "check the updater archive" "AppleDouble files in the archive" 588if logged codesign--verify --deep --strict " $ chk / $ top " ;then rec OK"updater archive: the unpacked app verifies" 589else fail "updater archive" "codesign --verify failed on the unpacked app" ;fi 590if [" $ DRY " -eq 0 ];then 591logged xcrun stapler validate" $ chk / $ top " ||fail "updater archive" "the unpacked app has no valid notarisation ticket" 592if spctl -a -vvv -t exec" $ chk / $ top " >> " $ LOG " 2 >&1;then rec OK"updater archive: spctl accepts the unpacked app" "Notarized Developer ID" 593else fail "updater archive" "Gatekeeper rejects the unpacked app" ;fi 594else 595rec INFO"updater archive: notarisation checks" "skipped in a dry run" 596fi 597rm -rf " $ chk " 598rec OK"pack the updater archive" " $ name " 599deliver " $ out " " $ INPUT " 600} 601 602build_dmg () {# app out.dmg volname 603 localapp =$ 1 out =$ 2 vol =$ 3 rw =" $ WORK /rw.dmg" mnt =" $ WORK /mnt-build" kb mb n =0icon 604kb =" $( du -sk " $ app " | awk '{print $1}' ) " ;mb =$((kb * 13 / 10240 + 80)) 605rm -f " $ rw " " $ out " 606logged hdiutil create-size " ${ mb } m" -fs HFS+-volname " $ vol " -type UDIF-ov " $ rw " ||return 1 607mkdir -p " $ mnt " 608logged hdiutil attach-nobrowse -noautoopen -noverify -mountpoint " $ mnt " " $ rw " ||return 1 609MOUNTS +=(" $ mnt " ) 610ditto --norsrc --noextattr --noqtn " $ app " " $ mnt / $( basename " $ app " ) " ||return 1 611ln -s /Applications" $ mnt /Applications" 612icon =" $ app /Contents/Resources/Strata.icns" 613if [ -f" $ icon " ]&& command -v SetFile> /dev/null2 >&1;then cp " $ icon " " $ mnt /.VolumeIcon.icns" && SetFile -a C" $ mnt " 2 > /dev/null;fi 614sync 615until hdiutil detach" $ mnt " > /dev/null2 >&1;do 616n =$((n + 1)); [" $ n " -lt 6 ] || {hdiutil detach" $ mnt " -force > /dev/null2 >&1;break ; } 617sleep 2 618done 619logged hdiutil convert" $ rw " -format UDZO-imagekey zlib-level=9-o " $ out " ||return 1 620rm -f " $ rw " 621} 622 623# ----------------------------------------------------------------------------------------------------------------- the Terminal archive 624repack_nested () {# extracted root: sign inside .tar.gz/.tgz/.zip archives within the archive (opt-in) 625 localroot =$ 1 a tmp kind 626while IFS =read -r a;do 627 [ -n" $ a " ] ||continue 628tmp =" $ WORK /nested. $ $. $ RANDOM " ;mkdir -p " $ tmp " 629case " $ a " in 630 *.zip)kind =zip;ditto -x -k " $ a " " $ tmp " ;; 631 *)kind =tar;tar -xzf " $ a " -C " $ tmp " ;; 632esac || {rm -rf " $ tmp " ;continue ; } 633if [ -z" $( find_macho " $ tmp " | head -n 1) " ];then rm -rf " $ tmp " ;continue ;fi 634sign_tree " $ tmp " "nested $( basename " $ a " ) " 635update_engine_manifests " $ tmp " "nested $( basename " $ a " ) " 636if [" $ kind " = zip ];then 637 (cd " $ tmp " && ditto -c -k --norsrc --noextattr --noqtn --noacl ." $ a " ) 638else 639 (cd " $ tmp " && COPYFILE_DISABLE =1tar -czf " $ a " . ) 640fi 641rm -rf " $ tmp " 642say " re-packed $( basename " $ a " ) ; a signed feed that pins its hash (strata-feed.json) must be regenerated and re-signed with the release key" 643done < <( find " $ root " -type f \( -name '*.tar.gz' -o -name '*.tgz' -o -name '*.zip' \) 2 > /dev/null) 644} 645 646process_terminal () { 647 localsrc =$ TERMINAL tdir =" $ WORK /terminal" top stem final notarize_zip name tops nested 648name =" $( basename " $ src " ) " 649mkdir -p " $ tdir " 650case " $ name " in 651 *.zip)ditto -x -k " $ src " " $ tdir " ||fail "unpack the Terminal archive" "ditto failed" ;; 652 *.tar.gz| *.tgz)tar -xzf " $ src " -C " $ tdir " ||fail "unpack the Terminal archive" "tar failed" ;; 653 *)fail "unpack the Terminal archive" " $ name is not a .zip or .tar.gz" ;; 654esac 655rm -rf " $ tdir /__MACOSX" 656xattr -cr " $ tdir " 2 > /dev/null 657rec OK"unpack the Terminal archive" " $ name " 658 659if [ -z" $( find_macho " $ tdir " | head -n 1) " ];then 660nested =" $( find " $ tdir " -type f \( -name '*.tar.gz' -o -name '*.tgz' -o -name '*.zip' \) 2 > /dev/null | wc -l | tr -d ' ' ) " 661if [" $ SIGN_NESTED " -eq 0 ]&& [" $ nested " -gt 0 ];then 662rec INFO"Terminal archive holds no program directly" "it contains $ nested nested archive(s) (a feed folder); use --sign-nested to sign inside them" 663fi 664fi 665sign_tree " $ tdir " "terminal" 666update_engine_manifests " $ tdir " "terminal" 667 [" $ SIGN_NESTED " -eq 0 ] ||repack_nested " $ tdir " 668if [" $ SIGN_NESTED " -eq 0 ];then 669nested =" $( find " $ tdir " -type f \( -name '*.tar.gz' -o -name '*.tgz' \) 2 > /dev/null | wc -l | tr -d ' ' ) " 670 [" $ nested " -eq 0 ] ||say " $ nested nested .tar.gz archive(s) were left untouched (they are installed by command-line tools and are not quarantined)" 671fi 672 673 localbad =0f 674while IFS =read -r f;do 675 [ -n" $ f " ] ||continue 676codesign --verify --strict " $ f " >> " $ LOG " 2 >&1 || {say " bad signature: $ f " ;bad =$((bad + 1)); } 677done < <( find_macho " $ tdir " ) 678 [" $ bad " -eq 0 ] ||fail "codesign --verify --strict (terminal)" " $ bad file(s)" 679rec OK"codesign --verify --strict (terminal)" "every Mach-O" 680 681smoke_terminal " $ tdir " 682 683# pack it again in the format it came in; Apple only takes a zip, so a tar.gz is notarised through a zip of the same files 684tops =" $( find " $ tdir " -mindepth 1 -maxdepth 1 | wc -l | tr -d ' ' ) " 685stem =" ${ name %.tar.gz} " ;stem =" ${ stem %.tgz} " ;stem =" ${ stem %.zip} " 686mkdir -p " $ WORK /final" 687case " $ name " in *.zip)final =" $ WORK /final/ $ stem .zip" ;; *)final =" $ WORK /final/ $ stem .tar.gz" ;;esac 688rm -f " $ final " 689notarize_zip =" $ WORK / $ stem -notarize.zip" ;rm -f " $ notarize_zip " 690if [" $ tops " -eq 1 ];then 691top =" $( find " $ tdir " -mindepth 1 -maxdepth 1) " 692zip_dir " $ top " " $ notarize_zip " ||fail "pack the Terminal archive" "ditto failed" 693case " $ name " in *.zip) ;; *) (cd " $ tdir " && COPYFILE_DISABLE =1tar -czf " $ final " " $( basename " $ top " ) " ) ||fail "pack the Terminal archive" "tar failed" ;;esac 694else 695 (cd " $ tdir " && ditto -c -k --norsrc --noextattr --noqtn --noacl ." $ notarize_zip " ) ||fail "pack the Terminal archive" "ditto failed" 696case " $ name " in *.zip) ;; *) (cd " $ tdir " && COPYFILE_DISABLE =1tar -czf " $ final " . ) ||fail "pack the Terminal archive" "tar failed" ;;esac 697fi 698case " $ name " in *.zip)cp " $ notarize_zip " " $ final " ;;esac 699rec OK"pack the Terminal archive" " $( basename " $ final " ) " 700 701if [" $ DRY " -eq 0 ];then 702notarize " $ notarize_zip " "terminal" 703rec INFO"staple the Terminal archive" "not possible for bare programs in a zip; Apple holds the ticket online" 704else 705rec INFO"notarise the Terminal archive" "skipped in a dry run" 706fi 707deliver " $ final " " $ src " 708} 709 710# ----------------------------------------------------------------------------------------------------------------- an engine package folder 711# A Mac engine package that Control installs from the update feed (a model shape's engine: its program, its metal/ sources, 712# engine-component.json) as a .tar.gz of ONE folder. The Qwen engine gets its Developer ID signature as a nested program of the signed 713# app; this does the same for a package outside the app. Same identity and options as every nested program above; no entitlements. 714process_engine () { 715 localsrc =$ ENGINE edir =" $ WORK /engine" name stem top tops final notarize_zip bin f bad =0n rc 716name =" $( basename " $ src " ) " 717mkdir -p " $ edir " 718tar -xzf " $ src " -C " $ edir " ||fail "unpack the engine folder" "tar failed" 719rm -rf " $ edir /__MACOSX" 720xattr -cr " $ edir " 2 > /dev/null 721tops =" $( find " $ edir " -mindepth 1 -maxdepth 1 | wc -l | tr -d ' ' ) " 722 [" $ tops " -eq 1 ] ||fail "unpack the engine folder" " $ name must hold exactly one folder at its top, found $ tops entries" 723top =" $( find " $ edir " -mindepth 1 -maxdepth 1) " 724 [ -d" $ top " ] ||fail "unpack the engine folder" " $ name holds a file at its top, not a folder" 725 [ -f" $ top /engine-component.json" ] ||fail "unpack the engine folder" " $( basename " $ top " ) has no engine-component.json (is this an engine package?)" 726rec OK"unpack the engine folder" " $ name ( $( basename " $ top " ) )" 727 728n =" $( find_macho " $ edir " | wc -l | tr -d ' ' ) " 729 [" $ n " -gt 0 ] ||fail "engine folder" "no Mach-O program found in $ name " 730sign_tree " $ edir " "engine package" 731update_engine_manifests " $ edir " "engine package" 732 733while IFS =read -r f;do 734 [ -n" $ f " ] ||continue 735codesign --verify --strict " $ f " >> " $ LOG " 2 >&1 || {say " bad signature: $ f " ;bad =$((bad + 1)); } 736done < <( find_macho " $ edir " ) 737 [" $ bad " -eq 0 ] ||fail "codesign --verify --strict (engine package)" " $ bad file(s)" 738rec OK"codesign --verify --strict (engine package)" "every Mach-O" 739 740# the signed program starts under the hardened runtime (a signing mistake kills it at once, exit 137); no model is loaded 741bin =" $( grep -o '"entrypoint"[^,]*' " $ top /engine-component.json" 2 > /dev/null | sed 's/.*: *"\(.*\)"/\1/' | head -n 1) " 742if [ -n" $ bin " ]&& [ -x" $ top / $ bin " ];then 743run_timeout 30" $ top / $ bin " --help > " $ WORK /engine-help.txt" 2 >&1;rc =$ ? 744cat " $ WORK /engine-help.txt" >> " $ LOG " 745if [" $ rc " -ge 128 ];then fail "smoke: engine package starts" " $ bin was killed (exit $ rc ) under the hardened runtime" 746else rec OK"smoke: engine package starts" " $ bin , no model loaded" ;fi 747else 748rec SKIP"smoke: engine package" "no entrypoint program found in engine-component.json" 749fi 750 751# pack again (modes kept, no AppleDouble files); Apple takes a zip, so the notarisation goes through a zip of the same folder 752stem =" ${ name %.tar.gz} " ;stem =" ${ stem %.tgz} " 753case " $ stem " in *-unsigned-folder)stem =" ${ stem %-unsigned-folder} -folder" ;; *-folder) ;; *)stem =" $ stem -signed" ;;esac 754mkdir -p " $ WORK /final" 755final =" $ WORK /final/ $ stem .tar.gz" 756rm -f " $ final " 757notarize_zip =" $ WORK / $ stem -notarize.zip" ;rm -f " $ notarize_zip " 758zip_dir " $ top " " $ notarize_zip " ||fail "pack the engine folder" "ditto failed" 759 (cd " $ edir " && COPYFILE_DISABLE =1tar -czf " $ final " " $( basename " $ top " ) " ) ||fail "pack the engine folder" "tar failed" 760find " $ edir " -name '._*' | grep -q .&& fail "pack the engine folder" "AppleDouble files in the folder" 761rec OK"pack the engine folder" " $( basename " $ final " ) " 762 763if [" $ DRY " -eq 0 ];then 764notarize " $ notarize_zip " "engine" 765rec INFO"staple the engine folder" "not possible for bare programs; Apple holds the ticket online" 766else 767rec INFO"notarise the engine folder" "skipped in a dry run" 768fi 769deliver " $ final " " $ src " 770} 771 772# ----------------------------------------------------------------------------------------------------------------- run 773if [ -n" $ INPUT " ];then process_app ;fi 774if [ -n" $ TERMINAL " ];then process_terminal ;fi 775if [ -n" $ ENGINE " ];then process_engine ;fi 776finish 0