Skip to content

Optional max_response_bytes cap on Comlink responses #240

Closed

Description

@MarTrepodi

Component

SwgohComlink / SwgohComlinkAsync (clients)

Problem or Use Case

_send in both clients reads the whole response body into memory (r.content) before decoding it, with no upper limit. A misbehaving or compromised Comlink, or a proxy in front of it, can exhaust memory. That matters on small hosts such as a Raspberry Pi.

Proposed Solution

Add a keyword-only max_response_bytes: int | None = None to both client constructors.

  • With the default None, behaviour is exactly as today.
  • When set, send the request with client.stream(...):
    • Reject at once if Content-Length is over the limit. That header gives the compressed size, so it only allows an early exit.
    • Count the decompressed bytes from iter_bytes() and abort with SwgohComlinkValueError once the count passes the limit. This is the real guard, and it also stops gzip bombs.
    • On error statuses, read the body, up to the limit, before calling SwgohComlinkHTTPError.from_response.
  • /data responses are far larger than any other endpoint's. A cap low enough to protect other calls would break get_game_data, the same reason /data already has its own timeout (GAME_DATA_TIMEOUT). Give get_game_data a separate, larger limit, or document the size that full game data needs.

Acceptance

  • Default behaviour and existing tests are unchanged.
  • A mocked oversized response raises with the limit set, in both sync and async.
  • A Content-Length over the limit fails before the body is read.
  • A body that starts with a UTF-8 BOM still raises SwgohComlinkDecodeError (regression guard).

Alternatives Considered

  • A default-on limit. Rejected: it changes default behaviour, and a sensible value depends on the endpoint and the host's memory.

Activity

  1. added
    enhancement
    on Nov 7, 2026
  2. github-actions commented on Nov 7, 2026

    @github-actions
    Contributor

    Addressed by #258, which is now merged into develop. This issue will close automatically when that change is released to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions