Skip to content

feat(accounts): add a custom policy test harness - #998

Open
dubzn wants to merge 1 commit into
OpenZeppelin:mainfrom
dubzn:fix/policy-signer-docs-#975
Open

dubzn wants to merge 1 commit into
OpenZeppelin:mainfrom
dubzn:fix/policy-signer-docs-#975

Conversation

@dubzn

@dubzn dubzn commented Nov 27, 2026 •

Copy link
Copy Markdown

Fixes #975

Document that attached policies decide which signers are required, including how to pair spending_limit with simple_threshold for a multi-signer rule.

Add stellar-accounts/testutils so policy authors can install a policy and run the real __check_auth with selected Ed25519 signers. The tests cover signer identity, unsigned attempts, and spending-limit composition with a threshold.

The companion site changes are in OpenZeppelin/docs#270.

PR Checklist

  • Tests
  • Documentation

Summary by CodeRabbit

  • Documentation
    • Clarified how policies are installed and removed, including that cleanup can fail while the rule removal still succeeds.
    • Added guidance on how policies affect signer requirements, including when a policy may authorize an action without a signature.
    • Explained that spending-limit policies require at least one signer and should be paired with a threshold policy when multiple signers are required.
    • Updated the authorization example to show a token transfer with a signer threshold and daily spending limit.
  • Tests
    • Expanded coverage of signer selection and how spending limits work alongside signer thresholds.

@openzeppelin-ai-auditor

Copy link
Copy Markdown

OpenZeppelin AI Auditor

Warning

Review skipped for commit 7c14a35: Pull requests from forks are not supported yet.

ℹ️ Review info
  • Project: Stellar Contracts
  • Commit: 7c14a35 (base 80075eb on main)
  • Changed lines: 424 across 8 files
  • Trigger: pull request opened

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes clarify signer requirements for account policies and add native test utilities for custom policies. Tests use the new harness to check required signers and the combination of spending-limit and threshold policies.

Changes

Policy signer requirements

Layer / File(s) Summary
Document policy signer requirements
packages/accounts/src/policies/mod.rs, packages/accounts/src/policies/spending_limit.rs, packages/accounts/README.md
The documentation describes the authenticated signer subset, policy-controlled signer requirements, and the spending-limit policy’s minimum signer requirement. The README also updates lifecycle guidance, test setup, and its authorization example.

Policy test utilities

Layer / File(s) Summary
Expose and implement the policy test harness
packages/accounts/Cargo.toml, packages/accounts/src/lib.rs, packages/accounts/src/testutils/mod.rs, packages/accounts/src/smart_account/test/auth_entries.rs
The package adds a non-WASM testutils feature and public module. PolicyTestHarness registers an account and verifier, installs a context rule, and invokes authorization with signatures from selected signers. An existing auth-entry test now uses TestEd25519Verifier.
Test custom and combined policy checks
packages/accounts/src/testutils/test.rs
Test policies and cases check required-signer enforcement. Composition tests verify that a spending limit accepts one signer and that adding a threshold of two requires two signers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Other · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Test
  participant PolicyTestHarness
  participant TestAccount
  participant TestEd25519Verifier
  participant do_check_auth
  Test->>PolicyTestHarness: new(env, context_type, signer_seeds, policies)
  PolicyTestHarness->>TestAccount: register account and install context rule
  PolicyTestHarness->>TestEd25519Verifier: register verifier
  Test->>PolicyTestHarness: try_check_auth(context, signer_indices)
  PolicyTestHarness->>TestAccount: invoke authorization hook
  TestAccount->>do_check_auth: delegate __check_auth
  do_check_auth-->>PolicyTestHarness: return authorization result
Loading

Suggested reviewers: brozorec

Merge Risk: Minimal · up to 7c14a

