[PR #8] [MERGED] saves: reject JSON nested deeper than 512 levels explicitly #66

Closed
opened 2026-11-26 20:16:41 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/8
Author: @wavehs
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @ubiquitarianism

Base: main ← Head: saves-json-nesting-limit


📝 Commits (1)

  • 20c7188 saves: reject JSON nested deeper than 512 levels explicitly

📊 Changes

2 files changed (+41 additions, -1 deletions)

View changed files

📝 docs/saves.md (+5 -0)
📝 tools/saves.py (+36 -1)

📄 Description

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.)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/8 **Author:** [@wavehs](https://github.com/wavehs) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@ubiquitarianism](https://github.com/ubiquitarianism) **Base:** `main` ← **Head:** `saves-json-nesting-limit` --- ### 📝 Commits (1) - [`20c7188`](https://github.com/ubiquitarianism/leonida/commit/20c7188efd82724eb32dd3baf383b0752c48a1a9) saves: reject JSON nested deeper than 512 levels explicitly ### 📊 Changes **2 files changed** (+41 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `docs/saves.md` (+5 -0) 📝 `tools/saves.py` (+36 -1) </details> ### 📄 Description 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.) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:16:41 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#66
No description provided.