omi icon indicating copy to clipboard operation
omi copied to clipboard

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

Open shitobu opened this issue 2 months ago • 1 comments

Problem

backend/database/daily_summaries.py manages daily summary generation, retrieval, settings/visibility, count metrics, and desktop daily usage counters across multiple client devices. It is invoked by core flows including proactive notifications, external integrations, memory sweeps, and MCP tools. Currently, it has several input validation and resilience vulnerabilities:

  1. Unchecked Identifiers & Path Traversal Risks:
    • create_daily_summary, get_daily_summary, update_daily_summary, delete_daily_summary, set_daily_summary_visibility, and get_summaries_count accept empty or whitespace strings without validation, creating invalid Firestore paths (e.g. users//daily_summaries/).
    • Identifiers with / are not sanitized against collection-path traversal.
  2. Missing ID / Payload Validation:
    • create_daily_summary indexed summary_data["id"] directly, throwing an unhandled KeyError if caller omitted the ID instead of auto-allocating a UUID.
    • Non-dictionary summary_data payloads caused unhandled exceptions.
  3. Unbounded / Negative Pagination Arguments:
    • get_daily_summaries accepted raw limit and offset arguments without bounds checking, allowing negative limits/offsets or unbounded reads.
  4. Unprotected Concurrency in Running Daily Usage:
    • upsert_desktop_daily_usage executed running-totals transaction commits without contention retry wrappers, causing failures when multiple device syncs commit concurrently.
    • Negative counter values from malfunctioning clients were not clamped.
  5. Fragile Cache Invalidation:
    • delete_daily_summary called Redis removal without fallback; an intermittent Redis error could fail the entire deletion flow.
    • Aggregation count queries did not have safe fallback on transient query errors.

Solution

  1. Added _clean_str and _clean_id helpers to validate and sanitize identifiers, preventing empty strings and / path-traversals across all database operations.
  2. In create_daily_summary, validated payload type, auto-allocated a UUID when missing, set default created_at timestamp, and returned the assigned document ID.
  3. In upsert_desktop_daily_usage, wrapped atomic merges with run_with_transaction_contention_retry to eliminate concurrency contention, sanitized timezone names, and clamped counters to non-negative integers.
  4. In get_daily_summaries, clamped limit to [1, 500] and offset to >= 0.
  5. In delete_daily_summary, wrapped Redis cache removal in error handling to ensure Firestore document deletion succeeds reliably.
  6. In get_summaries_count, provided resilient fallback on aggregation failure.
  7. Added 10 hermetic unit tests in backend/tests/unit/test_daily_summaries_resilience.py with 100% pass rate and registered check in .github/checks-manifest.yaml.

Proposed Bounty

  • Proposed Amount: $50

shitobu avatar Sep 29 '26 02:09 shitobu

Duplicate of #19596, the earlier open issue for the same backend/database/daily_summaries.py hardening.

aryanorastar avatar Oct 02 '26 03:10 aryanorastar