omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(backend): validate inputs and prevent counter crashes in daily summaries ($50 proposed)

Open foxck016077 opened this issue 2 months ago • 0 comments

Following the contribution guide's invitation to propose paid work — @kodjima33, @aryanorastar, @Git-on-my-level, would a US$50 bounty be appropriate for this? Proposed amount, not an existing award.

This addresses unhandled KeyError/TypeError exceptions, identifier path traversal, and unvalidated document keys in backend/database/daily_summaries.py to prevent crashes in desktop daily usage telemetry and daily summary retrieval.

Problem & Vulnerability

  1. Unhandled KeyError on Partial Counters:

    • In upsert_desktop_daily_usage, the code iterated through DESKTOP_DAILY_USAGE_COUNTER_FIELDS and directly performed counters[field]. If any expected field was omitted in client payload, it triggered an unhandled KeyError inside the Firestore transaction.
  2. Unhandled TypeError on Malformed Counter Types:

    • max(previous_value, counters[field]) directly compared existing Firestore values with incoming values. Non-integer inputs, booleans (which pass isinstance(x, int) in Python), or negative values could corrupt counters or raise TypeError.
  3. Unchecked Path Traversal & Blank Identifier Crashes:

    • upsert_desktop_daily_usage, get_desktop_daily_usage, create_daily_summary, and get_daily_summary constructed document paths directly without validating uid, date, client_device_id, or summary_data["id"].
    • Passing empty strings, whitespace, or strings with / can cause invalid Firestore document references or path traversal.
  4. Missing Field Validation in Summary Creation:

    • create_daily_summary directly indexed summary_data["id"] without type or null validation, causing KeyError or invalid document paths if id was missing or non-string.

Remediation

  1. Input & Identifier Validation:

    • Added _validate_identifier to strictly ensure uid, date, client_device_id, and id are non-empty strings without path separators (/).
    • Guarded get_desktop_daily_usage and get_daily_summary to fail-safe return defaults/None on blank identifiers without issuing wasteful Firestore calls.
  2. Defensive Counter Merging:

    • Safely get counter fields with default fallback 0.
    • Ensure only positive, non-boolean integers are merged with max().
  3. Summary ID Integrity:

    • Validated summary_data is a valid dictionary containing non-empty id.
  4. Hermetic Unit Test Suite:

    • Added backend/tests/unit/test_daily_summaries_resilience.py with 10 comprehensive unit tests covering partial counters, type sanitation, identifier checks, and error boundaries (10/10 passing). Existing tests in test_desktop_daily_usage.py (8/8) remain 100% passing.

Bounty Payout Destination (EVM - Base): 0x5892E49d30b3BDa6f97DaBC5B657d64cb9c02D80

foxck016077 avatar Sep 30 '26 08:09 foxck016077