omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] Harden backend/database/dream_history.py against invalid UIDs, unsafe cursor keys, boolean limits, and malformed aggregations ($50 proposed)

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

Summary

backend/database/dream_history.py provides owner-scoped history page queries (history_collection, history_page, history_count, history_from_key) and review state batch reads (review_id, review_states), but currently has several input validation and result-parsing bugs:

  1. Unvalidated uid in history_collection and review_states:
    • Passes uid directly to database.collection('users').document(uid) and database.collection('dream_users').document(uid) without stripping whitespace or rejecting empty/whitespace-only strings, /, \, .., control characters, or IDs > 128 chars.
  2. Unsafe after and key cursors and boolean/non-integer limit in history_page and history_from_key:
    • history_page does not validate after (allowing /, \, .., control characters, or whitespace-only cursors into source.document(after)) and accepts boolean limit=True/False or non-integer values in max(1, min(limit, 40)).
    • history_from_key only checks if not key or '/' in key, allowing non-string keys, whitespace-only keys, \, .., control characters, or overlong keys (> 128 chars).
  3. Uncaught IndexError/TypeError/AttributeError in history_count, review_id, and review_states:
    • history_count accesses count().get(timeout=3)[0][0].value without guarding against empty aggregation results ([], [[]]) or non-integer/boolean .value.
    • review_id(ref) crashes with AttributeError on non-string ref and accepts blank strings; review_states iterates over a raw str if passed instead of a sequence of refs, and crashes if snapshot.to_dict() returns a non-dict object or an unexpected snapshot.id.

Proposed Fix

  • Add _sanitize_uid, _sanitize_doc_key, and _sanitize_limit to validate uid, after/key cursors, and integer limit bounds.
  • Guard history_count against empty or malformed aggregation rows, returning 0 safely.
  • Validate ref and refs in review_id and review_states, and guard snapshot.to_dict() with isinstance(..., dict).
  • Add 47 hermetic unit tests in backend/tests/unit/test_dream_history_resilience.py.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Implemented in #22899: owner/cursor/key validation, limit normalization, count and review-reference hardening, hermetic tests

Bounty settlement address (Base Mainnet USDC): 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af

Andu1223 avatar Oct 20 '26 16:10 Andu1223

Hi! I'm interested in contributing to this bounty. I noticed that PR #22901 has already been mentioned for this issue. Is the bounty still open for additional contributions, or has the existing PR already covered all the requirements? I'd like to confirm before starting work. Thanks!

Nikhil1869 avatar Oct 20 '26 18:10 Nikhil1869

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