omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] Harden backend/database/owner_profile_updates.py against invalid UIDs, non-finite embeddings, and malformed snapshots ($50 proposed)

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

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:

  1. Unvalidated document IDs (uid):
    • get_user_speaker_embedding_recovery_state(uid) and recover_user_speaker_embedding(uid, ...) pass uid directly into collection('users').document(uid) and account_deletion_document(uid) without stripping whitespace or rejecting empty/whitespace-only strings, path separators (/, \), .. segments, control characters, or overlong IDs (> 128 chars).
  2. Unvalidated / non-finite speaker embeddings:
    • recover_user_speaker_embedding writes embedding directly to Firestore without validating that it is a non-empty list/tuple of finite numbers (allowing empty lists, booleans, non-numeric items, or NaN/inf floats to corrupt the user document's speaker_embedding field).
  3. Uncaught AttributeError on non-dict Firestore snapshots & unhandled read exceptions:
    • Both get_user_speaker_embedding_recovery_state and recover_user_speaker_embedding use data = snapshot.to_dict() or {}, which raises AttributeError on data.get(...) if a corrupted document snapshot returns a truthy non-dict object (such as a list or string).
    • get_user_speaker_embedding_recovery_state does not catch transient Firestore read exceptions, propagating uncaught errors during opportunistic owner voiceprint recovery checks.
  4. Transaction execution & testability:
    • Calls repair(client.transaction()) directly instead of run_transactional(client, repair) and does not accept an optional firestore_client keyword argument for hermetic testing.

Proposed Fix

  • Add _sanitize_uid, _clean_optional_uid, and _validate_embedding to validate and normalize uid and embedding vectors (coercing valid ints/floats to float and rejecting empty lists, booleans, and NaN/inf).
  • Guard snapshot.to_dict() in both functions with isinstance(..., dict) checks and catch/log transient read failures in get_user_speaker_embedding_recovery_state.
  • Use run_transactional(client, repair) and accept an optional firestore_client keyword argument in both functions while preserving backward compatibility with get_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).

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