Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions contracts/escrow/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,106 @@ fn test_pause_pause_unpause_ends_unpaused() {

assert!(!client.is_paused());
}

// ---------------------------------------------------------------------------
// Issue #22 — negative authorization coverage. `setup_initialized` uses
// `mock_all_auths`, which can never prove that a *missing* signature is
// rejected. These tests initialise with a scoped mock that authorises only
// `init`, then invoke privileged entrypoints with no matching auth and assert
// the call panics in `require_auth`. A positive control proves the same call
// succeeds once the signature is supplied.
// ---------------------------------------------------------------------------

use soroban_sdk::testutils::{MockAuth, MockAuthInvoke};

/// Register and `init` the contract authorising only `admin` for the `init`
/// call. Subsequent privileged calls are intentionally left unauthorised so
/// their `require_auth` fails.
fn setup_scoped_auth(env: &Env) -> EscrowClient<'_> {
let contract_id = env.register_contract(None, Escrow);
let client = EscrowClient::new(env, &contract_id);
let admin = Address::generate(env);
env.mock_auths(&[MockAuth {
address: &admin,
invoke: &MockAuthInvoke {
contract: &contract_id,
fn_name: "init",
args: (admin.clone(),).into_val(env),
sub_invokes: &[],
},
}]);
client.init(&admin);
client
}

#[test]
#[should_panic]
fn test_i22_pause_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
client.pause();
}

#[test]
#[should_panic]
fn test_i22_set_service_price_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
client.set_service_price(&Symbol::new(&env, "infer"), &10i128);
}

#[test]
#[should_panic]
fn test_i22_register_service_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
client.register_service(&Symbol::new(&env, "infer"));
}

#[test]
#[should_panic]
fn test_i22_set_agent_allowed_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
let agent = Address::generate(&env);
client.set_agent_allowed(&agent, &true);
}

#[test]
#[should_panic]
fn test_i22_set_service_disabled_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
client.set_service_disabled(&Symbol::new(&env, "infer"), &true);
}

#[test]
#[should_panic]
fn test_i22_migrate_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
client.migrate_v1_to_v2();
}

#[test]
#[should_panic]
fn test_i22_propose_admin_transfer_requires_admin_auth() {
let env = Env::default();
let client = setup_scoped_auth(&env);
let next = Address::generate(&env);
client.propose_admin_transfer(&next);
}

/// Positive control: with `mock_all_auths` the same privileged call
/// succeeds, proving the panics above stem from the missing signature.
#[test]
fn test_i22_pause_succeeds_with_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, Escrow);
let client = EscrowClient::new(&env, &contract_id);
let admin = Address::generate(&env);
client.init(&admin);
client.pause();
assert!(client.is_paused());
}
66 changes: 66 additions & 0 deletions docs/escrow/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Escrow — Authorization Model

This document records the authorization model for the AgentPay escrow
contract (`contracts/escrow/src/lib.rs`) and the testing pattern used to
prove it.

## Model

Authorization is hand-rolled per entrypoint. Every privileged (state
-changing) entrypoint loads the stored `Admin` address and calls
`admin.require_auth()` **before** performing any write. If the admin slot
is unset the call panics with `NotInitialized` (#3) — never a silent
partial write. The two-step admin handover instead authorizes the
*caller* principal (`accept_admin_transfer`) or the current admin
(`propose_admin_transfer` / `cancel_admin_transfer`).

`require_auth` is invoked before the first storage `set`, so a failed
authorization can never leave a half-applied state change.

## Authorization matrix

| Entrypoint | Principal required | Pause-gated |
|---|---|---|
| `init` | the `admin` argument (once) | no |
| `record_usage` | none (public, metered) | yes |
| `settle` | admin | yes |
| `set_service_price` | admin | no |
| `register_service` / `unregister_service` | admin | no |
| `set_service_disabled` | admin | no |
| `set_service_metadata` / `clear_service_metadata` | admin | no |
| `transfer_service_ownership` | current owner **or** admin | yes |
| `set_agent_allowed` / `set_allowlist_enabled` | admin | no |
| `set_min_requests_per_call` / `set_max_requests_per_call` | admin | no |
| `set_require_service_registration` | admin | no |
| `pause` / `unpause` | admin | n/a |
| `propose_admin_transfer` / `cancel_admin_transfer` | current admin | no |
| `accept_admin_transfer` | the pending admin (caller) | no |
| `migrate_v1_to_v2` | admin | no |

Read-only entrypoints (`get_*`, `is_*`, `compute_billing`, `version`)
require no authorization.

## Testing pattern

The default test helper uses `env.mock_all_auths()`, which authorizes
every call and therefore can never prove a *missing* signature is
rejected. Negative-authorization tests instead use scoped mocking:

```rust
env.mock_auths(&[MockAuth {
address: &admin,
invoke: &MockAuthInvoke {
contract: &contract_id,
fn_name: "init",
args: (admin.clone(),).into_val(env),
sub_invokes: &[],
},
}]);
client.init(&admin);
// The init mock does not cover `pause`, so this must panic in require_auth:
client.pause();
```

See `test_i22_*` in `contracts/escrow/src/test.rs` for one such test per
privileged entrypoint, plus a positive control proving the same call
succeeds once the signature is supplied.
Loading