Repository navigation
[UTXO-BUG] Reject non-finite JSON output metadata - #8171
Conversation
|
Welcome to RustChain! Thanks for your first pull request. Before we review, please make sure:
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! |
|
Checklist update: the PR already has the |
lequangsang01
left a comment
There was a problem hiding this comment.
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
ValueErroronNaN/Infinitycorrectly triggers the existingexcept (TypeError, ValueError):handler in_normalize_outputs, causing_normalize_outputsto returnNoneand 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_jsonorregisters_jsoncontaining[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) andapply_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
|
This PR has been inactive for 21 days. It will be closed in 14 days unless updated. |
|
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. |
Scottcjn
left a comment
There was a problem hiding this comment.
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).
RTC RewardThis merged PR earned 5 RTC — sent to |
Summary
NaN,Infinity, and-InfinityJSON constants in UTXOtokens_jsonandregisters_jsonFinding
Python's
json.loads()accepts non-standard numeric constants by default. Before this change, output metadata such astokens_json='[NaN]'orregisters_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:The fix uses
parse_constantto 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 passedPYTHONPATH=node python3 -m pytest node/test_utxo_db.py -q-> 105 passed, 34 subtests passedpython3 -m py_compile node/utxo_db.py node/test_utxo_db.py-> passedgit diff --check-> passedpython3 tools/bcos_spdx_check.py --base-ref origin/main-> BCOS SPDX check: OK