omi icon indicating copy to clipboard operation
omi copied to clipboard

fix(wikipedia): guard against non-dict payloads, null nested fields, and add hermetic unit test suite

Open Ivansky1 opened this issue 2 months ago • 1 comments

Description of Bug

In plugins/omi-wikipedia-app, multiple endpoints and helper functions are vulnerable to unhandled fatal crashes under non-standard or error upstream API payloads:

  1. Fatal AttributeError on Chained Dictionary Lookups:

    • In search_articles: data.get("query", {}).get("search", []). If upstream returns {"query": null}, data.get("query") evaluates to None, and calling .get("search") crashes with AttributeError: 'NoneType' object has no attribute 'get'.
    • In _format_summary: data.get("content_urls", {}).get("desktop", {}).get("page"). When content_urls is None or desktop is None, calling .get(...) crashes with AttributeError: 'NoneType' object has no attribute 'get'.
    • In get_random_article: data.get("query", {}).get("random", []) has the same null query vulnerability, and indexing random_items[0] crashes if the list is empty or contains non-dict elements.
  2. Unhandled Non-Dict Payloads in _request_json:

    • If Wikipedia returns an unexpected list or error string, calling .get(...) on data immediately crashes with AttributeError: 'list' object has no attribute 'get'.
  3. Missing Lifespan Client & Connection Overhead:

    • _request_json previously instantiated a new httpx.AsyncClient on every single request rather than utilizing FastAPI lifespan client pooling, causing connection overhead and lacking fallback when app.state.http_client is closed.
  4. Untyped Request Payloads & Missing Input Validation:

    • Routes accepted untyped payload: dict[str, Any], omitting request normalization, whitespace trimming, and parameter bounds (e.g. limit exceeding 10 or being non-positive).
  5. Complete Absence of Unit Tests:

    • plugins/omi-wikipedia-app had zero unit tests and was omitted from CI test automation in .github/checks-manifest.yaml.

Proposed Fix

  1. Pydantic Models (models.py):

    • Introduce SearchArticlesRequest, GetArticleSummaryRequest, GetRandomArticleRequest, and ChatToolResponse.
    • Implement mode="before" whitespace normalization and input validation.
  2. Lifespan Client & Null-Safe Protections (main.py):

    • Add FastAPI lifespan context manager initializing http_client on app.state.
    • Add fallback client creation when app.state.http_client is None or is_closed = True.
    • Harden _format_summary with defensive null-safe attribute extraction.
    • Pre-filter valid dictionary items in search_articles and get_random_article before slicing and indexing.
    • Add validation_exception_handler returning structured ChatToolResponse(error=...).
  3. Hermetic Test Suite (test_main.py):

    • 19 comprehensive unit tests covering all endpoints, disambiguation pages, 404 responses, empty results, malformed payload filtering, closed-client fallback, and constraint enforcement.
    • Works hermetically without third-party dependencies using isolated stdlib stubs.
  4. CI Integration:

    • Register wikipedia-tests in .github/checks-manifest.yaml.

Bounty Request & Payout Details

  • Proposed Bounty: $40.00 USDC
  • Payout Destination (EVM / Base Network): 0xb5aFc89b57Fa8270bB7261348179D28099BEa2a0
  • PR: To be submitted referencing this issue.

Ivansky1 avatar Sep 14 '26 21:09 Ivansky1

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

aryanorastar avatar Nov 05 '26 16:11 aryanorastar