From 9e04477ec7fe75a2db599a1bd39853e83b40f756 Mon Sep 17 00:00:00 2001 From: atlas Date: Thu, 19 Nov 2026 12:41:24 +0100 Subject: [PATCH] squid vm test: derive the relay's source address from the agent's, and assert a no-lease source is refused, never forwarded --- nix/vm-tests/hive-squid.nix | 40 +++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/nix/vm-tests/hive-squid.nix b/nix/vm-tests/hive-squid.nix index f796aba03..47c3a5310 100644 --- a/nix/vm-tests/hive-squid.nix +++ b/nix/vm-tests/hive-squid.nix @@ -362,6 +362,7 @@ pkgs.testers.runNixOSTest { }; testScript = '' + import ipaddress import re import secrets import shlex @@ -925,6 +926,13 @@ pkgs.testers.runNixOSTest { # three addresses, one per agent, and the exemption is by its MAC. AGENT_MAC = agent.succeed("cat /sys/class/net/eth1/address").strip() ATLAS_IP, LOGAN_IP, NOBODY_IP = "192.168.1.3", "192.168.1.4", "192.168.1.7" + # squid logs an intercepted HTTPS flow from the relay's source address, the pool + # address of the agent (`pool_source` in hive-priv/src/egress_ech.rs): 198.18.0.0/15 + # with the low 17 bits of the agent's address. + def relay_source(ip): + return str(ipaddress.IPv4Address(0xC6120000 | (int(ipaddress.IPv4Address(ip)) & 0x1FFFF))) + + AGENT_SOURCES = "|".join(re.escape(a) for ip in (ATLAS_IP, LOGAN_IP, NOBODY_IP) for a in (ip, relay_source(ip))) BOUND = {"atlas": (ATLAS_IP, AGENT_MAC), "logan": (LOGAN_IP, "02:00:00:00:00:04")} agent.succeed(f"ip addr add {LOGAN_IP}/24 dev eth1; ip addr add {NOBODY_IP}/24 dev eth1") @@ -959,7 +967,7 @@ pkgs.testers.runNixOSTest { def agent_lines(): # Log lines of requests from the agent node's addresses. The self-check # connects from the bridge address, so its lines are not counted. - return len(re.findall(r" (?:192\.168\.1|198\.18\.0)\.[347] [A-Z_]+/\d+ ", journal())) + return len(re.findall(rf" (?:{AGENT_SOURCES}) [A-Z_]+/\d+ ", journal())) seeded = proxy.succeed(f"cat {ALLOWED}") bind("atlas", "logan") @@ -982,7 +990,8 @@ pkgs.testers.runNixOSTest { with checked("a restricted agent's allowed name is tunnelled without the proxy variables, and the log names the agent"): wait_icpt("https://forge.t.local/", "200") assert re.search( - r"198\.18\.0\.3 TCP_TUNNEL/200 .* CONNECT forge\.t\.local:443 atlas ORIGINAL_DST/203\.0\.113\.2 ", journal() + re.escape(relay_source(ATLAS_IP)) + r" TCP_TUNNEL/200 .* CONNECT forge\.t\.local:443 atlas ORIGINAL_DST/203\.0\.113\.2 ", + journal(), ), "the HTTPS flow, as atlas" assert icpt("http://forge.t.local/") == "200", "plain HTTP" assert re.search(r"192\.168\.1\.3 TCP_\w+/200 .* GET http://forge\.t\.local/ atlas ", journal()), "the HTTP flow, as atlas" @@ -994,7 +1003,9 @@ pkgs.testers.runNixOSTest { with checked("a restricted agent's unlisted name is refused, intercepted, and logged as the agent's"): assert icpt("https://other.t.local/") == "000", "HTTPS" - assert re.search(r"198\.18\.0\.3 TCP_DENIED/403 .* CONNECT other\.t\.local:443 atlas ", journal()), "logged as atlas" + assert re.search( + re.escape(relay_source(ATLAS_IP)) + r" TCP_DENIED/403 .* CONNECT other\.t\.local:443 atlas ", journal() + ), "logged as atlas" assert icpt("http://other.t.local/") == "403", "plain HTTP" assert re.search(r"192\.168\.1\.3 TCP_DENIED/403 .* GET http://other\.t\.local/ atlas ", journal()), "logged as atlas" # Control: the same address reaches the listed name, so the refusal is the policy's. @@ -1027,15 +1038,32 @@ pkgs.testers.runNixOSTest { write_deny("") wait_icpt("https://api.t.local/", "200") + def forwarded(ip): + # Log lines of requests from `ip`, or its relay source, that squid sent on + # to an origin. A refusal has no hierarchy entry: `HIER_NONE/-`. + srcs = f"{re.escape(ip)}|{re.escape(relay_source(ip))}" + return re.findall(rf"^.* (?:{srcs}) \S+ \S+ \S+ \S+ \S+ (?!HIER_NONE/)\S+/\S+ .*$", journal(), re.M) + with checked("a source the bindings do not name is refused, and so is every source when they are gone"): - assert icpt("https://forge.t.local/", NOBODY_IP) == "000", "no lease for the address" - assert re.search(r"198\.18\.0\.7 TCP_DENIED/403 .* CONNECT 203\.0\.113\.2:443 - ", journal()), "logged without a user" + # `http_access deny icpt !icpt_agent` meets the CONNECT squid builds from the + # destination address, before the peek, so no SNI is read and nothing is + # sent to the origin. Squid then answers the request on the bumped + # connection with its own 403 page. A refusal after the peek, such as a + # bound agent's unlisted name, closes the connection instead: that is the 000. + assert icpt("https://forge.t.local/", NOBODY_IP) in ("000", "403"), "no lease for the address" + assert re.search( + re.escape(relay_source(NOBODY_IP)) + r" TCP_DENIED/\d{3} .* CONNECT 203\.0\.113\.2:443 - HIER_NONE/- ", journal() + ), "logged without a user" assert icpt("http://forge.t.local/", NOBODY_IP) == "403" + assert not forwarded(NOBODY_IP), "nothing of a source without a lease reached an origin" + assert forwarded(ATLAS_IP), "control: the same lookup finds the bound address's tunnelled requests" assert icpt("https://forge.t.local/") == "200", "control: the bound address, the same name" # A hive whose pin file is missing: the lease names a MAC, no port holds it. proxy.succeed("rm /run/hive-egress-pins") time.sleep(12) # the helper's answers are kept for 10 seconds - assert icpt("https://forge.t.local/") == "000", "no pin file" + sent_on = len(forwarded(ATLAS_IP)) + assert icpt("https://forge.t.local/") in ("000", "403"), "no pin file" + assert len(forwarded(ATLAS_IP)) == sent_on, "no pin file, nothing sent on" bind("atlas", "logan") wait_icpt("https://forge.t.local/", "200")