saves: reject JSON nested deeper than 512 levels explicitly #8

Merged
leonida/mandy
wavehs merged 1 commit from saves-json-nesting-limit into main 2026-11-24 01:40:26 +00:00

Pull request successfully merged and closed

wavehs commented 2026-11-24 01:30:34 +00:00

Claimed in the coordination issue as: none — the original project's coordination issue was lost with its repository, and this repository has no such issue yet. No open pull request touches tools/saves.py.

Depends on: none.

What and why

Python 3.14's json decoder is iterative, so a deeply nested document no longer raises RecursionError. parse_jsav accepted a body nesting 100,000 levels, info printed JSAV (200112 bytes), verify said OK, and extract died with a 1,027-line traceback and exit 1 (json.dump is still recursive). tests/test_saves.py already expects a validation error here and its comment notes that the C decoder's limit differs by platform, so the tool should not depend on the interpreter's.

The tool now counts nesting itself (JSON_NESTING_LIMIT = 512, brackets inside strings ignored) in parse_jsav, in verify's per-block check and when it reads a packed index. 512 is far above real saves (a few levels) and below the depth at which Python's recursive encoder fails around the default recursion limit. docs/saves.md records the rule.

Tested

main@30daa6958, macOS 15 arm64 (Apple M3 Pro), CPython 3.15.2, CPU only, no game files. Synthetic JSAV with one block {"nested": + 100,000 [ + 0 + 100,000 ] + }:

command main@30daa6958 this commit
saves.py info exit 0, prints the save exit 2, ERROR: JSAV body is not valid UTF-8 JSON: nesting exceeds 512 levels
saves.py verify exit 0, OK exit 2, same error
saves.py extract exit 1, 1,027-line traceback exit 2, one error line

python -I tests/test_saves.py → 70 tests OK (skipped=1) with the fix, FAILED (failures=7) before it on this Python. All tests/test_docs_*.py pass, compileall clean, check_no_game_data.py clean.

On Python 3.12 (CI) the depth guard is additional and the suite is green there before and after. Not run: Windows, any GPU test, the translator suite.

AI-assisted: yes.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)
Claimed in the coordination issue as: none — the original project's coordination issue was lost with its repository, and this repository has no such issue yet. No open pull request touches `tools/saves.py`. Depends on: none. ### What and why Python 3.14's `json` decoder is iterative, so a deeply nested document no longer raises `RecursionError`. `parse_jsav` accepted a body nesting 100,000 levels, `info` printed `JSAV (200112 bytes)`, `verify` said `OK`, and `extract` died with a 1,027-line traceback and exit 1 (`json.dump` is still recursive). `tests/test_saves.py` already expects a validation error here and its comment notes that the C decoder's limit differs by platform, so the tool should not depend on the interpreter's. The tool now counts nesting itself (`JSON_NESTING_LIMIT = 512`, brackets inside strings ignored) in `parse_jsav`, in `verify`'s per-block check and when it reads a packed index. 512 is far above real saves (a few levels) and below the depth at which Python's recursive encoder fails around the default recursion limit. `docs/saves.md` records the rule. ### Tested `main@30daa6958`, macOS 15 arm64 (Apple M3 Pro), CPython 3.15.2, CPU only, no game files. Synthetic JSAV with one block `{"nested":` + 100,000 `[` + `0` + 100,000 `]` + `}`: | command | `main@30daa6958` | this commit | |---|---|---| | `saves.py info` | exit 0, prints the save | exit 2, `ERROR: JSAV body is not valid UTF-8 JSON: nesting exceeds 512 levels` | | `saves.py verify` | exit 0, `OK` | exit 2, same error | | `saves.py extract` | exit 1, 1,027-line traceback | exit 2, one error line | `python -I tests/test_saves.py` → 70 tests OK (skipped=1) with the fix, `FAILED (failures=7)` before it on this Python. All `tests/test_docs_*.py` pass, `compileall` clean, `check_no_game_data.py` clean. On Python 3.12 (CI) the depth guard is additional and the suite is green there before and after. Not run: Windows, any GPU test, the translator suite. AI-assisted: yes. ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (Not applicable: no translator change.)
Sign in to join this conversation.
No description provided.