omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] Harden backend/database/locked_content.py against boolean limits, empty updates, invalid UIDs, and stream errors ($50 proposed)

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

Summary

backend/database/locked_content.py provides the shared bounded unlock helper (unlock_query_rows) and paid-lock existence probe (has_recoverable_locked_content), but currently has several input validation and resilience gaps:

  1. Boolean and non-integer limit bypasses in unlock_query_rows:
    • if not 1 <= limit <= 499: allows limit=True because isinstance(True, int) is True and 1 <= True <= 499 evaluates to True, passing True into query.limit(True). Non-integer values such as floats (1.5) also pass the range check or raise TypeError instead of ValueError.
  2. Unvalidated empty or non-dict update payloads in unlock_query_rows:
    • Passing an empty dict {} or non-dict update streams query results before failing at Firestore batch commit time instead of failing fast with ValueError.
    • Streamed items without a valid .reference attribute are not skipped before batch.update.
  3. Unvalidated uid and unhandled collection stream failures in has_recoverable_locked_content:
    • has_recoverable_locked_content(uid) passes uid directly to db.collection('users').document(uid) and MemoryCollections(uid=uid) without stripping whitespace or rejecting empty/whitespace-only strings, /, \, .., control characters, or overlong IDs (> 128 chars).
    • A transient Firestore stream failure on one collection aborts the entire probe instead of logging a warning and checking the remaining collections.
    • Does not accept an optional firestore_client keyword argument for hermetic testing.

Proposed Fix

  • Reject boolean and non-integer limit values (isinstance(limit, bool) or not isinstance(limit, int)) and empty/non-dict update payloads in unlock_query_rows, and skip streamed snapshots lacking a .reference.
  • Sanitize uid in has_recoverable_locked_content, support optional firestore_client injection (preserving db fallback), and wrap each collection probe in a try/except warning boundary.
  • Add 22 hermetic unit tests in backend/tests/unit/test_locked_content_resilience.py.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Implemented in #22884. Payout address: 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af (Base Mainnet USDC).

Each failure reproduced against the current main:

from database.locked_content import unlock_query_rows, has_recoverable_locked_content

q = <query with 2 rows>
unlock_query_rows(db, q, {"is_locked": False}, limit=True)   # query.limit() receives True
unlock_query_rows(db, q, {"is_locked": False}, limit=1.5)    # query.limit() receives 1.5
unlock_query_rows(db, q, {"is_locked": False}, limit="10")   # TypeError: '<=' not supported between 'int' and 'str'
unlock_query_rows(db, q, {})                                  # streams both rows, then ValueError at batch.update
has_recoverable_locked_content("")                            # ValueError: Invalid document path: ''

The empty-update case is the one that costs work: the query streams completely before the write is rejected, and when the caller has already queued earlier batches in the same run those commits are not rolled back.

One streamed row without .reference currently aborts the loop with AttributeError after earlier batches in the same call have committed; #22884 skips it.

#22884 also sanitizes uid before it reaches document() / MemoryCollections(uid=...), adds an optional firestore_client= for hermetic testing, and wraps each collection probe in its own warning boundary so a transient stream failure on one collection no longer aborts the whole probe.

Tests: 34 hermetic cases in backend/tests/unit/test_locked_content_resilience.py, verified locally with pytest.

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