Import ECS deployed relay source and historical patches (2026-10-27)

This commit is contained in:
ex_tianzy
2026-10-27 17:26:58 +08:00
commit 011a7797ee
319 changed files with 34265 additions and 0 deletions
@@ -0,0 +1,141 @@
import asyncio
import json
import secrets
import time
import httpx
import pytest
from relay.crypto import load_private, sign
from relay.web_session import COOKIE, digest
from .conftest import provision, connect_device
async def issue(env, directory, host):
config = json.loads((directory / "device.json").read_text())
proof = sign({"host_id": host, "owner": config["owner"], "purpose": "h5-pair-v1",
"nonce": secrets.token_hex(16), "ts": int(time.time())}, load_private(directory / "device.key"))
response = await env["client"].post("/device/web-pair", json={"host_id": host, "proof": proof})
assert response.status_code == 200, response.text
return response.json(), proof
async def claim(env, code):
return await env["client"].post("/web/claim", json={"code": code}, headers={"Origin": env["settings"].public_url})
def cookie(response):
return {"Cookie": COOKIE + "=" + response.cookies.get(COOKIE)}
async def browser(env, directory, host):
issued, _ = await issue(env, directory, host)
response = await claim(env, issued["code"])
assert response.status_code == 200, response.text
return {**cookie(response), "Origin": env["settings"].public_url}
async def test_browser_pairing_one_time_signed_and_secure_cookie(env):
directory, host = await provision(env)
issued, proof = await issue(env, directory, host)
assert "signature" in issued
# The anonymous browser can neither name an owner nor invent a device signature.
assert (await env["client"].post("/device/web-pair", json={"host_id": host, "proof": {}})).status_code == 403
assert (await env["client"].post("/device/web-pair", json={"host_id": host, "proof": proof})).status_code == 409
response = await claim(env, issued["code"])
assert response.status_code == 200
header = response.headers["set-cookie"].lower()
assert "secure" in header and "httponly" in header and "samesite=strict" in header
assert "code" not in response.json() and "owner" not in response.json()
assert (await claim(env, issued["code"])).status_code == 404
assert (await env["client"].get("/web/session")).status_code == 401 # Secure cookies never sent over test HTTP.
session = await env["client"].get("/web/session", headers=cookie(response))
assert session.status_code == 200 and session.json()["host_id"] == host
assert session.headers["cache-control"] == "no-store"
async def test_cross_origin_cannot_consume_pair_code_or_send_chat(env):
directory, host = await provision(env)
issued, _ = await issue(env, directory, host)
for origin in [None, "https://evil.invalid", "null", "http://localhost.evil.invalid"]:
headers = {} if origin is None else {"Origin": origin}
result = await env["client"].post("/web/claim", json={"code": issued["code"]}, headers=headers)
assert result.status_code == 403
response = await claim(env, issued["code"])
assert response.status_code == 200
result = await env["client"].post("/web/chat", headers={**cookie(response), "Origin": "https://evil.invalid"},
json={"text": "hello", "request_id": secrets.token_hex(16)})
assert result.status_code == 403
async def test_web_devices_are_isolated_and_chat_deduplicates(env):
a, ah = await provision(env, "ou_a")
b, bh = await provision(env, "ou_b")
await connect_device(env, a)
await connect_device(env, b)
a_headers = await browser(env, a, ah)
b_headers = await browser(env, b, bh)
rid = secrets.token_hex(16)
a_result, b_result = await asyncio.gather(
env["client"].post("/web/chat", headers=a_headers, json={"text": "/回显 A独立回复", "request_id": rid}),
env["client"].post("/web/chat", headers=b_headers, json={"text": "/回显 B独立回复", "request_id": rid}))
assert a_result.json()["result"] == "A独立回复"
assert b_result.json()["result"] == "B独立回复"
again = await env["client"].post("/web/chat", headers=a_headers, json={"text": "/回显 repeat", "request_id": rid})
assert again.json()["status"] == "duplicate"
# Redis contains only pairing/session metadata, never the plaintext chat transcript.
for key in await env["redis"].keys("hn:web*"):
value = await env["redis"].get(key)
assert "独立回复" not in str(value)
async def test_logout_expiry_and_revoke_invalidate_browser(env):
directory, host = await provision(env)
headers = await browser(env, directory, host)
assert (await env["client"].post("/web/logout", headers=headers)).status_code == 200
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
headers = await browser(env, directory, host)
await env["redis"].delete("hn:web-session:" + digest(headers["Cookie"].split("=", 1)[1]))
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
headers = await browser(env, directory, host)
await env["client"].post(f"/admin/devices/{host}/revoke", headers=env["admin"])
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
async def test_binding_generation_invalidates_old_codes_and_sessions(env):
directory, host = await provision(env)
issued, _ = await issue(env, directory, host)
headers = await browser(env, directory, host)
await env["redis"].incr("hn:web-generation:" + host)
assert (await claim(env, issued["code"])).status_code == 401
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
async def test_pair_code_expiry_and_anonymous_chat_denied(env):
directory, host = await provision(env)
issued, _ = await issue(env, directory, host)
await env["redis"].delete("hn:web-code:" + digest(issued["code"]))
assert (await claim(env, issued["code"])).status_code == 404
assert (await env["client"].post("/web/chat", headers={"Origin": env["settings"].public_url},
json={"text": "hello", "request_id": secrets.token_hex(16)})).status_code == 401
async def test_unprefixed_question_uses_native_agent_prefix(env):
directory, host = await provision(env)
headers = await browser(env, directory, host)
seen = []
async def capture(owner, text, turn):
seen.append((owner, text))
return {"status": "completed", "result": "42", "turn_id": turn}
env["connector"].state.broker.dispatch = capture
response = await env["client"].post("/web/chat", headers=headers,
json={"text": "17加25", "request_id": secrets.token_hex(16), "open_id": "attacker"})
assert response.json()["result"] == "42"
assert seen == [("ou_test", "/助手 17加25")]
async def test_concurrent_claim_exactly_one_browser(env):
directory, host = await provision(env)
issued, _ = await issue(env, directory, host)
results = await asyncio.gather(claim(env, issued["code"]), claim(env, issued["code"]))
assert sorted(x.status_code for x in results) == [200, 404]