feat: decide who may connect at all, by source address
mirror has no accounts, so on a real network the address a connection arrives
from is not a nicety — it is the entire access control. Before this change the
answer was "everyone who can open a TCP connection", which is fine for a laptop
demo and alarming for a box someone leaves running.
`mirror serve --allow-cidr 10.2.0.0/22` (or MIRROR_ALLOW_CIDRS) now restricts
pages, the JSON API and the signaling socket to the networks named, and it is
strict where it needs to be: a malformed CIDR exits 2 with one line instead of
starting a server whose list silently failed to parse, because the two
outcomes of that typo are "nobody can connect" and "everybody can connect".
Empty remains the only permissive state, on purpose.
The rules that matter live in src/mirror/access.py and are the ones that get
either outcome:
* X-Forwarded-For is believed only from MIRROR_TRUSTED_PROXIES (loopback by
default), and only its left-most value. An untrusted peer that sends the
header gets judged on its own address — there is a test for exactly that,
because it is the shape of a bypass.
* The socket is closed before it accepts, so a refused address never creates
a roster entry to clean up behind it.
* An address that cannot be parsed fails closed, and a v6 address is never
"inside" a v4 network (comparing the two raises TypeError, which would be
a 500 on a dual-stack host).
* /healthz is exempt: the container health probe comes from loopback, which
a production list otherwise excludes.
The refused client is told why, and which address it was judged as — "403" in
front of a proxy is undiagnosable otherwise. Browsers get a page, fetches get
JSON, and the page is self-contained because a refused address cannot load
/static/app.css any more than it can load a page. net.js keys its own notice on
the /api/config 403 and stops: without that the pill reads "offline,
reconnecting in 32s" forever for a decision that will never change.
The static files drop their only inline script and style attribute (the landing
page's counter is /static/home.js, its fine print is .fineprint) so the
deployment can send `script-src 'self'; style-src 'self'` with no
'unsafe-inline'. That policy was verified through a real nginx edge carrying the
exact header set — capture through SRTP to decoded, painted frames at
1280x720, eight distinct, zero securitypolicyviolation events — which also
caught the edge sending `Cache-Control: no-store, no-store` and needed
proxy_hide_header, since nginx appends rather than replaces.
24 tests added; 176 pass, 2 skipped, pyright and ruff clean.
LLM-Generated: true
This commit is contained in:
@@ -267,3 +267,36 @@ class TestRosterTruthfulness:
|
||||
mirror = payload["mirrors"][0]
|
||||
assert mirror["width"] and mirror["height"]
|
||||
assert mirror["displaySurface"] in ("monitor", "window", "browser")
|
||||
|
||||
|
||||
class TestAddressGate:
|
||||
"""`--allow-cidr` in a real process, refused by a real browser.
|
||||
|
||||
``tests/test_app.py::TestAddressGate`` covers the JSON refusal the pages key
|
||||
their own notice on, the proxy unwrapping, and the socket closing before it
|
||||
accepts. This covers what a person actually gets when a server refuses them:
|
||||
an explanation naming the address it judged, not an empty wall that looks
|
||||
like a bug in the tool.
|
||||
"""
|
||||
|
||||
@pytest.mark.parametrize("path", ["/mirror", "/watch"])
|
||||
def test_a_refused_address_sees_why_and_none_of_the_app(self, browser, restricted_url, path):
|
||||
context = browser.new_context(viewport={"width": 1100, "height": 720})
|
||||
page = context.new_page()
|
||||
try:
|
||||
response = page.goto(f"{restricted_url}{path}")
|
||||
assert response.status == 403
|
||||
expect(page.locator("body")).to_contain_text("not open to your address")
|
||||
expect(page.locator("body")).to_contain_text("203.0.113.0/24")
|
||||
# Which address it judged — without this, "you are not allowed" in
|
||||
# front of a proxy is undiagnosable.
|
||||
expect(page.locator("body")).to_contain_text("127.0.0.1")
|
||||
# None of the app shell leaked past the gate.
|
||||
expect(page.locator("#start, #grid, #notice")).to_have_count(0)
|
||||
finally:
|
||||
context.close()
|
||||
|
||||
def test_the_health_probe_is_not_gated(self, restricted_url):
|
||||
import httpx
|
||||
|
||||
assert httpx.get(f"{restricted_url}/healthz", timeout=5).status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user