Skip to content

Reject game mode JSON that lobby clients cannot read - #3074

Open
moss-orbit-9f339e wants to merge 1 commit into
ZeroK-RTS:masterfrom
moss-orbit-9f339e:mods-validate-json
Open

moss-orbit-9f339e wants to merge 1 commit into
ZeroK-RTS:masterfrom
moss-orbit-9f339e:mods-validate-json

Conversation

@moss-orbit-9f339e

Copy link
Copy Markdown
Contributor

The mod page saved the game mode JSON without a check. A mode with a trailing comma was saved and sent to clients, but Chobby cannot read it, so the mode disappears from the list.
This change parses the JSON with System.Text.Json (strict JSON; the site already gets this package through ZkLobbyServer) on create and edit. It also requires an object with a "name" field, which Chobby needs. The error message gives the line and position. 47 stored modes that Chobby loads are not strict JSON (mostly a missing comma): they keep working, but their next save needs a fix (list in the first comment).
Tested the method under Mono on 54 JSON files against Chobby's real json.lua under Lua 5.1: it rejects every file that Chobby cannot load, except valid JSON 1E5 (Chobby's decoder does not read a capital E). Details are in the first comment.

Fixes #3073

This PR was made with AI help (Claude Code); I checked the change and the test.

🤖 Generated with Claude Code

The mod edit page stored GameModeJson unchecked, so a mode with a trailing
comma was saved and sent to clients, where Chobby's decoder rejects it and
the mode disappears from the list. Parse it with System.Text.Json (strict
JSON, already referenced by ZkLobbyServer) on create and edit, require an object
with a "name" text field, and show the line and position of the error.

Fixes ZeroK-RTS#3073

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@moss-orbit-9f339e

Copy link
Copy Markdown
Contributor Author

Test details.

Corpus. 54 small files. Each one went through Chobby's LuaMenu/Addons/json.lua (master 982028c3, unchanged, Lua 5.1, with the same loadFile and name steps as GetCustomModes) and through the new GetGameModeJsonError method (copied from the branch, Mono 6.8, System.Text.Json 5.0.0 net461 DLL).

input Chobby this PR
trailing comma in object (this issue), in array; double comma cannot load rejects
unquoted key; NaN, Infinity, -Infinity; +1; 1e; 0x10; True; undefined cannot load rejects
BOM; empty; whitespace only; unterminated object or string; raw newline in string cannot load rejects
top-level array, string or null; object without "name" not listed rejects
top-level number or true Lua error in GetCustomModes rejects
valid modes (CRLF, all value types, \u escapes, duplicate keys, empty key) loads accepts
1E5 (valid JSON) cannot load (no capital E in its number scanner) accepts
// and /* */ comments; single quotes; missing comma; leading comma; leading zero; .5; 1.; 1-1; number key; text after the object; raw tab or control character; \q \x41 \065 escapes loads rejects
valid JSON nested 100 or 1000 deep loads rejects (System.Text.Json max depth 64)

Every input that Chobby cannot load is rejected, except 1E5. The last two rows are stricter than Chobby: they are not JSON (or nest far deeper than a mode file).

The issue case gives: Game mode JSON is not valid at line 4, position 1: The JSON object contains a trailing comma at the end which is not supported in this mode. Change the reader options.

Stored modes. I downloaded all 248 modes listed on https://zero-k.info/Mods (8 featured) and ran the same two checks.

  • Chobby cannot load 14 of them today. None of them are featured: FW, 11 (its JSON is the number 11, so Chobby raises a Lua error in GetCustomModes while the file is in CustomModes), futurewarsnuclear, zk_constricting_ring, FwMu, VMRTS, X, fortnite, old_no_ai_super, saturdaygaming, TTTEST, AntiChick, chicken_disable_units, testcomma. This PR rejects all 14 at their next save. No mode named siegebots is in the list.
  • Chobby loads 47 more that are not valid JSON. 38 have a missing comma, 8 have an extra } at the end, and 1 has a second object after the first. They keep working, but their next save shows the error with its line until the JSON is fixed.
The 47 modes

Test, king_builder, flying_ships, infection, limited_tech, old_random_morphs, 1_unit, old_strider_goo, CAPTURE_SIDEARM, detriment_rush, ulti_spam, capture, zombie_building, chicken_farm_mex, half_price, Flip, i_love_geo, Geo_Flip, wallmap, rampmap, freakmap, sphere, MYAI, diceandsplice, ora, dasor, noregenhermit, randland, BossAI, fallback, ScaleAI, kingAI, chickmex, temple, moreme, terraforma, upgrader, ultimateAI, UltAIGreed, SliceTemple, UltimateAIGEOFlip, UltimateAIsplice, sg5050f, UltimateAIRelick, SKUAI, rampmapMEGA, FORT

  • The 8 featured modes all pass.

Build. I could not build the web project here (no .NET Framework web build on this machine). The method was compiled alone with mcs, with the same System.Text.Json 5.0.0 DLL that the site already loads through ZkLobbyServer.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom mod upload accepts invalid JSON

1 participant