Skip to content

[UTXO-BUG] Reject non-finite JSON output metadata - #8171

Merged
Scottcjn merged 1 commit into
Scottcjn:mainfrom
2lll5:fix/reject-nonfinite-utxo-metadata
Sep 7, 2026
Merged

Scottcjn merged 1 commit into
Scottcjn:mainfrom
2lll5:fix/reject-nonfinite-utxo-metadata

Conversation

@2lll5

@2lll5 2lll5 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • reject Python's non-standard NaN, Infinity, and -Infinity JSON constants in UTXO tokens_json and registers_json
  • enforce the same strict metadata validation in direct transaction application and mempool admission through the shared output normalizer
  • add regressions proving malformed metadata cannot mutate balances or reserve an input

Finding

Python's json.loads() accepts non-standard numeric constants by default. Before this change, output metadata such as tokens_json='[NaN]' or registers_json='{"R4": Infinity}' passed _normalize_outputs(). Such transactions could enter the mempool, reserve inputs, be returned as block candidates, and then be persisted in the UTXO set.

These values are not valid RFC 8259 JSON. Persisting them in consensus-visible metadata creates an interoperability hazard for strict JSON consumers and non-Python validators, which reject the same transaction/box data. The state root also commits to this invalid raw metadata.

Pre-fix reproduction on current main:

pre_fix_mempool_accepted= True
candidate_count= 1
pre_fix_apply_accepted= True

The fix uses parse_constant to fail closed while preserving valid token arrays and register objects.

Bounty: Scottcjn/rustchain-bounties#2819

Verification

  • PYTHONPATH=node python3 -m pytest node/test_utxo_db.py -q -k 'nonfinite_json_metadata' -> 2 passed, 3 subtests passed
  • PYTHONPATH=node python3 -m pytest node/test_utxo_db.py -q -> 105 passed, 34 subtests passed
  • python3 -m py_compile node/utxo_db.py node/test_utxo_db.py -> passed
  • git diff --check -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Non-doc PRs have a BCOS-L1 or BCOS-L2 label
  • Doc-only PRs are exempt from BCOS tier labels when they only touch docs/**, *.md, or common image/PDF files
  • New code files include an SPDX license header
  • You've tested your changes against the live node

Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150)

A maintainer will review your PR soon. Thanks for contributing!

@github-actions github-actions Bot added size/M PR: 51-200 lines 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 labels Aug 2, 2026
@2lll5

2lll5 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor Author

Checklist update: the PR already has the BCOS-L1/BCOS-L2 labels, modifies only existing SPDX-covered files, and the repository SPDX check passes. The malformed metadata path was reproduced on current main and verified after the fix through both mempool admission and direct transaction application; the full node/test_utxo_db.py suite passes (105 tests, 34 subtests). I have not claimed a live-node deployment test, since this validation change is not deployed until merged.

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

Code Review: Reject non-finite JSON output metadata (UTXO-BUG)

Summary

Python's built-in json.loads by default accepts non-standard IEEE 754 float constants (NaN, Infinity, -Infinity), which are invalid under standard RFC 8259 JSON and cause parsing crashes across Rust/Go/Node.js miners/nodes. This PR rejects non-finite constants at the _normalize_outputs layer in node/utxo_db.py using parse_constant.

Line-Level Analysis

1. node/utxo_db.py:124-126 — _reject_nonstandard_json_constant

def _reject_nonstandard_json_constant(value: str):
    """Reject Python's non-standard NaN/Infinity JSON extensions."""
    raise ValueError(f"non-standard JSON constant: {value}")
  • Analysis: Raising ValueError on NaN / Infinity correctly triggers the existing except (TypeError, ValueError): handler in _normalize_outputs, causing _normalize_outputs to return None and cleanly reject invalid transaction outputs without throwing an unhandled exception.

2. node/utxo_db.py:586-591 — parse_constant hook in json.loads

tokens = json.loads(tokens_json, parse_constant=_reject_nonstandard_json_constant)
registers = json.loads(registers_json, parse_constant=_reject_nonstandard_json_constant)
  • Analysis: Prevents mempool poisoning where an attacker submits malformed tokens_json or registers_json containing [NaN] or {"R4": -Infinity}.

3. node/test_utxo_db.py:1803-1858 — Unit Test Coverage

  • Tests both mempool_add (verifying input box is NOT double-spend locked on rejection) and apply_transaction (verifying state rollbacks correctly).

Verdict & Security Assessment

✅ LGTM — Clean, line-level security fix that enforces RFC 8259 JSON compliance and prevents cross-language UTXO deserialization consensus failures.

Wallet: RTCfe13452d122263caf633ab1876bd9631133b68b1

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been inactive for 21 days. It will be closed in 14 days unless updated.
Need help finishing? Ask in the PR comments — we're happy to assist!

@github-actions github-actions Bot added the stale Inactive — will close soon label Aug 24, 2026
@2lll5

2lll5 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Following up on this PR to keep it active.

Validation remains complete: node/test_utxo_db.py passed (105 tests, 34 subtests). Non-finite JSON constants are rejected on both the mempool admission and direct transaction application paths, and rejection rollback behavior was verified. Live-node deployment testing is not claimed because this change is not merged yet.

I also noted the review suggestion to add a +Infinity test case. Please schedule maintainer review so the PR is not closed for inactivity.

@github-actions github-actions Bot removed the stale Inactive — will close soon label Aug 31, 2026

@Scottcjn Scottcjn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed and verified locally: parse_constant rejects NaN/Infinity/-Infinity, and the except was correctly widened to (TypeError, ValueError) (which subsumes JSONDecodeError), so malformed metadata cleanly returns None instead of crashing. Applies to both direct apply and mempool admission. Ran the full suite: node/test_utxo_db.py = 105 passed, 34 subtests. Real consensus-metadata bug, clean fix. Bounty #2819 attribution to @2lll5 (payout is the maintainer's call).

@Scottcjn
Scottcjn merged commit c8c7869 into Scottcjn:main Sep 7, 2026
3 checks passed
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

RTC Reward

This merged PR earned 5 RTC — sent to 2lll5.

RustChain Bounty Program

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/M PR: 51-200 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants