omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty Proposal] Validate id and use merge writes in database apps and tester access

Open Ariyachan opened this issue 2 months ago • 0 comments

Problem

  1. NotFound exception in tester access mutations (backend/database/apps.py:325-334): In add_app_access_for_tester_db and remove_app_access_for_tester_db:

    def add_app_access_for_tester_db(app_id: str, uid: str) -> None:
        app_ref = db.collection(testers_collection).document(uid)
        app_ref.update({'apps': ArrayUnion([app_id])})
    

    When granting or revoking tester access via admin endpoints (POST /v1/apps/tester/access and DELETE /v1/apps/tester/access in backend/routers/apps.py:2291-2313), if the target user's document in testers/{uid} has not yet been initialized, app_ref.update(...) raises google.api_core.exceptions.NotFound: 404 No document to update. This unhandled exception bubbles up as an HTTP 500 server error instead of safely initializing or modifying the record.

  2. NotFound on set_app_popular_db (backend/database/apps.py:96-98): app_ref.update({'is_popular': popular}) fails if the target plugin document is not yet initialized or undergoing restore.

  3. Unhandled KeyError and overwrite risks in add_app_to_db, upsert_app_to_db, update_app_in_db, and update_persona_in_db:

    • app_data['id'] and persona_data['id'] raise an unhandled KeyError if payloads lack explicit 'id'.
    • update_app_in_db and update_persona_in_db use .update(...), throwing NotFound on missing documents.
    • upsert_app_to_db uses bare .set(...) without merge=True, wiping existing fields not present in the update dictionary.

Proposed Solution

  • Switch add_app_access_for_tester_db and remove_app_access_for_tester_db to app_ref.set({'apps': ArrayUnion([app_id])}, merge=True) and app_ref.set({'apps': ArrayRemove([app_id])}, merge=True) for idempotent lifecycle persistence.
  • Switch set_app_popular_db to app_ref.set({'is_popular': popular}, merge=True).
  • Validate required id with explicit ValueError in add_app_to_db, upsert_app_to_db, update_app_in_db, and update_persona_in_db, and use .set(..., merge=True).
  • Add comprehensive unit test suite in backend/tests/unit/test_database_apps_resilience.py.

Ariyachan avatar Sep 26 '26 02:09 Ariyachan