[Bounty proposal] Harden backend/database/speaker_learning.py against malformed snapshots, invalid IDs, and non-finite seconds ($50 proposed)
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:
- Unvalidated document IDs (
uid,person_id) and labels (outcome,state):update_person_voice_learning(uid, person_id, ...)passesuidandperson_iddirectly todb.collection('users').document(uid)anduser_ref.collection('people').document(person_id)without stripping whitespace or rejecting empty/whitespace-only strings, path separators (/,\),..segments, control characters, or overlong IDs (> 128chars).- Empty or non-string
outcomeandstatelabels are written directly into person documents without validation.
- Uncaught
AttributeErroronNoneor non-dict Firestore snapshots inside_update_voice_learning_transaction:_update_voice_learning_transactionassignsperson = snapshot.to_dict()without guarding againstNoneor non-dict payloads (e.g.,person = snapshot.to_dict() or {}), crashing withAttributeError: 'NoneType' object has no attribute 'get'whenperson.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 toNoneor a non-dict whenuser_snapshot.existsis truthy butto_dict()returnsNoneor a primitive, crashing onsettings.get('save_other_voice_profiles', True).
- Unbounded / non-finite float values (
NaN,inf, negative seconds, booleans):speech_secondsandneeded_seconds(as well as storedperson.get('voice_speech_seconds')) are persisted without checking forNaN,inf, booleans, or negative durations.
- Transaction invocation & testability:
- Uses raw
_update_voice_learning_transaction(db.transaction(), ...)instead ofrun_transactional(client, ...)fromdatabase._clientand does not accept an optionalfirestore_clientparameter for hermetic testing.
- Uses raw
Proposed Fix
- Add
_sanitize_id(value, field_name)and_sanitize_label(value, field_name)to validate and stripuid,person_id,outcome, andstate. - Guard
snapshot.to_dict()anduser_snapshot.to_dict()in_update_voice_learning_transactionsoNoneor non-dict payloads safely fall back to{}. - Normalize
speech_seconds,needed_seconds, and storedvoice_speech_secondsvia_normalize_seconds(clamping negative finite numbers to0.0and convertingNaN/inf/booleans/non-numbers toNone). - Route transactional execution through
run_transactionaland support optionalfirestore_clientinjection. - 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
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! 🙏