[Bounty proposal] fix(mcp_oauth): coerce naive and ISO-8601 timestamps to UTC in grant and token paths ($50 proposed)
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):
_grant_recency_key/list_user_grants:_grant_recency_keyreturnscandidatewheneverisinstance(candidate, datetime)without checkingcandidate.tzinfo, and falls back todatetime.min.replace(tzinfo=timezone.utc)when neitherupdated_atnorcreated_atis adatetime(e.g. an ISO-8601 string orNone).- When
list_user_grantssorts grants (grants.sort(key=_grant_recency_key, reverse=True)) across records where one grant has an offset-naivedatetimeand another has an offset-awaredatetime(or ISO-8601 string fallback), Python raisesTypeError: can't compare offset-naive and offset-aware datetimes.
consume_authorization_code&exchange_authorization_code_for_tokens:- Both compare
expires_at <= nowafter only checkingisinstance(expires_at, datetime). Ifexpires_atis stored as a naivedatetime,expires_at <= nowraisesTypeError: can't compare offset-naive and offset-aware datetimes. Ifexpires_atis stored as an ISO-8601 string, valid authorization codes are falsely rejected as expired.
- Both compare
_is_unexpired,validate_access_token, &rotate_refresh_token:_is_unexpiredcomparesexpires_at > _utcnow()without normalizing naivedatetimeobjects or parsing ISO-8601 strings, crashing withTypeError: can't compare offset-naive and offset-aware datetimeson naiveexpires_at.validate_access_tokenchecksisinstance(expires_at, datetime)and returnsexpires_at.astimezone(timezone.utc).isoformat(), which omits ISO-8601 string timestamps.rotate_refresh_tokenchecksisinstance(used_at, datetime)and computes(now - used_at).total_seconds(), which raisesTypeError: can't subtract offset-naive and offset-aware datetimeswhenused_atis a naivedatetimeand skips grace-window replay protection whenused_atis an ISO-8601 string.
Proposed Fix
- Introduce
_coerce_utc_datetime(value)inbackend/database/mcp_oauth.pyto normalize naivedatetimevalues (assuming UTC), non-UTC awaredatetimevalues (.astimezone(timezone.utc)), and ISO-8601 timestamp strings (includingZsuffixes) into timezone-aware UTCdatetimeobjects. - Use
_coerce_utc_datetimein_grant_recency_key,consume_authorization_code,exchange_authorization_code_for_tokens,_is_unexpired,validate_access_token, androtate_refresh_token. - Add unit tests in
backend/tests/unit/test_mcp_oauth.pycovering 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.
@Git-on-my-level Payout address for #22879, covering the naive/ISO timestamp coercion:
Base Mainnet USDC: 0x93c0d4e7100bc6b59e28e074f30a1ac37244b6af
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! 🙏