tooling: reject deeply nested bencode in decode_torrent #637

Merged
louisdibiasi merged 2 commits from upstream-pr/643 into main 2026-11-22 18:31:02 +00:00
louisdibiasi commented 2026-11-22 17:16:31 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by louisdibiasi on 2026-11-22T17:16:31Z.


Change

decode_torrent in tools/verify_build.py reads lists and dictionaries recursively with no depth limit. A torrent that nests a few thousand levels, closed or not, exhausts the interpreter's recursion limit and raises RecursionError. The decoder's contract, checked by check_decode_torrent, is that malformed input raises ValueError. This adds MAX_TORRENT_DEPTH = 64 and raises ValueError("Torrent nesting too deep") beyond it. Real multi-file torrents nest about four levels.

Independent problem, no linked issue.

Verification

Commit under test: 534f673 on b03f754, Windows 11, Python 3.12.10.

python -B tests/layout_chunks_check.py

  • Base with the new cases: RecursionError: maximum recursion depth exceeded, exit 1.
  • This branch: layout chunk summary ok, exit 0.

New cases in check_decode_torrent: b"l"*5000 + b"e"*5000 and b"l"*5000 and b"d1:a"*5000 must raise ValueError; a 32-level list still decodes. I also checked the boundary by hand: 64 levels decode, 65 raise ValueError.

Evidence and follow-up

Synthetic bytes only. The CLI (python tools/verify_build.py <dir>) still ends with an uncaught ValueError traceback for any malformed torrent, including this one; that is existing behavior for every malformed torrent and I left it out to keep this PR to the decoder. I can send a separate PR for reporting torrent errors through parser.error.

Checklist

  • One reviewable behavior.
  • Exact commands and results above.
  • Negative control: the new cases fail on base.
  • No README/docs change needed.
  • No game files, dumps, links or secrets.

AI-assisted: I used Claude to find the bug and draft the change. I ran the reproductions and tests above myself.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6545329757 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/643). Originally posted by [louisdibiasi](https://github.com/louisdibiasi) on 2026-11-22T17:16:31Z. --- ## Change `decode_torrent` in `tools/verify_build.py` reads lists and dictionaries recursively with no depth limit. A torrent that nests a few thousand levels, closed or not, exhausts the interpreter's recursion limit and raises `RecursionError`. The decoder's contract, checked by `check_decode_torrent`, is that malformed input raises `ValueError`. This adds `MAX_TORRENT_DEPTH = 64` and raises `ValueError("Torrent nesting too deep")` beyond it. Real multi-file torrents nest about four levels. Independent problem, no linked issue. ## Verification Commit under test: 534f673 on b03f754, Windows 11, Python 3.12.10. `python -B tests/layout_chunks_check.py` - Base with the new cases: `RecursionError: maximum recursion depth exceeded`, exit 1. - This branch: `layout chunk summary ok`, exit 0. New cases in `check_decode_torrent`: `b"l"*5000 + b"e"*5000` and `b"l"*5000` and `b"d1:a"*5000` must raise `ValueError`; a 32-level list still decodes. I also checked the boundary by hand: 64 levels decode, 65 raise `ValueError`. ## Evidence and follow-up Synthetic bytes only. The CLI (`python tools/verify_build.py <dir>`) still ends with an uncaught `ValueError` traceback for any malformed torrent, including this one; that is existing behavior for every malformed torrent and I left it out to keep this PR to the decoder. I can send a separate PR for reporting torrent errors through `parser.error`. ## Checklist - [x] One reviewable behavior. - [x] Exact commands and results above. - [x] Negative control: the new cases fail on base. - [x] No README/docs change needed. - [x] No game files, dumps, links or secrets. AI-assisted: I used Claude to find the bug and draft the change. I ran the reproductions and tests above myself.
upstream-sync closed this pull request 2026-11-24 06:44:07 +00:00
legacy-code-archaeologist commented 2026-11-24 06:48:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T17:35:29Z.


Coordination note: this PR and #644 both edit tests/layout_chunks_check.py. I checked 534f673 (#643) and a6668be (#644) on Windows 11 26100.9168 with Python 3.12.10:

  • Each one merges cleanly with main at 8f57366.
  • They also merge cleanly with each other.
  • With both applied, python -B tests/layout_chunks_check.py prints layout chunk summary ok.

They can land in either order.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7082954588 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/643#issuecomment-7082954588). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T17:35:29Z. --- Coordination note: this PR and #644 both edit `tests/layout_chunks_check.py`. I checked `534f673` (#643) and `a6668be` (#644) on Windows 11 26100.9168 with Python 3.12.10: - Each one merges cleanly with `main` at `8f57366`. - They also merge cleanly with each other. - With both applied, `python -B tests/layout_chunks_check.py` prints `layout chunk summary ok`. They can land in either order. AI-assisted (Claude Code).
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
R0Y00/Xbox2Windows!637
No description provided.