omi
omi copied to clipboard
[Bounty proposal] fix(backend): validate identifiers, clamp boundaries, and add transaction resilience in daily_summaries ($50 proposed)
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:
- Unchecked Identifiers & Path Traversal Risks:
create_daily_summary,get_daily_summary,update_daily_summary,delete_daily_summary,set_daily_summary_visibility, andget_summaries_countaccept empty or whitespace strings without validation, creating invalid Firestore paths (e.g.users//daily_summaries/).- Identifiers with
/are not sanitized against collection-path traversal.
- Missing ID / Payload Validation:
create_daily_summaryindexedsummary_data["id"]directly, throwing an unhandledKeyErrorif caller omitted the ID instead of auto-allocating a UUID.- Non-dictionary
summary_datapayloads caused unhandled exceptions.
- Unbounded / Negative Pagination Arguments:
get_daily_summariesaccepted rawlimitandoffsetarguments without bounds checking, allowing negative limits/offsets or unbounded reads.
- Unprotected Concurrency in Running Daily Usage:
upsert_desktop_daily_usageexecuted 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.
- Fragile Cache Invalidation:
delete_daily_summarycalled 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
- Added
_clean_strand_clean_idhelpers to validate and sanitize identifiers, preventing empty strings and/path-traversals across all database operations. - In
create_daily_summary, validated payload type, auto-allocated a UUID when missing, set defaultcreated_attimestamp, and returned the assigned document ID. - In
upsert_desktop_daily_usage, wrapped atomic merges withrun_with_transaction_contention_retryto eliminate concurrency contention, sanitized timezone names, and clamped counters to non-negative integers. - In
get_daily_summaries, clampedlimitto[1, 500]andoffsetto>= 0. - In
delete_daily_summary, wrapped Redis cache removal in error handling to ensure Firestore document deletion succeeds reliably. - In
get_summaries_count, provided resilient fallback on aggregation failure. - Added 10 hermetic unit tests in
backend/tests/unit/test_daily_summaries_resilience.pywith 100% pass rate and registered check in.github/checks-manifest.yaml.
Proposed Bounty
- Proposed Amount: $50
Duplicate of #19596, the earlier open issue for the same backend/database/daily_summaries.py hardening.