[Bounty proposal] Harden backend/database/locked_content.py against boolean limits, empty updates, invalid UIDs, and stream errors ($50 proposed)
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:
- Boolean and non-integer
limitbypasses inunlock_query_rows:if not 1 <= limit <= 499:allowslimit=Truebecauseisinstance(True, int)isTrueand1 <= True <= 499evaluates toTrue, passingTrueintoquery.limit(True). Non-integer values such as floats (1.5) also pass the range check or raiseTypeErrorinstead ofValueError.
- Unvalidated empty or non-dict
updatepayloads inunlock_query_rows:- Passing an empty dict
{}or non-dictupdatestreams query results before failing at Firestore batch commit time instead of failing fast withValueError. - Streamed items without a valid
.referenceattribute are not skipped beforebatch.update.
- Passing an empty dict
- Unvalidated
uidand unhandled collection stream failures inhas_recoverable_locked_content:has_recoverable_locked_content(uid)passesuiddirectly todb.collection('users').document(uid)andMemoryCollections(uid=uid)without stripping whitespace or rejecting empty/whitespace-only strings,/,\,.., control characters, or overlong IDs (> 128chars).- 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_clientkeyword argument for hermetic testing.
Proposed Fix
- Reject boolean and non-integer
limitvalues (isinstance(limit, bool) or not isinstance(limit, int)) and empty/non-dictupdatepayloads inunlock_query_rows, and skip streamed snapshots lacking a.reference. - Sanitize
uidinhas_recoverable_locked_content, support optionalfirestore_clientinjection (preservingdbfallback), and wrap each collection probe in atry/exceptwarning 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.
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! 🙏