[SECURITY] Non-finite and garbage weights pass the sybil-guard money chain — enrollment_weight has no isfinite guard and epoch_weight_to_units raises on NaN/inf

Open
#8,619 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 10 days

@thinhnx128 is already working on this.

Since Nov 6, 2026.

  • #8621 by @thinhnx128 — open
  • #8624 by @Scottcjn — open
  • #8707 by @tedTorus — open
  • #8749 by @tedTorus — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend, security

Research direction

Read node/sybil_guard.py lines 385-419, node/rustchain_v2_integrated_v2.2.1_rip200.py lines 1539-1548, and the enrollment and settlement call sites listed in the issue. Trace how invalid weights reach unit conversion and the settlement cap, then add regression coverage for None, strings, NaN, infinity, and very large finite values. Done means these inputs are handled safely through enrollment and settlement without uncaught errors or skipped caps.

Written by the indexing model from the issue text.

Description

bug
Component

Node (rustchain_v2_integrated)

What happened?

Reporter: @elvergon8888
Native RTC wallet: RTC36c07693911c7f3b0a7ede4d2c92a08f32e7edcb
Registered at: https://github.com/Scottcjn/rustchain-bounties/issues/17178
Program: [BOUNTY] Ongoing Bug Bounty Program #71 (rustchain-bounties)
Files affected (main, 2026-11-05):

  • node/sybil_guard.py:385-419 (enrollment_weight)
  • node/rustchain_v2_integrated_v2.2.1_rip200.py:1539-1548 (epoch_weight_to_units)
  • Uncaught call sites: node/rustchain_v2_integrated_v2.2.1_rip200.py:7097, 7115-7119, 7493 (attestation enroll + settlement escrow)
  • Settlement cap comparison: node/rustchain_v2_integrated_v2.2.1_rip200.py ~lines 5707-5712

Summary
The money-weight chain between enrollment and settlement has no non-finite guard. Two related defects in the same payment path:

  1. enrollment_weight() (node/sybil_guard.py:385-419) documents its contract as "Never raises. Apply the guard ... max() only ... min(weight, PROBATION_WEIGHT_CAP)". The implementation violates it:

    • any non-coercible hw_weight (e.g. None, "N/A") takes the except (TypeError, ValueError): return hw_weight branch and returns the RAW, uncapped value to the caller (code lines 398-401);
    • float("nan") / float("inf") parse without exception, and no math.isfinite check follows, so NaN and Inf degrade grandfathered and trusted states to unguarded, uncapped weights (lines 402-419 have no isfinite).
      Real call site (node, ~4821-4828): return sybil_guard.enrollment_weight(adjusted, probation_status) where adjusted feeds directly into the epoch-enroll weight write.
  2. epoch_weight_to_units() (node/rustchain_v2_integrated_v2.2.1_rip200.py:1539-1548) is the units converter used by every enroll weight write:

    • Decimal(str(float("nan"))) builds Decimal("NaN"), then value <= 0 evaluates False (NaN is not <= 0), so the positive-side path runs and int() raises decimal.InvalidOperation — an unhandled 500 on the enroll path (call sites are without try/except: nan/inf mode at node:7095-7110 with branch if/else);
    • Decimal(str(float("inf"))) -> OverflowError (same path);
    • huge-but-finite weights (1e308) turn into 317-digit integer units stored into epoch_enroll (node:7109), only capped later at settlement (~5707-5712);
    • the settlement cap check (if weight > MAX_EPOCH_WEIGHT_UNITS) is a float comparison that evaluates FALSE for NaN — cap skipped for NaN-weighted rows.

