omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] Harden backend/database/dream_canary.py against malformed snapshots, invalid UIDs, and non-numeric failure counts ($50 proposed)

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

Summary

backend/database/dream_canary.py guards the synthetic Dream canary account (ensure_synthetic_owner) and tracks consecutive canary health failures (failure_count, record_result, reset_failures), but currently has several snapshot and input resilience bugs:

  1. Unsafe document IDs in ensure_synthetic_owner:
    • if not uid or uid != canary_uid(): does not verify that uid and canary_uid() are safe Firestore document IDs (free of leading/trailing whitespace, /, \, .., control characters, or lengths > 128 chars) before passing uid to database.collection('users').document(uid).
  2. Uncaught AttributeError, TypeError, and ValueError on malformed snapshots and canary_failures fields:
    • In ensure_synthetic_owner, existing = owner.get(transaction=tx).to_dict() raises AttributeError when existing.get('dream_canary') is called on a non-dict snapshot payload instead of raising ValueError('dream_canary_existing_owner').
    • In failure_count and record_result, int(row.get('canary_failures', 0)) raises TypeError when canary_failures is explicitly None ({'canary_failures': None}), ValueError on non-numeric strings or NaN/inf, and returns negative counts when canary_failures < 0.
    • failure_count does not catch transient Firestore .get() errors, and record_result does not validate that passed is a boolean.
  3. Raw .transaction() invocation without run_transactional:
    • ensure_synthetic_owner, reset_failures, and record_result invoke database.transaction() directly instead of routing through run_transactional(database, ...).

Proposed Fix

  • Add _is_safe_doc_id to validate both uid and canary_uid() in ensure_synthetic_owner.
  • Guard snapshot.to_dict() with isinstance(..., dict) across ensure_synthetic_owner, failure_count, and record_result, and normalize canary_failures into [0, 100] via _coerce_failure_count.
  • Validate isinstance(passed, bool) in record_result, add a read error boundary in failure_count, and route transactions through run_transactional.
  • Add 35 hermetic unit tests in backend/tests/unit/test_dream_canary_resilience.py.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Implemented in PR #23256 with document ID validation, snapshot payload guards, failure count normalization, transactional routing, and 35 hermetic unit tests:

  • Pull Request: https://github.com/BasedHardware/omi/pull/23256
  • Unit Tests: 35/35 passing hermetically in test_dream_canary_resilience.py
  • Full suite health: 279/279 passing (0 failures)
  • Payout Address: 0xC345C07229307d4f1c7935aD2cAe48e390215808 (USDC Polygon/Base/Arbitrum)

Dimank92 avatar Oct 22 '26 07:10 Dimank92

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