Skip to content

fix(wallet): harden the secondary Solana account and program signing - #8740

Merged
senamakel merged 5 commits into
tinyhumansai:mainfrom
senamakel:wallet-index-hardening
Nov 16, 2026
Merged

senamakel merged 5 commits into
tinyhumansai:mainfrom
senamakel:wallet-index-hardening

Conversation

@senamakel

@senamakel senamakel commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Refs #8735

Follow-up to #8735, which merged at dacda387 (merge 399bdc9c). This PR hardens the secondary Solana account (index 1) and the program-signing path against four findings from the CodeRabbit and Copilot review of that PR.

Changes

  1. Index-0 / index-1 key collision. validate_setup refused a secondary account at a non-canonical path, but accepted an index-0 account at a secondary path (m/44'/501'/1'/0'). Both would resolve to the same key, so the secondary's program policy could sign for the primary. Setup now refuses a primary whose path is a known secondary path. A wallet state that already holds such an alias is quarantined at load (load runs the same validation), so no secondary key is ever resolved from it.
  2. Process-wide quotes. Prepared program quotes are now bound to their workspace, a hash of the encrypted phrase (so wallet replacement invalidates them), the account index and the Solana endpoint. A take that does not match leaves the quote in the map for its own context.
  3. Debit measured from two snapshots. The pre-state (getTokenAccountsByOwner) and the post-state (simulateTransaction) came from separate RPC reads. Every answer now carries its context slot. A measurement whose reads disagree on slot is repeated and then refused. The request also names only token accounts that are in the transaction's account keys, since the transaction cannot write any other account.
  4. Signing after expiry. The clock was read before the re-simulation, which can take up to about 45 seconds. Execution now reads the clock at the reservation point, refuses an expired quote there (RefusalKind::QuoteExpired), and uses that instant for the policy window and the pending ledger record. The snapshot comment now states what the caps guarantee.

Also: QUOTES gets a scripts/ci/process-statics.json ownership record (category B), and the commits are formatted with rustfmt.

Regression tests

Each fix has a test that fails when its fix is removed. I checked this by mutating each guard in turn and running the named test:

Finding Test Mutation that fails it
1. alias ops_tests::validate_setup_rejects_a_primary_at_a_secondary_path, ops_tests::a_stored_primary_at_the_secondary_path_is_quarantined_before_signing index-0 alias check disabled
2. quote binding program::ops_tests::a_quote_is_taken_only_in_the_context_it_was_prepared_in binding comparison disabled
3. two snapshots program::simulate_tests::a_measurement_whose_reads_span_slots_is_refused, program::simulate_tests::only_token_accounts_the_transaction_names_are_read_or_requested slot check disabled; account-key filter removed
4. expiry program::ops_tests::a_quote_is_refused_from_its_expiry_instant_onward covers the expiry predicate only

Gap: the expiry test covers the predicate. Wiring the fresh clock into the reservation closure is not unit-tested here, because it needs the ledger, simulation and signing stack.

Validation

  • cargo test -p openhuman --features web3 --lib -- web3::wallet: 99 passed, 1 failed. The failure is reveal_recovery_phrase_returns_phrase_for_existing_wallet, which needs the TinySecurity module ("security module unavailable: secret approval unavailable") and does not run in this sandbox. The same failure is documented in feat(wallet): secondary Solana account (index 1) with per-account program-signing policy #8735.
  • rustfmt --check on the changed files: clean after the style commit.

Not in this PR

Known CI state of the source PR

The #8735 macOS native-fixture failure reproduces on unrelated branches (tinysecurity-0-21-1, chore/343-*), so it predates this change. The Linux egress and Windows native jobs hit the job time limit during a cold compile after the Cargo.lock change, not on a test assertion.

Co-authored-by: Medulla medulla@tinyhumans.ai

Summary by CodeRabbit

  • Bug Fixes
    • Wallet setup now rejects a primary account that shares a derivation path with a secondary account on the same chain.
    • Prepared quotes are available only in the matching wallet and account context, and cannot be executed after expiry.
    • Transaction simulations now measure only relevant token accounts and retry when account data and simulation results come from inconsistent snapshots.

senamakel and others added 5 commits November 16, 2026 07:02
Wallet setup now refuses an index-0 account whose derivation path matches a known secondary account, since that primary would resolve to the same key and let the secondary's program policy sign for it. Stored wallets written before this check are quarantined at load time so they can never sign as the secondary.

Auto-committed-on: cuttlefish
Co-authored-by: Medulla <medulla@tinyhumans.ai>
The pre-state (getTokenAccountsByOwner) and post-state (simulateTransaction)
were read separately, so a deposit between them could hide a token debit.
Every RPC answer now carries its context slot; a measurement whose reads
disagree is repeated and then refused. Only token accounts named in the
transaction's account keys are read or requested, which also avoids the RPC
rejecting an address list longer than the account-key list.

