omi
omi copied to clipboard
fix(wikipedia): guard against non-dict payloads, null nested fields, and add hermetic unit test suite
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:
-
Fatal
AttributeErroron Chained Dictionary Lookups:- In
search_articles:data.get("query", {}).get("search", []). If upstream returns{"query": null},data.get("query")evaluates toNone, and calling.get("search")crashes withAttributeError: 'NoneType' object has no attribute 'get'. - In
_format_summary:data.get("content_urls", {}).get("desktop", {}).get("page"). Whencontent_urlsisNoneordesktopisNone, calling.get(...)crashes withAttributeError: 'NoneType' object has no attribute 'get'. - In
get_random_article:data.get("query", {}).get("random", [])has the same null query vulnerability, and indexingrandom_items[0]crashes if the list is empty or contains non-dict elements.
- In
-
Unhandled Non-Dict Payloads in
_request_json:- If Wikipedia returns an unexpected list or error string, calling
.get(...)ondataimmediately crashes withAttributeError: 'list' object has no attribute 'get'.
- If Wikipedia returns an unexpected list or error string, calling
-
Missing Lifespan Client & Connection Overhead:
_request_jsonpreviously instantiated a newhttpx.AsyncClienton every single request rather than utilizing FastAPIlifespanclient pooling, causing connection overhead and lacking fallback whenapp.state.http_clientis closed.
-
Untyped Request Payloads & Missing Input Validation:
- Routes accepted untyped
payload: dict[str, Any], omitting request normalization, whitespace trimming, and parameter bounds (e.g.limitexceeding 10 or being non-positive).
- Routes accepted untyped
-
Complete Absence of Unit Tests:
plugins/omi-wikipedia-apphad zero unit tests and was omitted from CI test automation in.github/checks-manifest.yaml.
Proposed Fix
-
Pydantic Models (
models.py):- Introduce
SearchArticlesRequest,GetArticleSummaryRequest,GetRandomArticleRequest, andChatToolResponse. - Implement
mode="before"whitespace normalization and input validation.
- Introduce
-
Lifespan Client & Null-Safe Protections (
main.py):- Add FastAPI
lifespancontext manager initializinghttp_clientonapp.state. - Add fallback client creation when
app.state.http_clientisNoneoris_closed = True. - Harden
_format_summarywith defensive null-safe attribute extraction. - Pre-filter valid dictionary items in
search_articlesandget_random_articlebefore slicing and indexing. - Add
validation_exception_handlerreturning structuredChatToolResponse(error=...).
- Add FastAPI
-
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.
-
CI Integration:
- Register
wikipedia-testsin.github/checks-manifest.yaml.
- Register
Bounty Request & Payout Details
- Proposed Bounty: $40.00 USDC
- Payout Destination (EVM / Base Network):
0xb5aFc89b57Fa8270bB7261348179D28099BEa2a0 - PR: To be submitted referencing this issue.
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! 🙏