This change clarifies how policies determine required signers and adds an opt-in test harness for custom policies. It does not alter production contract behavior, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat(accounts): add a custom policy test harness" matches the main change. The changeset adds a PolicyTestHarness in packages/accounts/src/testutils/mod.rs, exposes a testutils module…
Description check Passed The description follows the template. It links the issue (Fixes #975), describes the documentation changes and the new testutils feature, and includes the PR checklist with Tests and Documentation c…
Linked Issues check Passed Directly linked issue #975 has these coding requirements:

1. Security warning on Policy::enforce [#975]. The summary of packages/accounts/src/policies/mod.rs shows a # Security Warning stating …
Out of Scope Changes check Passed Each change connects to #975 or supports its testutils work:

- packages/accounts/Cargo.toml: adds the testutils feature and makes ed25519-dalek an optional non-WASM dependency. This supports th…
Full details: Title check

Explanation

The title "feat(accounts): add a custom policy test harness" matches the main change. The changeset adds a PolicyTestHarness in packages/accounts/src/testutils/mod.rs, exposes a testutils module, and adds test policies. The title is specific and follows a conventional-commit style.

Full details: Description check

Explanation

The description follows the template. It links the issue (Fixes #975), describes the documentation changes and the new testutils feature, and includes the PR checklist with Tests and Documentation checked. It does not describe the changes in as much detail as the template invites, but the required sections are present and the content is specific.

Full details: Linked Issues check

Explanation

Directly linked issue #975 has these coding requirements:

  1. Security warning on Policy::enforce [#975]. The summary of packages/accounts/src/policies/mod.rs shows a # Security Warning stating that rules with policies rely solely on those policies, and that a policy accepting an empty signer set can authorize calls with no signatures. This matches the requirement.

  2. Document spending_limit one-signer behaviour and recommend simple_threshold for multi-signer rules [#975]. The summary of packages/accounts/src/policies/spending_limit.rs adds a signer-requirements section. It documents that any single signer passes, that an empty set returns NotAllowed, and that simple_threshold should pair with multi-signer quorums. This matches the requirement.

  3. testutils feature in stellar-accounts [#975]. packages/accounts/Cargo.toml defines testutils, and src/lib.rs exposes pub mod testutils. PolicyTestHarness::new registers a smart account, a context rule and policies. try_check_auth signs with selected signer indices and invokes the real authorization hook. test.rs covers signer selection, unsigned attempts (MissingSigner) and spending-limit plus threshold composition. This matches the helper requirements for no, some, or all signers.

  4. README and docs items [#975]. The README summary covers enforce inputs, signer responsibility, install and uninstall lifecycle, and testing custom policies. The context-rules page correction and the custom-policy page are on the docs site. The PR states these are in docs(stellar): explain custom smart-account policies docs#270. They are documentation, not repository code, and this assessment does not verify them. The README summary does not explicitly mention state keying by (smart_account, context_rule_id) or the event-size (#852) and error-code range (#850) notes. Those are documentation gaps, not coding requirements, so they do not change this verdict.

No coding requirement is unmet in the evidence reviewed. Verdict is based on the whole-PR summary. No repository inspection was run.

Full details: Out of Scope Changes check

Explanation

Each change connects to #975 or supports its testutils work:

  • packages/accounts/Cargo.toml: adds the testutils feature and makes ed25519-dalek an optional non-WASM dependency. This supports the harness.
  • packages/accounts/src/lib.rs: gates pub mod testutils on test or feature = "testutils". This exposes the harness.
  • packages/accounts/src/smart_account/test/auth_entries.rs: replaces a local Ed25519 verifier wrapper with TestEd25519Verifier from the new testutils module. This is a refactor that reuses the harness verifier. It is not a behaviour change.
  • packages/accounts/src/testutils/test.rs: adds tests and test policies for the harness.
  • packages/accounts/README.md, policies/mod.rs, policies/spending_limit.rs: documentation required by #975.

The README AuthPayload example change replaces a DEX example with a token transfer example. This is a documentation change within the same #975 documentation scope. It does not change code.

No unrelated change was identified.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks each signer’s place,
Then tests the rule at a hopping pace.
The harness signs and calls the gate,
Two voices meet the threshold’s rate.
The spending limit joins the play,
And carrots celebrate the day.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4bd958da-67ca-43ed-a833-f891d9685529
Commits

Reviewing files that changed from the base of the PR and between 80075eb and 7c14a35.

Files selected for processing: 8

  • packages/accounts/Cargo.toml
  • packages/accounts/README.md
  • packages/accounts/src/lib.rs
  • packages/accounts/src/policies/mod.rs
  • packages/accounts/src/policies/spending_limit.rs
  • packages/accounts/src/smart_account/test/auth_entries.rs
  • packages/accounts/src/testutils/mod.rs
  • packages/accounts/src/testutils/test.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Policies: document that policies own the signer check, and add a test harness for custom policies

1 participant