testsAndMisc/linux_configuration/periodic_background/hosts/lib/hosts_protect_unblock.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

89 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# lib/hosts_protect_unblock.sh — the anti-tamper guard on the UNBLOCK list.
#
# The mirror of hosts_protect_custom.sh: it refuses the install when the
# whitelist has GROWN, so domains cannot be quietly un-blocked. Sourced by
# install.sh.
# Extract whitelisted domains from the protected list embedded in this script
extract_unblock_entries_from_script() {
local script_path="$1"
# The two marker lines match the domain pattern themselves -- they are
# word characters and underscores -- so a plain grep pulled
# PROTECTED_UNBLOCK_LIST_START and _END into the whitelist as if they were
# domains. They then went into the saved state, and renaming either marker
# would have read as the whitelist growing and blocked the install. Drop
# them explicitly rather than relying on the pattern to exclude them.
sed -n '/^# PROTECTED_UNBLOCK_LIST_START$/,/^# PROTECTED_UNBLOCK_LIST_END$/p' "$script_path" |
grep -E '^# [a-zA-Z0-9._-]+$' |
sed 's/^# //' |
grep -vxE 'PROTECTED_UNBLOCK_LIST_(START|END)' |
sort -u
}
# Save current unblock entries to immutable state file
save_unblock_entries_state() {
local entries="$1"
chattr -i "$UNBLOCK_STATE_FILE" 2>/dev/null || true
echo "$entries" | sort -u >"$UNBLOCK_STATE_FILE"
chmod 644 "$UNBLOCK_STATE_FILE"
chattr +i "$UNBLOCK_STATE_FILE" 2>/dev/null || true
}
# Block installation if the unblock list has grown (more sites being whitelisted)
check_unblock_entries_protection() {
local script_path
# The install script whose entry lists are being checked. Defaults to the
# running script, which is what production always wants; a test points it
# at a fixture instead, since these guards are the part of this file where
# a silent regression matters most.
script_path="$(readlink -f "${HOSTS_INSTALL_SCRIPT_PATH:-$0}")"
local new_entries
new_entries=$(extract_unblock_entries_from_script "$script_path")
local new_count
new_count=$(count_lines "$new_entries")
if [[ ! -f $UNBLOCK_STATE_FILE ]]; then
echo "ℹ️ First unblock-list run — no protection check needed."
return 0
fi
local saved_entries
saved_entries=$(sort -u "$UNBLOCK_STATE_FILE")
local saved_count
saved_count=$(count_lines "$saved_entries")
# Entries added since last install
local added_entries
added_entries=$(comm -13 <(echo "$saved_entries") <(echo "$new_entries"))
local added_count
added_count=$(count_lines "$added_entries")
echo ""
echo "📊 Unblock Entries Protection Check:"
echo " Previously whitelisted: $saved_count domains"
echo " Currently in script: $new_count domains"
echo " Newly added: $added_count"
if [[ $added_count -eq 0 ]]; then
echo " ✅ No new unblocks — protection check passed."
return 0
fi
echo ""
echo "============================================================"
echo " ❌ INSTALLATION BLOCKED — NEW UNBLOCK ENTRIES DETECTED"
echo "============================================================"
echo ""
echo "You are attempting to WHITELIST these additional domains:"
while IFS= read -r entry; do
echo " + $entry"
done <<<"$added_entries"
echo ""
echo "To proceed, manually delete the state file first:"
echo " sudo chattr -i $UNBLOCK_STATE_FILE && sudo rm $UNBLOCK_STATE_FILE"
echo ""
return 1
}