[Bounty proposal] fix(backend): validate inputs and prevent counter crashes in daily summaries ($50 proposed)
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
-
Unhandled KeyError on Partial Counters:
- In
upsert_desktop_daily_usage, the code iterated throughDESKTOP_DAILY_USAGE_COUNTER_FIELDSand directly performedcounters[field]. If any expected field was omitted in client payload, it triggered an unhandledKeyErrorinside the Firestore transaction.
- In
-
Unhandled TypeError on Malformed Counter Types:
max(previous_value, counters[field])directly compared existing Firestore values with incoming values. Non-integer inputs, booleans (which passisinstance(x, int)in Python), or negative values could corrupt counters or raiseTypeError.
-
Unchecked Path Traversal & Blank Identifier Crashes:
upsert_desktop_daily_usage,get_desktop_daily_usage,create_daily_summary, andget_daily_summaryconstructed document paths directly without validatinguid,date,client_device_id, orsummary_data["id"].- Passing empty strings, whitespace, or strings with
/can cause invalid Firestore document references or path traversal.
-
Missing Field Validation in Summary Creation:
create_daily_summarydirectly indexedsummary_data["id"]without type or null validation, causingKeyErroror invalid document paths ifidwas missing or non-string.
Remediation
-
Input & Identifier Validation:
- Added
_validate_identifierto strictly ensureuid,date,client_device_id, andidare non-empty strings without path separators (/). - Guarded
get_desktop_daily_usageandget_daily_summaryto fail-safe return defaults/None on blank identifiers without issuing wasteful Firestore calls.
- Added
-
Defensive Counter Merging:
- Safely get counter fields with default fallback
0. - Ensure only positive, non-boolean integers are merged with
max().
- Safely get counter fields with default fallback
-
Summary ID Integrity:
- Validated
summary_datais a valid dictionary containing non-emptyid.
- Validated
-
Hermetic Unit Test Suite:
- Added
backend/tests/unit/test_daily_summaries_resilience.pywith 10 comprehensive unit tests covering partial counters, type sanitation, identifier checks, and error boundaries (10/10 passing). Existing tests intest_desktop_daily_usage.py(8/8) remain 100% passing.
- Added
Bounty Payout Destination (EVM - Base): 0x5892E49d30b3BDa6f97DaBC5B657d64cb9c02D80