fix(audit): emit valid zero counts without nounset failures

This commit is contained in:
2026-10-30 11:01:17 -03:00
parent 14534947b9
commit 79d80c9386
4 changed files with 73 additions and 20 deletions
+9 -5
View File
@@ -30,10 +30,11 @@ generate_report() {
step "Installed Profiles" step "Installed Profiles"
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
local count local count
count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
count="${count:-0}"
if [ "$count" -gt 0 ]; then if [ "$count" -gt 0 ]; then
echo " $count profile(s) installed" echo " $count profile(s) installed"
sudo profiles -C -output=xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" \ sudo profiles -C -output stdout-xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" \
| sed 's/.*<string>\(.*\)<\/string>.*/ - \1/' | sed 's/.*<string>\(.*\)<\/string>.*/ - \1/'
else else
echo " No profiles installed" echo " No profiles installed"
@@ -78,7 +79,8 @@ generate_report() {
step "Hosts Block" step "Hosts Block"
if grep -q "iprofiles.apple.com" /private/etc/hosts 2>/dev/null; then if grep -q "iprofiles.apple.com" /private/etc/hosts 2>/dev/null; then
local blocked local blocked
blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || echo 0) blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || true)
blocked="${blocked:-0}"
echo " $blocked domain(s) blocked in /etc/hosts" echo " $blocked domain(s) blocked in /etc/hosts"
else else
echo " No block entries found" echo " No block entries found"
@@ -117,7 +119,8 @@ generate_report_json() {
local profile_count=0 local profile_count=0
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
profile_count="${profile_count:-0}"
fi fi
report="${report} \"installed_profiles\": $profile_count,\n" report="${report} \"installed_profiles\": $profile_count,\n"
@@ -147,7 +150,8 @@ generate_report_json() {
report="${report} },\n" report="${report} },\n"
local hosts_blocked=0 local hosts_blocked=0
hosts_blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || echo 0) hosts_blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || true)
hosts_blocked="${hosts_blocked:-0}"
report="${report} \"hosts_blocked\": $hosts_blocked\n" report="${report} \"hosts_blocked\": $hosts_blocked\n"
report="${report}}" report="${report}}"
+12 -5
View File
@@ -37,7 +37,8 @@ check_mdm_status() {
else else
echo -e " ${YEL}(none)${NC}" echo -e " ${YEL}(none)${NC}"
fi fi
local count; count=$(echo "$matches" | grep -c . 2>/dev/null || echo 0) local count; count=$(echo "$matches" | grep -c . 2>/dev/null || true)
count="${count:-0}"
echo -e " ${CYAN}Total: $count of 13 expected domains blocked${NC}" echo -e " ${CYAN}Total: $count of 13 expected domains blocked${NC}"
else else
echo -e " ${YEL}(hosts not found)${NC}" echo -e " ${YEL}(hosts not found)${NC}"
@@ -99,10 +100,11 @@ deep_status() {
step "Installed Configuration Profiles" step "Installed Configuration Profiles"
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
local profile_count local profile_count
profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
profile_count="${profile_count:-0}"
if [ "$profile_count" -gt 0 ]; then if [ "$profile_count" -gt 0 ]; then
warn "$profile_count profile(s) installed:" warn "$profile_count profile(s) installed:"
sudo profiles -C -output=xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" | sed 's/.*<string>\(.*\)<\/string>.*/ - \1/' sudo profiles -C -output stdout-xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" | sed 's/.*<string>\(.*\)<\/string>.*/ - \1/'
else else
info "No configuration profiles installed" info "No configuration profiles installed"
fi fi
@@ -183,7 +185,11 @@ deep_status() {
step "Overall Assessment" step "Overall Assessment"
local risk="LOW" local risk="LOW"
[ "$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0)" -gt 0 ] && risk="MEDIUM" local cfg="/private/var/db/ConfigurationProfiles/Settings"
local installed
installed=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
installed="${installed:-0}"
[ "$installed" -gt 0 ] && risk="MEDIUM"
pgrep -i "mdm|managedclient" >/dev/null 2>&1 && risk="HIGH" pgrep -i "mdm|managedclient" >/dev/null 2>&1 && risk="HIGH"
[ -f "$cfg/.cloudConfigRecordFound" ] && risk="CRITICAL" [ -f "$cfg/.cloudConfigRecordFound" ] && risk="CRITICAL"
@@ -203,7 +209,8 @@ deep_status_json() {
local profile_count=0 local profile_count=0
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
profile_count="${profile_count:-0}"
fi fi
json="${json} \"profile_count\": $profile_count,\n" json="${json} \"profile_count\": $profile_count,\n"
+31
View File
@@ -39,3 +39,34 @@ teardown() {
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
echo "$output" | python3 -c 'import json,sys; assert json.load(sys.stdin)["running_mdm_processes"] == 1' echo "$output" | python3 -c 'import json,sys; assert json.load(sys.stdin)["running_mdm_processes"] == 1'
} }
@test "text audit completes with zero profiles under nounset" {
run "${UNLEASH_TEST_SHELL:-bash}" -u -c '
source "$1/lib/colors.sh"
source "$1/lib/status.sh"
pf_status() { :; }
deep_status
' bash "$BATS_TEST_DIRNAME/.."
[ "$status" -eq 0 ]
[[ "$output" == *"Risk:"* ]]
[[ "$output" != *"integer expression expected"* ]]
}
@test "audit and report JSON contain numeric zero counts under nounset" {
local module function_name field
for module in status report; do
if [ "$module" = status ]; then function_name=deep_status_json; field=profile_count;
else function_name=generate_report_json; field=installed_profiles; fi
run "${UNLEASH_TEST_SHELL:-bash}" -u -c '
source "$1/lib/colors.sh"
source "$1/lib/$2.sh"
grep() {
if [ "${1:-}" = -c ] && [ "${3:-}" = /private/etc/hosts ]; then echo 0; return 1; fi
command grep "$@"
}
"$3"
' bash "$BATS_TEST_DIRNAME/.." "$module" "$function_name"
[ "$status" -eq 0 ]
echo "$output" | python3 -c 'import json,sys; data=json.load(sys.stdin); assert data[sys.argv[1]] == 0; assert data.get("hosts_blocked",0) == 0' "$field"
done
}
+21 -10
View File
@@ -745,7 +745,8 @@ check_mdm_status() {
else else
echo -e " ${YEL}(none)${NC}" echo -e " ${YEL}(none)${NC}"
fi fi
local count; count=$(echo "$matches" | grep -c . 2>/dev/null || echo 0) local count; count=$(echo "$matches" | grep -c . 2>/dev/null || true)
count="${count:-0}"
echo -e " ${CYAN}Total: $count of 13 expected domains blocked${NC}" echo -e " ${CYAN}Total: $count of 13 expected domains blocked${NC}"
else else
echo -e " ${YEL}(hosts not found)${NC}" echo -e " ${YEL}(hosts not found)${NC}"
@@ -807,10 +808,11 @@ deep_status() {
step "Installed Configuration Profiles" step "Installed Configuration Profiles"
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
local profile_count local profile_count
profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
profile_count="${profile_count:-0}"
if [ "$profile_count" -gt 0 ]; then if [ "$profile_count" -gt 0 ]; then
warn "$profile_count profile(s) installed:" warn "$profile_count profile(s) installed:"
sudo profiles -C -output=xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" | sed 's/.*<string>\(.*\)<\/string>.*/ - \1/' sudo profiles -C -output stdout-xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" | sed 's/.*<string>\(.*\)<\/string>.*/ - \1/'
else else
info "No configuration profiles installed" info "No configuration profiles installed"
fi fi
@@ -891,7 +893,11 @@ deep_status() {
step "Overall Assessment" step "Overall Assessment"
local risk="LOW" local risk="LOW"
[ "$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0)" -gt 0 ] && risk="MEDIUM" local cfg="/private/var/db/ConfigurationProfiles/Settings"
local installed
installed=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
installed="${installed:-0}"
[ "$installed" -gt 0 ] && risk="MEDIUM"
pgrep -i "mdm|managedclient" >/dev/null 2>&1 && risk="HIGH" pgrep -i "mdm|managedclient" >/dev/null 2>&1 && risk="HIGH"
[ -f "$cfg/.cloudConfigRecordFound" ] && risk="CRITICAL" [ -f "$cfg/.cloudConfigRecordFound" ] && risk="CRITICAL"
@@ -911,7 +917,8 @@ deep_status_json() {
local profile_count=0 local profile_count=0
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
profile_count="${profile_count:-0}"
fi fi
json="${json} \"profile_count\": $profile_count,\n" json="${json} \"profile_count\": $profile_count,\n"
@@ -2156,10 +2163,11 @@ generate_report() {
step "Installed Profiles" step "Installed Profiles"
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
local count local count
count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
count="${count:-0}"
if [ "$count" -gt 0 ]; then if [ "$count" -gt 0 ]; then
echo " $count profile(s) installed" echo " $count profile(s) installed"
sudo profiles -C -output=xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" \ sudo profiles -C -output stdout-xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "<string>" \
| sed 's/.*<string>\(.*\)<\/string>.*/ - \1/' | sed 's/.*<string>\(.*\)<\/string>.*/ - \1/'
else else
echo " No profiles installed" echo " No profiles installed"
@@ -2204,7 +2212,8 @@ generate_report() {
step "Hosts Block" step "Hosts Block"
if grep -q "iprofiles.apple.com" /private/etc/hosts 2>/dev/null; then if grep -q "iprofiles.apple.com" /private/etc/hosts 2>/dev/null; then
local blocked local blocked
blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || echo 0) blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || true)
blocked="${blocked:-0}"
echo " $blocked domain(s) blocked in /etc/hosts" echo " $blocked domain(s) blocked in /etc/hosts"
else else
echo " No block entries found" echo " No block entries found"
@@ -2243,7 +2252,8 @@ generate_report_json() {
local profile_count=0 local profile_count=0
if command -v profiles &>/dev/null; then if command -v profiles &>/dev/null; then
profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true)
profile_count="${profile_count:-0}"
fi fi
report="${report} \"installed_profiles\": $profile_count,\n" report="${report} \"installed_profiles\": $profile_count,\n"
@@ -2273,7 +2283,8 @@ generate_report_json() {
report="${report} },\n" report="${report} },\n"
local hosts_blocked=0 local hosts_blocked=0
hosts_blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || echo 0) hosts_blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || true)
hosts_blocked="${hosts_blocked:-0}"
report="${report} \"hosts_blocked\": $hosts_blocked\n" report="${report} \"hosts_blocked\": $hosts_blocked\n"
report="${report}}" report="${report}}"