omi
omi copied to clipboard
[Bounty proposal] fix(backend): validate identifiers, clamp boundaries, and add transaction retry in recording_sessions ($50 proposed)
Problem
backend/database/recording_sessions.py provides the source of truth for audio recording sessions, canonical conversation identity bindings, and monotonic lifecycle envelope state transitions. It manages real-time audio streams, live conversation creation, lease renewal, and empty conversation tombstoning.
Currently, it suffers from several resilience and validation vulnerabilities:
- Unchecked Identifiers & Path Traversal Risks:
- Functions like
create_or_get_recording_session,renew_recording_session_lease,get_recording_session,tombstone_and_delete_empty_conversation, andrecord_lifecycle_eventaccept raw string parameters (uid,recording_session_id,conversation_id,proposed_conversation_id) without verifying they are valid non-empty strings or sanitizing against path-traversal characters (/,\,..).
- Functions like
- Unprotected Firestore Transaction Contention on Streaming Audio:
- All 4 transaction operations (
create_or_get_recording_session,renew_recording_session_lease,tombstone_and_delete_empty_conversation,record_lifecycle_event) executed single-attempt Firestore transactions. Under high-throughput streaming audio, concurrent client updates, or simultaneous lease renewals, read-time contention causes unhandledAborted(409) transaction collisions.
- All 4 transaction operations (
- Corrupted or Missing Lifecycle Counter Boundaries:
- In
_record_lifecycle_event_txn,_binding, andtombstone_and_delete_empty_conversation, readinglifecycle_sequenceorlifecycle_versionfrom malformed or corrupted Firestore documents could produce negative numbers or unhandledValueError/TypeErrorcasting crashes.
- In
Solution
- Identifier Sanitization & Path Traversal Guards:
- Added
_validate_id()helper to guarantee thatuid,recording_session_id, andconversation_id/proposed_conversation_idare non-empty strings and free of/,\, and..path traversal characters. - Guarded
record_lifecycle_eventto safely return an unaccepted envelope withdiscard_reason: 'invalid_identifier'on malformed identifiers matching the event contract.
- Added
- Transaction Contention Retries:
- Wrapped all 4 Firestore transactions (
create_or_get_recording_session,renew_recording_session_lease,tombstone_and_delete_empty_conversation,record_lifecycle_event) inrun_with_transaction_contention_retrywith fresh transaction factories to gracefully recover from read-time contention and commit collisions.
- Wrapped all 4 Firestore transactions (
- Boundary Clamping & Malformed Payload Handling:
- Safe parsing and clamping for
lifecycle_sequence(>= 0) andlifecycle_version(>= 1, defaulting toLIFECYCLE_ENVELOPE_VERSION), preventing crashes from unexpected payload types or corrupted negative sequences.
- Safe parsing and clamping for
- Hermetic Test Suite & Check Registration:
- Added 13 hermetic unit tests in
backend/tests/unit/test_recording_sessions_resilience.pytesting identifier validation, contention retries, envelope sequencing, boundary clamping, and empty conversation tombstoning without external dependencies. - Registered
recording-sessions-resilience-testsin.github/checks-manifest.yaml.
- Added 13 hermetic unit tests in
Proposed Bounty
• Proposed Amount: $50
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! 🙏