"""Broker-mode public boundaries with synthetic devices, platform and secrets.""" import asyncio import base64 import hashlib import json import os import secrets import sqlite3 import sys import time from pathlib import Path from urllib.parse import parse_qs, urlsplit import fakeredis import fakeredis.aioredis import httpx import pytest import pytest_asyncio from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import ed25519 sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from relay.config import Settings from relay.connector import create_app from relay.crypto import canonical, fingerprint, issue_ca, issue_device, pem_private, sign from relay.credential_api import CredentialAPI from relay.store import Store class Platform: def __init__(self): self.owner = "ou_a" self.exchanges = self.refreshes = 0 self.exchanging = asyncio.Event() self.resume = None self.scopes = "read" self.sent = [] self.replies = [] self.user_info_failure = False async def credential_exchange(self, code): self.exchanges += 1 self.exchanging.set() if self.resume is not None: await self.resume.wait() return dict(user_access_token="fake-access-v2", refresh_token="fake-refresh-v2", expires_in=3600, refresh_expires_in=86400, scope=self.scopes) async def credential_refresh(self, token): self.refreshes += 1 self.exchanging.set() if self.resume is not None: await self.resume.wait() return dict(user_access_token="fake-access-refreshed", refresh_token="fake-refresh-rotated", expires_in=3600, refresh_expires_in=86400, scope=self.scopes) async def user_info(self, token): if self.user_info_failure: raise RuntimeError("fake-refresh-private-detail") return {"open_id": self.owner} async def tenant_token(self): return "fake-tenant" async def reply(self, message_id, text): self.replies.append((message_id,text)) async def send(self, owner, kind, content, delivery_id, *, token): self.sent.append((owner, content)) return {"message_id": "om_fake"} async def close(self): pass @pytest_asyncio.fixture async def env(tmp_path,request): ca_key, ca = issue_ca() ca_path = tmp_path / "ca.pem" ca_path.write_bytes(ca.public_bytes(serialization.Encoding.PEM)) connector_key = tmp_path / "connector.key" connector_key.write_bytes(pem_private(ed25519.Ed25519PrivateKey.generate())) keyring = tmp_path / "keyring.json" keyring.write_text(json.dumps({"active_key_id": "test-key", "keys": { "test-key": base64.b64encode(os.urandom(32)).decode()}})) settings = Settings(environment="test", admin_secret="a"*40, internal_secret="b"*40, router_admission_secret="c"*40, ca_cert=ca_path, connector_key=connector_key, data_dir=tmp_path, feishu_enabled=True, feishu_app_id="cli_test", feishu_app_secret="fake-app-secret", credential_mode="broker", credential_keyring_file=keyring, credential_permission_profiles={"product-v1": ["read"], "product-v2": ["read", "write"]}, public_url="https://relay.example.test", feishu_api_base="https://work.example.test", feishu_authorize_url="https://work.example.test/open-apis/authen/v1/index",work_files_enabled=bool(getattr(request,'param',False))) redis = fakeredis.aioredis.FakeRedis(server=fakeredis.FakeServer(), decode_responses=True) platform = Platform() app = create_app(settings, redis, platform) keys = {} for host, owner in [("a"*32, "ou_a"), ("b"*32, "ou_b")]: key = ed25519.Ed25519PrivateKey.generate() cert = issue_device(ca_key, ca, host, key.public_key()).public_bytes(serialization.Encoding.PEM).decode() app.state.store.add_device(host, cert, fingerprint(cert)) app.state.store.bind(host, owner) keys[host] = key async with app.router.lifespan_context(app): async with httpx.AsyncClient(transport=httpx.ASGITransport(app), base_url="https://relay.example.test") as client: yield dict(client=client, app=app, store=app.state.store, api=app.state.oauth, settings=settings, redis=redis, platform=platform, keys=keys) await redis.aclose() def wire(env, path, host="a"*32, *, request_id=None, proof_changes=None, **fields): owner = "ou_a" if host == "a"*32 else "ou_b" generation = env["store"].device(host)["binding_generation"] payload = {**dict(host_id=host, binding_owner=owner, binding_generation=generation, request_id=request_id or secrets.token_hex(8)), **fields} if path == "/work/v2/status" and "binding_generation" not in fields: payload["binding_generation"] = None proof = dict(version=2, environment=env["settings"].environment, app_id=env["settings"].feishu_app_id, host_id=host, purpose="work-v2", method="POST", path=path, ts=time.time(), nonce=secrets.token_hex(16), payload_hash=hashlib.sha256(canonical(payload)).hexdigest()) proof.update(proof_changes or {}) return {**payload, "proof": sign(proof, env["keys"][host])} async def start(env, **fields): path = "/work/v2/authorizations" result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", **fields)) assert result.status_code == 201, result.text data = result.json() data["state"] = parse_qs(urlsplit(data["authorization_url"]).query)["state"][0] return data async def authorize(env): item = await start(env) result = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) assert result.status_code == 200, result.text return item async def test_full_flow_only_access_and_no_store_secrets_in_responses(env): item = await authorize(env) path = "/work/v2/credentials/access-token" result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1")) assert result.status_code == 200 body = result.json() assert set(body) == {"access_token", "token_type", "expires_at", "expires_in", "server_time", "credential_version", "granted_scopes", "app_id"} assert body["access_token"] == "fake-access-v2" and 0 < body["expires_in"] <= 3600 assert result.headers["cache-control"] == "no-store" path = "/work/v2/authorizations/status" status = await env["client"].post(path, json=wire(env, path, transaction_id=item["transaction_id"])) assert status.json()["status"] == "authorized" assert "fake-access" not in status.text and "fake-refresh" not in status.text assert "fake-refresh" not in result.text dump = "\n".join(env["store"].db.iterdump()) assert "fake-refresh" not in dump and "fake-code" not in dump and item["state"] not in dump actions = env["store"].db.execute("SELECT action FROM audit WHERE actor='ou_a'").fetchall() assert {row["action"] for row in actions} >= {"credential_issue", "credential_authorized"} async def test_bootstrap_status_verifies_owner_and_returns_current_generation(env): path = "/work/v2/status" response = await env["client"].post(path, json=wire(env, path)) assert response.status_code == 200 and response.json()["binding_generation"] == 1 assert response.json()["credential_status"] == "authorization_required" wrong = wire(env, path, binding_owner="ou_b") assert (await env["client"].post(path, json=wrong)).status_code == 403 @pytest.mark.parametrize("changes", [{"purpose": "other"}, {"method": "GET"}, {"path": "/work/v2/status"}, {"ts": 1}, {"ts": True}, {"nonce": "bad"}, {"payload_hash": "bad"}, {"environment": "other"}, {"app_id": "other-app"}, {"version": 1}, {"version": True}]) async def test_invalid_proof_rejected(env, changes): path = "/work/v2/credentials/access-token" response = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", proof_changes=changes)) assert response.status_code == 403 async def test_tampering_replay_and_extra_owner_or_refresh_fields(env): path = "/work/v2/authorizations" body = wire(env, path, permission_profile="product-v1") tampered = {**body, "permission_profile": "product-v2"} assert (await env["client"].post(path, json=tampered)).status_code == 403 assert (await env["client"].post(path, json=body)).status_code == 201 assert (await env["client"].post(path, json=body)).status_code == 409 for name in ["open_id", "owner", "refresh_token", "access_token", "scope"]: body = wire(env, path, permission_profile="product-v1") response = await env["client"].post(path, json={**body, name: "fake-private-extra"}) assert response.status_code == 422 and "fake-private-extra" not in response.text async def test_unapproved_profile_and_missing_authorization(env): path = "/work/v2/credentials/access-token" assert (await env["client"].post(path, json=wire(env, path, permission_profile="unapproved"))).status_code == 403 assert (await env["client"].post(path, json=wire(env, path, permission_profile="product-v1"))).status_code == 401 async def test_wrong_authorized_user_not_saved_and_state_cannot_retry(env): item = await start(env) env["platform"].owner = "ou_b" for expected in (502, 400): result = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) assert result.status_code == expected assert "fake-code" not in result.text and "fake-refresh" not in result.text assert env["platform"].exchanges == 1 assert env["api"].broker.store.get("test", "cli_test", "ou_a") is None async def test_callback_state_invalid_expired_cancelled_and_replayed(env): client = env["client"] assert (await client.get("/oauth/cb", params={"state": "v2_" + "a"*43, "code": "fake-code"})).status_code == 400 expired = await start(env) # Advancing the injected clock preserves the authenticated state ciphertext. old_clock = env["api"].clock env["api"].clock = lambda: old_clock() + 601 assert (await client.get("/oauth/cb", params={"state": expired["state"], "code": "fake-code"})).status_code == 410 env["api"].clock = old_clock cancelled = await start(env) assert (await client.get("/oauth/cb", params={"state": cancelled["state"], "error": "access_denied"})).status_code == 400 assert (await client.get("/oauth/cb", params={"state": cancelled["state"], "code": "fake-code"})).status_code == 400 item = await authorize(env) assert (await client.get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})).status_code == 400 assert env["platform"].exchanges == 1 async def test_callback_claim_is_atomic_under_duplicate_delivery(env): item = await start(env) env["platform"].resume = asyncio.Event() first = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})) await asyncio.wait_for(env["platform"].exchanging.wait(), 2) duplicate = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) env["platform"].resume.set() assert duplicate.status_code == 400 and (await first).status_code == 200 assert env["platform"].exchanges == 1 async def test_cross_user_transactions_and_token_are_isolated(env): item = await authorize(env) path = "/work/v2/authorizations/status" other = await env["client"].post(path, json=wire(env, path, host="b"*32, transaction_id=item["transaction_id"])) assert other.status_code == 404 path = "/work/v2/credentials/access-token" other = await env["client"].post(path, json=wire(env, path, host="b"*32, permission_profile="product-v1")) assert other.status_code == 401 and "fake-access" not in other.text async def test_request_id_idempotence_and_profile_collision(env): first = await start(env, request_id="stable-request") second = await start(env, request_id="stable-request") assert first["transaction_id"] == second["transaction_id"] and first["state"] == second["state"] path = "/work/v2/authorizations" response = await env["client"].post(path, json=wire(env, path, request_id="stable-request", permission_profile="product-v2")) assert response.status_code == 409 async def test_persistent_authorization_survives_api_and_database_reopen(env): item = await start(env) other_store = Store(env["settings"].data_dir / "metadata.sqlite") try: api = CredentialAPI(env["settings"], env["redis"], other_store, env["api"].broker) assert api.authorization_url_for_notification("ou_a", "a"*32, item["transaction_id"]) == item["authorization_url"] assert api._row(item["transaction_id"])["status"] == "waiting" finally: other_store.close() async def test_unbind_rebind_invalidates_old_signature_and_authorization(env): item = await start(env) path = "/work/v2/credentials/access-token" old = wire(env, path, permission_profile="product-v1") env["store"].unbind("ou_a") env["store"].bind("a"*32, "ou_a") assert env["store"].device("a"*32)["binding_generation"] == 3 assert (await env["client"].post(path, json=old)).status_code == 403 assert (await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})).status_code == 403 assert env["platform"].exchanges == 0 async def test_binding_changes_during_exchange_prevent_credential_commit(env): item = await start(env) env["platform"].resume = asyncio.Event() task = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})) await asyncio.wait_for(env["platform"].exchanging.wait(), 2) env["store"].unbind("ou_a") env["platform"].resume.set() response = await task assert response.status_code == 502 assert env["api"].broker.store.get("test", "cli_test", "ou_a") is None async def test_binding_changes_during_refresh_keep_rotated_pair_but_never_issue(env): vault = env["api"].broker.store vault.save("test", "cli_test", "ou_a", expected_version=0, access_token="fake-access-old", refresh_token="fake-refresh-old", access_expires_at=time.time()+10, refresh_expires_at=time.time()+86400, scopes=["read"]) env["platform"].resume = asyncio.Event() path = "/work/v2/credentials/access-token" task = asyncio.create_task(env["client"].post(path, json=wire(env, path, permission_profile="product-v1"))) await asyncio.wait_for(env["platform"].exchanging.wait(), 2) env["store"].unbind("ou_a") env["platform"].resume.set() response = await task assert response.status_code == 403 and "fake-access" not in response.text assert vault.get("test", "cli_test", "ou_a").refresh_token == "fake-refresh-rotated" async def test_scope_missing_requires_product_permission_update(env): await authorize(env) path = "/work/v2/credentials/access-token" result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v2")) assert result.status_code == 403 and result.json()["detail"] == "permission_update_required" assert env["platform"].refreshes == 0 async def test_old_refresh_claim_start_proxy_routes_absent_in_broker_mode(env): for path in ("/oauth/start", "/oauth/refresh", "/oauth/claim", "/oauth/pending", "/work/api"): response = await env["client"].post(path, json={"refresh_token": "fake-refresh-old"}) assert response.status_code == 404 and "fake-refresh" not in response.text assert (await env["client"].get("/oauth/claim?state=anything")).status_code == 404 async def test_authorization_notification_uses_server_owned_transaction(env): item = await start(env) host = "a"*32 fields = dict(request_id="notify-test-001", kind="authorization", text="", title="", oauth_state=item["transaction_id"], binding_owner="ou_a", binding_generation=env["store"].device(host)["binding_generation"]) proof = sign(dict(host_id=host, purpose="work-notify-v1", ts=int(time.time()), nonce=secrets.token_hex(16), payload_hash=hashlib.sha256(canonical(fields)).hexdigest()), env["keys"][host]) response = await env["client"].post("/device/notifications", json={**fields, "host_id": host, "proof": proof}) assert response.status_code == 200 and response.json()["status"] == "sent" assert env["platform"].sent[-1][0] == "ou_a" assert item["authorization_url"] in json.dumps(env["platform"].sent[-1][1]) assert "fake-refresh" not in response.text async def test_second_connector_can_complete_persisted_authorization(env): item = await start(env) second = create_app(env["settings"], env["redis"], env["platform"]) try: async with httpx.AsyncClient(transport=httpx.ASGITransport(second), base_url="https://relay.example.test") as client: response = await client.get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) assert response.status_code == 200 path = "/work/v2/credentials/access-token" result = await client.post(path, json=wire(env, path, permission_profile="product-v1")) assert result.status_code == 200 and "fake-refresh" not in result.text assert env["api"].broker.store.get("test", "cli_test", "ou_a").version == 1 finally: second.state.store.close() async def test_two_connectors_duplicate_callback_only_exchange_once(env): item = await start(env) second = create_app(env["settings"], env["redis"], env["platform"]) env["platform"].resume = asyncio.Event() try: async with httpx.AsyncClient(transport=httpx.ASGITransport(second), base_url="https://relay.example.test") as client: first = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"})) await asyncio.wait_for(env["platform"].exchanging.wait(), 2) response = await client.get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) env["platform"].resume.set() assert response.status_code == 400 and (await first).status_code == 200 assert env["platform"].exchanges == 1 finally: second.state.store.close() async def test_revoked_unbound_and_wrong_private_key_rejected(env): path = "/work/v2/status" wrong = wire(env, path) plain = {k: v for k, v in wrong["proof"].items() if k != "signature"} wrong["proof"] = sign(plain, env["keys"]["b"*32]) assert (await env["client"].post(path, json=wrong)).status_code == 403 env["store"].unbind("ou_a") assert (await env["client"].post(path, json=wire(env, path))).status_code == 403 env["store"].revoke("b"*32) assert (await env["client"].post(path, json=wire(env, path, host="b"*32))).status_code == 403 async def test_state_ciphertext_and_profile_changes_fail_closed(env): item = await start(env) with env["store"].db: env["store"].db.execute("UPDATE work_authorizations_v2 SET state_ciphertext=? WHERE transaction_id=?", ("broken-ciphertext", item["transaction_id"])) response = await env["client"].get("/oauth/cb", params={"state": item["state"], "code": "fake-code"}) assert response.status_code == 503 and env["platform"].exchanges == 0 second = await start(env) env["settings"].credential_permission_profiles["product-v1"] = ["other-scope"] response = await env["client"].get("/oauth/cb", params={"state": second["state"], "code": "fake-code"}) assert response.status_code == 409 and env["platform"].exchanges == 0 async def test_capacity_is_bounded_and_no_reused_request_extends_expiry(env): env["settings"].max_pending = 1 first = await start(env, request_id="capacity-test") repeated = await start(env, request_id="capacity-test") assert first["expires_at"] == repeated["expires_at"] path = "/work/v2/authorizations" response = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1")) assert response.status_code == 503 async def test_partial_scope_authorization_has_explicit_status(env): path = "/work/v2/authorizations" result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v2")) assert result.status_code == 201 data = result.json() state = parse_qs(urlsplit(data["authorization_url"]).query)["state"][0] result = await env["client"].get("/oauth/cb", params={"state": state, "code": "fake-code"}) assert result.status_code == 200 path = "/work/v2/authorizations/status" result = await env["client"].post(path, json=wire(env, path, transaction_id=data["transaction_id"])) assert result.json()["status"] == "permission_update_required" assert result.json()["granted_scopes"] == ["read"] async def test_new_authorization_supersedes_older_intent(env): older = await start(env) newer = await start(env) response = await env["client"].get("/oauth/cb", params={"state": older["state"], "code": "fake-old-code"}) assert response.status_code == 400 and env["platform"].exchanges == 0 response = await env["client"].get("/oauth/cb", params={"state": newer["state"], "code": "fake-new-code"}) assert response.status_code == 200 async def test_new_intent_during_exchange_prevents_old_consent_commit(env): older = await start(env) env["platform"].resume = asyncio.Event() task = asyncio.create_task(env["client"].get("/oauth/cb", params={"state": older["state"], "code": "fake-old-code"})) await asyncio.wait_for(env["platform"].exchanging.wait(), 2) newer = await start(env) env["platform"].resume.set() response = await task assert response.status_code == 502 and env["api"].broker.store.get("test", "cli_test", "ou_a") is None assert env["api"]._row(older["transaction_id"])["status"] == "superseded" response = await env["client"].get("/oauth/cb", params={"state": newer["state"], "code": "fake-new-code"}) assert response.status_code == 200 async def test_expiry_and_generation_are_not_client_selectable(env): path = "/work/v2/credentials/access-token" for generation in (-1, True, "1", None): result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", binding_generation=generation)) assert result.status_code == 422 result = await env["client"].post(path, json=wire(env, path, permission_profile="product-v1", binding_generation=999)) assert result.status_code == 403 @pytest.mark.skipif(not os.environ.get("HN_TEST_DATABASE_URL"), reason="real PostgreSQL URL not supplied") async def test_postgres_authorization_and_binding_generation(env, tmp_path): import uuid import psycopg from psycopg import sql from psycopg.conninfo import make_conninfo url = os.environ["HN_TEST_DATABASE_URL"] schema = "hn_oauth_test_" + uuid.uuid4().hex admin = psycopg.connect(url, autocommit=True) admin.execute(sql.SQL("CREATE SCHEMA {}").format(sql.Identifier(schema))) app = None try: settings = env["settings"].model_copy(update={"database_url": make_conninfo(url, options=f"-c search_path={schema}"), "data_dir": tmp_path / "pg"}) app = create_app(settings, env["redis"], env["platform"]) device = env["store"].device("a"*32) app.state.store.add_device(device["host_id"], device["cert"], device["fingerprint"]) app.state.store.bind(device["host_id"], "ou_a") async with httpx.AsyncClient(transport=httpx.ASGITransport(app), base_url="https://relay.example.test") as client: pg_env = {**env, "client": client, "app": app, "store": app.state.store, "api": app.state.oauth} await authorize(pg_env) path = "/work/v2/credentials/access-token" result = await client.post(path, json=wire(pg_env, path, permission_profile="product-v1")) assert result.status_code == 200 and "fake-refresh" not in result.text app.state.store.unbind("ou_a") assert app.state.store.device("a"*32)["binding_generation"] == 2 lifecycle = app.state.store.lifecycle lifecycle.issue_code("ou_a","fake-pg-old-code") app.state.store.unbind("ou_a") with pytest.raises(LookupError): lifecycle.consume_code("a"*32,"fake-pg-old-code") lifecycle.issue_code("ou_a","fake-pg-new-code") assert lifecycle.consume_code("a"*32,"fake-pg-new-code") == "ou_a" assert app.state.store.db.execute("SELECT COUNT(*) AS n FROM work_binding_events_v2").fetchone()["n"] == 1 finally: if app: app.state.store.close() admin.execute(sql.SQL("DROP SCHEMA {} CASCADE").format(sql.Identifier(schema))) admin.close() def test_settings_require_explicit_key_and_product_profiles(tmp_path): common = dict(environment="test", admin_secret="a"*40, internal_secret="b"*40, credential_mode="broker", router_admission_secret="c"*40, feishu_enabled=True, feishu_app_id="test-app", feishu_app_secret="fake-secret") with pytest.raises(ValueError): Settings(**common) for profile in ({"bad profile": ["read"]}, {"v1": []}, {"v1": ["bad scope"]}): with pytest.raises(ValueError): Settings(**common, credential_keyring_file=tmp_path/"keys.json", credential_permission_profiles=profile) def test_existing_sqlite_schema_migration_preserves_device_and_increments_generations(tmp_path): path = tmp_path / "old.sqlite" conn = sqlite3.connect(path) conn.execute("CREATE TABLE devices(host_id TEXT PRIMARY KEY,cert TEXT NOT NULL,fingerprint TEXT NOT NULL,owner TEXT UNIQUE,revoked INTEGER NOT NULL DEFAULT 0)") conn.execute("INSERT INTO devices VALUES('host','cert','fp','owner',0)") conn.commit() conn.close() store = Store(path) try: assert store.device("host")["owner"] == "owner" and store.device("host")["binding_generation"] == 0 store.unbind("owner") store.bind("host", "owner") assert store.device("host")["binding_generation"] == 2 store.revoke("host") store.revoke("host") assert store.device("host")["binding_generation"] == 3 finally: store.close()