omi icon indicating copy to clipboard operation
omi copied to clipboard

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

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

Summary

backend/database/speaker_learning.py performs version-fenced updates of voice-learning diagnostics (voice_learning_state, voice_learning_outcome, voice_speech_seconds, voice_needed_seconds) on users/{uid}/people/{person_id} documents inside a Firestore transaction, but currently lacks input validation, malformed-snapshot guards, and numeric normalization:

  1. Unvalidated document IDs (uid, person_id) and labels (outcome, state):
    • update_person_voice_learning(uid, person_id, ...) passes uid and person_id directly to db.collection('users').document(uid) and user_ref.collection('people').document(person_id) without stripping whitespace or rejecting empty/whitespace-only strings, path separators (/, \), .. segments, control characters, or overlong IDs (> 128 chars).
    • Empty or non-string outcome and state labels are written directly into person documents without validation.
  2. Uncaught AttributeError on None or non-dict Firestore snapshots inside _update_voice_learning_transaction:
    • _update_voice_learning_transaction assigns person = snapshot.to_dict() without guarding against None or non-dict payloads (e.g., person = snapshot.to_dict() or {}), crashing with AttributeError: 'NoneType' object has no attribute 'get' when person.get('updated_at') is evaluated on an empty or corrupted document snapshot.
    • Similarly, settings = user_snapshot.to_dict() if user_snapshot.exists else {} can evaluate to None or a non-dict when user_snapshot.exists is truthy but to_dict() returns None or a primitive, crashing on settings.get('save_other_voice_profiles', True).
  3. Unbounded / non-finite float values (NaN, inf, negative seconds, booleans):
    • speech_seconds and needed_seconds (as well as stored person.get('voice_speech_seconds')) are persisted without checking for NaN, inf, booleans, or negative durations.
  4. Transaction invocation & testability:
    • Uses raw _update_voice_learning_transaction(db.transaction(), ...) instead of run_transactional(client, ...) from database._client and does not accept an optional firestore_client parameter for hermetic testing.

Proposed Fix

  • Add _sanitize_id(value, field_name) and _sanitize_label(value, field_name) to validate and strip uid, person_id, outcome, and state.
  • Guard snapshot.to_dict() and user_snapshot.to_dict() in _update_voice_learning_transaction so None or non-dict payloads safely fall back to {}.
  • Normalize speech_seconds, needed_seconds, and stored voice_speech_seconds via _normalize_seconds (clamping negative finite numbers to 0.0 and converting NaN/inf/booleans/non-numbers to None).
  • Route transactional execution through run_transactional and support optional firestore_client injection.
  • Add comprehensive hermetic unit tests in backend/tests/unit/test_speaker_learning_resilience.py.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Implemented in #22898: id/label validation, snapshot guards, finite-duration normalization, hermetic tests

Bounty settlement address (Base Mainnet USDC): 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af

Andu1223 avatar Oct 20 '26 16: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