Skip to content

saves: reject JSAV blocks outside the JSON body - #2603

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Kosmabb8:codex/jsav-entry-bounds
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Kosmabb8:codex/jsav-entry-bounds

Conversation

@Kosmabb8

@Kosmabb8 Kosmabb8 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

A malformed JSAV entry can point outside its actual JSON document. Python slicing silently returns empty or truncated bytes, so extract could report success and diff could report identical blocks for damaged saves. Parse the outer UTF-8 JSON first, convert its end position to a byte offset, validate every entry against that boundary, and reject non-padding trailing data. Errors return exit code 2 before block exports are created or overwritten.

Claim: #2516 (comment) (new JSAV entry-bounds item). Independent of #1438's output-path validation and #2504's signature probing.

The regression cases include offsets into the header/table/padding, truncated and uint32-sized extents, and the appended-block boundary case raised in review. Invalid JSON, trailing documents and invalid UTF-8 preserve prior exports; non-ASCII content uses byte offsets correctly, and ordinary whitespace before zero padding remains accepted.

Validation on Windows 11 Pro, CPython 3.12.10, CPU only:

  • Initial range regressions failed on 49475796; the three review regressions failed on the initial PR head 24ac584b.
  • python -I tests/test_saves.py -q: 39 passed, including parser/CLI errors, byte-identical rebuilds and extract/edit/pack round trips.
  • python -I tests/test_saves_import_args.py: 1 passed.
  • python -I tests/test_save_keys.py: 24 passed.
  • python -I tests/test_text_write_line_endings.py: 2 passed on the initial patch; the review correction adds no file writers.
  • Focused compileall and git diff --check: passed.

Synthetic containers only; no game files, GPU or driver validation. No translator or C++ runtime changes. AI-assisted with Codex.

@ricarmandoagent

ricarmandoagent commented Nov 22, 2026 •

Copy link
Copy Markdown

Checked f1d4504b57ef7f0b34775bedf7dc6c8affb1e1af (Linux 7.3.4, Python 3.15.2, no game files).

I had checked the first head 24ac584b219e420d6d556777be305b3bc17849dd first. It still accepted a block copied
after the outer JSON's zero padding (entry 0 pointed at it): parse_jsav passed, diff reported "blocks
identical" and exited 0, and extract wrote partial exports before failing on the trailing bytes. Kosmabb8
reproduced that and fixed it in f1d4504 (their comment:
#2603 (comment)): the body is decoded and bounded with
json.JSONDecoder().raw_decode, and trailing non-padding data is rejected.

$ python3 -m compileall -q make_game.py tools tests   # exit 0
$ python3 -I tests/test_saves.py                      # Ran 39 tests in 0.392s ... OK
$ python3 -I tests/test_saves_import_args.py          # Ran 1 test ... OK
$ python3 -I tests/test_save_keys.py                  # Ran 24 tests ... OK

Refreshed appended-block case on f1d4504 (block 0 copied after the padding, entry 0 pointed at it; synthetic
container only):

info     rc=2  ERROR: JSAV entry 0 range 0x3b0..0x441 is outside the JSON body 0x80..0x3a5
diff     rc=2  ERROR: JSAV entry 0 range 0x3b0..0x441 is outside the JSON body 0x80..0x3a5
verify   rc=2  ERROR: JSAV entry 0 range 0x3b0..0x441 is outside the JSON body 0x80..0x3a5
extract  rc=2  ERROR: JSAV entry 0 range 0x3b0..0x441 is outside the JSON body 0x80..0x3a5
# no blocks/ directory is created

Also probed on this head: a second JSON document no entry points at ("JSAV has non-padding data after the JSON
body"), a malformed body and invalid UTF-8 ("JSAV body is not valid UTF-8 JSON") — all exit 2 with no partial
exports (index.json untouched); a valid body with trailing whitespace before the padding still parses
(body_end 933).

No defect found in f1d4504.

AI-assisted review.

@fredrocha9

Copy link
Copy Markdown

Independent Windows check of 24ac584b against base 49475796, Python 3.13.15. All 36 test_saves.py tests and the import-argument test passed. The base still accepted the out-of-file entry in info, extract, and diff with exit 0. In test_save_keys.py, 23 checks passed and one could not finish here because the sandbox denied os.link with WinError 5.

One boundary case remains if the guard is intended to cover the JSON body itself. I appended a copy of the first block after the original save's zero padding, then pointed entry 0 at that appended block. In the synthetic fixture, the outer JSON ends at byte 933 and entry 0 starts at 944. parse_jsav() reports body_end=1089 because rstrip(b'\0') treats the appended bytes as part of the body.

With that fixture, diff against itself still reports 5 blocks identical and exits 0. extract writes all five block files and index.json before the JSON parser rejects the trailing bytes as Extra data and exits 2. Could a regression case distinguish the end of the outer JSON from the last nonzero byte? At minimum, parsing the body before creating exports would prevent partial output when extract encounters this corruption.

AI-assisted. Synthetic files only, with no game files or GPU.

@Kosmabb8

Copy link
Copy Markdown
Contributor Author

Reproduced the appended-block case and corrected it in f1d4504. parse_jsav() now decodes the outer JSON before any export, derives body_end from the decoder's document end in UTF-8 bytes, and rejects trailing non-padding data. Entry 0 pointing to a copied block after the original padding is now rejected as outside the body.

Added your case plus regressions for malformed JSON, a trailing document, invalid UTF-8, non-ASCII byte offsets and whitespace before zero padding. Invalid inputs leave an existing index.json untouched and create no blocks/. The three new tests failed on 24ac584. With the correction: 39 save tests, 1 import-argument test and 24 save-key tests pass; compile/diff checks pass. Synthetic files on Windows 11, Python 3.12.10; no game files or GPU.

The PR description and local checkpoint now reflect the correction. AI-assisted with Codex.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested f1d4504 merged onto main 4947579 (clean merge) on Windows 11, Python 3.13: pytest tests/test_saves.py: 39 passed, 19 subtests passed. Negative control: the same tests against main's copies of the changed sources give 17 failed, so the tests really cover the change.

AI-assisted (Claude Code); runs on my machine.

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.

5 participants