diff --git a/steam_backlog_enforcer/_hosts_lines.py b/steam_backlog_enforcer/_hosts_lines.py new file mode 100644 index 0000000..21628be --- /dev/null +++ b/steam_backlog_enforcer/_hosts_lines.py @@ -0,0 +1,94 @@ +"""Which /etc/hosts lines block the Steam store, in both address families. + +The hosts installer writes every blocked domain twice: ``0.0.0.0 `` +and ``:: ``. Steam and browsers prefer IPv6, so a block counts while +either line is live: commenting out only the ``0.0.0.0`` line left the store +blocked while the unblock reported success. + +A line counts only when its address field is exactly a sinkhole and one of +its hostnames exactly equals a store domain, so ``steamkarta.pl``, +``::1 localhost`` or a prose comment naming a domain are never touched. +""" + +from __future__ import annotations + +from typing import Final + +from steam_backlog_enforcer._store_tools import HOSTS_REDIRECT_IP +from steam_backlog_enforcer.config import BLOCKED_DOMAINS + +# The sinkhole addresses (not a bind: these are hosts-file targets). +SINKHOLES: Final = frozenset({HOSTS_REDIRECT_IP, "::"}) +# An address and at least one hostname. +_MIN_FIELDS: Final = 2 + + +def _store_hosts(entry: str) -> list[str] | None: + """Fields of a live hosts *entry* that sinkholes a store domain, else None.""" + fields = entry.split("#", 1)[0].split() + if len(fields) < _MIN_FIELDS or fields[0] not in SINKHOLES: + return None + if any(host in BLOCKED_DOMAINS for host in fields[1:]): + return fields + return None + + +def is_live_store_line(line: str) -> bool: + """Whether *line* is an uncommented sinkhole entry for a store domain.""" + return _store_hosts(line) is not None + + +def _commented_entry(line: str) -> str | None: + """The entry behind a commented-out store line, else None.""" + stripped = line.strip() + if not stripped.startswith("#"): + return None + entry = stripped.lstrip("#").strip() + return entry if is_live_store_line(entry) else None + + +def comment_out_store_line(line: str) -> str | None: + """*line* commented out if it is a live store line, else None.""" + if not is_live_store_line(line): + return None + return f"# {line}" if line.endswith("\n") else f"# {line}\n" + + +def uncomment_store_line(line: str) -> str | None: + """*line* made live again if it is a commented-out store line, else None.""" + entry = _commented_entry(line) + if entry is None: + return None + return f"{entry}\n" + + +def _candidates(content: str) -> list[str]: + """Lines naming a store domain at all: a cheap prefilter for a 8 MB file.""" + return [ + line.strip() + for line in content.splitlines() + if any(domain in line for domain in BLOCKED_DOMAINS) + ] + + +def live_store_lines(content: str) -> list[str]: + """Every live store block line in *content* (stripped).""" + return [line for line in _candidates(content) if is_live_store_line(line)] + + +def store_lines(content: str) -> list[str]: + """Every store block line, live or commented out (for failure reports).""" + return [ + line + for line in _candidates(content) + if is_live_store_line(line) or _commented_entry(line) is not None + ] + + +def hosts_fully_blocks_store(content: str) -> bool: + """Whether every store domain has a live line for both sinkholes.""" + pairs: set[tuple[str, str]] = set() + for line in live_store_lines(content): + fields = line.split("#", 1)[0].split() + pairs.update((fields[0], host) for host in fields[1:]) + return all((ip, d) in pairs for d in BLOCKED_DOMAINS for ip in SINKHOLES) diff --git a/steam_backlog_enforcer/_hosts_protection.py b/steam_backlog_enforcer/_hosts_protection.py index 7165af4..ccdccf9 100644 --- a/steam_backlog_enforcer/_hosts_protection.py +++ b/steam_backlog_enforcer/_hosts_protection.py @@ -6,19 +6,18 @@ introduces no cycle. Split to keep both files under the 250-line cap. from __future__ import annotations +import contextlib import logging import subprocess from typing import TYPE_CHECKING +from steam_backlog_enforcer._hosts_lines import uncomment_store_line from steam_backlog_enforcer._store_tools import ( GUARDCTL, SUDO, TEE, ) -from steam_backlog_enforcer.config import ( - BLOCKED_DOMAINS, - HOSTS_FILE, -) +from steam_backlog_enforcer.config import HOSTS_FILE if TYPE_CHECKING: from collections.abc import Callable @@ -92,20 +91,19 @@ def rewrite_hosts_lines( if changed: _sudo_write_hosts("".join(new_lines)) logger.info(done_message) - - _enable_hosts_protection() - except OSError: + except OSError, subprocess.SubprocessError: logger.exception("Failed to modify /etc/hosts") return False + finally: + # Re-lock even after a failed write: never leave /etc/hosts open. + with contextlib.suppress(OSError, subprocess.SubprocessError): + _enable_hosts_protection() return True def _uncomment_blocked(line: str) -> str | None: - """The line with its '# ' prefix removed, if it is a commented-out block.""" - stripped = line.strip() - if stripped.startswith("# ") and any(d in stripped for d in BLOCKED_DOMAINS): - return line.replace("# ", "", 1) - return None + """The line made live again, if it is a commented-out store sinkhole.""" + return uncomment_store_line(line) def _reblock_hosts() -> bool: diff --git a/steam_backlog_enforcer/store_blocker.py b/steam_backlog_enforcer/store_blocker.py index a5955f6..1dc4673 100644 --- a/steam_backlog_enforcer/store_blocker.py +++ b/steam_backlog_enforcer/store_blocker.py @@ -14,6 +14,11 @@ from __future__ import annotations import logging import subprocess +from steam_backlog_enforcer._hosts_lines import ( + comment_out_store_line, + hosts_fully_blocks_store, + live_store_lines, +) from steam_backlog_enforcer._hosts_protection import ( _reblock_hosts, rewrite_hosts_lines, @@ -27,13 +32,9 @@ from steam_backlog_enforcer._store_iptables import ( from steam_backlog_enforcer._store_tools import ( BASH, HOSTS_INSTALL_SCRIPT, - HOSTS_REDIRECT_IP, SUDO, ) -from steam_backlog_enforcer.config import ( - BLOCKED_DOMAINS, - HOSTS_FILE, -) +from steam_backlog_enforcer.config import HOSTS_FILE logger = logging.getLogger(__name__) @@ -47,23 +48,23 @@ def is_store_blocked() -> bool: def hosts_blocks_store() -> bool: - """Check /etc/hosts alone: a file read, no subprocess, safe to poll.""" + """Whether any store line in /etc/hosts is live (either address family). + + A file read, no subprocess, safe to poll: one live ``::`` line is enough + to keep the store unreachable. + """ try: - content = HOSTS_FILE.read_text(encoding="utf-8") - # Check for at least the primary store domain. - if "store.steampowered.com" in content: - # Verify it's actually blocked (not commented out). - for line in content.splitlines(): - stripped = line.strip() - if ( - not stripped.startswith("#") - and "store.steampowered.com" in stripped - and stripped.startswith(HOSTS_REDIRECT_IP) - ): - return True + return bool(live_store_lines(HOSTS_FILE.read_text(encoding="utf-8"))) except OSError: - pass - return False + return False + + +def hosts_fully_block_store() -> bool: + """Whether every store domain is sinkholed in both address families.""" + try: + return hosts_fully_blocks_store(HOSTS_FILE.read_text(encoding="utf-8")) + except OSError: + return False def block_store() -> bool: @@ -71,12 +72,14 @@ def block_store() -> bool: Returns True if at least one blocking method succeeded. """ - if is_store_blocked(): + # "Already blocked" needs both families of every domain: a half-done + # unblock (IPv4 commented, IPv6 live) must still get its lines back. + if hosts_fully_block_store(): logger.info("Steam Store already blocked in /etc/hosts.") return True # Try quick re-block (uncomment lines) first. - if _reblock_hosts() and is_store_blocked(): + if _reblock_hosts() and hosts_fully_block_store(): _block_store_iptables() flush_dns_cache() return True @@ -116,7 +119,7 @@ def _block_via_hosts_install() -> bool: The install script handles: immutable flag removal, bind mount remounting, writing the file, re-applying protections, and DoH disabling. """ - if is_store_blocked(): + if hosts_fully_block_store(): logger.info("Steam Store already blocked in /etc/hosts.") return True @@ -189,12 +192,5 @@ def _unblock_hosts() -> bool: def _comment_out_blocked(line: str) -> str | None: - """The line commented out, if it is a live redirect of a blocked domain.""" - stripped = line.strip() - if ( - not stripped.startswith("#") - and stripped.startswith(HOSTS_REDIRECT_IP) - and any(d in stripped for d in BLOCKED_DOMAINS) - ): - return f"# {line}" if line.endswith("\n") else f"# {line}\n" - return None + """The line commented out, if it is a live sinkhole of a store domain.""" + return comment_out_store_line(line) diff --git a/steam_backlog_enforcer/tests/test_hosts_lines.py b/steam_backlog_enforcer/tests/test_hosts_lines.py new file mode 100644 index 0000000..2815358 --- /dev/null +++ b/steam_backlog_enforcer/tests/test_hosts_lines.py @@ -0,0 +1,107 @@ +"""Tests for the /etc/hosts store-line matcher (both address families).""" + +from __future__ import annotations + +import pytest + +from steam_backlog_enforcer._hosts_lines import ( + comment_out_store_line, + hosts_fully_blocks_store, + is_live_store_line, + live_store_lines, + store_lines, + uncomment_store_line, +) +from steam_backlog_enforcer.config import BLOCKED_DOMAINS + +_STORE = "store.steampowered.com" + + +def _full_block() -> str: + """The hosts text the hosts installer writes: both families per domain.""" + return "".join(f"0.0.0.0 {d}\n:: {d}\n" for d in BLOCKED_DOMAINS) + + +@pytest.mark.parametrize( + "line", + [ + f"0.0.0.0 {_STORE}", + f":: {_STORE}", + f"::\t{_STORE}", + f" 0.0.0.0 {_STORE} # inline note", + f"0.0.0.0 other.example {_STORE}", + "0.0.0.0 checkout.steampowered.com\n", + ], +) +def test_live_store_lines(line: str) -> None: + """Both sinkholes, any whitespace, multi-host lines and trailing notes.""" + assert is_live_store_line(line) + + +@pytest.mark.parametrize( + "line", + [ + f"# 0.0.0.0 {_STORE}", + f"# :: {_STORE}", + f"127.0.0.1 {_STORE}", + f"::1 {_STORE}", + f"::ffff:0.0.0.0 {_STORE}", + "::1 localhost", + "0.0.0.0 steamkarta.pl", + ":: steamkarta.pl", + f"0.0.0.0 x{_STORE}", + f"0.0.0.0 {_STORE}.evil", + f"# blocks {_STORE} below", + "", + ], +) +def test_not_live_store_lines(line: str) -> None: + """Comments, other addresses, other hosts and substrings never match.""" + assert not is_live_store_line(line) + + +def test_comment_out_both_families() -> None: + """Unblock comments out the IPv4 and the IPv6 line, keeping newlines.""" + assert comment_out_store_line(f"0.0.0.0 {_STORE}\n") == f"# 0.0.0.0 {_STORE}\n" + assert comment_out_store_line(f":: {_STORE}\n") == f"# :: {_STORE}\n" + assert comment_out_store_line(f":: {_STORE}") == f"# :: {_STORE}\n" + + +def test_comment_out_leaves_other_lines() -> None: + """``steamkarta.pl`` and ``::1 localhost`` stay untouched.""" + for line in (":: steamkarta.pl\n", "::1 localhost\n", f"# :: {_STORE}\n"): + assert comment_out_store_line(line) is None + + +def test_uncomment_both_families() -> None: + """Re-block uncomments the IPv4 and the IPv6 line.""" + assert uncomment_store_line(f"# 0.0.0.0 {_STORE}\n") == f"0.0.0.0 {_STORE}\n" + assert uncomment_store_line(f"# :: {_STORE}\n") == f":: {_STORE}\n" + assert uncomment_store_line(f"#:: {_STORE}\n") == f":: {_STORE}\n" + + +def test_uncomment_leaves_prose_comments() -> None: + """A comment that only mentions a store domain is not a block line.""" + for line in ( + f"# blocks {_STORE} below\n", + "# 0.0.0.0 steamkarta.pl\n", + f"0.0.0.0 {_STORE}\n", + ): + assert uncomment_store_line(line) is None + + +def test_live_store_lines_ipv6_left_behind() -> None: + """The bug kuhy hit: IPv4 commented, IPv6 still live, counts as blocked.""" + text = f"# 0.0.0.0 {_STORE}\n:: {_STORE}\n0.0.0.0 steamkarta.pl\n" + assert live_store_lines(text) == [f":: {_STORE}"] + assert store_lines(text) == [f"# 0.0.0.0 {_STORE}", f":: {_STORE}"] + + +def test_fully_blocks_needs_both_families_for_every_domain() -> None: + """Half a block (one family, or one domain missing) is not a full block.""" + full = _full_block() + assert hosts_fully_blocks_store(full) + assert not hosts_fully_blocks_store(full.replace(f"0.0.0.0 {_STORE}\n", "")) + half = full.replace(f"0.0.0.0 {_STORE}\n", f"# 0.0.0.0 {_STORE}\n") + assert not hosts_fully_blocks_store(half) + assert not hosts_fully_blocks_store("") diff --git a/steam_backlog_enforcer/tests/test_store_blocker.py b/steam_backlog_enforcer/tests/test_store_blocker.py index b8d0422..60381a6 100644 --- a/steam_backlog_enforcer/tests/test_store_blocker.py +++ b/steam_backlog_enforcer/tests/test_store_blocker.py @@ -99,7 +99,7 @@ class TestBlockStore: def test_already_blocked(self) -> None: """Test already blocked.""" with patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=True, ): assert block_store() is True @@ -108,7 +108,7 @@ class TestBlockStore: """Test reblock succeeds.""" with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", side_effect=[False, True], ), patch( @@ -128,7 +128,7 @@ class TestBlockStore: """Test fallback to install script.""" with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", side_effect=[False, False], ), patch( @@ -153,7 +153,7 @@ class TestBlockStore: """Test all fail.""" with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", side_effect=[False, False], ), patch( @@ -175,7 +175,7 @@ class TestBlockStore: """Test iptables only succeeds.""" with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", side_effect=[False, False], ), patch( diff --git a/steam_backlog_enforcer/tests/test_store_blocker_block_via_hosts_install.py b/steam_backlog_enforcer/tests/test_store_blocker_block_via_hosts_install.py index d19edcf..410f3e7 100644 --- a/steam_backlog_enforcer/tests/test_store_blocker_block_via_hosts_install.py +++ b/steam_backlog_enforcer/tests/test_store_blocker_block_via_hosts_install.py @@ -22,7 +22,7 @@ class TestBlockViaHostsInstall: def test_already_blocked(self) -> None: """Test already blocked.""" with patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=True, ): assert _block_via_hosts_install() is True @@ -31,7 +31,7 @@ class TestBlockViaHostsInstall: """Test script missing.""" with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=False, ), patch( @@ -48,7 +48,7 @@ class TestBlockViaHostsInstall: mock_result = MagicMock(returncode=0) with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=False, ), patch( @@ -69,7 +69,7 @@ class TestBlockViaHostsInstall: mock_result = MagicMock(returncode=1, stderr="error", stdout="") with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=False, ), patch( @@ -90,7 +90,7 @@ class TestBlockViaHostsInstall: mock_result = MagicMock(returncode=1, stderr="", stdout="out") with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=False, ), patch( @@ -110,7 +110,7 @@ class TestBlockViaHostsInstall: script.touch() with ( patch( - "steam_backlog_enforcer.store_blocker.is_store_blocked", + "steam_backlog_enforcer.store_blocker.hosts_fully_block_store", return_value=False, ), patch( diff --git a/steam_backlog_enforcer/tests/test_store_blocker_ipv6.py b/steam_backlog_enforcer/tests/test_store_blocker_ipv6.py new file mode 100644 index 0000000..dbb3c15 --- /dev/null +++ b/steam_backlog_enforcer/tests/test_store_blocker_ipv6.py @@ -0,0 +1,97 @@ +"""Store unblock / re-block on a hosts file that blocks both address families.""" + +from __future__ import annotations + +import subprocess +from typing import TYPE_CHECKING +from unittest.mock import patch + +from steam_backlog_enforcer import store_blocker +from steam_backlog_enforcer._hosts_protection import rewrite_hosts_lines +from steam_backlog_enforcer.config import BLOCKED_DOMAINS + +if TYPE_CHECKING: + from collections.abc import Callable + from pathlib import Path + +PKG = "steam_backlog_enforcer.store_blocker" +_HP = "steam_backlog_enforcer._hosts_protection" +_OTHER = "0.0.0.0 steamkarta.pl\n:: steamkarta.pl\n::1 localhost\n" + + +def _blocked() -> str: + return _OTHER + "".join(f"0.0.0.0 {d}\n:: {d}\n" for d in BLOCKED_DOMAINS) + + +def _run(hosts: Path, fn: Callable[[], bool]) -> bool: + """Call *fn* with /etc/hosts redirected to *hosts* and writes landing there.""" + + def write(content: str) -> None: + hosts.write_text(content, encoding="utf-8") + + with ( + patch(f"{PKG}.HOSTS_FILE", hosts), + patch(f"{_HP}.HOSTS_FILE", hosts), + patch(f"{_HP}._disable_hosts_protection"), + patch(f"{_HP}._enable_hosts_protection"), + patch(f"{_HP}._sudo_write_hosts", side_effect=write), + patch(f"{PKG}._is_iptables_blocked", return_value=False), + patch(f"{PKG}._block_store_iptables", return_value=True), + patch(f"{PKG}._unblock_store_iptables", return_value=True), + patch(f"{PKG}.flush_dns_cache"), + ): + return fn() + + +def test_unblock_comments_out_both_families(tmp_path: Path) -> None: + """Every ``0.0.0.0`` and ``::`` store line is commented; others stay.""" + hosts = tmp_path / "hosts" + hosts.write_text(_blocked(), encoding="utf-8") + assert _run(hosts, store_blocker.unblock_store) is True + text = hosts.read_text(encoding="utf-8") + for domain in BLOCKED_DOMAINS: + assert f"# 0.0.0.0 {domain}\n" in text + assert f"# :: {domain}\n" in text + assert text.startswith(_OTHER) + assert not _run(hosts, store_blocker.hosts_blocks_store) + + +def test_ipv6_line_alone_counts_as_blocked(tmp_path: Path) -> None: + """A live ``::`` line is a block even with the IPv4 line commented out.""" + hosts = tmp_path / "hosts" + hosts.write_text("# 0.0.0.0 store.steampowered.com\n:: store.steampowered.com\n") + assert _run(hosts, store_blocker.hosts_blocks_store) is True + + +def test_reblock_from_half_state_uncomments_both(tmp_path: Path) -> None: + """IPv4 commented + IPv6 live is not "already blocked": both end up live.""" + hosts = tmp_path / "hosts" + half = _OTHER + "".join(f"# 0.0.0.0 {d}\n:: {d}\n" for d in BLOCKED_DOMAINS) + hosts.write_text(half, encoding="utf-8") + assert _run(hosts, store_blocker.block_store) is True + assert hosts.read_text(encoding="utf-8") == _blocked() + + +def test_reblock_after_unblock_round_trips(tmp_path: Path) -> None: + """Unblock then re-block gives back the original file.""" + hosts = tmp_path / "hosts" + hosts.write_text(_blocked(), encoding="utf-8") + _run(hosts, store_blocker.unblock_store) + _run(hosts, store_blocker.block_store) + assert hosts.read_text(encoding="utf-8") == _blocked() + + +def test_failed_write_reports_failure_and_relocks(tmp_path: Path) -> None: + """A failing ``sudo tee`` returns False and still re-locks /etc/hosts.""" + hosts = tmp_path / "hosts" + hosts.write_text(_blocked(), encoding="utf-8") + error = subprocess.CalledProcessError(1, ["tee"]) + with ( + patch(f"{_HP}.HOSTS_FILE", hosts), + patch(f"{_HP}._disable_hosts_protection"), + patch(f"{_HP}._enable_hosts_protection") as relock, + patch(f"{_HP}._sudo_write_hosts", side_effect=error), + ): + ok = rewrite_hosts_lines(lambda line: f"# {line}", "done") + assert ok is False + relock.assert_called_once()