Co-authored-by: Medulla <medulla@tinyhumans.ai>
…cution

The quote map is process-wide, so a quote prepared in one workspace or wallet
could be executed, and spend its allowance, in another. A quote now records
its workspace, a hash of the encrypted phrase, its account index and the
Solana endpoint, and is taken only under the same binding; a mismatched take
leaves it in place. Execution also reads the clock at the reservation point
and refuses a quote that expired while it was being re-simulated, before
anything is signed. The snapshot comment now states what the caps guarantee.

Co-authored-by: Medulla <medulla@tinyhumans.ai>
The quote map is a new process static. It is partitioned by QuoteBinding and
the record names the test that pins that partition.

Co-authored-by: Medulla <medulla@tinyhumans.ai>
Applied rustfmt formatting across the wallet program ops and simulate modules and their tests, wrapping long call sites, assertions, and log macros. No behaviour changed.

Auto-committed-on: cuttlefish
Co-authored-by: Medulla <medulla@tinyhumans.ai>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 07:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T07:24:48.928373Z cdd4c45 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes add primary-account path validation and update prepared program quote handling. Quotes are bound to execution context and checked for expiry. Simulation measures transaction-named token accounts and retries when RPC reads use different slots.

Changes: Primary account path validation

Layer / File(s) Summary
Reject primary path aliases
crates/openhuman-core/src/web3/wallet/ops.rs, crates/openhuman-core/src/web3/wallet/ops_tests.rs
Setup validation rejects a primary account that matches a known secondary account’s chain and derivation path. Tests cover setup rejection and invalid saved state.

Prepared program quote execution

Layer / File(s) Summary
Bind quotes to execution context
crates/openhuman-core/src/web3/wallet/program/ops.rs, crates/openhuman-core/src/web3/wallet/program/ops_tests.rs
Stored quotes include workspace, wallet hash, account index, and endpoint. Quote-taking checks those fields and expiry; tests verify mismatches leave quotes available.
Validate and reserve quotes
crates/openhuman-core/src/web3/wallet/policy.rs, crates/openhuman-core/src/web3/wallet/program/ops.rs, crates/openhuman-core/src/web3/wallet/program/simulate.rs, crates/openhuman-core/src/web3/wallet/program/ops_tests.rs, crates/openhuman-core/src/web3/wallet/program/simulate_tests.rs, scripts/ci/process-statics.json
Execution checks expiry after simulation and uses the reservation timestamp for policy evaluation and the pending ledger record. Simulation measures transaction-named token accounts, retries split-slot reads up to three times, and returns an error if the reads remain split. Tests and the statics inventory cover the updated behavior.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant execute_prepared_program
  participant take_quote
  participant simulate
  participant spend_ledger
  execute_prepared_program->>take_quote: Match binding and check expiry
  execute_prepared_program->>simulate: Measure transaction account keys
  simulate-->>execute_prepared_program: Return measured report or split-slot error
  execute_prepared_program->>spend_ledger: Record pending attempt at reservation time
Loading

Possibly related PRs

  • tinyhumansai/openhuman#8735: Introduces the secondary Solana account and prepared-program tools that this change validates and hardens.
  • tinyhumansai/openhuman#8649: Adds the spend ledger and prepared-program execution path whose reservation behavior this change updates.

Suggested reviewers: m3ga-mind, tinysweeper

Merge Risk: Minimal · up to cdd4c

This change rejects a primary wallet account that aliases a secondary derivation path and binds prepared program quotes to their execution context. It also checks quote expiry at reservation time and measures debits from a single RPC slot. No concrete defect was established in the reviewed changes. The remaining unknowns are how TinyWallet interprets equivalent path spellings and some CI jobs that did not complete, which are worth owner awareness but do not block merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two main areas of the change: hardening the secondary Solana account and hardening program signing in the wallet module. The changeset covers the setup alias rejection, quote bindi…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two main areas of the change: hardening the secondary Solana account and hardening program signing in the wallet module. The changeset covers the setup alias rejection, quote binding and expiry, and debit measurement via simulation slots, so the title is specific enough to describe the primary work. It does not name each finding, which is acceptable.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the wallet’s trail,
A quote stays with its matching tale.
Three slots align, or tries begin,
Named accounts alone count in.
The ledger notes the time to sign.

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: 0767c2fd-b7b9-456a-bf74-8d13a53fae03
Commits

Reviewing files that changed from the base of the PR and between 399bdc9 and cdd4c45.

Files selected for processing: 8

  • crates/openhuman-core/src/web3/wallet/ops.rs
  • crates/openhuman-core/src/web3/wallet/ops_tests.rs
  • crates/openhuman-core/src/web3/wallet/policy.rs
  • crates/openhuman-core/src/web3/wallet/program/ops.rs
  • crates/openhuman-core/src/web3/wallet/program/ops_tests.rs
  • crates/openhuman-core/src/web3/wallet/program/simulate.rs
  • crates/openhuman-core/src/web3/wallet/program/simulate_tests.rs
  • scripts/ci/process-statics.json

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The missing TinyWallet implementation leaves derivation-path alias safety unverified, and the expiry-sensitive reservation-to-signing flow still requires human integration validation.

No open issues 🎉

What changed in this PR

This follow-up to #8735 hardens OpenHuman’s secondary Solana account and prepared program-signing flow.

Changes:

  • Rejects primary accounts that use a known secondary derivation path, including stored wallet state.
  • Binds quotes to their workspace, wallet, account and endpoint, and rechecks expiry at ledger reservation.
  • Retries or refuses simulations whose reads span different slots, limits measured accounts to transaction keys, and adds regression tests.
File Description
scripts/​ci/​process-statics.json Records ownership and context partitioning of the process-wide quote map.
crates/​openhuman-core/​src/​web3/​wallet/​program/​simulate.rs Checks snapshot slots and filters token accounts to transaction keys.
crates/​openhuman-core/​src/​web3/​wallet/​program/​simulate_tests.rs Tests split-slot refusal and token-account filtering.
crates/​openhuman-core/​src/​web3/​wallet/​program/​ops.rs Adds quote binding and reservation-time expiry checks; clarifies simulation limits.
crates/​openhuman-core/​src/​web3/​wallet/​program/​ops_tests.rs Tests quote isolation and the expiry predicate.
crates/​openhuman-core/​src/​web3/​wallet/​policy.rs Adds the QuoteExpired refusal kind.
crates/​openhuman-core/​src/​web3/​wallet/​ops.rs Rejects primary accounts at known secondary derivation paths.
crates/​openhuman-core/​src/​web3/​wallet/​ops_tests.rs Tests alias rejection during setup and quarantine during load.

Review effort: Balanced

@senamakel
senamakel merged commit e78115c into tinyhumansai:main Nov 16, 2026
16 of 17 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cdd4c450b9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +201 to +204
for _ in 0..SNAPSHOT_ATTEMPTS {
match measure(url, transaction, owner, account_keys).await? {
Attempt::Measured(report) => return Ok(report),
Attempt::SplitAcrossSlots(slots) => seen = slots,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move the new snapshot-measurement logic into TinyWallet

The new snapshot retries and transaction-account filtering implement Solana wallet-engine/provider behavior in the OpenHuman host, with direct behavior tests in simulate_tests.rs. AGENTS.md assigns the wallet engine and per-chain transaction flows to vendor/tinywallet, leaving OpenHuman configuration, consent, controllers and seam implementations. Land this measurement fix and its behavior tests upstream, then consume it through the released module contract and gitlink update; adding the algorithm here extends the host-side engine workaround rather than respecting that ownership boundary.

AGENTS.md reference: AGENTS.md:L616-L616

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not resolved here. The ownership point is correct: AGENTS.md assigns per-chain build/sign/broadcast flows to vendor/tinywallet. Moving the snapshot measurement needs a tinywallet bus extension, a test suite upstream, a release and a gitlink bump. That is follow-up work tracked separately, so this thread stays open until that lands. The safety fixes in this PR are unchanged.

Comment on lines +273 to +274
slots.push(context_slot(&answer, "simulateTransaction")?);
if slots.iter().any(|slot| *slot != slots[0]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid requiring three serial RPC reads to fit within one slot

For an otherwise healthy remote endpoint with roughly 250 ms round trips and confirmed banks advancing every roughly 400 ms, measure's two awaited getTokenAccountsByOwner calls followed by simulateTransaction necessarily span slots. This equality check therefore rejects every attempt even when all relevant balances remain unchanged, and restarting the same serial sequence cannot recover. Both program preparation and execution become unavailable for such endpoints. Obtain comparable pre/post state through an atomic or coordinated measurement strategy rather than requiring three serial RPC calls to complete against one latest-confirmed bank.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that three serial reads can span slots on a busy endpoint, and the equality check then refuses every attempt. It fails closed, so it is not a debit-masking hole, but it can make prepare and execute unavailable there. A correct fix needs a coordinated read strategy, which belongs with the upstream measurement work in the first thread. Leaving this open for that change.

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.

2 participants