Skip to content

fix(omi-hacker-news-app): guard non-dict payloads, validate item_id, and safe limit bounds #14283

Closed

Description

@PINYOPATTANAWASANPORN

Problem Description

In plugins/omi-hacker-news-app/main.py, several reliability and error-handling bugs can lead to unhandled 500 exceptions and malformed API requests:

  1. Crash on Null or Non-Dict Request Body: get_front_page, search_stories, and get_discussion expect a payload: dict[str, Any] and call payload.get(...). When client invocations send empty/null JSON bodies or non-dict payloads, AttributeError: 'NoneType' object has no attribute 'get' is raised.
  2. Missing item_id Type & Range Validation: In get_discussion, int(item_id) is invoked without validating whether item_id is a boolean (in Python, int(True) == 1), empty string, or non-positive integer. Sending 0 or negative numbers generates malformed requests to /items/{id}.
  3. Boolean Handling in _safe_limit: In Python, isinstance(True, int) is True. Passing limit: True results in limit=1 instead of falling back to default 10.
  4. Malformed URL When objectID is Missing: When Algolia returns a hit with missing objectID / story_id, _format_story constructs invalid URLs ending with item?id=None.

Proposed Solution

  • Add _safe_payload helper ensuring any non-dict input safely defaults to {} without raising AttributeError.
  • Harden _safe_limit to reject boolean values (True/False) and clamp safely between 1 and MAX_LIMIT.
  • Add strict validation for item_id: reject booleans, empty strings, and non-positive integers with clean error messages ("item_id must be a positive integer").
  • Omit or format fallback URLs cleanly when objectID is absent.
  • Extend test_main.py with unit tests for safe limits, non-dict payloads, missing object IDs, and item ID boundary validation.

Proposed bounty: $50

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions