Skip to content

language in get_unit_stats is not URL-encoded or validated #235

Closed

Description

@MarTrepodi

Bug Description

SwgohComlinkBase._build_unit_stats_endpoint (src/swgoh_comlink/_base.py) builds the query string with f"language={language}" and does no encoding or validation. If an application passes user input through, for example a Discord bot's language option, that input can add query parameters or a fragment to the stats request.

The HMAC signer signs /{endpoint} including the query, so any fix must keep the signed string and the requested URL byte-identical.

Steps to Reproduce

from swgoh_comlink._base import SwgohComlinkBase
SwgohComlinkBase._build_unit_stats_endpoint(flags=["calcGP"], language="eng_us&flags=onlyGP#x")

Expected Behavior

SwgohComlinkValueError for a value that is not a language code.

Actual Behavior

Reproduced on develop (9cc3760):

api?flags=calcGP&language=eng_us&flags=onlyGP#x

Suggested Fix

Two checks with different jobs:

  1. Format check, which raises. Validate language against ^[A-Za-z]{2,3}_[A-Za-z]{2}$ and raise SwgohComlinkValueError for anything else. This is the security fix: no value that passes can contain &, #, = or /.
  2. Known-language check, which only warns. If a value passes the format check but isn't in Constants.LANGUAGES (the 14 entries in helpers/_stat_data.py, compared case-insensitively), log a logger.warning naming the value and the known languages, then send it anyway. This catches typos such as eng_uk without blocking a language the game adds before the list is updated.

Then build the query with urllib.parse.urlencode(..., safe=","), so the comma-joined flags stay readable.

_base.py already imports Constants from .helpers, so the known-language check adds no new import. _build_unit_stats_endpoint stays a pure @staticmethod with no network access.

Why not validate against a list taken from the game data?

  • No cheap source. No small Comlink endpoint lists the supported languages. The only live source is the localization bundle, whose zip has one Loc_<LANG>.txt per language and runs to several MB. Fetching or caching it would add hidden network I/O to every get_unit_stats call.
  • Wrong service. language goes to the swgoh-stats /api service, not to Comlink. What it accepts depends on which localizations that service has loaded, and the game data can't tell us that.
  • Not needed for security. The format check plus urlencode already prevents injection.
  • Why a fixed list only warns. Raising on a fixed allowlist would reject any new language until a library release, so it only produces a warning.

Acceptance

  • The injection string above raises SwgohComlinkValueError.
  • Valid input gives the same output as today: api?flags=calcGP&language=eng_us.
  • ENG_US and eng_us are both accepted without a warning.
  • A well-formed unknown code (for example eng_uk) logs exactly one warning and is still sent unchanged.
  • An HMAC test confirms the signature still matches a request path that has a query string.
  • docs/migration.md notes that values which aren't language codes now raise instead of being passed through.

Activity

  1. added
    bug
    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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions