omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty Proposal] Validate inputs, handle exceptions, and use merge writes in database wrapped

Open Ariyachan opened this issue 2 months ago • 2 comments

Problem

  1. Uncaught exceptions on invalid or empty parameters (backend/database/wrapped.py:47-91):

    • get_wrapped(uid, year), create_wrapped(uid, year), reset_wrapped_for_regeneration(uid, year), update_wrapped_status(...), and update_wrapped_progress(...) operate directly on db.collection('users').document(uid).collection('wrapped').document(str(year)).
    • Passing an empty or whitespace uid raises ValueError: A document id must not be empty inside Firestore.
    • Non-positive or non-integer year arguments create unintended subcollections or malformed document paths.
  2. Destructive overwrites on creation and reset (backend/database/wrapped.py:91, 193):

    • create_wrapped and reset_wrapped_for_regeneration invoke wrapped_ref.set(wrapped_data) without merge=True.
    • Any auxiliary metadata, client flags, or custom fields previously attached to the document are wiped out.
  3. Check-then-act race and uncaught NotFound exceptions (backend/database/wrapped.py:119-136, 155-163):

    • update_wrapped_status and update_wrapped_progress perform a two-step get() then update(...).
    • If the document is deleted or expires concurrently between .get() and .update(), Firestore raises an unhandled google.api_core.exceptions.NotFound, crashing background worker processes.
    • is_wrapped_stuck does not verify that wrapped_data is a valid dictionary before accessing keys.

Proposed Solution

  • Add input validation for uid (non-empty string) and year (positive integer) across all database wrapped methods.
  • Switch wrapped_ref.set(wrapped_data) in create_wrapped and reset_wrapped_for_regeneration to wrapped_ref.set(wrapped_data, merge=True) for non-destructive persistence.
  • Wrap wrapped_ref.update(...) calls in try...except to catch NotFound or transient exceptions on concurrent deletion, returning False cleanly.
  • Add defensive type validation to is_wrapped_stuck.
  • Provide full regression and resilience coverage with unit tests in backend/tests/unit/test_database_wrapped_resilience.py.

Ariyachan avatar Sep 26 '26 07:09 Ariyachan

🚀 Bounty Fix Submitted

I have completed the fix for this issue in Pull Request: PR #19121

  • Full implementation, tested, zero regressions.
  • Ready for review and merge.

💸 Contributor Bounty Payout Coordinates:

shruhicollections-beep avatar Sep 26 '26 08:09 shruhicollections-beep

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 12:10 undivisible