Skip to content

[UTXO-BUG] Reject malformed UTXO spend inputs - #5454

Closed
galanime wants to merge 1 commit into
Scottcjn:mainfrom
galanime:codex/utxo-input-shape-guard
Closed

galanime wants to merge 1 commit into
Scottcjn:mainfrom
galanime:codex/utxo-input-shape-guard

Conversation

@galanime

Copy link
Copy Markdown
Contributor

Summary

Fixes a UTXO spend-input validation gap in node/utxo_db.py.

Before this patch, malformed inputs payloads could reach raw indexing in both transaction application and mempool admission:

  • inputs=None or a string could raise instead of returning a validation failure.
  • non-dict input entries could raise when indexed.
  • missing, blank, or whitespace-only box_id values could raise or drift into later validation.
  • duplicate box_id handling was local to apply_transaction() and not shared with mempool_add().

The patch adds a shared _normalize_spend_inputs() helper and applies it at both boundaries, so malformed spend inputs fail closed before they can raise, persist, or claim UTXOs in the mempool.

Bounty

Duplicate Boundary

This overlaps the broad UTXO validation area, but it covers a distinct spend-input shape path on current main: malformed inputs are normalized before both apply_transaction() and mempool_add(). Prior nearby fixes covered output metadata, timestamps, tx types, data inputs, or transfer endpoint scalar fields.

Verification

  • python -B node\test_utxo_db.py -> 73 passed
  • python -m py_compile node\utxo_db.py node\test_utxo_db.py -> passed
  • git diff --check -> passed
  • python -B test_bcos_spdx_check.py -> passed

This is a public receive address only. No private key, seed phrase, keystore, password, bank, exchange, withdrawal, or transfer action is included.

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related size/M PR: 51-200 lines labels May 16, 2026

@minyanyi minyanyi 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.

Reviewed the spend-input normalization changes and the new regression coverage. The shared _normalize_spend_inputs() helper closes the malformed input shape paths before both block application and mempool admission, preserves minting transactions with empty internal inputs, and keeps duplicate spend-input rejection centralized.

Local verification:

  • python -B node\test_utxo_db.py -> 73 passed
  • python -m py_compile node\utxo_db.py node\test_utxo_db.py
  • git diff --check origin/main...HEAD

No blocking issues found.

@kekehanshujun kekehanshujun 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.

Reviewed the spend-input normalization path in both apply_transaction() and mempool_add().

The shared _normalize_spend_inputs() boundary rejects non-list inputs, non-dict entries, missing/blank box_id values, and duplicate box_ids before raw indexing or mempool input claims. I also checked the later mempool existence and conservation checks still run after normalization, so malformed inputs fail closed without locking the existing UTXO.

Verification run locally on the PR version:

  • python -B node\test_utxo_db.py -> 73 passed
  • python -m py_compile node\utxo_db.py node\test_utxo_db.py -> passed

@jaxint jaxint 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.

LGTM! Thanks for contributing. Approved.

@HCIE2054 HCIE2054 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.

LGTM!

@TJCurnutte TJCurnutte 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.

Approved. I focused this pass on the spend-input shape guard in both transaction application and mempool admission.

Validation run from detached PR head b38d21c9e615:

git diff --check origin/main...HEAD -- node/utxo_db.py node/test_utxo_db.py
/tmp/rustchain-bounty-review-venv/bin/python -B -m py_compile node/utxo_db.py node/test_utxo_db.py
PYTHONPATH=node /tmp/rustchain-bounty-review-venv/bin/python -m pytest -q node/test_utxo_db.py

Result: 73 passed, 13 subtests passed in 0.37s.

I also ran a runtime probe over the malformed spend-input cases this patch is meant to harden: inputs=None, a string instead of a list, a dict missing box_id, blank/whitespace box_id, a non-dict list entry, and duplicate use of the same real box_id. For every case:

  • apply_transaction() returned False without raising.
  • mempool_add() returned False without raising.
  • Alice’s balance stayed at 100 * UNIT, Bob stayed at 0.
  • The real input box was not marked as a mempool double-spend.
  • mempool_get_block_candidates() stayed empty.

The shared _normalize_spend_inputs() path now makes the duplicate and malformed-input checks consistent before either apply-time balance mutation or mempool input locking. I do not see a blocking issue in this patch.

@HCIE2054 HCIE2054 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.

LGTM!

@ZacharyZhang-NY ZacharyZhang-NY 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.

Reviewed PR #5454 at head b38d21c.

Validation performed:

  • Checked rustchain-bounties #2819. The scoped category includes UTXO code-quality validation hardening and mempool DoS prevention.
  • git fetch origin pull/5454/head:review-pr-5454 --force
  • git diff --check origin/main...review-pr-5454 -- node/utxo_db.py node/test_utxo_db.py -> passed.
  • python -m py_compile node/utxo_db.py node/test_utxo_db.py on an extracted PR-head tree -> passed.
  • python -B node/test_utxo_db.py on the extracted PR-head tree -> 73 tests passed.
  • python -m pytest node/test_utxo_db.py -q --confcutdir= -> 73 passed.

