fix(suppression): repair generated Rosetta hosts blocks

This commit is contained in:
2026-10-30 11:03:00 -03:00
parent 1a48fca9fb
commit e130716813
3 changed files with 94 additions and 6 deletions
+24
View File
@@ -2,6 +2,29 @@
PB="${PB:-/usr/libexec/PlistBuddy}" PB="${PB:-/usr/libexec/PlistBuddy}"
_restore_rosetta_hosts() {
local hosts="$1" tmp_hosts
tmp_hosts=$(mktemp "${hosts}.XXXXXX") || return 1
if ! cp -p "$hosts" "$tmp_hosts" || ! awk '
$0=="# Added by unleash — DEP enrollment block" { owned=1; print; next }
owned && /^(0[.]0[.]0[.]0|::)[[:space:]]+configuration[.]apple[.]com[[:space:]]*$/ { next }
owned && $0 !~ /^(0[.]0[.]0[.]0|::)[[:space:]]+[[:alnum:]_.-]+[[:space:]]*$/ { owned=0 }
{ print }
' "$hosts" > "$tmp_hosts"; then
rm -f "$tmp_hosts"
return 1
fi
if cmp -s "$hosts" "$tmp_hosts"; then
rm -f "$tmp_hosts"
return 0
fi
if [ ! -e "$hosts.unleash-rosetta.bak" ]; then
cp -p "$hosts" "$hosts.unleash-rosetta.bak" || { rm -f "$tmp_hosts"; return 1; }
fi
mv "$tmp_hosts" "$hosts" || { rm -f "$tmp_hosts"; return 1; }
info "Removed old Unleash Rosetta blocks; backup: $hosts.unleash-rosetta.bak"
}
suppress_enrollment() { suppress_enrollment() {
local data_mount="$1" local data_mount="$1"
@@ -35,6 +58,7 @@ suppress_enrollment() {
step "Blocking enrollment domains (Data volume hosts)..." step "Blocking enrollment domains (Data volume hosts)..."
[ -f "$hosts" ] || { mkdir -p "$(dirname "$hosts")"; touch "$hosts"; } [ -f "$hosts" ] || { mkdir -p "$(dirname "$hosts")"; touch "$hosts"; }
_restore_rosetta_hosts "$hosts" || error_exit "Could not repair old Rosetta hosts blocks."
grep -q "Added by unleash" "$hosts" 2>/dev/null || { grep -q "Added by unleash" "$hosts" 2>/dev/null || {
echo "" >>"$hosts" echo "" >>"$hosts"
echo "# Added by unleash — DEP enrollment block" >>"$hosts" echo "# Added by unleash — DEP enrollment block" >>"$hosts"
+46 -6
View File
@@ -1,5 +1,15 @@
#!/usr/bin/env bats #!/usr/bin/env bats
suppress_fixture() {
local script="$1" target="$2"
run bash -u -c '
source <(sed '\''/^main "\$@"$/d'\'' "$1")
PB=:
suppress_enrollment "$2"
' "$script" "$script" "$target"
[ "$status" -eq 0 ]
}
@test "suppression does not add hosts blocks for Rosetta configuration" { @test "suppression does not add hosts blocks for Rosetta configuration" {
local entrypoint local entrypoint
for entrypoint in unleash unleash-standalone.sh; do for entrypoint in unleash unleash-standalone.sh; do
@@ -7,14 +17,44 @@
local target="$BATS_TEST_TMPDIR/$entrypoint" local target="$BATS_TEST_TMPDIR/$entrypoint"
mkdir -p "$target/private/etc" mkdir -p "$target/private/etc"
printf '127.0.0.1 localhost\n' > "$target/private/etc/hosts" printf '127.0.0.1 localhost\n' > "$target/private/etc/hosts"
run bash -u -c ' suppress_fixture "$script" "$target"
source <(sed '\''/^main "\$@"$/d'\'' "$1")
PB=:
suppress_enrollment "$2"
' "$script" "$script" "$target"
[ "$status" -eq 0 ]
run grep -q 'configuration[.]apple[.]com' "$target/private/etc/hosts" run grep -q 'configuration[.]apple[.]com' "$target/private/etc/hosts"
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
grep -q '^127.0.0.1 localhost$' "$target/private/etc/hosts" grep -q '^127.0.0.1 localhost$' "$target/private/etc/hosts"
done done
} }
@test "suppression repairs its old Rosetta blocks with an unchanged backup on rerun" {
local entrypoint
for entrypoint in unleash unleash-standalone.sh; do
local script="$BATS_TEST_DIRNAME/../$entrypoint"
local target="$BATS_TEST_TMPDIR/$entrypoint" hosts
hosts="$target/private/etc/hosts"
mkdir -p "$(dirname "$hosts")"
printf '127.0.0.1 localhost\n# Added by unleash — DEP enrollment block\n0.0.0.0 configuration.apple.com\n:: configuration.apple.com\n' > "$hosts"
cp "$hosts" "$target/original"
suppress_fixture "$script" "$target"
run grep -q 'configuration[.]apple[.]com' "$hosts"
[ "$status" -eq 1 ]
cmp "$target/original" "$hosts.unleash-rosetta.bak"
cp "$hosts" "$target/repaired"
suppress_fixture "$script" "$target"
cmp "$target/repaired" "$hosts"
cmp "$target/original" "$hosts.unleash-rosetta.bak"
done
}
@test "suppression preserves Rosetta blocks outside its generated section" {
local entrypoint
for entrypoint in unleash unleash-standalone.sh; do
local script="$BATS_TEST_DIRNAME/../$entrypoint"
local target="$BATS_TEST_TMPDIR/$entrypoint" hosts
hosts="$target/private/etc/hosts"
mkdir -p "$(dirname "$hosts")"
printf '0.0.0.0 configuration.apple.com\n:: configuration.apple.com manual.alias\n' > "$hosts"
suppress_fixture "$script" "$target"
grep -Fxq '0.0.0.0 configuration.apple.com' "$hosts"
grep -Fxq ':: configuration.apple.com manual.alias' "$hosts"
[ ! -e "$hosts.unleash-rosetta.bak" ]
done
}
+24
View File
@@ -425,6 +425,29 @@ add_to_filevault() {
PB="${PB:-/usr/libexec/PlistBuddy}" PB="${PB:-/usr/libexec/PlistBuddy}"
_restore_rosetta_hosts() {
local hosts="$1" tmp_hosts
tmp_hosts=$(mktemp "${hosts}.XXXXXX") || return 1
if ! cp -p "$hosts" "$tmp_hosts" || ! awk '
$0=="# Added by unleash — DEP enrollment block" { owned=1; print; next }
owned && /^(0[.]0[.]0[.]0|::)[[:space:]]+configuration[.]apple[.]com[[:space:]]*$/ { next }
owned && $0 !~ /^(0[.]0[.]0[.]0|::)[[:space:]]+[[:alnum:]_.-]+[[:space:]]*$/ { owned=0 }
{ print }
' "$hosts" > "$tmp_hosts"; then
rm -f "$tmp_hosts"
return 1
fi
if cmp -s "$hosts" "$tmp_hosts"; then
rm -f "$tmp_hosts"
return 0
fi
if [ ! -e "$hosts.unleash-rosetta.bak" ]; then
cp -p "$hosts" "$hosts.unleash-rosetta.bak" || { rm -f "$tmp_hosts"; return 1; }
fi
mv "$tmp_hosts" "$hosts" || { rm -f "$tmp_hosts"; return 1; }
info "Removed old Unleash Rosetta blocks; backup: $hosts.unleash-rosetta.bak"
}
suppress_enrollment() { suppress_enrollment() {
local data_mount="$1" local data_mount="$1"
@@ -458,6 +481,7 @@ suppress_enrollment() {
step "Blocking enrollment domains (Data volume hosts)..." step "Blocking enrollment domains (Data volume hosts)..."
[ -f "$hosts" ] || { mkdir -p "$(dirname "$hosts")"; touch "$hosts"; } [ -f "$hosts" ] || { mkdir -p "$(dirname "$hosts")"; touch "$hosts"; }
_restore_rosetta_hosts "$hosts" || error_exit "Could not repair old Rosetta hosts blocks."
grep -q "Added by unleash" "$hosts" 2>/dev/null || { grep -q "Added by unleash" "$hosts" 2>/dev/null || {
echo "" >>"$hosts" echo "" >>"$hosts"
echo "# Added by unleash — DEP enrollment block" >>"$hosts" echo "# Added by unleash — DEP enrollment block" >>"$hosts"