[Bounty proposal] Harden backend/database/owner_profile_updates.py against invalid UIDs, non-finite embeddings, and malformed snapshots ($50 proposed)
Summary
backend/database/owner_profile_updates.py manages missing owner voiceprint recovery (get_user_speaker_embedding_recovery_state and recover_user_speaker_embedding) on the customer data plane, but currently lacks input validation, non-finite embedding guards, and malformed snapshot resilience:
- Unvalidated document IDs (
uid):get_user_speaker_embedding_recovery_state(uid)andrecover_user_speaker_embedding(uid, ...)passuiddirectly intocollection('users').document(uid)andaccount_deletion_document(uid)without stripping whitespace or rejecting empty/whitespace-only strings, path separators (/,\),..segments, control characters, or overlong IDs (> 128chars).
- Unvalidated / non-finite speaker embeddings:
recover_user_speaker_embeddingwritesembeddingdirectly to Firestore without validating that it is a non-empty list/tuple of finite numbers (allowing empty lists, booleans, non-numeric items, orNaN/inffloats to corrupt the user document'sspeaker_embeddingfield).
- Uncaught
AttributeErroron non-dict Firestore snapshots & unhandled read exceptions:- Both
get_user_speaker_embedding_recovery_stateandrecover_user_speaker_embeddingusedata = snapshot.to_dict() or {}, which raisesAttributeErrorondata.get(...)if a corrupted document snapshot returns a truthy non-dict object (such as a list or string). get_user_speaker_embedding_recovery_statedoes not catch transient Firestore read exceptions, propagating uncaught errors during opportunistic owner voiceprint recovery checks.
- Both
- Transaction execution & testability:
- Calls
repair(client.transaction())directly instead ofrun_transactional(client, repair)and does not accept an optionalfirestore_clientkeyword argument for hermetic testing.
- Calls
Proposed Fix
- Add
_sanitize_uid,_clean_optional_uid, and_validate_embeddingto validate and normalizeuidandembeddingvectors (coercing valid ints/floats tofloatand rejecting empty lists, booleans, andNaN/inf). - Guard
snapshot.to_dict()in both functions withisinstance(..., dict)checks and catch/log transient read failures inget_user_speaker_embedding_recovery_state. - Use
run_transactional(client, repair)and accept an optionalfirestore_clientkeyword argument in both functions while preserving backward compatibility withget_firestore_client(). - Add 29 hermetic unit tests in
backend/tests/unit/test_owner_profile_updates_resilience.py.
Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94
Implemented in #22883.
The three failure classes, each reproduced against the current main:
from database.owner_profile_updates import get_user_speaker_embedding_recovery_state, recover_user_speaker_embedding
# 1. truthy non-mapping snapshot -> AttributeError: 'list' object has no attribute 'get'
# (a stubbed snapshot whose to_dict() returns a list or a string)
get_user_speaker_embedding_recovery_state("u1")
# 2. blank uid -> ValueError: Invalid document path: '' (raised inside the SDK)
get_user_speaker_embedding_recovery_state("")
# 3. poisoned vectors are written and reported as success
recover_user_speaker_embedding("u1", [], expected_updated_at=None) # writes speaker_embedding: []
recover_user_speaker_embedding("u1", [float('nan')], expected_updated_at=None) # writes speaker_embedding: [nan]
recover_user_speaker_embedding("u1", [True], expected_updated_at=None) # writes speaker_embedding: [True]
Case 3 is the one that costs data: speaker_embedding is compared element-wise downstream, so an empty, NaN or boolean vector silently makes every later similarity score meaningless while the repair reports True.
#22883 rejects all three at the boundary (ValueError from _sanitize_uid / _validate_embedding), replaces snapshot.to_dict() or {} with _as_mapping so a truthy non-mapping no longer reaches .get, logs and returns None on a transient read failure, and routes the transaction through run_transactional(client, repair) with an optional firestore_client=.
Fail-closed behaviour is unchanged: a newer speaker_embedding_updated_at, an already-present vector, or an active deletion marker still return False.
Tests: 42 hermetic cases in backend/tests/unit/test_owner_profile_updates_resilience.py, verified locally with pytest (42 passed).
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! 🙏