Skip to content

fix: correct replay defense status logic and test isolation - #1932

Merged
Scottcjn merged 1 commit into
Scottcjn:mainfrom
createkr:fix/issue2640-replay-defense-status-logic
Mar 31, 2026
Merged

fix: correct replay defense status logic and test isolation#1932
Scottcjn merged 1 commit into
Scottcjn:mainfrom
createkr:fix/issue2640-replay-defense-status-logic

Conversation

@createkr

Copy link
Copy Markdown
Contributor

This PR fixes the replay defense logic issues behind rustchain-bounties#2640.

What is included:

  • correct replay-defense status handling for nonce collision and related cases
  • dynamic database path resolution at call time instead of import-time caching
  • replay-defense test isolation fixes so the targeted replay test suite can run together cleanly
  • updated replay-related tests and a concise verification note

Validation performed:

python3 -m pytest tests/test_replay_defense.py tests/test_replay_defense_standalone.py tests/test_replay_bounty.py tests/test_fingerprint_replay.py tests/test_signed_transfer_replay.py --tb=short

Result:

  • 74 passed
  • 0 failed

RTC payout wallet: RTC1d48d848a5aa5ecf2c5f01aa5fb64837daaf2f35

@github-actions github-actions Bot added documentation Improvements or additions to documentation BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related tests Test suite changes size/M PR: 51-200 lines labels Mar 28, 2026
@Scottcjn

Copy link
Copy Markdown
Owner

@createkr — Bounty #2640 was already fulfilled by #1951, but your fix has additional improvements to the replay defense logic (26 line changes to hardware_fingerprint_replay.py). Would you like to resubmit as a follow-up fix PR? We'll review and pay for the incremental improvements separately.

@createkr

Copy link
Copy Markdown
Contributor Author

Understood. I am happy to treat this as a follow-up fix PR rather than the original bounty claim. The main incremental changes here are the replay-defense logic improvements in , along with the related test adjustments to validate the behavior cleanly. Please review it as a follow-up improvement set. RTC payout wallet: RTC1d48d848a5aa5ecf2c5f01aa5fb64837daaf2f35

@createkr
createkr force-pushed the fix/issue2640-replay-defense-status-logic branch from e7719d7 to 0878914 Compare March 29, 2026 01:27

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

Thorough Code Review

PR Summary

Title: fix: correct replay defense status logic and test isolation
Changes: +216/-55 lines across 6 files (including progress doc)

Architecture Review

Problem Addressed:
Test isolation failures when running multiple test files together due to module-level DB_PATH constant being evaluated at import time rather than call time.

Solution:
Convert DB_PATH constant to get_db_path() function that reads environment variables at call time.

Code Quality Assessment

Strengths:

  1. Clean Architecture Fix:

    • Dynamic database path resolution ensures proper test isolation
    • get_db_path() function evaluates environment at call time
    • Tests can now set DB_PATH independently without import-order race conditions
  2. Test Isolation Improvements:

    • autouse=True fixtures ensure fresh database for each test
    • Environment variables set at test runtime, not import time
    • Cleanup simplified (no more unique DB files per test)
  3. Edge Case Handling:

    • compute_fingerprint_hash() now correctly handles:
      • None input -> ""
      • Non-dict input -> ""
      • Empty dict {} -> valid SHA-256 hash
  4. Documentation:

    • Comprehensive ISSUE_2640_PROGRESS.md with:
      • Test results (74 passed)
      • Bounty requirements verification
      • Attack vectors defended
      • Technical notes on database path resolution

Observations:

  1. Lines 118-125 (hash function):

    • Split None and type checks for clarity
    • Defensive coding pattern: early returns for invalid inputs
    • Correct: Empty dict now produces valid hash (was inconsistent before)
  2. Test fixture changes:

    • Removed unique DB file per test (was causing file locking issues)
    • Uses single test DB path with proper cleanup
    • autouse=True ensures fixture runs automatically
  3. Security Logic:

    • Replay defense checks unchanged (status logic fix only)
    • 5-minute replay window maintained
    • Rate limiting (10/hour) maintained
    • Entropy collision detection maintained

Security Review:

  • No new attack vectors introduced
  • Replay defense logic preserved
  • Test isolation does not weaken production security

Test Coverage

Test File Tests Status
test_replay_defense.py 31 PASS
test_replay_defense_standalone.py 16 PASS
test_replay_bounty.py 4 PASS
test_fingerprint_replay.py 21 PASS
test_signed_transfer_replay.py 2 PASS
Total 74 PASS

Bounty Requirements Verification

All three requirements satisfied:

  1. Replayed fingerprint rejected
  2. Fresh fingerprint accepted
  3. Modified replay (changed nonce, old data) rejected

Recommendation

Approve - This is a well-executed fix that:

  • Addresses the root cause of test isolation failures
  • Maintains all security guarantees
  • Provides comprehensive documentation
  • Passes all 74 tests

Assessment: Thorough review - substantial fix with proper test coverage and documentation.


Code review per Bounty #73

@Scottcjn
Scottcjn merged commit fbfa772 into Scottcjn:main Mar 31, 2026
6 of 8 checks passed
@Scottcjn

Copy link
Copy Markdown
Owner

Merged — clean fix. Thanks @createkr.

Repository owner deleted a comment from yatescleta-afk Mar 31, 2026
Repository owner deleted a comment from yatescleta-afk Mar 31, 2026
@Scottcjn

Scottcjn commented Apr 2, 2026

Copy link
Copy Markdown
Owner

Transfer confirmed — this was included in the batch settlement of 1,091 RTC to @createkr's wallet.

@FlintLeng

Copy link
Copy Markdown
Contributor

Code Review — PR #1932

Reviewer: FlintLeng

✅ LGTM

— FlintLeng

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

PR #1932 — Review (issue #2640):

Replay Defense Fixes — clean submission pass. Comprehensive test suite covering replay defense, fingerprint replay, and signed transfer replay. pytest command documented for verification. Well-structured security fix submission. ✅

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) documentation Improvements or additions to documentation node Node server related size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants