[Bounty proposal] fix(byok): coerce last_seen_at timestamps and guard null BYOK state maps ($50 proposed)
Summary
In ackend/database/users.py and ackend/utils/byok.py, BYOK activation and header validation crash with unhandled TypeError or AttributeError when stored Firestore yok sub-maps contain naive datetime / ISO-8601 string last_seen_at values or explicit None fields:
- Mixed naive/aware datetime subtraction & ISO-8601 string last_seen_at:
- is_byok_active (ackend/database/users.py:327) and _validated_byok_keys (ackend/utils/byok.py:268) evaluate (datetime.now(timezone.utc) - last_seen).total_seconds().
- When last_seen_at is deserialized as a timezone-naive datetime (from legacy backfill scripts or Firestore emulator/REST writes), subtracting it from datetime.now(timezone.utc) raises TypeError: can't subtract offset-naive and offset-aware datetimes.
- When last_seen_at is stored as an ISO-8601 string, isinstance(last_seen, datetime) evaluates to False, silently skipping TTL enforcement (BYOK_TTL_SECONDS) in is_byok_active or skipping the ecord_byok_live_Read throttle in _validated_byok_keys.
- Explicit None yok / ingerprints maps and non-string fingerprint entries:
- get_byok_state (ackend/database/users.py:302) returns (data or {}).get('byok'). When a user document has {'byok': None}, get_byok_state returns None instead of {} and is_byok_active crashes on state.get('active') with AttributeError: 'NoneType' object has no attribute 'get'.
- _validated_byok_keys (ackend/utils/byok.py:278) calls state.get('fingerprints') or {} without checking isinstance(state, dict) or isinstance(stored_fps, dict), and passes stored_fp directly to hmac.compare_digest(stored_fp, ...) (line 290), raising TypeError: a bytes-like object is required, not 'NoneType' if a provider entry in ingerprints is None or non-string.
Proposed Fix
- Add _coerce_utc_datetime() in ackend/database/users.py and ackend/utils/byok.py to normalize naive datetime, aware datetime, and ISO-8601 strings to UTC-aware datetime before computing elapsed seconds.
- Ensure get_byok_state returns dict(state) only when isinstance(state, dict) (otherwise {}), and guard state / ingerprints dicts and non-string fingerprint values in _validated_byok_keys.
- Add unit tests in ackend/tests/unit/test_byok_state_timestamp_guards.py.
Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94
Bounty Payout Coordinates
- PayPal:
[email protected] - Base Mainnet USDC:
0xb9f3765aeb1974c27a193463f655a1a57b3aec94
Fix submitted in #23168 (huynhnhatquandongnai-source:fix/byok-state-timestamp-guards).
Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94
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! 🙏
Fix submitted in PR #23848: https://github.com/BasedHardware/omi/pull/23848
Payout Address
- Wallet: 0xC345C07229307d4f1c7935aD2cAe48e390215808
- Network: Polygon / Base / Arbitrum / Stellar