Repository navigation
Description
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:
- Format check, which raises. Validate
languageagainst^[A-Za-z]{2,3}_[A-Za-z]{2}$and raiseSwgohComlinkValueErrorfor anything else. This is the security fix: no value that passes can contain&,#,=or/. - Known-language check, which only warns. If a value passes the format check but isn't in
Constants.LANGUAGES(the 14 entries inhelpers/_stat_data.py, compared case-insensitively), log alogger.warningnaming the value and the known languages, then send it anyway. This catches typos such aseng_ukwithout 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>.txtper language and runs to several MB. Fetching or caching it would add hidden network I/O to everyget_unit_statscall. - Wrong service.
languagegoes to the swgoh-stats/apiservice, 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
urlencodealready 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_USandeng_usare 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.mdnotes that values which aren't language codes now raise instead of being passed through.
Activity
github-actions commentedon Nov 7, 2026
Addressed by #258, which is now merged into
develop. This issue will close automatically when that change is released tomain.