Wire the ring code into the app: per-poll code style (ring default for new polls), projector rings + entry qr, voter camera loop, both token formats

This commit is contained in:
2026-08-16 18:39:58 +01:00
parent c6934080f1
commit 7cbf62baa7
13 changed files with 250 additions and 51 deletions
+64 -6
View File
@@ -90,8 +90,8 @@ async def projector(server, p, key=None):
async def reader():
async for raw in ws:
m = json.loads(raw)
if m["type"] == "qr":
latest.update(tok=m["url"].split("#", 1)[1], slot=m["slot"])
if m["type"] in ("qr", "ring"):
latest.update(tok=m["token"], slot=m["slot"])
changed.set()
task = asyncio.create_task(reader())
@@ -191,7 +191,7 @@ def test_full_flow_and_anonymity(server):
def test_stale_duplicate_and_forged_tokens(server):
p = create(server, scans_required=3)
p = create(server, scans_required=3, code_style="qr")
admin(server, p, "PATCH", "", json={"enrollment_open": True})
async def run():
@@ -431,7 +431,7 @@ def proxied(tmp_path_factory):
def test_behind_proxy(proxied):
fwd = {"X-Forwarded-Host": "vote.example.org", "X-Forwarded-Proto": "https"}
p = httpx.post(proxied["base"] + "/api/polls", json={"title": "P", "interval_ms": 300, "scans_required": 3}).json()
p = httpx.post(proxied["base"] + "/api/polls", json={"title": "P", "interval_ms": 300, "scans_required": 3, "code_style": "qr"}).json()
httpx.patch(f"{proxied['base']}/api/polls/{p['id']}", json={"enrollment_open": True},
headers={"authorization": f"Bearer {p['admin_key']}"})
@@ -440,9 +440,12 @@ def test_behind_proxy(proxied):
await ws.send(json.dumps({"key": p["screen_key"]}))
while True:
m = json.loads(await asyncio.wait_for(ws.recv(), 3))
if m["type"] == "entry":
entry = m
if m["type"] == "qr":
break
assert m["url"].startswith(f"https://vote.example.org/v/{p['id']}#")
assert entry["url"] == f"https://vote.example.org/v/{p['id']}"
await ws.close()
# browser origin is the public host, not the rewritten Host: must pass the origin check
v = await websockets.connect(f"{proxied['ws']}/ws/{p['id']}/voter", origin="https://vote.example.org",
@@ -574,14 +577,16 @@ def test_no_presence_qr_when_everyone_has_a_code(server):
ws = await websockets.connect(f"{server['ws']}/ws/{p['id']}/screen", **server["ws_kw"])
await ws.send(json.dumps({"key": p["screen_key"]}))
st = json.loads(await asyncio.wait_for(ws.recv(), 3))
assert st["type"] == "state" and st["presence"] is False
while st["type"] != "state":
st = json.loads(await asyncio.wait_for(ws.recv(), 3))
assert st["presence"] is False
t0 = time.time()
while time.time() - t0 < 2:
try:
m = json.loads(await asyncio.wait_for(ws.recv(), 0.5))
except asyncio.TimeoutError:
continue
assert m["type"] != "qr"
assert m["type"] not in ("qr", "ring")
await ws.close()
await a.aclose()
@@ -609,3 +614,56 @@ def test_heartbeat_survives_dead_client(server):
await alive.close()
asyncio.run(run())
def test_ring_tokens_stale_forged_and_cross_format(server):
from rotatyvote import tokens as tk
p = create(server, scans_required=3, code_style="ring")
admin(server, p, "PATCH", "", json={"enrollment_open": True})
con = sqlite3.connect(server["db"])
secret, iv = con.execute("SELECT secret, interval_ms FROM polls WHERE id=?", (p["id"],)).fetchone()
con.close()
async def run():
async with projector(server, p) as next_token:
c = device(server)
tok = await next_token()
assert tok.startswith("r")
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tok})).json()
assert r["count"] == 1 and r["accepted"]
# the same slot in the classic format is the same code: counted once
s16 = int(tok[1:].split(".")[0])
now = tk.current_slot(iv)
full = next(s for s in range(now, now - 10, -1) if s & 0xFFFF == s16)
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": tk.make_token(secret, p["id"], iv, full)})).json()
assert r["valid"] and not r["accepted"] and r["count"] == 1
# forged mac, malformed
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": f"r{s16}.00000000"})).json()
assert not r["valid"]
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": "r99999999.zz"})).json()
assert not r["valid"]
# stale ring code
old = await next_token()
await asyncio.sleep(3.2)
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": old})).json()
assert not r["valid"]
# ring code of another poll
other = create(server, code_style="ring")
admin(server, other, "PATCH", "", json={"enrollment_open": True})
async with projector(server, other) as other_next:
ot = await other_next()
r = (await c.post(f"/api/v/{p['id']}/scan", json={"token": ot})).json()
assert not r["valid"]
# and a full enrollment with ring codes only
d = device(server)
assert (await scan_until(d, server, p, next_token))["status"] == "enrolled"
for x in (c, d):
await x.aclose()
asyncio.run(run())
def test_entry_redirect(server):
p = create(server)
r = httpx.get(f"{server['base']}/V/{p['id'].upper()}", follow_redirects=False, verify=server["verify"])
assert r.status_code == 301 and r.headers["location"] == f"/v/{p['id']}"