omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] Harden backend/database/import_quotas.py against unsafe Redis hash tags, invalid amounts, and malformed env limits ($50 proposed)

Open huynhnhatquandongnai-source opened this issue 1 month ago • 2 comments

Summary

backend/database/import_quotas.py enforces rolling 30-day upload and byte quotas for transcript imports using Redis Lua scripts, but currently lacks input validation and environment parsing resilience:

  1. Unvalidated Redis Cluster hash-tag uid and kind in _quota_keys:
    • _quota_keys(uid, kind) formats f'import:monthly:{kind}:{{{uid}}}' directly without validating uid or kind. An empty/whitespace-only uid or one containing {, }, :, /, \, .., or control characters breaks Redis Cluster hash-tag slot routing ({uid}) or collapses users into the empty {} slot.
    • import_quota_limit(kind) raises an uncaught KeyError on unknown kind values and crashes with ValueError if IMPORT_MONTHLY_<KIND>_LIMIT is set to a malformed non-integer string.
  2. Boolean and non-positive amount bypasses in reserve_import_quota:
    • reserve_import_quota(uid, kind, amount) accepts amount=True (since isinstance(True, int) is True), amount <= 0 (which can decrement total or insert zero-cost entries into the sorted set/hash), or non-integer amounts.
  3. Unvalidated reservation in release_import_quota and missing redis_client injection:
    • release_import_quota executes a Redis Lua round-trip even when reservation is None, empty/whitespace-only, or contains control characters, and neither reserve_import_quota nor release_import_quota accepts an optional redis_client parameter for hermetic testing.

Proposed Fix

  • Add _sanitize_uid, _sanitize_kind, and _sanitize_amount to validate uid, kind, and positive integer amount values.
  • Guard import_quota_limit against malformed environment variable values by logging a warning and falling back to the default limit.
  • Skip Redis Lua execution in release_import_quota when reservation is blank/invalid, and add optional redis_client injection to reserve_import_quota and release_import_quota.
  • Add 34 hermetic unit tests in backend/tests/unit/test_import_quotas_resilience.py.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Implemented in #22897: hash-tag/kind/amount validation, safe env parsing, and hermetic tests

Bounty settlement address (Base Mainnet USDC): 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af

Andu1223 avatar Oct 20 '26 16:10 Andu1223

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