diff --git a/.github/workflows/application-proof-proposal.yml b/.github/workflows/application-proof-proposal.yml new file mode 100644 index 0000000..64dcacd --- /dev/null +++ b/.github/workflows/application-proof-proposal.yml @@ -0,0 +1,35 @@ +name: Application proof transactions (offline only) +on: + pull_request: + branches: [main] + paths: + - "proposals/application-proof-2104/**" + - ".github/workflows/application-proof-proposal.yml" +permissions: + contents: read +jobs: + disposable-postgres: + runs-on: ubuntu-24.04 + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + postgres: [15, 17] + steps: + - uses: actions/checkout@v4 + timeout-minutes: 1 + with: + persist-credentials: false + - name: Install PostgreSQL + timeout-minutes: 12 + run: | + sudo apt-get update + sudo apt-get install -y postgresql-common + sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y + sudo apt-get update + sudo apt-get install -y postgresql-${{ matrix.postgres }} + - name: Qualify disabled proof receipts and transaction races + timeout-minutes: 3 + run: | + export PATH="/usr/lib/postgresql/${{ matrix.postgres }}/bin:$PATH" + python3 proposals/application-proof-2104/test_transactions.py diff --git a/proposals/application-proof-2104/PROTOCOL.md b/proposals/application-proof-2104/PROTOCOL.md new file mode 100644 index 0000000..69648bf --- /dev/null +++ b/proposals/application-proof-2104/PROTOCOL.md @@ -0,0 +1,167 @@ +# Application proof transactions — offline proposal + +October 27, 2026. Related: [application registration #2104](https://github.com/recoupable/app/issues/2104). +The adopted source-only starting contract is [API #1189](https://github.com/recoupable/api/pull/1189), +head `fd515f9981265fec04d48f2a449c4180ad360842`, merged as +`31d28cd7ac88ebc62d567a11c8ff452acb1369ea` (coordinator merge report). +This proposal implements transactions **only inside a disposable fixture**. It is +outside `supabase/migrations`, has no API/MCP caller, and adds no deployed registry, +role grants, DNS lookup, credential issuance, provider flow, or access activation. + +## First scope and adoption boundaries + +Authorized issuance, verification receipts, revocation and receipt read are the +first scope. Every returned receipt has `bootstrap_enabled: false`, +`access_activated: false`, and `evidence_kind: synthetic_observation`. Database +constraints also require both disabled fields. A successful TXT observation is +not a permission grant. No authentication, billing, quota, fan identity, points, +Context or customer-data store is introduced or duplicated. + +The one-hour challenge expiry, 60-second observation freshness and seven-day +`qualification_valid_until` are explicit qualification parameters. They are not +production lease/access policy, provider promises or approved customer renewal +burdens. Early renewal is permitted; a fresh issue/verification records a new +historical receipt. Existing receipts and retry outputs never get new timestamps. +No active domain lease is stored or consumed by another system here. + +## Files and authority + +- `fixture.sql`: minimal synthetic workspace → application → environment → exact + origin parents. Origins contain a prevalidated hostname. This is not a proposed + full registry schema or an ownership/canonicalization implementation. +- `proposal.sql`: challenges, immutable receipts/retry records and an invoker + transaction boundary in `application_proof_proposal`. The management-authority + adapter **always denies**, including for workspace members or owners. PUBLIC + schema/table/function access is revoked; no replacement grant is supplied. +- `fixture-authority.sql`: explicitly test-only replacement adapter. It locks a + synthetic actor/workspace/application/environment record with an operation list. + This models current authority and revocation races; it is not an adopted role + policy, real account authentication, or evidence of production permission. +- `test_transactions.py`: creates a disposable PostgreSQL cluster with only a Unix + socket and no TCP listener; loads these three files and synthetic data, runs + assertions and stops/removes the cluster. No hosted connection string is accepted. + +The runner uses the local cluster owner to prepare and execute the fixture. A +browser-role denial test proves absent PUBLIC access, not least-privilege runtime +service grants. Those grants, real role/delegated-scope mapping, resolver policy, +hostname eligibility and consumer enforcement require separate adoption and tests. +Do not load the fixture-authority override as an activation shortcut. + +## Exact binding and revisions + +Each origin's identity is the exact composite `(workspace, application, +environment, origin)`. Parent composite foreign keys prevent attaching a child to +a different workspace/application/environment. DNS query hostname is derived from +the trusted parent fixture, not a caller-supplied URL. The future admission adapter +must validate canonical origin/hostname consistency and DNS eligibility; these +fixture rows are not domain-ownership evidence. Production/development and +unrelated applications never share a challenge even if they name the same host. +Global challenge-digest uniqueness prevents nonce reuse across all bindings. +No wildcard, parent-domain inheritance, origin normalization, or shared-host grant. + +Three revisions are deliberately separate: + +| Revision | Owner and change rule | +| --- | --- | +| Configuration | Parent registry; changes to configured origin/return destinations or applicable security configuration advance it and CAS. This fixture's config-change test mutates the origin row directly under its row lock. | +| Origin proof | Verification/revocation protocol; advances on a new verified receipt or security revocation, not challenge issuance/rotation. | +| CAS | Per-origin serialization version; advances on every issue/rotate/verify/revoke and modeled configuration change. | + +Issue snapshots configuration and proof revisions into a pending challenge. +Rotation supersedes the old pending challenge and advances CAS only. Verify must +match the exact challenge ID, binding, snapshot revisions and caller's current +three revisions; consumes once, advances proof/CAS, and appends a receipt. Revoke +marks the origin terminally revoked, invalidates pending challenges and advances +proof/CAS. Reactivation is not implemented; no old challenge can restore it. +Changing parent configuration invalidates an outstanding observation even if the +hostname still matches. It cannot rewrite historical proof receipts. A future +parent adapter must advance revisions atomically; unrelated writers bypassing that +contract are not covered by this prototype. + +## Transaction order and concurrency + +Only READ COMMITTED is accepted. All participating writers follow this order: + +1. Management-authority adapter locks and validates its policy dependencies. +2. Transaction advisory lock on `(actor, workspace, operation, idempotency key)`. + A hash collision serializes extra requests without granting access. +3. Application then environment `FOR SHARE`, followed by exact origin `FOR UPDATE`. +4. Current idempotency lookup or challenge `FOR UPDATE`; then atomic state change, + immutable receipt and retry result insertion. + +Fresh statements after locks recheck current state. The server samples +`clock_timestamp()` after locks, including the challenge lock, rather than using +transaction-start `now()`. A request that waited past expiry/freshness cannot pass +using an earlier clock. Suspension blocks fresh mutations. Historical reads and +exact retries remain available to currently authorized management actors even +when revoked/suspended, because all outputs are access-disabled history. + +Real two-connection tests hold transactions open, observe PostgreSQL lock waits, +commit the winner and inspect the loser's result. Verify loses to an already +committed revoke, challenge rotation or configuration change through revision +conflict, and to authority revocation through denial. If verification commits +first, stale revoke conflicts; retry against current revisions revokes while +preserving the prior receipt. Revocation waits for an already-authorized transaction; +it does not retrospectively undo a receipt that serialized first. + +The prototype serializes all operations for one origin. The parent adapter must +also serialize its mutations against these locks. It does not claim multi-operation +arbitrary transaction lock ordering or immunity to superuser/direct-table writes. +Use one domain operation per transaction; future broader transactions need review. + +## Retry, rollback and observation contract + +Idempotency is scoped to authenticated actor, workspace and operation. The stored +JSONB fingerprint contains exact application/environment/origin, expected revisions +and payload. An authorized exact retry returns its original receipt **before** +stale-revision checks, without another receipt or time extension. Changed payload +or revision on the same key conflicts. A different key cannot reconsume a challenge. +Read requires current `read` management authority and exact receipt binding; a +receipt ID alone is insufficient. Authorization is checked before retry lookup. + +A committed result whose response was lost is recovered with the same key and +payload; never rotate or submit a fresh verification just because the result is +unknown. The test discards a committed response and recovers it. Explicit rollback +after issue or verify leaves no receipt/retry side effect and restores challenge +state. Errors, including a globally duplicate digest, roll back superseding the +old challenge in the same transaction. Immutable triggers reject receipt and retry +updates/deletes; deleting/truncating a fixture is test teardown, not retention policy. +Retention/tombstones and real audit requirements remain an adoption gate. + +The issuer supplies a synthetic SHA-256 digest; this database prototype does not +generate or publish a nonce. Verify accepts a trusted fixture observation containing +challenge ID, matched digest, exact query name, observation timestamp and the only +supported policy marker `synthetic-v1`. It verifies exact equality and time bounds. +There are no DNS responses, credentials, HTTP URLs to fetch, or caller-controlled +resolver destinations. The API proposal's separate offline model covers TXT string +assembly; this layer does not parse raw TXT or validate resolver authenticity. +A live adapter must supply an authenticated, bounded observation and adopted policy; +a client claiming `synthetic-v1` is never acceptable evidence in production. + +Errors are controlled messages for invalid input, missing management authority or +resource, suspension, revision/idempotency conflict, expired/unavailable challenge, +and rejected observation. They are prototype SQL exceptions, not an adopted HTTP +status mapping. Future public transports must sanitize database constraint errors +(e.g. duplicate digests) to avoid exposing other bindings. No transport exists here. + +## Run and remaining qualification + +With `initdb`, `pg_ctl` and `psql` from one PostgreSQL installation on PATH: + +```sh +python3 proposals/application-proof-2104/test_transactions.py +``` + +The CI workflow runs PostgreSQL 15 and 17. Qualification covers exact binding, +default denial, cross-workspace/environment rejection, browser denial, digest +uniqueness, rotation, replay/idempotency, immutable historical read, disabled +activation, expiry/freshness, rollback/unknown outcome and competing transactions. +This is neither a full migration replay nor evidence of deployed registration. + +Before API exposure: coordinator reviews this protocol; adopt real management +roles/delegation, parent registry mapping/revision writers and server privilege +model; qualify resolver/eligibility and observation provenance; decide lifecycle +metadata/retention. Any subsequent persistence migration belongs in a separate +Database PR. Keep bootstrap/access activation disabled until its own authority and +revocation contract is approved. #2110's provider, independent-host, mobile and +shared-callback gates and the separate #2345 browser hold remain unchanged. diff --git a/proposals/application-proof-2104/fixture-authority.sql b/proposals/application-proof-2104/fixture-authority.sql new file mode 100644 index 0000000..6b7c0c1 --- /dev/null +++ b/proposals/application-proof-2104/fixture-authority.sql @@ -0,0 +1,14 @@ +-- TEST-ONLY override. Never load outside this disposable fixture. +CREATE OR REPLACE FUNCTION application_proof_proposal.authorize_management( + p_actor uuid,p_workspace uuid,p_application uuid,p_environment text,p_operation text +) RETURNS void LANGUAGE plpgsql SET search_path='' AS $$ +DECLARE enabled_value boolean; permitted text[]; +BEGIN + SELECT a.enabled,a.operations INTO enabled_value,permitted + FROM proof_parent_fixture.authority a + WHERE a.actor=p_actor AND a.workspace_id=p_workspace AND a.application_id=p_application + AND a.environment=p_environment FOR SHARE; + IF enabled_value IS DISTINCT FROM true OR NOT (p_operation=ANY(permitted)) + THEN RAISE EXCEPTION 'management authority unavailable' USING ERRCODE='42501'; END IF; +END $$; +REVOKE ALL ON FUNCTION application_proof_proposal.authorize_management(uuid,uuid,uuid,text,text) FROM PUBLIC; diff --git a/proposals/application-proof-2104/fixture.sql b/proposals/application-proof-2104/fixture.sql new file mode 100644 index 0000000..d170d96 --- /dev/null +++ b/proposals/application-proof-2104/fixture.sql @@ -0,0 +1,32 @@ +-- SYNTHETIC parent schema, not an application registry or production migration. +CREATE SCHEMA proof_parent_fixture; +REVOKE ALL ON SCHEMA proof_parent_fixture FROM PUBLIC; +CREATE TABLE proof_parent_fixture.workspaces (id uuid PRIMARY KEY); +CREATE TABLE proof_parent_fixture.applications ( + workspace_id uuid REFERENCES proof_parent_fixture.workspaces, + id uuid NOT NULL, suspended boolean NOT NULL DEFAULT false, + PRIMARY KEY(workspace_id,id) +); +CREATE TABLE proof_parent_fixture.environments ( + workspace_id uuid NOT NULL, application_id uuid NOT NULL, + environment text CHECK(environment IN ('production','development')), + suspended boolean NOT NULL DEFAULT false, + PRIMARY KEY(workspace_id,application_id,environment), + FOREIGN KEY(workspace_id,application_id) REFERENCES proof_parent_fixture.applications +); +CREATE TABLE proof_parent_fixture.origins ( + workspace_id uuid NOT NULL, application_id uuid NOT NULL, environment text NOT NULL, + origin text NOT NULL, hostname text NOT NULL, + config_revision bigint NOT NULL DEFAULT 1 CHECK(config_revision>0), + proof_revision bigint NOT NULL DEFAULT 1 CHECK(proof_revision>0), + cas_revision bigint NOT NULL DEFAULT 1 CHECK(cas_revision>0), + revoked boolean NOT NULL DEFAULT false, + PRIMARY KEY(workspace_id,application_id,environment,origin), + FOREIGN KEY(workspace_id,application_id,environment) REFERENCES proof_parent_fixture.environments +); +-- No grants or membership inference. This is replaced only in disposable tests. +CREATE TABLE proof_parent_fixture.authority ( + actor uuid, workspace_id uuid, application_id uuid, environment text, + operations text[] NOT NULL, enabled boolean NOT NULL DEFAULT true, + PRIMARY KEY(actor,workspace_id,application_id,environment) +); diff --git a/proposals/application-proof-2104/proposal.sql b/proposals/application-proof-2104/proposal.sql new file mode 100644 index 0000000..9616df0 --- /dev/null +++ b/proposals/application-proof-2104/proposal.sql @@ -0,0 +1,166 @@ +-- EXECUTABLE PROPOSAL ONLY. Depends on fixture.sql; never load as a migration. +CREATE SCHEMA application_proof_proposal; +REVOKE ALL ON SCHEMA application_proof_proposal FROM PUBLIC; +CREATE TABLE application_proof_proposal.challenges ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + workspace_id uuid NOT NULL, application_id uuid NOT NULL, environment text NOT NULL, origin text NOT NULL, + config_revision bigint NOT NULL, proof_revision bigint NOT NULL, + digest text NOT NULL UNIQUE CHECK(digest ~ '^[0-9a-f]{64}$'), + issued_at timestamptz NOT NULL, expires_at timestamptz NOT NULL CHECK(expires_at>issued_at), + state text NOT NULL CHECK(state IN ('pending','consumed','superseded','revoked')), + FOREIGN KEY(workspace_id,application_id,environment,origin) REFERENCES proof_parent_fixture.origins +); +CREATE UNIQUE INDEX one_pending_challenge ON application_proof_proposal.challenges + (workspace_id,application_id,environment,origin) WHERE state='pending'; +CREATE TABLE application_proof_proposal.receipts ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + workspace_id uuid NOT NULL, application_id uuid NOT NULL, environment text NOT NULL, origin text NOT NULL, + actor uuid NOT NULL, operation text NOT NULL CHECK(operation IN ('issue','verify','revoke')), + challenge_id uuid REFERENCES application_proof_proposal.challenges, + recorded_at timestamptz NOT NULL, result jsonb NOT NULL, + CHECK(result->'access_activated' = 'false'::jsonb AND result ? 'access_activated'), + CHECK(result->'bootstrap_enabled' = 'false'::jsonb AND result ? 'bootstrap_enabled'), + FOREIGN KEY(workspace_id,application_id,environment,origin) REFERENCES proof_parent_fixture.origins +); +CREATE TABLE application_proof_proposal.requests ( + actor uuid NOT NULL, workspace_id uuid NOT NULL, operation text NOT NULL, key text NOT NULL, + input jsonb NOT NULL, receipt_id uuid NOT NULL REFERENCES application_proof_proposal.receipts, + PRIMARY KEY(actor,workspace_id,operation,key) +); +CREATE FUNCTION application_proof_proposal.immutable_receipt() RETURNS trigger + LANGUAGE plpgsql SET search_path='' AS $$ +BEGIN RAISE EXCEPTION 'immutable receipt'; END $$; +CREATE TRIGGER immutable_receipt BEFORE UPDATE OR DELETE ON application_proof_proposal.receipts + FOR EACH ROW EXECUTE FUNCTION application_proof_proposal.immutable_receipt(); +CREATE TRIGGER immutable_retry BEFORE UPDATE OR DELETE ON application_proof_proposal.requests + FOR EACH ROW EXECUTE FUNCTION application_proof_proposal.immutable_receipt(); + +-- DEFAULT AUTHORITY: no membership fallback, feature flag or caller-provided boolean. +CREATE OR REPLACE FUNCTION application_proof_proposal.authorize_management( + p_actor uuid,p_workspace uuid,p_application uuid,p_environment text,p_operation text +) RETURNS void LANGUAGE plpgsql SET search_path='' AS $$ +BEGIN RAISE EXCEPTION 'management authority unavailable' USING ERRCODE='42501'; END $$; +REVOKE ALL ON FUNCTION application_proof_proposal.authorize_management(uuid,uuid,uuid,text,text) FROM PUBLIC; + +-- TRANSACTION: the authority adapter must lock/recheck its actual policy dependencies. +CREATE FUNCTION application_proof_proposal.transact( + p_actor uuid,p_workspace uuid,p_application uuid,p_environment text,p_origin text, + p_operation text,p_key text,p_config bigint,p_proof bigint,p_cas bigint,p_payload jsonb +) RETURNS jsonb LANGUAGE plpgsql SET search_path='' SET timezone='UTC' AS $$ +DECLARE + parent proof_parent_fixture.origins%ROWTYPE; + challenge application_proof_proposal.challenges%ROWTYPE; + prior application_proof_proposal.requests%ROWTYPE; + request_input jsonb; output jsonb; receipt_id uuid; challenge_id uuid; + app_suspended boolean; env_suspended boolean; at_time timestamptz; observed_at timestamptz; +BEGIN + IF current_setting('transaction_isolation') <> 'read committed' THEN + RAISE EXCEPTION 'read committed required'; END IF; + IF p_actor IS NULL OR p_workspace IS NULL OR p_application IS NULL OR p_environment IS NULL + OR p_origin IS NULL OR p_operation IS NULL OR p_operation NOT IN ('issue','verify','revoke','read') + OR p_key IS NULL OR length(p_key) NOT BETWEEN 1 AND 128 + OR p_config IS NULL OR p_proof IS NULL OR p_cas IS NULL OR least(p_config,p_proof,p_cas)<1 + OR p_payload IS NULL OR jsonb_typeof(p_payload)<>'object' OR octet_length(p_payload::text)>4096 + THEN RAISE EXCEPTION 'invalid input'; END IF; + PERFORM application_proof_proposal.authorize_management(p_actor,p_workspace,p_application,p_environment,p_operation); + -- Serialize idempotency keys before parent locks; hash collisions only serialize extra work. + PERFORM pg_advisory_xact_lock(hashtextextended(jsonb_build_array(p_actor,p_workspace,p_operation,p_key)::text,0)); + SELECT a.suspended INTO app_suspended FROM proof_parent_fixture.applications a + WHERE a.workspace_id=p_workspace AND a.id=p_application FOR SHARE; + IF NOT FOUND THEN RAISE EXCEPTION 'resource unavailable' USING ERRCODE='42501'; END IF; + SELECT e.suspended INTO env_suspended FROM proof_parent_fixture.environments e + WHERE e.workspace_id=p_workspace AND e.application_id=p_application AND e.environment=p_environment FOR SHARE; + IF NOT FOUND THEN RAISE EXCEPTION 'resource unavailable' USING ERRCODE='42501'; END IF; + SELECT o.* INTO parent FROM proof_parent_fixture.origins o + WHERE o.workspace_id=p_workspace AND o.application_id=p_application AND o.environment=p_environment + AND o.origin=p_origin FOR UPDATE; + IF NOT FOUND THEN RAISE EXCEPTION 'resource unavailable' USING ERRCODE='42501'; END IF; + IF p_operation='read' THEN + IF NOT (p_payload ? 'receipt_id') OR p_payload - 'receipt_id' <> '{}'::jsonb + THEN RAISE EXCEPTION 'invalid input'; END IF; + SELECT r.result INTO output FROM application_proof_proposal.receipts r + WHERE r.id=(p_payload->>'receipt_id')::uuid AND r.workspace_id=p_workspace + AND r.application_id=p_application AND r.environment=p_environment AND r.origin=p_origin; + IF NOT FOUND THEN RAISE EXCEPTION 'resource unavailable' USING ERRCODE='42501'; END IF; + RETURN output; -- Historical, always access-disabled; never a current grant. + END IF; + request_input:=jsonb_build_object('application',p_application,'environment',p_environment, + 'origin',p_origin,'config',p_config,'proof',p_proof,'cas',p_cas,'payload',p_payload); + SELECT q.* INTO prior FROM application_proof_proposal.requests q + WHERE q.actor=p_actor AND q.workspace_id=p_workspace AND q.operation=p_operation AND q.key=p_key; + IF FOUND THEN + IF prior.input IS DISTINCT FROM request_input THEN RAISE EXCEPTION 'idempotency conflict'; END IF; + SELECT r.result INTO output FROM application_proof_proposal.receipts r WHERE r.id=prior.receipt_id; + RETURN output; -- No refresh of timestamps or lease; current authority already rechecked. + END IF; + IF parent.config_revision<>p_config OR parent.proof_revision<>p_proof OR parent.cas_revision<>p_cas + THEN RAISE EXCEPTION 'revision conflict'; END IF; + IF parent.revoked THEN RAISE EXCEPTION 'origin revoked'; END IF; + IF app_suspended OR env_suspended THEN RAISE EXCEPTION 'resource suspended'; END IF; + at_time:=clock_timestamp(); -- Sample after every contended lock, never transaction-start now(). + IF p_operation='issue' THEN + IF NOT (p_payload ? 'digest') OR p_payload - 'digest' <> '{}'::jsonb + OR jsonb_typeof(p_payload->'digest')<>'string' OR (p_payload->>'digest') !~ '^[0-9a-f]{64}$' + THEN RAISE EXCEPTION 'invalid input'; END IF; + UPDATE application_proof_proposal.challenges SET state='superseded' + WHERE workspace_id=p_workspace AND application_id=p_application AND environment=p_environment + AND origin=p_origin AND state='pending'; + INSERT INTO application_proof_proposal.challenges(workspace_id,application_id,environment,origin, + config_revision,proof_revision,digest,issued_at,expires_at,state) + VALUES(p_workspace,p_application,p_environment,p_origin,p_config,p_proof,p_payload->>'digest', + at_time,at_time+interval '1 hour','pending') RETURNING id INTO challenge_id; + output:=jsonb_build_object('challenge_id',challenge_id,'query_name','_recoup-app.'||parent.hostname, + 'issued_at',at_time,'expires_at',at_time+interval '1 hour'); + ELSIF p_operation='verify' THEN + IF (p_payload - ARRAY['challenge_id','digest','query_name','queried_at','resolver_policy'])<>'{}'::jsonb + OR NOT (p_payload ?& ARRAY['challenge_id','digest','query_name','queried_at','resolver_policy']) + OR EXISTS(SELECT 1 FROM jsonb_each(p_payload) x WHERE jsonb_typeof(x.value)<>'string') + THEN RAISE EXCEPTION 'invalid input'; END IF; + SELECT c.* INTO challenge FROM application_proof_proposal.challenges c + WHERE c.id=(p_payload->>'challenge_id')::uuid AND c.workspace_id=p_workspace + AND c.application_id=p_application AND c.environment=p_environment AND c.origin=p_origin FOR UPDATE; + IF NOT FOUND OR challenge.state<>'pending' THEN RAISE EXCEPTION 'challenge unavailable'; END IF; + IF challenge.config_revision<>p_config OR challenge.proof_revision<>p_proof + THEN RAISE EXCEPTION 'challenge revision conflict'; END IF; + -- Challenge lock may also wait; sample again before evaluating expiry/freshness. + at_time:=clock_timestamp(); + IF at_time>=challenge.expires_at THEN RAISE EXCEPTION 'challenge expired'; END IF; + observed_at:=(p_payload->>'queried_at')::timestamptz; + IF NOT isfinite(observed_at) OR observed_atat_time + OR at_time-observed_at>interval '60 seconds' + OR p_payload->>'query_name' IS DISTINCT FROM '_recoup-app.'||parent.hostname + OR p_payload->>'digest' IS DISTINCT FROM challenge.digest + OR p_payload->>'resolver_policy' IS DISTINCT FROM 'synthetic-v1' + THEN RAISE EXCEPTION 'observation rejected'; END IF; + challenge_id:=challenge.id; + UPDATE application_proof_proposal.challenges SET state='consumed' WHERE id=challenge_id; + parent.proof_revision:=parent.proof_revision+1; + output:=jsonb_build_object('challenge_id',challenge_id,'observed_at',observed_at, + 'query_name',p_payload->>'query_name','matched_digest',challenge.digest,'resolver_policy','synthetic-v1', + 'qualification_valid_until',at_time+interval '7 days'); + ELSE + IF p_payload<>'{}'::jsonb THEN RAISE EXCEPTION 'invalid input'; END IF; + UPDATE application_proof_proposal.challenges SET state='revoked' + WHERE workspace_id=p_workspace AND application_id=p_application AND environment=p_environment + AND origin=p_origin AND state='pending'; + parent.proof_revision:=parent.proof_revision+1; + parent.revoked:=true; + output:=jsonb_build_object('revoked',true); + END IF; + parent.cas_revision:=parent.cas_revision+1; + UPDATE proof_parent_fixture.origins SET proof_revision=parent.proof_revision, + cas_revision=parent.cas_revision,revoked=parent.revoked + WHERE workspace_id=p_workspace AND application_id=p_application AND environment=p_environment AND origin=p_origin; + receipt_id:=gen_random_uuid(); + output:=output||jsonb_build_object('receipt_id',receipt_id,'operation',p_operation,'actor',p_actor, + 'workspace_id',p_workspace,'application_id',p_application,'environment',p_environment,'origin',p_origin, + 'config_revision',p_config,'proof_revision',parent.proof_revision,'cas_revision',parent.cas_revision, + 'recorded_at',at_time,'access_activated',false,'bootstrap_enabled',false,'evidence_kind','synthetic_observation'); + INSERT INTO application_proof_proposal.receipts(id,workspace_id,application_id,environment,origin, + actor,operation,challenge_id,recorded_at,result) + VALUES(receipt_id,p_workspace,p_application,p_environment,p_origin,p_actor,p_operation,challenge_id,at_time,output); + INSERT INTO application_proof_proposal.requests VALUES(p_actor,p_workspace,p_operation,p_key,request_input,receipt_id); + RETURN output; +END $$; +REVOKE ALL ON ALL TABLES IN SCHEMA application_proof_proposal FROM PUBLIC; +REVOKE ALL ON ALL FUNCTIONS IN SCHEMA application_proof_proposal FROM PUBLIC; diff --git a/proposals/application-proof-2104/test_transactions.py b/proposals/application-proof-2104/test_transactions.py new file mode 100644 index 0000000..180482c --- /dev/null +++ b/proposals/application-proof-2104/test_transactions.py @@ -0,0 +1,313 @@ +"""Offline transaction proposal: synthetic authority and observations, Unix socket only.""" +import json +import os +import pathlib +import select +import subprocess +import tempfile +import time +import unittest +from concurrent.futures import ThreadPoolExecutor + +HERE = pathlib.Path(__file__).resolve().parent + + +def uid(n): + return f'00000000-0000-0000-0000-{n:012d}' + + +def literal(value): + return "'" + str(value).replace("'", "''") + "'" + + +class ProofTransactions(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.TemporaryDirectory(prefix='recoup-proof-') + cls.addClassCleanup(cls.tmp.cleanup) + cls.cluster = pathlib.Path(cls.tmp.name) / 'pg' + subprocess.run(['initdb', '-D', str(cls.cluster), '-A', 'trust'], check=True, capture_output=True) + subprocess.run(['pg_ctl', '-D', str(cls.cluster), '-l', str(pathlib.Path(cls.tmp.name) / 'pg.log'), + '-o', f"-k {cls.tmp.name} -p 55479 -c listen_addresses=''", '-w', 'start'], + check=True, capture_output=True) + cls.addClassCleanup(cls.stop) + cls.sql((HERE / 'fixture.sql').read_text()) + cls.sql((HERE / 'proposal.sql').read_text()) + cls.sql('CREATE ROLE proof_browser;') + + @classmethod + def stop(cls): + subprocess.run(['pg_ctl', '-D', str(cls.cluster), '-m', 'immediate', '-w', 'stop'], + check=True, capture_output=True) + + @classmethod + def args(cls): + return ['psql', '-h', cls.tmp.name, '-p', '55479', '-d', 'postgres', '-XqAt', '-v', 'ON_ERROR_STOP=1'] + + @classmethod + def sql(cls, sql): + result = subprocess.run(cls.args(), input=sql, text=True, capture_output=True, timeout=15) + if result.returncode: + raise AssertionError(result.stderr) + return result.stdout.strip() + + def setUp(self): + self.sql('TRUNCATE proof_parent_fixture.workspaces,proof_parent_fixture.authority CASCADE;') + self.sql((HERE / 'fixture-authority.sql').read_text()) + for workspace, application, actor in [(1, 2, 3), (11, 12, 13)]: + self.sql(f"""INSERT INTO proof_parent_fixture.workspaces VALUES ('{uid(workspace)}'); + INSERT INTO proof_parent_fixture.applications(workspace_id,id) VALUES ('{uid(workspace)}','{uid(application)}'); + INSERT INTO proof_parent_fixture.environments(workspace_id,application_id,environment) + VALUES ('{uid(workspace)}','{uid(application)}','production'),('{uid(workspace)}','{uid(application)}','development'); + INSERT INTO proof_parent_fixture.origins(workspace_id,application_id,environment,origin,hostname) + SELECT workspace_id,application_id,environment,'https://music.example','music.example' FROM proof_parent_fixture.environments + WHERE workspace_id='{uid(workspace)}'; + INSERT INTO proof_parent_fixture.authority + SELECT '{uid(actor)}',workspace_id,application_id,environment,ARRAY['issue','verify','revoke','read'],true + FROM proof_parent_fixture.environments WHERE workspace_id='{uid(workspace)}';""") + + def statement(self, operation, key='key', revisions=(1, 1, 1), payload=None, + actor=3, workspace=1, application=2, environment='production', origin='https://music.example'): + args = [uid(actor), uid(workspace), uid(application), environment, origin, operation, key] + return ('SELECT application_proof_proposal.transact(' + ','.join(literal(x) for x in args) + + ',' + ','.join(str(x) for x in revisions) + ',' + literal(json.dumps(payload or {})) + '::jsonb);') + + def call(self, *args, **kwargs): + return json.loads(self.sql(self.statement(*args, **kwargs))) + + def issue(self, **kwargs): + return self.call('issue', payload={'digest': 'a' * 64}, **kwargs) + + def observation(self, issued, **changes): + observed = self.sql('SELECT clock_timestamp()') + result = {'challenge_id': issued['challenge_id'], 'digest': 'a' * 64, + 'query_name': '_recoup-app.music.example', 'queried_at': observed, + 'resolver_policy': 'synthetic-v1'} + result.update(changes) + return result + + def test_default_authority_and_browser_denial(self): + # Restore the actual default-deny adapter without loading a real management policy. + source = (HERE / 'proposal.sql').read_text() + self.sql(source[source.index('-- DEFAULT AUTHORITY'):source.index('-- TRANSACTION')]) + with self.assertRaisesRegex(AssertionError, 'management authority unavailable'): + self.issue() + with self.assertRaisesRegex(AssertionError, 'permission denied'): + self.sql('SET ROLE proof_browser; ' + self.statement('issue', payload={'digest': 'a' * 64})) + + def test_exact_retry_and_receipt_read_never_activate(self): + issued = self.issue() + self.assertEqual(issued, self.issue()) + payload = self.observation(issued) + receipt = self.call('verify', 'verify', (1, 1, 2), payload) + self.assertEqual(receipt, self.call('verify', 'verify', (1, 1, 2), payload)) + self.assertEqual(receipt, self.call('read', payload={'receipt_id': receipt['receipt_id']})) + self.assertFalse(receipt['access_activated']) + self.assertFalse(receipt['bootstrap_enabled']) + self.assertEqual(receipt['proof_revision'], 2) + self.assertEqual(receipt['config_revision'], 1) + self.assertEqual(receipt['cas_revision'], 3) + with self.assertRaisesRegex(AssertionError, 'idempotency conflict'): + self.call('verify', 'verify', (1, 1, 2), {**payload, 'digest': 'b' * 64}) + with self.assertRaisesRegex(AssertionError, 'challenge unavailable'): + self.call('verify', 'again', (1, 2, 3), payload) + + def test_cross_binding_and_authority_failures(self): + issued = self.issue() + for override in [{'workspace': 11}, {'actor': 13}, {'environment': 'invalid'}, {'application': 12}]: + with self.assertRaisesRegex(AssertionError, 'management authority unavailable'): + self.issue(**override) + for override in [dict(actor=13,workspace=11,application=12), dict(environment='development')]: + with self.assertRaisesRegex(AssertionError, 'challenge unavailable'): + self.call('verify', 'foreign', (1, 1, 1), self.observation(issued), **override) + with self.assertRaisesRegex(AssertionError, 'resource unavailable'): + self.issue(origin='https://other.example') + self.sql('UPDATE proof_parent_fixture.authority SET enabled=false') + with self.assertRaisesRegex(AssertionError, 'management authority unavailable'): + self.issue() + + def test_digest_uniqueness_and_rotation(self): + first = self.issue() + with self.assertRaisesRegex(AssertionError, 'duplicate key'): + self.issue(actor=13,workspace=11,application=12) + rotated = self.call('issue','rotate',(1,1,2),{'digest':'b'*64}) + self.assertNotEqual(first['challenge_id'],rotated['challenge_id']) + with self.assertRaisesRegex(AssertionError, 'challenge unavailable'): + self.call('verify','old',(1,1,3),self.observation(first)) + self.assertEqual(self.sql("SELECT state FROM application_proof_proposal.challenges WHERE id="+literal(first['challenge_id'])), 'superseded') + + def test_failed_duplicate_rotation_restores_previous_pending_challenge(self): + original=self.issue() + self.call('issue',payload={'digest':'b'*64},actor=13,workspace=11,application=12) + with self.assertRaisesRegex(AssertionError,'duplicate key'): + self.call('issue','collision',(1,1,2),{'digest':'b'*64}) + self.assertEqual(self.sql('SELECT state FROM application_proof_proposal.challenges WHERE id='+literal(original['challenge_id'])),'pending') + self.assertEqual(self.sql('SELECT cas_revision FROM proof_parent_fixture.origins WHERE workspace_id='+literal(uid(1))+" AND environment='production'"),'2') + self.assertEqual(self.sql('SELECT count(*) FROM application_proof_proposal.receipts'),'2') + self.assertEqual(self.sql("SELECT count(*) FROM application_proof_proposal.requests WHERE key='collision'"),'0') + self.assertEqual(self.call('verify','verify',(1,1,2),self.observation(original))['proof_revision'],2) + + def test_expiry_freshness_and_wrong_observations(self): + issued = self.issue() + for changes in [{'digest':'b'*64},{'query_name':'_recoup-app.example'}, + {'queried_at':'infinity'},{'queried_at':'2000-01-01'}, + {'queried_at':'2099-01-01'},{'resolver_policy':'unknown'}, {'extra':True}]: + with self.assertRaises(AssertionError): + self.call('verify','bad',(1,1,2),self.observation(issued,**changes)) + self.sql("UPDATE application_proof_proposal.challenges SET issued_at=clock_timestamp()-interval '2 hours',expires_at=clock_timestamp()-interval '1 hour'") + with self.assertRaisesRegex(AssertionError,'challenge expired'): + self.call('verify','expired',(1,1,2),self.observation(issued)) + + def test_rollback_unknown_outcome_and_history(self): + # An explicit rollback leaves neither challenge nor retry result. + self.sql('BEGIN;'+self.statement('issue',payload={'digest':'a'*64})+'ROLLBACK;') + self.assertEqual(self.sql('SELECT count(*) FROM application_proof_proposal.challenges'),'0') + # Discard the committed response to model a client that cannot tell whether commit succeeded. + self.sql('BEGIN;'+self.statement('issue',payload={'digest':'a'*64})+'COMMIT;') + issued = self.issue() + self.assertEqual(issued,self.issue()) + payload = self.observation(issued) + receipt = self.call('verify','verify',(1,1,2),payload) + revoked = self.call('revoke','revoke',(1,2,3)) + self.assertFalse(revoked['access_activated']) + self.assertEqual(receipt,self.call('verify','verify',(1,1,2),payload)) + self.assertEqual(receipt,self.call('read',payload={'receipt_id':receipt['receipt_id']})) + with self.assertRaisesRegex(AssertionError,'revoked'): + self.call('issue','new',(1,3,4),{'digest':'b'*64}) + with self.assertRaisesRegex(AssertionError,'immutable'): + self.sql('UPDATE application_proof_proposal.receipts SET result=result') + + def test_invalid_input_and_isolation(self): + for payload in [{'digest':None},{'digest':'not-a-digest'},{'digest':'a'*64,'caller_grant':True}]: + with self.assertRaisesRegex(AssertionError,'invalid input'): + self.call('issue',payload=payload) + with self.assertRaisesRegex(AssertionError,'read committed required'): + self.sql('BEGIN ISOLATION LEVEL REPEATABLE READ;'+self.statement('issue',payload={'digest':'a'*64})) + self.assertEqual(self.sql('SELECT count(*) FROM application_proof_proposal.receipts'),'0') + + def test_stale_observation_and_transaction_rollback_after_verify(self): + issued=self.issue() + self.sql("UPDATE application_proof_proposal.challenges SET issued_at=clock_timestamp()-interval '2 minutes'") + stale=self.sql("SELECT clock_timestamp()-interval '61 seconds'") + with self.assertRaisesRegex(AssertionError,'observation rejected'): + self.call('verify','stale',(1,1,2),self.observation(issued,queried_at=stale)) + payload=self.observation(issued) + self.sql('BEGIN;'+self.statement('verify','verify',(1,1,2),payload)+'ROLLBACK;') + self.assertEqual(self.sql('SELECT state FROM application_proof_proposal.challenges'),'pending') + self.assertEqual(self.sql("SELECT cas_revision FROM proof_parent_fixture.origins WHERE environment='production' AND workspace_id="+literal(uid(1))),'2') + self.assertEqual(self.call('verify','verify',(1,1,2),payload)['proof_revision'],2) + + def test_suspension_and_read_scope(self): + issued=self.issue() + for table in ['applications','environments']: + self.sql('UPDATE proof_parent_fixture.'+table+' SET suspended=true') + with self.assertRaisesRegex(AssertionError,'resource suspended'): + self.call('verify','blocked',(1,1,2),self.observation(issued)) + self.sql('UPDATE proof_parent_fixture.'+table+' SET suspended=false') + with self.assertRaisesRegex(AssertionError,'resource unavailable'): + self.call('read',payload={'receipt_id':issued['receipt_id']},actor=13,workspace=11,application=12) + self.sql("UPDATE proof_parent_fixture.authority SET operations=ARRAY['issue']") + with self.assertRaisesRegex(AssertionError,'management authority unavailable'): + self.call('read',payload={'receipt_id':issued['receipt_id']}) + + def holder(self, sql): + process = subprocess.Popen(self.args(),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True) + process.stdin.write('BEGIN;'+sql+';\n\\echo HELD\n') + process.stdin.flush() + deadline=time.monotonic()+5 + buffered = '' + while time.monotonic()