omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(gmail-tools): guard string max_results coercion, sanitize labels, and support nested MIME multipart body ($50 proposed)

Open Dimank92 opened this issue 1 month ago • 2 comments

Problem & Root Cause Analysis

In backend/utils/retrieval/tools/gmail_tools.py:

  1. TypeError on String max_results: In get_gmail_messages_tool:

    max_results = ensure_capped(max_results, 50, "⚠️ get_gmail_messages_tool - max_results capped from {} to {}")
    

    When LLMs pass max_results as a string (e.g. "10" or "5" via structured JSON tool calling), val > cap raises TypeError: '>' not supported between instances of 'str' and 'int'. Similarly, min(max_results, 50) in get_gmail_messages raises TypeError.

  2. AttributeError / HTTP 400 Bad Request on label: When label is passed with whitespace (e.g. " "), label_ids = [label_upper] creates labelIds=[" "], triggering an immediate HTTP 400 Bad Request from the Gmail API (Invalid labelIds value). Furthermore, if a non-string or boolean is passed, calling label.upper() raises AttributeError.

  3. AttributeError & TypeError in parse_gmail_message on Malformed or None Payloads:

    payload = message.get('payload', {})
    headers = payload.get('headers', [])
    header_dict = {h['name'].lower(): h['value'] for h in headers}
    

    If a message has payload: None, payload becomes None and .get('headers') crashes with AttributeError. If headers is None (common in minimal drafts or metadata messages), iterating over headers raises TypeError: 'NoneType' object is not iterable. If a header dictionary contains 'name': None, h['name'].lower() raises AttributeError.

  4. Empty Body on Nested MIME Multipart Messages (multipart/mixed + multipart/alternative): Standard MIME messages containing both attachments and formatted text nest multipart/alternative inside a parent multipart/mixed part. The existing parse_gmail_message only examined direct children of payload['parts'], completely missing nested plain text and HTML bodies, resulting in body_text = ''.

  5. TypeError on None messages in API Response: ids = [msg['id'] for msg in data.get('messages', [])] raises TypeError if the Gmail API response explicitly contains 'messages': None.

  6. Missing Top-Level user_id Configuration Resolution: If an invoking caller supplies config={"user_id": "..."} without a nested "configurable" dictionary, prepare_access fails with an access error.


Proposed Solution

  1. Type Coercion & Bounds: Safely coerce max_results to int clamped to 1..50 (default 10) in both get_gmail_messages_tool and get_gmail_messages.
  2. Label Sanitization: Strip and validate label string before converting to upper case, preventing empty/whitespace labelIds and ensuring UNREAD properly transforms into is:unread search operator.
  3. MIME Recursive Text Extractor: Implement _extract_body_text to recursively traverse nested MIME parts for text/plain and text/html bodies with safe base64 decoding.
  4. Resilient Payload & Header Parsing: Safely guard payload, headers, and header name entries against None.
  5. Safe Response Handling: Check (data.get('messages') or []) and validate message dicts with valid id keys.
  6. Config Normalization: Ensure top-level user_id in config is forwarded to configurable.
  7. Comprehensive Unit Tests: Add backend/tests/unit/test_gmail_tools.py with 9 hermetic unit tests verifying all edge cases.

Proposed Bounty

  • $50 USDC

Payout Details

  • USDC (Polygon / Base / Arbitrum): 0xC345C07229307d4f1c7935aD2cAe48e390215808

Dimank92 avatar Oct 22 '26 03:10 Dimank92

Fix implemented and covered by 9 hermetic unit tests in PR #23192.

Bounty Proposal & Claim

  • Target Issue: #23191 ($50 proposed)
  • PR: https://github.com/BasedHardware/omi/pull/23192
  • USDC (Polygon / Base / Arbitrum): 0xC345C07229307d4f1c7935aD2cAe48e390215808

Dimank92 avatar Oct 22 '26 03:10 Dimank92

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