[Bounty proposal] Harden backend/database/dream_history.py against invalid UIDs, unsafe cursor keys, boolean limits, and malformed aggregations ($50 proposed)
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:
- Unvalidated
uidinhistory_collectionandreview_states:- Passes
uiddirectly todatabase.collection('users').document(uid)anddatabase.collection('dream_users').document(uid)without stripping whitespace or rejecting empty/whitespace-only strings,/,\,.., control characters, or IDs> 128chars.
- Passes
- Unsafe
afterandkeycursors and boolean/non-integerlimitinhistory_pageandhistory_from_key:history_pagedoes not validateafter(allowing/,\,.., control characters, or whitespace-only cursors intosource.document(after)) and accepts booleanlimit=True/Falseor non-integer values inmax(1, min(limit, 40)).history_from_keyonly checksif not key or '/' in key, allowing non-string keys, whitespace-only keys,\,.., control characters, or overlong keys (> 128chars).
- Uncaught
IndexError/TypeError/AttributeErrorinhistory_count,review_id, andreview_states:history_countaccessescount().get(timeout=3)[0][0].valuewithout guarding against empty aggregation results ([],[[]]) or non-integer/boolean.value.review_id(ref)crashes withAttributeErroron non-stringrefand accepts blank strings;review_statesiterates over a rawstrif passed instead of a sequence of refs, and crashes ifsnapshot.to_dict()returns a non-dict object or an unexpectedsnapshot.id.
Proposed Fix
- Add
_sanitize_uid,_sanitize_doc_key, and_sanitize_limitto validateuid,after/keycursors, and integerlimitbounds. - Guard
history_countagainst empty or malformed aggregation rows, returning0safely. - Validate
refandrefsinreview_idandreview_states, and guardsnapshot.to_dict()withisinstance(..., 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
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!
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! 🙏