tests: the resolver trap counts the BROWSER (a browser process and what it starts), not the test process nor the core it runs: the core asks github.com for the latest GeoIP tag as root, the test says the browser asked

Measured on Linux: a process named firefox and its child counted 20 of 20 each; the test's own python and an orphan of the same user foreign 20 of 20 each.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
feder-crandClaude Sonnet 5.5 committed 2026-11-12 13:50:37 +01:00
1 parent 78277565f5
commit fc10e257ca
3 files changed
+73 -24

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@
{"id": "wrapper.e2e.web", "giro": "G2", "os": ["linux", "windows"], "min_tests": 27, "exact": true, "tests": ["tests/test_networkidle_through_a_served_browser_with_routes_e2e.py", "tests/test_http3_through_the_proxy_e2e.py"], "marker": "e2e", "needs_root": false, "note": "The networkidle e2e with routes and the HTTP/3 e2e, split from wrapper.e2e.udp (about 260 s of 500 on Linux; the whole suite did not fit the 15 minutes of Windows)."},
{"id": "wrapper.e2e.doh", "giro": "G2", "os": ["linux", "windows"], "min_tests": 19, "exact": true, "tests": ["tests/test_the_hosts_file_of_the_host_is_never_read_e2e.py", "tests/test_resolver_race_e2e.py", "tests/test_doh_under_stress_e2e.py", "tests/test_context_proxy_only_resolves_by_doh_e2e.py", "tests/test_doh_through_the_proxy_e2e.py", "tests/test_ice_names_by_the_context_proxy_e2e.py"], "marker": "e2e", "needs_root": ["linux"], "note": "Linux needs root: the resolver trap of the file (ResolverTrap) points /etc/resolv.conf at a counting resolver on port 53. Su Windows il resolver trap non gira: l'asserzione sul resolver di sistema è l'unica omessa. Run it without xdist (port 53 is one per host). On the Linux leg the gate exports INVPW_REQUIRE_TRAP=1: a trap that cannot run or bind port 53 is a failure, not a skipped assertion."},
{"id": "wrapper.e2e.realness", "giro": "G2", "os": ["linux", "windows"], "min_tests": 32, "exact": true, "tests": ["tests/test_realness_behind_a_declared_exit_e2e.py"], "marker": "e2e", "needs_root": ["linux"], "note": "The realness check without a real proxy: six declared exits, the values a page reads, and the roads. Linux runs as root without xdist like the DoH suite (the resolver trap); on Windows the trap does not exist and the assertion on the system resolver is the only one omitted."},
{"id": "wrapper.unit.udp", "giro": "G1", "os": ["linux", "windows"], "min_tests": 204, "exact": true, "tests": ["tests/test_the_witness_picks_a_port_both_sockets_bind.py", "tests/test_preload_newtab_reaches_the_core_only_when_asked.py", "tests/test_the_resolver_trap_counts_its_own_users.py", "tests/test_the_doh_e2e_launch_on_a_virtual_display.py", "tests/test_the_realness_helpers.py", "tests/test_the_h3_line_helpers.py", "tests/test_the_fake_doh_delays_and_refuses.py", "tests/test_the_host_address_helper.py", "tests/test_the_udp_chain_helper.py", "tests/test_the_fake_doh_answers.py", "tests/test_doh_session_prefs_unit.py", "tests/test_the_leak_watcher_sees_a_direct_connection.py", "tests/test_the_http_proxy_helper_asks_for_credentials.py", "tests/test_every_route_command_is_answered_may_fail.py", "tests/test_the_network_declaration_reaches_the_engine.py", "tests/test_the_resolver_trap_is_required_by_the_gate.py", "tests/test_the_exit_of_a_context_is_discovered_through_its_own_gateway.py", "tests/test_the_witness_hears_a_direct_send.py"], "marker": "unit", "needs_root": false, "note": "92 = 6+32+21+7+8+3+9+6. The leak watcher file defines exactly one of its two resolver-trap tests per host (7 everywhere), so no test is skipped. `exact`: min_tests is raised by hand when a test is added."},
{"id": "wrapper.unit.udp", "giro": "G1", "os": ["linux", "windows"], "min_tests": 206, "exact": true, "tests": ["tests/test_the_witness_picks_a_port_both_sockets_bind.py", "tests/test_preload_newtab_reaches_the_core_only_when_asked.py", "tests/test_the_resolver_trap_counts_its_own_users.py", "tests/test_the_doh_e2e_launch_on_a_virtual_display.py", "tests/test_the_realness_helpers.py", "tests/test_the_h3_line_helpers.py", "tests/test_the_fake_doh_delays_and_refuses.py", "tests/test_the_host_address_helper.py", "tests/test_the_udp_chain_helper.py", "tests/test_the_fake_doh_answers.py", "tests/test_doh_session_prefs_unit.py", "tests/test_the_leak_watcher_sees_a_direct_connection.py", "tests/test_the_http_proxy_helper_asks_for_credentials.py", "tests/test_every_route_command_is_answered_may_fail.py", "tests/test_the_network_declaration_reaches_the_engine.py", "tests/test_the_resolver_trap_is_required_by_the_gate.py", "tests/test_the_exit_of_a_context_is_discovered_through_its_own_gateway.py", "tests/test_the_witness_hears_a_direct_send.py"], "marker": "unit", "needs_root": false, "note": "92 = 6+32+21+7+8+3+9+6. The leak watcher file defines exactly one of its two resolver-trap tests per host (7 everywhere), so no test is skipped. `exact`: min_tests is raised by hand when a test is added."},
{"id": "wrapper.e2e.page", "giro": "G2", "os": ["linux", "windows"], "min_tests": 114, "exact": true, "tests": ["tests/test_a_page_that_moves_on_e2e.py", "tests/test_a_popup_is_reported_when_the_engine_says_it_is_ready_as_playwright_does_e2e.py", "tests/test_a_popup_of_a_link_reaches_the_client_e2e.py", "tests/test_popup_destinations_e2e.py", "tests/test_popups_reach_the_client_e2e.py", "tests/test_route_superseded_e2e.py"], "marker": "e2e", "needs_root": false, "note": "114 = 100 + the 14 of test_a_page_that_moves_on_e2e.py (a navigation the page overtook with location.replace ends; the engine fix 860ef8a6d9 is proved by it). 100 counted at the collection; was 96 = 10+32+16+34+4 before the popup tests of 6200bc2). A page is handed over when the engine says Page.ready; a popup reaches the client (link, noopener, window.open; own site and another; with and without a route; in-process and over launch_server) as Playwright's does; a superseded route. Needs the candidate binary, and the stock Playwright client of the dev extra."},
{"id": "wrapper.unit.page", "giro": "G1", "os": ["linux", "windows"], "min_tests": 34, "exact": true, "tests": ["tests/test_every_order_of_the_events_of_a_popup_gives_one_page_event.py", "tests/test_ci_drive_gate_goes_through_the_core.py", "tests/test_a_page_is_handed_over_only_when_the_engine_says_it_is_ready.py", "tests/test_a_new_page_waits_for_the_engine.py", "tests/test_page_number_follows_the_call.py"], "marker": "unit", "needs_root": false, "note": "24 (counted at the collection; was 20 = 3+8+1+8 before 6200bc2). The page dispatcher's wait for Page.ready and what it hands over; the drive gate goes through the core (the engine waits for the session's road)."}
]
+38 -9
View File
@@ -272,6 +272,32 @@ def descends_from(pid: int, root: int, parent=parent_of) -> bool:
return False
def is_a_browser_process(pid: int, proc: str = "/proc") -> bool:
"""Whether a process is the browser's own (the main process, ``firefox``, or ``firefox-bin``), by its name in /proc/<pid>/comm.
Its children (content, socket, forkserver) are found by their parents."""
try:
with open(os.path.join(proc, str(pid), "comm"), encoding="utf-8", errors="replace") as handle:
return handle.read().strip() in ("firefox", "firefox-bin", "firefox.exe")
except OSError:
return False
def under_a_browser(pid: int, root: int, parent=parent_of, is_browser=is_a_browser_process) -> bool:
"""Whether ``pid`` is a browser process or one of its descendants, going up the parents and stopping at ``root`` (the test
process: it is NOT the browser, and neither is anything it runs that is not a browser, like the core's own download of a
database) or at a process with no parent known (at most 64 steps)."""
for _ in range(64):
if is_browser(pid):
return True
if pid == root:
return False
up = parent(pid)
if up is None or up <= 1 or up == pid:
return False
pid = up
return False
def who_asked(protocol: str, peer) -> Optional[Asker]:
"""Who sent a question to the trap, read where the kernel says it (Linux): the row of the client's socket in /proc/net
gives the user and the inode, and the inode gives the process that holds it. None off Linux or when the socket is
@@ -302,19 +328,21 @@ def owner_of_the_peer(protocol: str, peer) -> Optional[int]:
class ResolverTrap:
"""A resolver on loopback that counts what it is asked. See the module docstring.
It counts what the PROCESSES OF THE TEST ask: the test process itself and its descendants (the browser), found from the
socket the question came from (its inode, the process that holds it, its parents up to ``root_pid``, by default this
process). A question from any other process is answered and listed in :attr:`foreign` with its pid and command, not
counted: /etc/resolv.conf is the machine's, and on a CI runner the agent of the runner, even as root, asks for github.com
through it (run 43188313516, 43211837926: the browser was blamed for names it never asked, and a user filter cannot tell
a root agent from a root browser). There is no list of names. A question whose process cannot be traced is judged by
its user (``only_uid``, by default the effective uid), and one whose socket cannot be found any more counts as the browser's."""
It counts what the BROWSER asks, and the test says "the browser asked the resolver of the machine". The browser is found from
the socket the question came from (its inode, the process that holds it, its parents up to ``root_pid``, by default the
test process): a question counts if that process is the browser or descends from it. A question from any other process is
answered and listed in :attr:`foreign` with its pid and command, not counted: /etc/resolv.conf is the machine's, and on a
CI runner the agent of the runner, even as root, asks for github.com through it; and the test process's own python (the
core, which asks github.com for the latest GeoIP tag when no database is given) is not the browser either (runs
43188313516, 43211837926: the browser was blamed for names it never asked, and a user filter cannot tell them apart). There
is no list of names. A question whose process cannot be traced is judged by its user (``only_uid``, by default the
effective uid), and one whose socket cannot be found any more counts as the browser's."""
available, reason = _trap_capability()
def __init__(self, answers: Optional[Dict[str, str]] = None, *, port: int = 53,
rewrite_resolv_conf: bool = True, only_uid: Optional[int] = None, owner_of=None,
root_pid: Optional[int] = None, parent_of_pid=None):
root_pid: Optional[int] = None, parent_of_pid=None, is_browser=None):
self.answers = {k.lower(): v for k, v in (answers or {}).items()}
self.port = port
#: The loopback address the trap listens on once it is bound (see TRAP_ADDRESSES).
@@ -326,6 +354,7 @@ class ResolverTrap:
self.foreign: List[Tuple[DohQuery, Asker]] = []
self.root_pid = root_pid if root_pid is not None else os.getpid()
self._parent_of = parent_of_pid or parent_of
self._is_browser = is_browser or is_a_browser_process
self.only_uid = only_uid if only_uid is not None else (os.geteuid() if hasattr(os, "geteuid") else None)
self._owner_of = owner_of or who_asked
self._lock = threading.Lock()
@@ -472,7 +501,7 @@ class ResolverTrap:
ours = True
if asker is not None:
if asker.pid is not None:
ours = descends_from(asker.pid, self.root_pid, self._parent_of)
ours = under_a_browser(asker.pid, self.root_pid, self._parent_of, self._is_browser)
elif self.only_uid is not None:
ours = asker.uid == self.only_uid
with self._lock:
@@ -12,7 +12,8 @@ import socket
import pytest
from _doh_servers import QTYPE_A, encode_query
from _leaks import Asker, ResolverTrap, descends_from, owner_of_the_peer, row_from_proc_net, uid_from_proc_net, who_asked
from _leaks import (Asker, ResolverTrap, descends_from, owner_of_the_peer, row_from_proc_net, uid_from_proc_net,
under_a_browser, who_asked)
pytestmark = pytest.mark.unit
@@ -113,9 +114,16 @@ def test_the_owner_of_a_question_that_waits_for_its_answer_is_always_found():
# ----------------------------------------------------------------------------------------------- by the tree of processes
#: A machine: the test is pid 100, the browser 400 under it (a content process 777 under the browser), the agent of the runner 555
#: under the provisioner 1 - as ROOT too, the user a filter cannot tell from the root browser.
PARENT = {400: 100, 777: 400, 555: 90, 90: 1}
#: A machine: the test is pid 100 (its core makes its own requests); the browser is 400, started by the test, with a content
#: process 777 under it; the agent of the runner is 555 under the provisioner 90 - as ROOT too, the user a filter cannot tell
#: from the root browser.
PARENT = {400: 100, 777: 400, 555: 90, 90: 1, 650: 100}
BROWSERS = {400}
def _trap(owner, answers, **kwargs):
return ResolverTrap(answers, port=0, rewrite_resolv_conf=False, only_uid=0, root_pid=100, parent_of_pid=PARENT.get,
is_browser=lambda pid: pid in BROWSERS, owner_of=owner, **kwargs)
def test_a_process_is_the_root_or_one_of_its_descendants():
@@ -124,6 +132,15 @@ def test_a_process_is_the_root_or_one_of_its_descendants():
assert not descends_from(300, 100, PARENT.get), "a process with no known parent is not a descendant"
def test_the_browser_and_what_it_starts_are_under_a_browser_and_the_test_and_the_rest_are_not():
def under(pid):
return under_a_browser(pid, 100, PARENT.get, lambda p: p in BROWSERS)
assert under(400) and under(777)
assert not under(100), "the test process is not the browser"
assert not under(650), "a process the test started that is not a browser (a download) is not the browser"
assert not under(555) and not under(300)
def test_a_row_of_proc_net_gives_the_user_and_the_inode():
assert row_from_proc_net(UDP_TABLE, 0xC350) == (1001, 67890)
assert row_from_proc_net(UDP_TABLE, 4242) is None
@@ -131,30 +148,33 @@ def test_a_row_of_proc_net_gives_the_user_and_the_inode():
def test_a_root_process_that_is_not_the_browsers_is_foreign_even_with_the_same_user():
"""The agent of the runner runs as root, like the browser under sudo: the user cannot separate them, the tree does."""
with ResolverTrap({"github.com": "127.0.0.1"}, port=0, rewrite_resolv_conf=False, only_uid=0, root_pid=100,
parent_of_pid=PARENT.get,
owner_of=lambda protocol, peer: Asker(0, 555, "Runner.Provisioner: /opt/agent/provisioner")) as trap:
with _trap(lambda protocol, peer: Asker(0, 555, "Runner.Provisioner: /opt/agent/provisioner"), {"github.com": "127.0.0.1"}) as trap:
assert _ask(trap, "github.com")
assert trap.names() == [] and [q.name for q, _ in trap.foreign] == ["github.com"]
say = trap.describe()
assert "github.com from pid 555" in say and "Runner.Provisioner" in say and "not counted" in say
def test_the_test_process_itself_and_its_own_python_are_not_the_browser():
"""The core of the test process asks github.com for the latest GeoIP tag: a question of the test, not of the browser."""
for pid in (100, 650):
with _trap(lambda protocol, peer, pid=pid: Asker(0, pid, "python"), {"github.com": "127.0.0.1"}) as trap:
_ask(trap, "github.com")
assert trap.names() == [] and len(trap.foreign) == 1, pid
def test_the_browser_and_what_it_starts_are_counted():
for pid in (100, 400, 777):
with ResolverTrap({"page-d.test": "127.0.0.1"}, port=0, rewrite_resolv_conf=False, only_uid=0, root_pid=100,
parent_of_pid=PARENT.get, owner_of=lambda protocol, peer, pid=pid: Asker(0, pid, "firefox")) as trap:
for pid in (400, 777):
with _trap(lambda protocol, peer, pid=pid: Asker(0, pid, "firefox"), {"page-d.test": "127.0.0.1"}) as trap:
assert _ask(trap, "page-d.test")
assert trap.names() == ["page-d.test"] and trap.foreign == [], pid
def test_a_process_that_cannot_be_traced_is_judged_by_its_user():
with ResolverTrap({"a.test": "127.0.0.1"}, port=0, rewrite_resolv_conf=False, only_uid=0, root_pid=100,
parent_of_pid=PARENT.get, owner_of=lambda protocol, peer: Asker(1001, None, "")) as trap:
with _trap(lambda protocol, peer: Asker(1001, None, ""), {"a.test": "127.0.0.1"}) as trap:
_ask(trap, "a.test")
assert trap.names() == [] and len(trap.foreign) == 1
with ResolverTrap({"b.test": "127.0.0.1"}, port=0, rewrite_resolv_conf=False, only_uid=0, root_pid=100,
parent_of_pid=PARENT.get, owner_of=lambda protocol, peer: Asker(0, None, "")) as trap:
with _trap(lambda protocol, peer: Asker(0, None, ""), {"b.test": "127.0.0.1"}) as trap:
_ask(trap, "b.test")
assert trap.names() == ["b.test"] and trap.foreign == []