Skip to content

security(utxo): require canonical RTC recipient on /utxo/transfer (#2819, by @antoleod) - #8396

Merged
Scottcjn merged 3 commits into
mainfrom
fix/utxo-transfer-validate-to-address
Sep 11, 2026
Merged

Scottcjn merged 3 commits into
mainfrom
fix/utxo-transfer-validate-to-address

Conversation

@Scottcjn

Copy link
Copy Markdown
Owner

Finding by @antoleod (rustchain-bounties#2819 Low, 25 RTC paid): /utxo/transfer binds from_address to the public key but accepted any non-empty string as to_address, so "not-a-wallet" became a persisted box owner no key can ever spend.

Fix: to_address must fullmatch RTC[0-9a-fA-F]{40}, checked before signature verification or any state mutation; 400 invalid_to_address_format otherwise. Two regression tests (rejects arbitrary string, wrong length, non-hex, lowercase prefix, bcn_; canonical passes the format gate).

Test note: the dual-write fixtures sent the mock recipient RTC_test_eeffgghh; they now use a canonical address. Three failures in test_dual_write_shadow_balance.py (409 on shadow-balance paths) pre-exist on main — verified by running the file with this change stashed — and are not touched here.

🤖 Generated with Claude Code

https://claude.ai/code/session_014RDgENXHE8sjiekfdvw3jn

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) node Node server related tests Test suite changes size/M PR: 51-200 lines labels Sep 11, 2026
@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ BCOS v2 Scan Results

Metric Value
Trust Score 36/100
Certificate ID BCOS-0b681534
Tier L2 (not met)

BCOS Badge

What does this mean?

The BCOS (Beacon Certified Open Source) engine scans for:

  • SPDX license header compliance
  • Known CVE vulnerabilities (OSV database)
  • Static analysis findings (Semgrep)
  • SBOM completeness
  • Dependency freshness
  • Test infrastructure evidence
  • Review attestation tier

Full report | What is BCOS?


BCOS v2 Engine - Free & Open Source (MIT) - Elyan Labs

@github-actions github-actions Bot added size/L PR: 201-500 lines and removed size/M PR: 51-200 lines labels Sep 11, 2026
Crypteaucajun and others added 3 commits September 11, 2026 09:40
Reported by @antoleod under rustchain-bounties#2819 (Low, paid). The sender
was bound to its Ed25519 key but to_address was accepted as any non-empty
string, so a typo or hostile value became a persisted box owner that no
wallet key can spend, permanently locking the RTC. Reject anything but
RTC + 40 hex before signature checks or state mutation.

Tests: dual-write fixtures now use a canonical recipient (they sent the
mock 'RTC_test_eeffgghh'); two regression tests added. Three failures in
test_dual_write_shadow_balance.py (409 on shadow-balance paths) pre-exist
this change on main and are unrelated.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014RDgENXHE8sjiekfdvw3jn
Signed-off-by: Scott <scottbphone12@gmail.com>
…after required-field validation

The recipient format check now runs after the existing required-fields /
nonce block so error precedence is unchanged. Endpoint tests that sent mock
recipients ('bob', 'RTC1bbbb', 'RTC_test_receiver', ALICE-as-recipient) now
use canonical RTC+40hex addresses; mock SENDER addresses are untouched.
Remaining failures (3 in the dual-write files) pre-exist on main.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014RDgENXHE8sjiekfdvw3jn
Signed-off-by: Scott <scottbphone12@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014RDgENXHE8sjiekfdvw3jn
Signed-off-by: Scott <scottbphone12@gmail.com>
@Scottcjn
Scottcjn force-pushed the fix/utxo-transfer-validate-to-address branch from 2a9e184 to 00df45f Compare September 11, 2026 14:40
@Scottcjn
Scottcjn merged commit 8c79fba into main Sep 11, 2026
12 checks passed
@Scottcjn
Scottcjn deleted the fix/utxo-transfer-validate-to-address branch September 11, 2026 14:43
@github-actions

Copy link
Copy Markdown
Contributor

RTC Reward

This merged PR earned 5 RTC — sent to Scottcjn.

RustChain Bounty Program

Scottcjn pushed a commit that referenced this pull request Sep 19, 2026
…; annotate bounded fetch

- The tests used 'bob' as the recipient. /utxo/transfer format-checks
  to_address since #8396/#8465 (lower-case RTC + 40 hex), so the transfer
  returned 400 before reaching the code under test. Use a canonical address.
- test_dual_write_exact_balance_goes_to_zero seeded 100 RTC of UTXO against
  a 10 RTC account. With receiver/change outputs registered as account
  mirrors, the 90 RTC change exceeds the (now 0) account balance and the
  MIRROR_EXCEEDS_BALANCE guard correctly fails closed. Seed the consistent
  dual-write state (UTXO == account) the invariant assumes.
- Annotate the per-transfer output fetch for the fetchall guard (bounded by
  one transfer's outputs).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Scott <scottbphone12@gmail.com>
Scottcjn added a commit that referenced this pull request Sep 19, 2026
…venance (#8388)

* fix(utxo): dual-write reward boxes exactly mirror the account credit

Two ways finalize_epoch's UTXO_DUAL_WRITE mint diverged from the account
model it is supposed to mirror (#2819, favoritegrandson-tech):

1. Precision split: the account credit truncated the share to 6 decimals
   (amount_i64) while the mint truncated the same Decimal to 8 decimals,
   so each reward box was up to 99 nRTC larger than the credit and
   /utxo/integrity reported the models disagreeing after every epoch with
   fractional shares. The mint is now derived from the truncated credit:
   amount_nrtc = amount_i64 * (UTXO_UNIT // ACCOUNT_UNIT). A module-level
   assert pins that ratio as an exact integer.
2. Ghost mint: a miner enrolled without a balances row gets no account
   credit (no-phantom invariant) but was still minted a UTXO box. The
   mint is now gated on the account row actually being credited.

This is a prerequisite for #8394 (registering reward boxes as mirrors
must not create mirrors larger than the balance they mirror).

Tests (end-to-end finalize_epoch, dual-write on): the reward box equals
the credit x100 for co-prime weights, a ghost gets nothing, and the
credited miner still mirrors exactly while a ghost dilutes the weight.
Both fail on main (45 nRTC drift; 0.75 RTC ghost mint) and pass here.
tests/ + epoch/UTXO/dual-write suites: 0 regressions (4192 -> 4194).
UTXO_DUAL_WRITE is off and prod has 0 UTXO rows, so there is no history
to reconcile.

Reported-by: favoritegrandson-tech
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Scott <scottbphone12@gmail.com>

* fix(utxo): register epoch mining-reward boxes as account-mirror provenance (danaher #2819, reward path)

Same cross-model double-spend class as the /utxo/transfer receiver residual,
in the epoch settlement path. Under UTXO_DUAL_WRITE=1, finalize_epoch credits
each miner's ACCOUNT balance (primary ledger) AND mints a UTXO reward box for
them — but never registered those boxes in account_mirror_boxes. So each reward
was spendable via BOTH models (UTXO box + account balance) = double spend; a
total-only integrity check stays models_agree=True. Latent today (prod runs
UTXO_DUAL_WRITE=0).

Fix:
- account_mirror_boxes is now part of the canonical UTXO SCHEMA_SQL, so
  init_tables() creates it for every dual-write writer. This avoids a CREATE
  TABLE inside finalize_epoch's open settlement transaction, which Python's
  sqlite3 would implicit-commit (splitting the atomic epoch settle). (Comment in
  SCHEMA_SQL is kept free of the ';' character — _execute_schema splits on it.)
- finalize_epoch registers every reward box of each dual-write batch into
  account_mirror_boxes (located by the batch's creation_height; apply_transaction
  enforces one mining_reward per height, so all boxes at that height are exactly
  the batch's outputs). Pure INSERTs, same connection/transaction as the settle.

The mirror-input exclusion then blocks re-spending these boxes via the UTXO
path, forcing the reward to move through the account path (single spend).

Tests (node/test_utxo_dualwrite_reward_provenance.py): init_tables creates the
table; a registered reward box is excluded from UTXO spendable candidates
(double-spend closed) while an unregistered one is not (control); finalize_epoch
wires the registration inside the dual-write block (source/AST, matching
test_epoch_utxo_dual_write_guard's convention). All fail on origin/main, pass
here. UTXO + settlement suites: 165 passed, no new failures (2 pre-existing
genesis-migration failures are identical on origin/main).

Companion to PR #8388 (transfer receiver residual). Keep UTXO_DUAL_WRITE=0 on
prod until both merge. danaher-j #2819 bounty remains held pending destination.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KbyXP4eiiRYEa8GsQtQPhR

* fix(utxo): register only this epoch's mint outputs as reward mirrors

The reward-mirror registration selected every box at batch_height. The
one-mining_reward-per-height guard only limits MINT transactions: an
ordinary /utxo/transfer box can share that height (both use slot numbers),
and would be tagged as an account mirror, locking the user's own funds
(every spend -> 409 ACCOUNT_MIRROR_BOX_NOT_SPENDABLE). Join on the
mining_reward tx so only this batch's mint outputs are registered.
Materialize the rows (bounded by UTXO_MAX_OUTPUTS) because the INSERTs
reuse the same cursor.

End-to-end test (real finalize_epoch, dual-write on, a user transfer box
at the batch height): the user box is not tagged, each reward box is
registered once, and each mirror equals its account credit exactly
(needs the precision fix underneath). Fails on the previous height-only
query ('user-box' tagged) and on main (nothing registered).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Scott <scottbphone12@gmail.com>

* fix(utxo): register dual-write transfer outputs as account-mirror provenance (danaher #2819 receiver residual)

Under UTXO_DUAL_WRITE=1, `balances` (account) is the primary ledger and UTXO
boxes are its shadow; `account_mirror_boxes` records which boxes back account
value, with the consensus invariant `mirror <= balance`. The mirror-input
exclusion already blocks spending a mirror box via the UTXO path (input side,
fixed earlier). The RESIDUAL: a /utxo/transfer credits the receiver's account
balance AND creates a spendable UTXO output box for the receiver, but never
registered that output as mirror provenance — so the same value was spendable
via BOTH models (UTXO output box + account credit) = double spend. A total-only
integrity check stays models_agree=True throughout (danaher-j private report,
#2819 residual; latent today: UTXO_DUAL_WRITE is off on prod).

Fix:
- utxo_db.apply_transaction now exposes the authoritative tx_id on the caller's
  tx dict (tx['tx_id']), so the endpoint can locate the boxes it created.
- The dual-write branch of /utxo/transfer registers every output box of the
  transfer (receiver at index 0, change at index 1) into account_mirror_boxes,
  so the unconditional mirror-input exclusion blocks re-spending them via UTXO;
  the value must move via the account path (which consumes the mirror on settle).
- Added a fail-closed per-wallet `mirror <= balance` assertion (compared in nRTC:
  mirror value_nrtc vs balance amount_i64 * (UNIT//ACCOUNT_UNIT)); a violation
  rolls the transfer back rather than committing money that exists twice.

Tests: node/test_utxo_dualwrite_receiver_provenance.py (3 tests) — receiver
output is mirror-registered, receiver box is excluded from UTXO spendable
candidates (double-spend closed), invariant holds. All 3 fail on origin/main and
pass with this change. Existing UTXO suites: 154 passed, no new failures (17
pre-existing POC/isolation failures are identical on origin/main).

RELATED (not fixed here, flagged for follow-up): the epoch mining-reward
dual-write path (rustchain_v2_integrated...:~5698) likewise creates UTXO reward
boxes without account_mirror_boxes provenance — same class, same latency behind
UTXO_DUAL_WRITE=0. Keep UTXO_DUAL_WRITE=0 on prod until both are resolved.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KbyXP4eiiRYEa8GsQtQPhR

* test(utxo): update receiver-provenance tests for canonical recipients; annotate bounded fetch

- The tests used 'bob' as the recipient. /utxo/transfer format-checks
  to_address since #8396/#8465 (lower-case RTC + 40 hex), so the transfer
  returned 400 before reaching the code under test. Use a canonical address.
- test_dual_write_exact_balance_goes_to_zero seeded 100 RTC of UTXO against
  a 10 RTC account. With receiver/change outputs registered as account
  mirrors, the 90 RTC change exceeds the (now 0) account balance and the
  MIRROR_EXCEEDS_BALANCE guard correctly fails closed. Seed the consistent
  dual-write state (UTXO == account) the invariant assumes.
- Annotate the per-transfer output fetch for the fetchall guard (bounded by
  one transfer's outputs).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Scott <scottbphone12@gmail.com>

---------

Signed-off-by: Scott <scottbphone12@gmail.com>
Co-authored-by: Scott <scottbphone12@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) BCOS-L2 Beacon Certified Open Source tier BCOS-L2 (required for non-doc PRs) node Node server related size/L PR: 201-500 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants