omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] Harden backend/database/review_exact_undo.py against non-dict intermediate paths, None generations, and malformed fences ($50 proposed)

Open huynhnhatquandongnai-source opened this issue 1 month ago • 3 comments

Summary

backend/database/review_exact_undo.py computes and verifies reversible field-level Undo/Redo fences (patched_row, restored_row, journal_fences, validate_generation, validate_row, witness_snapshot), but currently crashes on several real document edge cases:

  1. Uncaught TypeError / AttributeError in patched_row and restored_row when an intermediate dotted path segment is None or non-dict:
    • Both functions traverse dotted paths using parent = parent.setdefault(part, {}). When a key like 'promotion' already exists in row with a value of None or a scalar ({'promotion': None}), parent.setdefault('promotion', {}) returns None, and the subsequent parent[parts[-1]] = ... or parent.pop(parts[-1], None) raises TypeError: 'NoneType' object does not support item assignment or AttributeError: 'NoneType' object has no attribute 'pop'.
  2. Uncaught TypeError / ValueError / AttributeError on account_generation in journal_fences and validate_generation:
    • int((raw or {}).get('account_generation', 0)) in journal_fences and int(current.get('account_generation', 0)) in validate_generation raise TypeError when account_generation is explicitly None ({'account_generation': None}), ValueError on non-numeric strings or NaN/inf, and AttributeError when snapshot.to_dict() returns a non-dict object.
    • validate_generation also raises ValueError: not enough values to unpack if a generation_fences key lacks '/'.
  3. Uncaught KeyError / TypeError in validate_row and witness_snapshot:
    • validate_row crashes if journal is not a dict, if reversal_fences contains non-dict entries or entries missing 'collection'/'document_id', or if expected is absent from fence.
    • witness_snapshot crashes if path is not a string or raw is not a dict when passed to original_fingerprint.

Proposed Fix

  • Harden patched_row and restored_row so non-dict row inputs and None/primitive intermediate parents are safely handled without raising TypeError or AttributeError.
  • Add _coerce_generation to safely normalize account_generation values (handling None, booleans, NaN/inf, negative numbers, and strings) in journal_fences and validate_generation.
  • Guard validate_generation, validate_row, and witness_snapshot against non-dict snapshots and malformed journal structures.
  • Add 22 hermetic unit tests in backend/tests/unit/test_review_exact_undo_resilience.py.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Implemented in #22880.

Minimal reproductions for each crash class — all of these raise out of the fence today, so the whole undo transaction aborts before it can record anything:

from database.review_exact_undo import patched_row, journal_fences, validate_generation, validate_row

# 1. None intermediate parent
patched_row({'promotion': None}, {'promotion.id': 5})
# TypeError: 'NoneType' object does not support item assignment

# 2. None / non-finite account_generation
journal_fences([], [], {'task_intelligence_control/state': {'account_generation': None}})
# TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

# 3. fence key without a collection separator
validate_generation('u', None, {'generation_fences': {'no-slash': 3}})
# ValueError: not enough values to unpack (expected 2, got 1)

# 4. malformed reversal fence
validate_row({'reversal_fences': ['junk']}, 'action_items', 'a1', {}, 'before')
# TypeError / KeyError depending on the malformed entry

#22880 normalises each of these fail-closed:

  • _coerce_generation maps None, booleans, NaN/inf, non-numeric strings and negatives to 0.
  • _apply_dotted materialises a dict for a None/scalar intermediate parent before traversal.
  • validate_generation skips fence keys without a usable collection/document split.
  • validate_row skips malformed fence entries and tolerates a non-list fields, while still raising ReviewConflict for a genuinely newer edit or a missing current row.

Tests: 36 hermetic cases in backend/tests/unit/test_review_exact_undo_resilience.py. Locally verified with a standalone harness that executes the real module against stubbed database.review_store / google.cloud.firestore: 51/51 assertions pass.

Andu1223 avatar Oct 20 '26 15:10 Andu1223

@Git-on-my-level Payout address for #22880, covering the review_exact_undo fence hardening:

Base Mainnet USDC: 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af

Andu1223 avatar Oct 20 '26 15:10 Andu1223

Thanks for putting this together. We aren't doing bounties at the moment, so we're closing the bounty proposal issues for now — thanks for the fix! 🙏

undivisible avatar Oct 22 '26 11:10 undivisible