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

Manually merged
github merged 1 commit from gh-pr/8/head into gh-pr/8/base 2026-11-26 08:40:06 +00:00
Owner

@⁠wavehs on GitHub · 2026-11-24 01:30 UTC · original
wavehs:saves-json-nesting-limit → main · +41 −1 · 2 files · 1 commits · merged 2026-11-24 01:40 UTC

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.)
**@⁠wavehs** on GitHub · 2026-11-24 01:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/8) `wavehs:saves-json-nesting-limit` → `main` · +41 −1 · 2 files · 1 commits · merged 2026-11-24 01:40 UTC 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.)
Python 3.14's json decoder is iterative, so a 100,000-level JSAV body no
longer raises RecursionError: parse_jsav accepted it, info printed
JSAV (...), verify said OK and extract died with a 1,027-line traceback
and exit 1 (json.dump recurses). The tests already expected a validation
error and noted that the C decoder's limit differs by platform; the tool
now checks the nesting depth itself (JSON_NESTING_LIMIT = 512, brackets
inside strings ignored) in parse_jsav, in verify's per-block check and in
the packed index, so a malformed save is one error line and exit 2 on
every Python.

(cherry picked from commit 79da3a9161fb0a238ddf4fd5179559bcd69c44a0)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:40:04 +00:00
github manually merged commit 20c7188efd into gh-pr/8/base 2026-11-26 08:40:06 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/ubiquitarianism--leonida!8
No description provided.