omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(mcp_oauth): coerce naive and ISO-8601 timestamps to UTC in grant and token paths ($50 proposed)

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

Problem

In backend/database/mcp_oauth.py, several OAuth grant, authorization code, access token, and refresh token operations compare or sort Firestore-stored timestamp fields directly against datetime.now(timezone.utc) or datetime.min.replace(tzinfo=timezone.utc):

  1. _grant_recency_key / list_user_grants:
    • _grant_recency_key returns candidate whenever isinstance(candidate, datetime) without checking candidate.tzinfo, and falls back to datetime.min.replace(tzinfo=timezone.utc) when neither updated_at nor created_at is a datetime (e.g. an ISO-8601 string or None).
    • When list_user_grants sorts grants (grants.sort(key=_grant_recency_key, reverse=True)) across records where one grant has an offset-naive datetime and another has an offset-aware datetime (or ISO-8601 string fallback), Python raises TypeError: can't compare offset-naive and offset-aware datetimes.
  2. consume_authorization_code & exchange_authorization_code_for_tokens:
    • Both compare expires_at <= now after only checking isinstance(expires_at, datetime). If expires_at is stored as a naive datetime, expires_at <= now raises TypeError: can't compare offset-naive and offset-aware datetimes. If expires_at is stored as an ISO-8601 string, valid authorization codes are falsely rejected as expired.
  3. _is_unexpired, validate_access_token, & rotate_refresh_token:
    • _is_unexpired compares expires_at > _utcnow() without normalizing naive datetime objects or parsing ISO-8601 strings, crashing with TypeError: can't compare offset-naive and offset-aware datetimes on naive expires_at.
    • validate_access_token checks isinstance(expires_at, datetime) and returns expires_at.astimezone(timezone.utc).isoformat(), which omits ISO-8601 string timestamps.
    • rotate_refresh_token checks isinstance(used_at, datetime) and computes (now - used_at).total_seconds(), which raises TypeError: can't subtract offset-naive and offset-aware datetimes when used_at is a naive datetime and skips grace-window replay protection when used_at is an ISO-8601 string.

Proposed Fix

  • Introduce _coerce_utc_datetime(value) in backend/database/mcp_oauth.py to normalize naive datetime values (assuming UTC), non-UTC aware datetime values (.astimezone(timezone.utc)), and ISO-8601 timestamp strings (including Z suffixes) into timezone-aware UTC datetime objects.
  • Use _coerce_utc_datetime in _grant_recency_key, consume_authorization_code, exchange_authorization_code_for_tokens, _is_unexpired, validate_access_token, and rotate_refresh_token.
  • Add unit tests in backend/tests/unit/test_mcp_oauth.py covering mixed naive/aware/ISO-8601 timestamps across grant sorting, authorization code consumption/exchange, access token validation, and refresh token rotation.

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Bounty Payout Coordinates

  • PayPal: [email protected]
  • Base Mainnet USDC: 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

Fix submitted in #22661

Bounty settlement address (Base Mainnet USDC): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

I would like to work on this. I will open a pull request that addresses the acceptance criteria.

airdropia avatar Oct 20 '26 01:10 airdropia

@Git-on-my-level Payout address for #22879, covering the naive/ISO timestamp coercion:

Base Mainnet USDC: 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af

Andu1223 avatar Oct 20 '26 15:10 Andu1223

Note for maintainers (@kodjima33 / @Git-on-my-level): As the original reporter of #22660 (2026-10-20T00:32Z), our companion fix PR #22661 (fix(mcp_oauth): coerce naive and ISO-8601 timestamps to UTC) was submitted immediately at 2026-10-20T00:32:36Z (15 hours before duplicate PRs #22879 and #22955) with full unit test coverage in backend/tests/unit/test_mcp_oauth.py (90 passed).

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! 🙏

undivisible avatar Oct 22 '26 11:10 undivisible