The shared _normalize_spend_inputs() path now validates spend input shape before both transaction application and mempool admission. The new coverage confirms malformed input containers, non-dict entries, missing or blank box_id values, and duplicate box_id values fail validation without applying balance changes or claiming mempool inputs. Approving.

@HCIE2054 HCIE2054 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.

LGTM!

@HCIE2054 HCIE2054 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.

LGTM!

@ZacharyZhang-NY ZacharyZhang-NY 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.

Reviewed PR #5454 at head b38d21c.

Validation performed:

  • Checked rustchain-bounties#2819. This is a UTXO spend-input validation hardening finding covering malformed inputs shapes at both apply_transaction() and mempool_add() boundaries.
  • git fetch origin pull/5454/head:review-pr-5454 --force
  • git diff --check origin/main...review-pr-5454 -- node/utxo_db.py node/test_utxo_db.py -> passed.
  • python -m py_compile node/utxo_db.py node/test_utxo_db.py on an extracted PR-head tree -> passed.
  • From the extracted PR-head node/ directory: python -B -m unittest test_utxo_db.TestUtxoDB.test_malformed_spend_inputs_rejected_without_exception test_utxo_db.TestUtxoDB.test_mempool_rejects_malformed_spend_inputs_without_locking -v -> 2 passed.
  • From the extracted PR-head node/ directory: python -B test_utxo_db.py -> 73 passed.
  • python test_bcos_spdx_check.py on the extracted PR-head tree -> passed.

Malformed spend inputs now fail closed before raw indexing or mempool UTXO claiming, and duplicate box_id rejection is shared across transaction application and mempool admission. Approving.

@HCIE2054 HCIE2054 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.

LGTM!

@HCIE2054 HCIE2054 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.

LGTM!

@HCIE2054 HCIE2054 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.

LGTM!

@508704820 508704820 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.

Reject malformed UTXO spend inputs. HIGH VALUE - UTXO validation is core to chain integrity. Verify: (1) Malformed inputs are rejected before any state mutation. (2) Input format validation covers all UTXO types. (3) Double-spend of malformed inputs is impossible. - Xeophon (security review, UTXO)

@HCIE2054 HCIE2054 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.

LGTM!

@HCIE2054 HCIE2054 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.

LGTM!

@508704820

Copy link
Copy Markdown
Contributor

Security Review ✅ IMPORTANT

UTXO spend-input validation gap fix. Before this patch, malformed inputs could crash the node:

  • inputs=None or string → raise instead of validation error
  • non-dict input entries → raise on indexing
  • missing/blank/whitespace-only box_id → raise on DB lookup

These are DoS vectors — attacker sends malformed transaction and the node crashes instead of rejecting it. Fix adds proper type checking and validation before any DB operations.

Reviewed by Xeophon - Solana: Lt9nERv6VHsojw15LpFeiaabuphAggzfLF9sM9UXRrZ

@HCIE2054 HCIE2054 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.

LGTM!

@HCIE2054 HCIE2054 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.

LGTM!

@jaxint jaxint 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.

LGTM! Great work on this PR. 🚀

@guangningsun

Copy link
Copy Markdown
Contributor

PR Review — PR #5454

Title: [UTXO-BUG] Reject malformed UTXO spend inputs

Author: galanime

What I reviewed

  • node/utxo_db.py
  • node/test_utxo_db.py

Observations

  1. Adds validation for spend inputs in the UTXO database — 25 additions and 3 deletions in utxo_db.py.
  2. The fix adds _normalize_spend_inputs() to properly validate and normalize spend inputs before processing.
  3. The new test in test_utxo_db.py adds 55 lines of regression coverage for malformed input rejection.
  4. This is another important consensus/validation fix preventing invalid transactions from entering the mempool.

LGTM pending CI. Good validation fix with appropriate test coverage. Input validation is critical for UTXO security.

Bounty: #2782
Disclosure: I received RTC compensation for this review.

@HCIE2054 HCIE2054 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.

LGTM!

@Scottcjn

Copy link
Copy Markdown
Owner

Closing as stale branch — would cause destructive deletions if merged.

Your branch is 155 commits behind current main. Filed during the May 11-13 contributor burst, the codebase has since moved substantially. GitHub's mergeable=clean indicator doesn't detect this; only an explicit git diff main..your-branch --shortstat does.

Bounty credit acknowledged where applicable

Most of the canonical fixes from your work-period have already shipped via other contributors' parallel PRs that landed earlier this week. Specific cases credited via the Codex audit batches:

If you want fresh review

Rebase against current main and verify your diff matches the size of your original changes:

git fetch upstream main
git rebase upstream/main
git diff main..HEAD --shortstat

If the deletion count is much higher than what you intended, the branch is still picking up stale assumptions — recreate from a fresh main.

Thanks for the contribution work.

@Scottcjn Scottcjn closed this May 18, 2026
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) node Node server related size/M PR: 51-200 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants