omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(backend): validate identifiers, clamp boundaries, and add transaction retry in recording_sessions ($50 proposed)

Open shitobu opened this issue 2 months ago • 1 comments

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:

  1. Unchecked Identifiers & Path Traversal Risks:
    • Functions like create_or_get_recording_session, renew_recording_session_lease, get_recording_session, tombstone_and_delete_empty_conversation, and record_lifecycle_event accept 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 (/, \, ..).
  2. 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 unhandled Aborted (409) transaction collisions.
  3. Corrupted or Missing Lifecycle Counter Boundaries:
    • In _record_lifecycle_event_txn, _binding, and tombstone_and_delete_empty_conversation, reading lifecycle_sequence or lifecycle_version from malformed or corrupted Firestore documents could produce negative numbers or unhandled ValueError / TypeError casting crashes.

Solution

  1. Identifier Sanitization & Path Traversal Guards:
    • Added _validate_id() helper to guarantee that uid, recording_session_id, and conversation_id / proposed_conversation_id are non-empty strings and free of /, \, and .. path traversal characters.
    • Guarded record_lifecycle_event to safely return an unaccepted envelope with discard_reason: 'invalid_identifier' on malformed identifiers matching the event contract.
  2. 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) in run_with_transaction_contention_retry with fresh transaction factories to gracefully recover from read-time contention and commit collisions.
  3. Boundary Clamping & Malformed Payload Handling:
    • Safe parsing and clamping for lifecycle_sequence (>= 0) and lifecycle_version (>= 1, defaulting to LIFECYCLE_ENVELOPE_VERSION), preventing crashes from unexpected payload types or corrupted negative sequences.
  4. Hermetic Test Suite & Check Registration:
    • Added 13 hermetic unit tests in backend/tests/unit/test_recording_sessions_resilience.py testing identifier validation, contention retries, envelope sequencing, boundary clamping, and empty conversation tombstoning without external dependencies.
    • Registered recording-sessions-resilience-tests in .github/checks-manifest.yaml.

Proposed Bounty

• Proposed Amount: $50

shitobu avatar Sep 29 '26 20:09 shitobu

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