Add serve --local for using the web page on this machine
CI / test (3.10) (push) Successful in 1m24s
CI / test (3.12) (push) Successful in 1m5s
CI / test (3.13) (push) Successful in 1m3s

The page is unreachable without nginx, since every route but /healthz
needs the X-Receipt-Token header it adds. `serve --local` serves it
without a token instead: it only binds to a loopback address and answers
only requests whose Host is localhost, 127.0.0.1 or ::1, so another site
can't reach it by DNS rebinding. POSTs still need the page header.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-14 22:24:52 +00:00
co-authored by Claude Opus 5.5
parent ba92f1fbda
commit 0d4e2130a6
5 changed files with 91 additions and 14 deletions
+40 -1
View File
@@ -215,12 +215,51 @@ def test_printer_error_fails_the_job(client, monkeypatch):
def test_serve_refuses_without_token(monkeypatch, capsys):
monkeypatch.delenv(receipt_web.TOKEN_ENV, raising=False)
rc = receipt_web.serve(make_args(listen="127.0.0.1:8765"))
rc = receipt_web.serve(make_args(listen="127.0.0.1:8765", local=False))
assert rc == 1
assert receipt_web.TOKEN_ENV in capsys.readouterr().err
def test_local_serve_refuses_other_addresses(capsys):
rc = receipt_web.serve(make_args(listen="0.0.0.0:8765", local=True))
assert rc == 1
assert "localhost" in capsys.readouterr().err
@pytest.fixture
def local_client():
config = receipt.PrinterConfig(no_setup=True)
return TestClient(receipt_web.create_app(config, None), base_url="http://localhost:8765")
def test_local_mode_needs_no_token(local_client, fake_printer):
assert "Receipt printer" in local_client.get("/").text
res = local_client.post("/api/note", headers={"X-Requested-With": "receipt"},
json={"text": "hi"})
job = finished_job(local_client, res)
assert job["state"] == "done"
assert fake_printer.calls == ["open", "raw", "close"]
@pytest.mark.parametrize("host", ["evil.example", "192.168.1.5:8765"])
def test_local_mode_refuses_other_hosts(local_client, fake_printer, host):
res = local_client.post("/api/note", json={"text": "hi"},
headers={"Host": host, "X-Requested-With": "receipt"})
assert res.status_code == 400
assert local_client.get("/", headers={"Host": host}).status_code == 400
assert fake_printer.calls == []
def test_local_mode_still_needs_page_header(local_client, fake_printer):
assert local_client.post("/api/note", json={"text": "hi"}).status_code == 403
assert fake_printer.calls == []
SCHEDULE = {"name": "Chore", "trigger": {"type": "recurring", "times": ["08:00"]},
"blocks": [{"type": "note", "text": "Water the plants"}]}