Impact (requesting Low/Medium, 5-30 RTC per #71)

  • Attacker-influenced temporal metrics reach this chain directly: the module's own docstring acknowledges every temporal metric is "self-reported JSON" (node/sybil_guard.py:2-53). Inflation of rotation_eval (hw_weight * rotation_eval["active_ratio"], node:7097, 7493) with hostile temporal-profile parameters can push weight to non-finite without any isfinite guard in flight.
  • Local DoS with reward-path effect: a crashed enrollment (InvalidOperation/OverflowError) leaves an epoch stuck for that miner's enroll write and any hostile wallet can repeat the same crash once per epoch — line 7095-7110 has no try/except wrapper; and epoch_weight_to_units handling before 400 is absent.
  • Defense-in-depth break in the money path specifically on the units converter and cap-comparison semantics (NaN skips the capped value at settlement — NaN > MAX_EPOCH_WEIGHT_UNITS is False).
  • Contract violation on the coin-new-miner gate may allow an unguarded grandfathered miner to attain weights above PROBATION_WEIGHT_CAP without triggering the measurement-binding treatment intended for it (REQUIRE_MEASUREMENT_BINDING_FOR_NEW_PREMIUM=False on line 116 lets trusted-bound miners bypass the cap).

Reproduction (offline, code-exact, Executed locally on cpython-3.14.10 (Termux); PoC code below is verbatim from main)

PoC 1 (sybil_guard.enrollment_weight, guard-contract violations):

  • probation + hw_weight=None -> returns None (no cap applied, NOT 0.0)
  • probation + hw_weight="N/A" -> returns "N/A" (guard bypass; raw value reaches enroll layer)
  • grandfathered + hw_weight=NaN -> returns nan
  • trusted + hw_weight=+inf -> returns inf

PoC 2 (epoch_weight_to_units, the units converter on the enroll money path):

  • hw_weight=NaN -> decimal.InvalidOperation (unhandled in caller, 500)
  • hw_weight=+inf -> OverflowError: cannot convert Infinity to integer
  • hw_weight=1e308 -> accepted, 1e317-unit integer stored to epoch_enroll

PoC 3 (cap comparison used at settlement):

  • nan > MAX_EPOCH_WEIGHT_UNITS -> False (cap skipped)

Suggested fix

  1. In enrollment_weight (node/sybil_guard.py:398-401): replace the raw return with a guarded floor (min(0 or min(cap, weight), cap) semantics mirroring fail-closed decisions elsewhere in the file), and add a math.isfinite(weight) check in the same branch.
  2. In epoch_weight_to_units (node:1539-1548): reject non-finite weights up front the way payout_preflight._safe_decimal (node/payout_preflight.py:35-42) already does with amount.is_finite() — same guard the codebase deployed to withdrawal validation in #5728.
  3. Wrap node:7095-7122 (enroll weight write) in try/except and return a canonical 400/503 error consistent with the already-fixed node/sophia_elya_service.py paths (#6028) instead of an unhandled exception.
  4. Add NaN-guard before the settle cap comparison at ~5707 so NaN cannot skip the clamp.
  5. Add regression tests exercising non-finite + non-coercible weights through enrollment_weight and epoch_weight_to_units (same style as #6033's ark).

Duplicates check (verified 2026-11-05 via repo issues search)
#5728 (withdrawals), #6028/#6033 (ELYA /epoch/enroll tickets), #5361 (bridge NaN), #6466/#7879 (SPV bloom size) — all same CLASS on other routes. The attestation-enroll weight chain (node:7095-7122) and the sybil-guard gate itself (node/sybil_guard.py:385-419) remain unpatched on main as of 2026-11-05, demonstrated by the PoC.

Evidence
PoC (all cases verbatim from main; pasted below). Executed locally on Python 3.14.10.

--- PoC code (poc.py, runs as-is; stdlib only) ---
[Full PoC attached as poc.py — verbatim from repo functions + 3 assertion groups]

--- PoC stdout ---

[OK     ] C1 probation + hw_weight=None      -> None
[OK     ] C2 probation + hw_weight=str       -> 'N/A'
[OK     ] C3 grandfathered + hw_weight=NaN   -> nan
[OK     ] C4 trusted + hw_weight=+inf        -> inf
[RAISED ] C5 hw_weight=NaN                   -> InvalidOperation: [<class 'decimal.InvalidOperation'>]
[RAISED ] C6 hw_weight=+inf                  -> OverflowError: cannot convert Infinity to integer
[OK     ] C7 hw_weight=1e308                 -> 100000000000000000000000...
NaN > MAX_EPOCH_WEIGHT_UNITS  ->  False   (cap SKIPPED)

### Expected behavior

The system should handle non-finite and non-coercible values safely without crashing the node or bypassing the settlement cap.

1) enrollment_weight() should always return a finite float (e.g., 0.0 or capped at PROBATION_WEIGHT_CAP) when receiving None, strings, NaN, or Inf, rather than returning the raw uncapped value.
2) epoch_weight_to_units() should explicitly reject NaN/Inf values (returning 0 or raising a handled ValueError) instead of throwing an unhandled decimal.InvalidOperation or OverflowError that leads to a 500 error.
3) The settlement cap comparison should properly clamp or reject NaN-weighted rows, preventing them from skipping the cap.

[poc.py](https://github.com/user-attachments/files/34458468/poc.py)

### Steps to reproduce

1. Clone the repository at main (v2.2.1).
2. Use the attached poc.py or copy the verbatim functions `enrollment_weight` (sybil_guard.py:385-419) and `epoch_weight_to_units` (rustchain_v2_integrated...py:1539-1548).
3. Execute the functions with non-finite values (NaN, Inf) or non-coercible types (None, "N/A").
4. Observe that enrollment_weight returns the raw uncapped value instead of a safe float.
5. Observe that epoch_weight_to_units raises unhandled InvalidOperation/OverflowError, leading to an HTTP 500 in the enrollment path.

### Version / Commit

main (v2.2.1-rip200)

### Operating System

Other

### Relevant logs

```shell
[OK     ] C1 probation + hw_weight=None      -> None
[OK     ] C2 probation + hw_weight=str       -> 'N/A'
[OK     ] C3 grandfathered + hw_weight=NaN   -> nan
[OK     ] C4 trusted + hw_weight=+inf        -> inf
[RAISED ] C5 hw_weight=NaN                   -> InvalidOperation: [<class 'decimal.InvalidOperation'>]
[RAISED ] C6 hw_weight=+inf                  -> OverflowError: cannot convert Infinity to integer
[OK     ] C7 hw_weight=1e308                 -> 100000000000000000000000...
NaN > MAX_EPOCH_WEIGHT_UNITS  ->  False   (cap SKIPPED)
Dominant language
Python
Stars
850
Forks
589
Avg merge
11d 7h
Merged PRs (30d)
29

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Scottcjn/Rustchain

All issues in Scottcjn/Rustchain

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.