Skip to content

awc_names: reject stream tables outside the declared header - #6646

Merged
shayderrr merged 2 commits into
ItsNotRexed:mainfrom
cloph-dsp:fix/awc-names-header-bound
Nov 23, 2026
Merged

shayderrr merged 2 commits into
ItsNotRexed:mainfrom
cloph-dsp:fix/awc-names-header-bound

Conversation

@cloph-dsp

Copy link
Copy Markdown
Contributor

Claim: AWC-NAMES-HEADER-BOUND (coordination #6214).

awc_names.bank_ids() unpacked the AWC header's declared header size but discarded it. It only checked the physical file size, so a bank whose stream table does not fit in its declared header was accepted and bytes belonging to tags/data were decoded as stream ids. rsc8_audio.parse_adat_header_bytes() rejects those same bytes, so the two AWC readers disagreed.

Fix: reject header_size < 16 or a stream table ending after header_size, after the existing file-size check (so the short-file error and its read behaviour are unchanged). The synthetic bank fixture now writes its real header size, and a new test covers invalid header sizes 8, 16 and 23 for a two-stream table needing 24 bytes.

AI-assisted (Hy4 flash). Synthetic bytes only — no game data, no extracted files.

Validation:

GPU/driver: N/A (pure Python, no rendering).

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted review of b25303c3. A 20-byte bank that declares a 21-byte header still returns stream ID 7. The canonical reader rejects it. The ID table fits, but the declared header extends past the end of the file.

Run this script from the repository root with py -3.14 -I probe.py:

import struct
import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path("tools").resolve()))
from awc_names import bank_ids
from rsc8_audio import parse_adat_header_bytes

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "bank.awc"
    raw = b"ADAT" + struct.pack("<IIII", 0, 1, 21, 7)
    p.write_bytes(raw)
    print(bank_ids(str(p)))                           # [7]
    print(parse_adat_header_bytes(raw, 0, len(raw)))  # None

Expected: bank_ids raises ValueError for the truncated header. Consider retaining the existing file-size result and checking the declared header size against it. This needs no additional read.

I also tested declarations of 19 and 20 bytes with the same data. The head rejects 19 and accepts 20. Merge base 1cc6e3c1 accepts all three sizes. All 31 name tests pass on Windows. The manifest suite passes 18 tests and skips two.

@Kev-Ball

Copy link
Copy Markdown

Retail compatibility check on b25303c3 (Xbox 1.0.2.1): I compared the complete ordered stream-ID list from bank_ids() on the base and revised code with the IDs decoded by rsc8_audio.parse_adat_at_file(). All three agreed for every bank tested.

Input Banks Stream-ID entries
Standalone banks extracted from two package archives 653 4,410
Valid banks found by scanning the main audio archive 7,162 1,606,241

The second group includes both zero-stream banks and 810 banks with all three optional table flags set. For that group I passed the original header bytes and a sparse file of the bank's measured length to the path-based reader. This checks that valid retail layouts survive the tightened bound. The reported malformed-header case is a separate question.

CachyOS x86-64, CPython 3.14.10, CPU only. AI-assisted.

@cloph-dsp
cloph-dsp force-pushed the fix/awc-names-header-bound branch from b25303c to d4575b1 Compare November 23, 2026 15:50
@cloph-dsp

Copy link
Copy Markdown
Contributor Author

Addressed @shdann's finding in commit d4575b1 (rebased onto current main): bank_ids() now keeps the os.fstat() file size and raises ValueError("declared header extends past the end of the file") when header_size > file_size, before the existing table-bound checks. The 20-byte/21-byte probe now raises instead of returning [7], matching rsc8_audio.parse_adat_header_bytes(), which returns None.

Added TestBanks.test_declared_header_past_the_file with the same synthetic bytes.

Validation (Windows, Python 3.14.6, synthetic inputs only): python -I tests/test_awc_names.py -> 32 tests OK; python -I tests/test_awc_manifest.py -> 20 OK (2 skips); awc_opus 56, audio_wav 40, audio_config 21, awc_markers 8, awc_shorts 13, rsc8_audio 37, audio_stage_resume 12 all OK; python -I -m compileall -q tools tests and git diff --check origin/main HEAD clean. test_opus_tags.py retains its pre-existing repr-path failure, covered by #6049/#5927/#5760.

Retail compatibility: the new bound only rejects a declared header longer than the file, which the canonical reader already rejects; Kev-Ball's ordered stream-ID comparison on real retail layouts is unaffected.

AI-assisted (Hy4 flash). GPU/driver: N/A (pure Python). No game data used.

@kapitche

Copy link
Copy Markdown
Contributor

Tested d4575b1 merged with main b2801e2 on Linux/Python 3.12.16: pass

Commands run (merged worktree, -I isolated mode):

  • python -I tests/test_awc_names.py -> Ran 32 tests, OK (skipped=1)
  • test_awc_manifest.py OK; test_opus_tags.py OK (both read banks through awc_names)
  • test_joaat_agreement.py -> FAILED (failures=6) on the merged tree and with main's tools/awc_names.py restored, so it is not from this PR.
  • Negative control: git checkout upstream/main -- tools/awc_names.py, rerun test_awc_names.py -> FAILED (failures=4, skipped=1): the new header-size cases get no ValueError. Restored with git checkout HEAD -- tools/awc_names.py.

Diff read: bank_ids now also rejects a declared header size larger than the file, smaller than 16, or too small to hold the stream table, matching what the test says rsc8_audio's parser rejects. The test helper make_bank now writes the real header size into its banks so the existing cases stay valid. I did not run rsc8_audio on the new cases to confirm that equivalence claim.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

awc_names.bank_ids() now binds the third header field as header_size and rejects a stream table whose computed table_end exceeds the declared header or where header_size < 16, with a "declared header" ValueError, before seeking. make_bank() writes the real total length at offset 12 so synthetic banks stay valid. The new test builds an ADAT bank long enough for its two-entry table but declares header sizes 8, 16, and 23, and asserts all three are refused.

Why merge

A crafted bank could previously place its stream table outside the declared header while passing the file-size check; matching the canonical rsc8_audio parser closes that disagreement.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

awc_names.bank_ids() now binds the third header field as header_size and rejects a stream table whose computed table_end exceeds the declared header or where header_size < 16, with a "declared header" ValueError, before seeking. make_bank() writes the real total length at offset 12 so synthetic banks stay valid. The new test builds an ADAT bank long enough for its two-entry table but declares header sizes 8, 16, and 23, and asserts all three are refused.

Why merge

A crafted bank could previously place its stream table outside the declared header while passing the file-size check; matching the canonical rsc8_audio parser closes that disagreement.

Verdict: Approve

@shayderrr
shayderrr merged commit 25e425a into ItsNotRexed:main Nov 23, 2026
0 of 2 checks passed
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