Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(desktop): force CloudKit stapling over TCP (#8122)

Eric Hayeseb2f126775fa
diff --git a/.github/workflows/build_desktop_on_tag.yml b/.github/workflows/build_desktop_on_tag.yml
index 645df921f2..4ab0d6f8f8 100644
--- a/.github/workflows/build_desktop_on_tag.yml
+++ b/.github/workflows/build_desktop_on_tag.yml
@@ -561,6 +561,149 @@ jobs:
exit 1
fi
shell: bash
+ - name: Prepare CloudKit TCP fallback
+ run: |
+ set -euo pipefail
+ cat > "$RUNNER_TEMP/macro-cloudkit-tcp.sh" <<'MACRO_CLOUDKIT_TCP'
+ #!/usr/bin/env bash
+ set -euo pipefail
+ umask 077
+
+ if [ "$#" -eq 0 ]; then
+ echo 'Usage: cloudkit_tcp.sh <dmg-or-app> [<dmg-or-app> ...]' >&2
+ exit 2
+ fi
+
+ # The only traffic filtered is UDP/443 to Apple's published CloudKit ticket
+ # delivery ranges. TCP/443 remains available for stapler's HTTP/2 fallback.
+ # https://developer.apple.com/documentation/security/customizing-the-notarization-workflow
+ pf_dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/macro-cloudkit-pf.XXXXXX")
+ pf_rules="$pf_dir/rules"
+ anchor="com.apple/macro_cloudkit_${GITHUB_RUN_ID:-0}_${GITHUB_RUN_ATTEMPT:-0}_${pf_dir##*.}"
+ pf_token=''
+ pf_loaded=0
+
+ cleanup() {
+ local status=$? attempt rules tables counters packets cleared=0
+ trap - EXIT
+ if [ "$pf_loaded" -eq 1 ]; then
+ if counters=$(sudo -n pfctl -a "$anchor" -vvsr 2> /dev/null); then
+ packets=$(printf '%s\n' "$counters" | awk '/Packets:/ {
+ for (i = 1; i <= NF; i++) if ($i == "Packets:") total += $(i + 1)
+ } END { print total + 0 }')
+ echo "CloudKit ticket UDP/443 blocked packets=$packets"
+ if [ "$packets" -eq 0 ]; then
+ echo '::warning::No CloudKit UDP/443 packets were blocked; stapler may have chosen TCP without PF intervention'
+ fi
+ else
+ echo '::warning::Could not read CloudKit PF packet counters'
+ fi
+ for attempt in 1 2 3; do
+ if sudo -n pfctl -a "$anchor" -F rules > /dev/null 2>&1 &&
+ sudo -n pfctl -a "$anchor" -F Tables > /dev/null 2>&1 &&
+ rules=$(sudo -n pfctl -a "$anchor" -sr 2> /dev/null) &&
+ tables=$(sudo -n pfctl -a "$anchor" -s Tables 2> /dev/null) &&
+ [ -z "$rules" ] && [ -z "$tables" ]; then
+ cleared=1
+ break
+ fi
+ sleep 1
+ done
+ if [ "$cleared" -ne 1 ]; then
+ echo "Failed to clear and verify CloudKit PF anchor $anchor; discard this runner" >&2
+ # Keep our enable reference if another component also has PF enabled;
+ # releasing it could leave the uncleared anchor active under theirs.
+ rm -rf "$pf_dir" || true
+ exit 1
+ fi
+ fi
+ if [ -n "$pf_token" ] && ! sudo -n pfctl -X "$pf_token" > /dev/null; then
+ echo 'Failed to release CloudKit PF enable reference; discard this runner' >&2
+ status=1
+ fi
+ if ! rm -rf "$pf_dir"; then
+ echo 'Failed to remove temporary CloudKit PF rules' >&2
+ status=1
+ fi
+ exit "$status"
+ }
+ trap cleanup EXIT
+ trap 'exit 130' INT
+ trap 'exit 143' TERM
+
+ cat > "$pf_rules" <<'RULES'
+ block return out quick inet proto udp from any to 17.36.196.0/22 port 443
+ block return out quick inet proto udp from any to 17.57.0.0/22 port 443
+ block return out quick inet proto udp from any to 17.248.128.0/17 port 443
+ block return out quick inet proto udp from any to 17.250.48.0/20 port 443
+ block return out quick inet proto udp from any to 17.250.64.0/18 port 443
+ block return out quick inet proto udp from any to 17.252.62.0/23 port 443
+ block return out quick inet6 proto udp from any to 2403:300:a41::/48 port 443
+ block return out quick inet6 proto udp from any to 2403:300:a50:100::/56 port 443
+ block return out quick inet6 proto udp from any to 2403:300:1360::/44 port 443
+ block return out quick inet6 proto udp from any to 2403:300:1502::/48 port 443
+ block return out quick inet6 proto udp from any to 2620:149:a40::/46 port 443
+ block return out quick inet6 proto udp from any to 2620:149:1360::/44 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:984::/56 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:984:100::/64 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:a41::/48 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:1360::/44 port 443
+ RULES
+
+ # The standard macOS com.apple/* parent anchor must evaluate our child rules.
+ # Fail the release rather than silently running unprotected if that changes.
+ sudo -n pfctl -s info > /dev/null
+ main_rules=$(sudo -n pfctl -sr)
+ if [[ "$main_rules" != *'anchor "com.apple/*" all'* ]]; then
+ echo 'The macOS com.apple/* PF anchor is not active' >&2
+ exit 1
+ fi
+ sudo -n pfctl -n -a "$anchor" -f "$pf_rules" > /dev/null
+
+ # Record references before -E so an unexpected token format can still be
+ # recovered. No filtering rules are loaded until the token is known.
+ refs_before=$(sudo -n pfctl -s References)
+ enable_output=$(sudo -n pfctl -E 2>&1)
+ pf_token=$(printf '%s\n' "$enable_output" |
+ awk 'tolower($0) ~ /token[[:space:]]*:/ { print $NF; exit }')
+ if [[ ! "$pf_token" =~ ^(0x[[:xdigit:]]+|[[:digit:]]+)$ ]]; then
+ pf_token=$(comm -13 <(printf '%s\n' "$refs_before" | sort) \
+ <(sudo -n pfctl -s References | sort) |
+ awk '$1 ~ /^[0-9]+$/ && $3 ~ /^(0x[0-9a-fA-F]+|[0-9]+)$/ { print $3 }' |
+ sort -u)
+ if [[ ! "$pf_token" =~ ^(0x[[:xdigit:]]+|[[:digit:]]+)$ ]]; then
+ pf_token=''
+ echo 'Could not recover the PF enable token; no CloudKit rules were loaded. Discard this runner' >&2
+ exit 1
+ fi
+ echo 'Unexpected PF enable output; recovered and will release its token' >&2
+ exit 1
+ fi
+
+ if ! sudo -n pfctl -s info | grep -Eq '^Status: Enabled'; then
+ echo 'PF did not become active' >&2
+ exit 1
+ fi
+
+ # Mark it loaded before the command: even a partial load must be flushed.
+ pf_loaded=1
+ sudo -n pfctl -a "$anchor" -f "$pf_rules" > /dev/null
+ rules=$(sudo -n pfctl -a "$anchor" -sr)
+ if [ "$(printf '%s\n' "$rules" | awk 'END { print NR }')" -ne 2 ] ||
+ ! printf '%s\n' "$rules" | grep -Eq '^block return out quick inet proto udp from any to <__automatic_[[:xdigit:]_]+> port = 443$' ||
+ ! printf '%s\n' "$rules" | grep -Eq '^block return out quick inet6 proto udp from any to <__automatic_[[:xdigit:]_]+> port = 443$'; then
+ echo 'CloudKit PF rules did not compile to the expected IPv4 and IPv6 UDP/443 table rules' >&2
+ exit 1
+ fi
+
+ echo 'CloudKit ticket UDP/443 guard active; stapler can use TCP/443'
+ for target in "$@"; do
+ xcrun stapler staple -v "$target"
+ xcrun stapler validate -v "$target"
+ done
+
+ MACRO_CLOUDKIT_TCP
+ shell: bash
- name: Notarize and staple DMG
run: |
set -euo pipefail
@@ -617,11 +760,11 @@ jobs:
echo '::endgroup::'
}
- run_stapler() {
- local operation="$1" target="$2" status=0
- xcrun stapler "$operation" -v "$target" || status=$?
+ run_stapler_pair() {
+ local target="$1" status=0
+ bash "$RUNNER_TEMP/macro-cloudkit-tcp.sh" "$target" || status=$?
if [ "$status" -ne 0 ]; then
- echo "stapler $operation failed for $target with exit code $status" >&2
+ echo "stapler failed for $target with exit code $status" >&2
cloudkit_diagnostics
fi
return "$status"
@@ -634,8 +777,7 @@ jobs:
cat "$credentials_dir/submission.json"
jq -e '.status == "Accepted"' "$credentials_dir/submission.json" > /dev/null
cloudkit_diagnostics
- run_stapler staple "$dmg"
- run_stapler validate "$dmg"
+ run_stapler_pair "$dmg"
spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
# The updater ships the app rather than the DMG. Notarize and staple that exact
@@ -646,8 +788,7 @@ jobs:
--wait --timeout 30m --output-format json > "$credentials_dir/app-submission.json"
jq -e '.status == "Accepted"' "$credentials_dir/app-submission.json" > /dev/null
cloudkit_diagnostics
- run_stapler staple updater-app/Macro.app
- run_stapler validate updater-app/Macro.app
+ run_stapler_pair updater-app/Macro.app
codesign --verify --deep --strict --verbose=2 updater-app/Macro.app
spctl --assess --type execute --verbose=2 updater-app/Macro.app
version=$(jq -er '.version' apps/web/tauri/desktop-release.json)
diff --git a/.github/workflows/build_dmg_on_tag.yml b/.github/workflows/build_dmg_on_tag.yml
index 2f7d8219c0..322c1365e9 100644
--- a/.github/workflows/build_dmg_on_tag.yml
+++ b/.github/workflows/build_dmg_on_tag.yml
@@ -374,6 +374,149 @@ jobs:
exit 1
fi
shell: bash
+ - name: Prepare CloudKit TCP fallback
+ run: |
+ set -euo pipefail
+ cat > "$RUNNER_TEMP/macro-cloudkit-tcp.sh" <<'MACRO_CLOUDKIT_TCP'
+ #!/usr/bin/env bash
+ set -euo pipefail
+ umask 077
+
+ if [ "$#" -eq 0 ]; then
+ echo 'Usage: cloudkit_tcp.sh <dmg-or-app> [<dmg-or-app> ...]' >&2
+ exit 2
+ fi
+
+ # The only traffic filtered is UDP/443 to Apple's published CloudKit ticket
+ # delivery ranges. TCP/443 remains available for stapler's HTTP/2 fallback.
+ # https://developer.apple.com/documentation/security/customizing-the-notarization-workflow
+ pf_dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/macro-cloudkit-pf.XXXXXX")
+ pf_rules="$pf_dir/rules"
+ anchor="com.apple/macro_cloudkit_${GITHUB_RUN_ID:-0}_${GITHUB_RUN_ATTEMPT:-0}_${pf_dir##*.}"
+ pf_token=''
+ pf_loaded=0
+
+ cleanup() {
+ local status=$? attempt rules tables counters packets cleared=0
+ trap - EXIT
+ if [ "$pf_loaded" -eq 1 ]; then
+ if counters=$(sudo -n pfctl -a "$anchor" -vvsr 2> /dev/null); then
+ packets=$(printf '%s\n' "$counters" | awk '/Packets:/ {
+ for (i = 1; i <= NF; i++) if ($i == "Packets:") total += $(i + 1)
+ } END { print total + 0 }')
+ echo "CloudKit ticket UDP/443 blocked packets=$packets"
+ if [ "$packets" -eq 0 ]; then
+ echo '::warning::No CloudKit UDP/443 packets were blocked; stapler may have chosen TCP without PF intervention'
+ fi
+ else
+ echo '::warning::Could not read CloudKit PF packet counters'
+ fi
+ for attempt in 1 2 3; do
+ if sudo -n pfctl -a "$anchor" -F rules > /dev/null 2>&1 &&
+ sudo -n pfctl -a "$anchor" -F Tables > /dev/null 2>&1 &&
+ rules=$(sudo -n pfctl -a "$anchor" -sr 2> /dev/null) &&
+ tables=$(sudo -n pfctl -a "$anchor" -s Tables 2> /dev/null) &&
+ [ -z "$rules" ] && [ -z "$tables" ]; then
+ cleared=1
+ break
+ fi
+ sleep 1
+ done
+ if [ "$cleared" -ne 1 ]; then
+ echo "Failed to clear and verify CloudKit PF anchor $anchor; discard this runner" >&2
+ # Keep our enable reference if another component also has PF enabled;
+ # releasing it could leave the uncleared anchor active under theirs.
+ rm -rf "$pf_dir" || true
+ exit 1
+ fi
+ fi
+ if [ -n "$pf_token" ] && ! sudo -n pfctl -X "$pf_token" > /dev/null; then
+ echo 'Failed to release CloudKit PF enable reference; discard this runner' >&2
+ status=1
+ fi
+ if ! rm -rf "$pf_dir"; then
+ echo 'Failed to remove temporary CloudKit PF rules' >&2
+ status=1
+ fi
+ exit "$status"
+ }
+ trap cleanup EXIT
+ trap 'exit 130' INT
+ trap 'exit 143' TERM
+
+ cat > "$pf_rules" <<'RULES'
+ block return out quick inet proto udp from any to 17.36.196.0/22 port 443
+ block return out quick inet proto udp from any to 17.57.0.0/22 port 443
+ block return out quick inet proto udp from any to 17.248.128.0/17 port 443
+ block return out quick inet proto udp from any to 17.250.48.0/20 port 443
+ block return out quick inet proto udp from any to 17.250.64.0/18 port 443
+ block return out quick inet proto udp from any to 17.252.62.0/23 port 443
+ block return out quick inet6 proto udp from any to 2403:300:a41::/48 port 443
+ block return out quick inet6 proto udp from any to 2403:300:a50:100::/56 port 443
+ block return out quick inet6 proto udp from any to 2403:300:1360::/44 port 443
+ block return out quick inet6 proto udp from any to 2403:300:1502::/48 port 443
+ block return out quick inet6 proto udp from any to 2620:149:a40::/46 port 443
+ block return out quick inet6 proto udp from any to 2620:149:1360::/44 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:984::/56 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:984:100::/64 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:a41::/48 port 443
+ block return out quick inet6 proto udp from any to 2a01:b740:1360::/44 port 443
+ RULES
+
+ # The standard macOS com.apple/* parent anchor must evaluate our child rules.
+ # Fail the release rather than silently running unprotected if that changes.
+ sudo -n pfctl -s info > /dev/null
+ main_rules=$(sudo -n pfctl -sr)
+ if [[ "$main_rules" != *'anchor "com.apple/*" all'* ]]; then
+ echo 'The macOS com.apple/* PF anchor is not active' >&2
+ exit 1
+ fi
+ sudo -n pfctl -n -a "$anchor" -f "$pf_rules" > /dev/null
+
+ # Record references before -E so an unexpected token format can still be
+ # recovered. No filtering rules are loaded until the token is known.
+ refs_before=$(sudo -n pfctl -s References)
+ enable_output=$(sudo -n pfctl -E 2>&1)
+ pf_token=$(printf '%s\n' "$enable_output" |
+ awk 'tolower($0) ~ /token[[:space:]]*:/ { print $NF; exit }')
+ if [[ ! "$pf_token" =~ ^(0x[[:xdigit:]]+|[[:digit:]]+)$ ]]; then
+ pf_token=$(comm -13 <(printf '%s\n' "$refs_before" | sort) \
+ <(sudo -n pfctl -s References | sort) |
+ awk '$1 ~ /^[0-9]+$/ && $3 ~ /^(0x[0-9a-fA-F]+|[0-9]+)$/ { print $3 }' |
+ sort -u)
+ if [[ ! "$pf_token" =~ ^(0x[[:xdigit:]]+|[[:digit:]]+)$ ]]; then
+ pf_token=''
+ echo 'Could not recover the PF enable token; no CloudKit rules were loaded. Discard this runner' >&2
+ exit 1
+ fi
+ echo 'Unexpected PF enable output; recovered and will release its token' >&2
+ exit 1
+ fi
+
+ if ! sudo -n pfctl -s info | grep -Eq '^Status: Enabled'; then
+ echo 'PF did not become active' >&2
+ exit 1
+ fi
+
+ # Mark it loaded before the command: even a partial load must be flushed.
+ pf_loaded=1
+ sudo -n pfctl -a "$anchor" -f "$pf_rules" > /dev/null
+ rules=$(sudo -n pfctl -a "$anchor" -sr)
+ if [ "$(printf '%s\n' "$rules" | awk 'END { print NR }')" -ne 2 ] ||
+ ! printf '%s\n' "$rules" | grep -Eq '^block return out quick inet proto udp from any to <__automatic_[[:xdigit:]_]+> port = 443$' ||
+ ! printf '%s\n' "$rules" | grep -Eq '^block return out quick inet6 proto udp from any to <__automatic_[[:xdigit:]_]+> port = 443$'; then
+ echo 'CloudKit PF rules did not compile to the expected IPv4 and IPv6 UDP/443 table rules' >&2
+ exit 1
+ fi
+
+ echo 'CloudKit ticket UDP/443 guard active; stapler can use TCP/443'
+ for target in "$@"; do
+ xcrun stapler staple -v "$target"
+ xcrun stapler validate -v "$target"
+ done
+
+ MACRO_CLOUDKIT_TCP
+ shell: bash
- name: Notarize and staple DMG
run: |
set -euo pipefail
@@ -430,11 +573,11 @@ jobs:
echo '::endgroup::'
}
- run_stapler() {
- local operation="$1" target="$2" status=0
- xcrun stapler "$operation" -v "$target" || status=$?
+ run_stapler_pair() {
+ local target="$1" status=0
+ bash "$RUNNER_TEMP/macro-cloudkit-tcp.sh" "$target" || status=$?
if [ "$status" -ne 0 ]; then
- echo "stapler $operation failed for $target with exit code $status" >&2
+ echo "stapler failed for $target with exit code $status" >&2
cloudkit_diagnostics
fi
return "$status"
@@ -447,8 +590,7 @@ jobs:
cat "$credentials_dir/submission.json"
jq -e '.status == "Accepted"' "$credentials_dir/submission.json" > /dev/null
cloudkit_diagnostics
- run_stapler staple "$dmg"
- run_stapler validate "$dmg"
+ run_stapler_pair "$dmg"
spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
# The updater ships the app rather than the DMG. Notarize and staple that exact
@@ -459,8 +601,7 @@ jobs:
--wait --timeout 30m --output-format json > "$credentials_dir/app-submission.json"
jq -e '.status == "Accepted"' "$credentials_dir/app-submission.json" > /dev/null
cloudkit_diagnostics
- run_stapler staple updater-app/Macro.app
- run_stapler validate updater-app/Macro.app
+ run_stapler_pair updater-app/Macro.app
codesign --verify --deep --strict --verbose=2 updater-app/Macro.app
spctl --assess --type execute --verbose=2 updater-app/Macro.app
version=$(jq -er '.version' apps/web/tauri/desktop-release.json)
diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs
index 1b06d6884a..98022120d0 100644
--- a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs
+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag.rs
@@ -51,6 +51,7 @@ pub fn build_dmg_job(ref_expr: &str) -> Job {
.add_step(save_nix_cache())
.add_step(collect_dmg())
.add_step(validate_signed_dmg())
+ .add_step(prepare_cloudkit_tcp())
.add_step(notarize_dmg())
.add_step(steps::upload_artifact(
"macro-dmg-${{ steps.metadata.outputs.safe_tag }}",
@@ -127,3 +128,12 @@ fn notarize_dmg() -> Step<Run> {
.shell("bash")
.add_env(("DOPPLER_TOKEN", vars::MACOS_RELEASE_DOPPLER_TOKEN))
}
+
+fn prepare_cloudkit_tcp() -> Step<Run> {
+ Step::new("Prepare CloudKit TCP fallback")
+ .run(format!(
+ "set -euo pipefail\ncat > \"$RUNNER_TEMP/macro-cloudkit-tcp.sh\" <<'MACRO_CLOUDKIT_TCP'\n{}\nMACRO_CLOUDKIT_TCP\n",
+ include_str!("scripts/cloudkit_tcp.sh")
+ ))
+ .shell("bash")
+}
diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rs
index d664d094d3..8ffe76799f 100644
--- a/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rs
+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/build_dmg_on_tag/test.rs
@@ -361,3 +361,181 @@ fn missing_build_roots_leave_cache_contents_untouched() {
assert!(!fixture.temp.path().join("planned-log").exists());
assert!(!fixture.temp.path().join("copy-log").exists());
}
+
+#[test]
+fn cloudkit_tcp_guard_is_wired_into_both_desktop_workflows() {
+ let workflows = [
+ super::build_dmg().to_string().expect("DMG workflow YAML"),
+ crate::workflows::build_desktop_on_tag::build_desktop_on_tag()
+ .to_string()
+ .expect("desktop workflow YAML"),
+ ];
+ for yaml in workflows {
+ let prepare = yaml
+ .find("- name: Prepare CloudKit TCP fallback")
+ .expect("embedded standalone helper step");
+ let notarize = yaml
+ .find("- name: Notarize and staple DMG")
+ .expect("notarization step");
+ assert!(prepare < notarize, "helper must be installed first");
+ assert_eq!(
+ yaml.matches("bash \"$RUNNER_TEMP/macro-cloudkit-tcp.sh\"")
+ .count(),
+ 1,
+ "the stapler pair function must invoke the guard"
+ );
+ assert_eq!(
+ yaml.matches("run_stapler_pair ").count(),
+ 2,
+ "both the DMG and updater app must use the guard"
+ );
+ assert_eq!(yaml.matches("xcrun notarytool submit").count(), 2);
+ let notarize_script = &yaml[notarize..];
+ let dmg_submit = notarize_script
+ .find("xcrun notarytool submit \"$dmg\"")
+ .unwrap();
+ let dmg_guard = notarize_script.find("run_stapler_pair \"$dmg\"").unwrap();
+ let app_submit = notarize_script
+ .find("xcrun notarytool submit \"$credentials_dir/updater-app.zip\"")
+ .unwrap();
+ let app_guard = notarize_script
+ .find("run_stapler_pair updater-app/Macro.app")
+ .unwrap();
+ assert!(dmg_submit < dmg_guard && dmg_guard < app_submit && app_submit < app_guard);
+ }
+}
+
+struct CloudKitFixture {
+ temp: TempDir,
+}
+
+impl CloudKitFixture {
+ fn new() -> Self {
+ let temp = tempfile::tempdir().expect("CloudKit fixture directory");
+ fs::create_dir(temp.path().join("bin")).expect("stub command directory");
+ fs::write(
+ temp.path().join("bin/sudo"),
+ "#!/usr/bin/env bash\n[[ \"$1\" == -n ]] || exit 2\nshift\nexec \"$@\"\n",
+ )
+ .expect("sudo stub");
+ fs::write(
+ temp.path().join("bin/pfctl"),
+ r#"#!/usr/bin/env bash
+set -euo pipefail
+case "$1" in
+ -s)
+ case "$2" in
+ info) echo 'Status: Enabled for 0 days' ;;
+ References) : ;;
+ *) exit 2 ;;
+ esac ;;
+ -sr) echo 'anchor "com.apple/*" all' ;;
+ -n) : ;;
+ -E) echo 'Token : 0x123' ;;
+ -X) [[ "$2" == 0x123 ]] && touch "$STUB_PF_DIR/token_released" ;;
+ -a)
+ case "$3" in
+ -f) touch "$STUB_PF_DIR/rules" "$STUB_PF_DIR/tables" ;;
+ -sr)
+ if [[ -e "$STUB_PF_DIR/rules" ]]; then
+ echo 'block return out quick inet proto udp from any to <__automatic_e874a65a_0> port = 443'
+ echo 'block return out quick inet6 proto udp from any to <__automatic_e874a65a_1> port = 443'
+ fi ;;
+ -vvsr) echo 'Packets: 2' ;;
+ -s) [[ "$4" == Tables ]] || exit 2
+ if [[ -e "$STUB_PF_DIR/tables" ]]; then
+ echo __automatic_e874a65a_0
+ echo __automatic_e874a65a_1
+ fi ;;
+ -F)
+ if [[ "${STUB_FAIL_FLUSH:-0}" == 1 ]]; then exit 1; fi
+ case "$4" in
+ rules) rm -f "$STUB_PF_DIR/rules" ;;
+ Tables) rm -f "$STUB_PF_DIR/tables" ;;
+ *) exit 2 ;;
+ esac ;;
+ *) exit 2 ;;
+ esac ;;
+ *) exit 2 ;;
+esac
+"#,
+ )
+ .expect("pfctl stub");
+ fs::write(
+ temp.path().join("bin/xcrun"),
+ r#"#!/usr/bin/env bash
+printf '%s\n' "$*" >> "$STUB_STAPLER_CALLS"
+if [[ "${STUB_FAIL_VALIDATE:-0}" == 1 && "$2" == validate ]]; then exit 27; fi
+"#,
+ )
+ .expect("xcrun stub");
+ for command in ["sudo", "pfctl", "xcrun"] {
+ fs::set_permissions(
+ temp.path().join("bin").join(command),
+ fs::Permissions::from_mode(0o755),
+ )
+ .expect("executable stub");
+ }
+ Self { temp }
+ }
+
+ fn run(&self, fail_validate: bool, fail_flush: bool) -> Output {
+ let path = format!(
+ "{}:/usr/bin:/bin:/usr/sbin:/sbin",
+ self.temp.path().join("bin").display(),
+ );
+ Command::new("bash")
+ .arg(
+ Path::new(env!("CARGO_MANIFEST_DIR")).join("src/workflows/scripts/cloudkit_tcp.sh"),
+ )
+ .arg("already-stapled.dmg")
+ .env("PATH", path)
+ .env("RUNNER_TEMP", self.temp.path())
+ .env("STUB_PF_DIR", self.temp.path())
+ .env("STUB_STAPLER_CALLS", self.temp.path().join("stapler.calls"))
+ .env("STUB_FAIL_VALIDATE", if fail_validate { "1" } else { "0" })
+ .env("STUB_FAIL_FLUSH", if fail_flush { "1" } else { "0" })
+ .output()
+ .expect("run CloudKit helper with stub commands")
+ }
+}
+
+#[test]
+fn cloudkit_tcp_guard_releases_its_anchor_after_stapling() {
+ let fixture = CloudKitFixture::new();
+ let output = fixture.run(false, false);
+ assert!(
+ output.status.success(),
+ "CloudKit helper failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert_eq!(
+ fs::read_to_string(fixture.temp.path().join("stapler.calls")).expect("stapler calls"),
+ "stapler staple -v already-stapled.dmg\nstapler validate -v already-stapled.dmg\n"
+ );
+ assert!(fixture.temp.path().join("token_released").exists());
+ assert!(!fixture.temp.path().join("rules").exists());
+ assert!(!fixture.temp.path().join("tables").exists());
+ assert!(String::from_utf8_lossy(&output.stdout).contains("blocked packets=2"));
+}
+
+#[test]
+fn cloudkit_tcp_guard_cleans_up_on_stapler_failure_but_keeps_reference_if_flush_fails() {
+ let fixture = CloudKitFixture::new();
+ let output = fixture.run(true, false);
+ assert_eq!(output.status.code(), Some(27));
+ assert!(fixture.temp.path().join("token_released").exists());
+ assert!(!fixture.temp.path().join("rules").exists());
+ assert!(!fixture.temp.path().join("tables").exists());
+
+ let fixture = CloudKitFixture::new();
+ let output = fixture.run(false, true);
+ assert_eq!(output.status.code(), Some(1));
+ assert!(!fixture.temp.path().join("token_released").exists());
+ assert!(fixture.temp.path().join("rules").exists());
+ assert!(
+ String::from_utf8_lossy(&output.stderr).contains("discard this runner"),
+ "{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+}
diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/cloudkit_tcp.sh b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/cloudkit_tcp.sh
new file mode 100644
index 0000000000..85f02263ab
--- /dev/null
+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/cloudkit_tcp.sh
@@ -0,0 +1,136 @@
+#!/usr/bin/env bash
+set -euo pipefail
+umask 077
+
+if [ "$#" -eq 0 ]; then
+ echo 'Usage: cloudkit_tcp.sh <dmg-or-app> [<dmg-or-app> ...]' >&2
+ exit 2
+fi
+
+# The only traffic filtered is UDP/443 to Apple's published CloudKit ticket
+# delivery ranges. TCP/443 remains available for stapler's HTTP/2 fallback.
+# https://developer.apple.com/documentation/security/customizing-the-notarization-workflow
+pf_dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/macro-cloudkit-pf.XXXXXX")
+pf_rules="$pf_dir/rules"
+anchor="com.apple/macro_cloudkit_${GITHUB_RUN_ID:-0}_${GITHUB_RUN_ATTEMPT:-0}_${pf_dir##*.}"
+pf_token=''
+pf_loaded=0
+
+cleanup() {
+ local status=$? attempt rules tables counters packets cleared=0
+ trap - EXIT
+ if [ "$pf_loaded" -eq 1 ]; then
+ if counters=$(sudo -n pfctl -a "$anchor" -vvsr 2> /dev/null); then
+ packets=$(printf '%s\n' "$counters" | awk '/Packets:/ {
+ for (i = 1; i <= NF; i++) if ($i == "Packets:") total += $(i + 1)
+ } END { print total + 0 }')
+ echo "CloudKit ticket UDP/443 blocked packets=$packets"
+ if [ "$packets" -eq 0 ]; then
+ echo '::warning::No CloudKit UDP/443 packets were blocked; stapler may have chosen TCP without PF intervention'
+ fi
+ else
+ echo '::warning::Could not read CloudKit PF packet counters'
+ fi
+ for attempt in 1 2 3; do
+ if sudo -n pfctl -a "$anchor" -F rules > /dev/null 2>&1 &&
+ sudo -n pfctl -a "$anchor" -F Tables > /dev/null 2>&1 &&
+ rules=$(sudo -n pfctl -a "$anchor" -sr 2> /dev/null) &&
+ tables=$(sudo -n pfctl -a "$anchor" -s Tables 2> /dev/null) &&
+ [ -z "$rules" ] && [ -z "$tables" ]; then
+ cleared=1
+ break
+ fi
+ sleep 1
+ done
+ if [ "$cleared" -ne 1 ]; then
+ echo "Failed to clear and verify CloudKit PF anchor $anchor; discard this runner" >&2
+ # Keep our enable reference if another component also has PF enabled;
+ # releasing it could leave the uncleared anchor active under theirs.
+ rm -rf "$pf_dir" || true
+ exit 1
+ fi
+ fi
+ if [ -n "$pf_token" ] && ! sudo -n pfctl -X "$pf_token" > /dev/null; then
+ echo 'Failed to release CloudKit PF enable reference; discard this runner' >&2
+ status=1
+ fi
+ if ! rm -rf "$pf_dir"; then
+ echo 'Failed to remove temporary CloudKit PF rules' >&2
+ status=1
+ fi
+ exit "$status"
+}
+trap cleanup EXIT
+trap 'exit 130' INT
+trap 'exit 143' TERM
+
+cat > "$pf_rules" <<'RULES'
+block return out quick inet proto udp from any to 17.36.196.0/22 port 443
+block return out quick inet proto udp from any to 17.57.0.0/22 port 443
+block return out quick inet proto udp from any to 17.248.128.0/17 port 443
+block return out quick inet proto udp from any to 17.250.48.0/20 port 443
+block return out quick inet proto udp from any to 17.250.64.0/18 port 443
+block return out quick inet proto udp from any to 17.252.62.0/23 port 443
+block return out quick inet6 proto udp from any to 2403:300:a41::/48 port 443
+block return out quick inet6 proto udp from any to 2403:300:a50:100::/56 port 443
+block return out quick inet6 proto udp from any to 2403:300:1360::/44 port 443
+block return out quick inet6 proto udp from any to 2403:300:1502::/48 port 443
+block return out quick inet6 proto udp from any to 2620:149:a40::/46 port 443
+block return out quick inet6 proto udp from any to 2620:149:1360::/44 port 443
+block return out quick inet6 proto udp from any to 2a01:b740:984::/56 port 443
+block return out quick inet6 proto udp from any to 2a01:b740:984:100::/64 port 443
+block return out quick inet6 proto udp from any to 2a01:b740:a41::/48 port 443
+block return out quick inet6 proto udp from any to 2a01:b740:1360::/44 port 443
+RULES
+
+# The standard macOS com.apple/* parent anchor must evaluate our child rules.
+# Fail the release rather than silently running unprotected if that changes.
+sudo -n pfctl -s info > /dev/null
+main_rules=$(sudo -n pfctl -sr)
+if [[ "$main_rules" != *'anchor "com.apple/*" all'* ]]; then
+ echo 'The macOS com.apple/* PF anchor is not active' >&2
+ exit 1
+fi
+sudo -n pfctl -n -a "$anchor" -f "$pf_rules" > /dev/null
+
+# Record references before -E so an unexpected token format can still be
+# recovered. No filtering rules are loaded until the token is known.
+refs_before=$(sudo -n pfctl -s References)
+enable_output=$(sudo -n pfctl -E 2>&1)
+pf_token=$(printf '%s\n' "$enable_output" |
+ awk 'tolower($0) ~ /token[[:space:]]*:/ { print $NF; exit }')
+if [[ ! "$pf_token" =~ ^(0x[[:xdigit:]]+|[[:digit:]]+)$ ]]; then
+ pf_token=$(comm -13 <(printf '%s\n' "$refs_before" | sort) \
+ <(sudo -n pfctl -s References | sort) |
+ awk '$1 ~ /^[0-9]+$/ && $3 ~ /^(0x[0-9a-fA-F]+|[0-9]+)$/ { print $3 }' |
+ sort -u)
+ if [[ ! "$pf_token" =~ ^(0x[[:xdigit:]]+|[[:digit:]]+)$ ]]; then
+ pf_token=''
+ echo 'Could not recover the PF enable token; no CloudKit rules were loaded. Discard this runner' >&2
+ exit 1
+ fi
+ echo 'Unexpected PF enable output; recovered and will release its token' >&2
+ exit 1
+fi
+
+if ! sudo -n pfctl -s info | grep -Eq '^Status: Enabled'; then
+ echo 'PF did not become active' >&2
+ exit 1
+fi
+
+# Mark it loaded before the command: even a partial load must be flushed.
+pf_loaded=1
+sudo -n pfctl -a "$anchor" -f "$pf_rules" > /dev/null
+rules=$(sudo -n pfctl -a "$anchor" -sr)
+if [ "$(printf '%s\n' "$rules" | awk 'END { print NR }')" -ne 2 ] ||
+ ! printf '%s\n' "$rules" | grep -Eq '^block return out quick inet proto udp from any to <__automatic_[[:xdigit:]_]+> port = 443$' ||
+ ! printf '%s\n' "$rules" | grep -Eq '^block return out quick inet6 proto udp from any to <__automatic_[[:xdigit:]_]+> port = 443$'; then
+ echo 'CloudKit PF rules did not compile to the expected IPv4 and IPv6 UDP/443 table rules' >&2
+ exit 1
+fi
+
+echo 'CloudKit ticket UDP/443 guard active; stapler can use TCP/443'
+for target in "$@"; do
+ xcrun stapler staple -v "$target"
+ xcrun stapler validate -v "$target"
+done
diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/notarize_dmg.sh b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/notarize_dmg.sh
index fbac7a38e7..f491a0365c 100644
--- a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/notarize_dmg.sh
+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/notarize_dmg.sh
@@ -52,11 +52,11 @@ cloudkit_diagnostics() {
echo '::endgroup::'
}
-run_stapler() {
- local operation="$1" target="$2" status=0
- xcrun stapler "$operation" -v "$target" || status=$?
+run_stapler_pair() {
+ local target="$1" status=0
+ bash "$RUNNER_TEMP/macro-cloudkit-tcp.sh" "$target" || status=$?
if [ "$status" -ne 0 ]; then
- echo "stapler $operation failed for $target with exit code $status" >&2
+ echo "stapler failed for $target with exit code $status" >&2
cloudkit_diagnostics
fi
return "$status"
@@ -69,8 +69,7 @@ xcrun notarytool submit "$dmg" \
cat "$credentials_dir/submission.json"
jq -e '.status == "Accepted"' "$credentials_dir/submission.json" > /dev/null
cloudkit_diagnostics
-run_stapler staple "$dmg"
-run_stapler validate "$dmg"
+run_stapler_pair "$dmg"
spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
# The updater ships the app rather than the DMG. Notarize and staple that exact
@@ -81,8 +80,7 @@ xcrun notarytool submit "$credentials_dir/updater-app.zip" \
--wait --timeout 30m --output-format json > "$credentials_dir/app-submission.json"
jq -e '.status == "Accepted"' "$credentials_dir/app-submission.json" > /dev/null
cloudkit_diagnostics
-run_stapler staple updater-app/Macro.app
-run_stapler validate updater-app/Macro.app
+run_stapler_pair updater-app/Macro.app
codesign --verify --deep --strict --verbose=2 updater-app/Macro.app
spctl --assess --type execute --verbose=2 updater-app/Macro.app
version=$(jq -er '.version' apps/web/tauri/desktop-release.json)