Files
Beqaandmorluto a6cca9dc62 feat(artifacts): decode UnityFS LZMA blocks (#14699)
* feat(artifacts): decode UnityFS LZMA blocks

Unity's default AssetBundle compression (type 1) left members as
hash_status unavailable. Decode 5-byte Unity LZMA streams with the
existing lzma-purejs dependency so inventory can hash and extract them.

* fix(compression): read Unity LZMA dictionary via BinaryReader

Architecture guards forbid raw Buffer numeric reads outside
binaryContainer; match the UEFI LZMA path.

* style(compression): format UnityFS LZMA helper

* fix(compression): make UnityFS LZMA cancelable and end-marker safe

Accept optional LZMA end markers without trailing junk, decode off-thread so
timer aborts land, return one expected-sized buffer, and mark xinzexi unverified.

* fix(compression): await Unity LZMA worker exit and transfer alone buffer

Await worker.terminate() before settling abort/reply paths, and allocate the
synthesized alone header with allocUnsafeSlow so the worker receives it without
a second encoded-stream copy.

* chore: retrigger CI after Greptile worker-exit follow-up

* test(compression): clarify Unity LZMA cancellation case title

Retriggers Actions after the worker-exit follow-up push did not receive a
GitHub Actions check suite.

* fix(unityfs): keep the directory for over-budget LZMA blocks and decode spec-valid streams

UnityFS writers emit one LZMA block for a whole data stream, so the per-block
decode budget refused the whole inventory of any LZMA bundle above 64 MiB,
where before this change the directory was retained. An over-budget block now
keeps the directory and only members inside it report an unavailable read
naming the budget, for LZ4 and LZMA alike.

The LZMA decoder window is bounded by the declared output instead of the
declared dictionary, so a large dictionary on a small block decodes and no
longer allocates a dictionary-sized window. Properties follow the LZMA
specification (lc 0-8, lp 0-4, pb 0-4) rather than the LZMA2 lc + lp limit.
Worker error events and exits without a reply stay runtime failures instead of
being reported as a malformed block.

Tests decode a liblzma-produced stream as a known answer, settle only after
the worker exits on completion and cancellation, and share one encoder.

* fix(artifacts): report Unity LZMA size mismatch as malformed

Overflow past the bundle's declared length is a format error, not a
decoder budget exhaustion. Declarations above maximumBytes still fail
closed as a resource limit before decode starts.

* fix(unityfs): name the encoded or decoded bound that refused a block

A block refused by its encoded size reported its decoded size against the decoded budget. The block now records which bound refused it, and the member read names that bound, its declared size and the budget.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-27 12:44:20 -05:00

118 lines
6.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# UnityFS AssetBundle archives
`inspect-artifact` and `extract-artifact` read standalone UnityFS AssetBundle
containers without running Unity or the producer application. Header format
versions 6 through 8 are supported. Version 9 adds a different block-table
layout and is unsupported until independently verified.
## Header and members
Inspection retains the format version, player and engine version strings,
declared size, dataflags, blocks-and-directory table bounds, the producer's
16-byte data hash as declared, and the resolved data-area offset. Desktop
application inspection uses the same header bounds; when its initial file probe
stops before both version strings and the fixed fields, the normal probe budget
applies to the additional bytes.
Members (`CAB-*` SerializedFiles, `.resS` resource streams, shared assets)
retain declared source paths, virtual stream offsets, sizes and raw node flags.
The directory flag identifies directories. `compare_artifacts` compares each
member's declared `source_path` and raw `flags`; its index and offset are
layout. Virtual offsets are not physical carrier locations, so member
`source_location` is null.
Member reads examine only their covering blocks. Uncompressed ranges stream
directly; compressed ranges reuse at most one completed decoded block, and
independent 64 KiB output chunks preserve cache ownership. Carrier freshness is
checked when a member is opened, including when a cached block suffices, and
after its stream completes. Closing the reader cancels and joins outstanding
streams before releasing its descriptor.
## Compression, encryption and malformed input
Data blocks and blocks-info tables compressed with none, LZMA, LZ4 or LZ4HC are
supported. Other compression values keep the directory when possible and report
unsupported coverage or an `unavailable` member read.
UnityFS writers emit one LZMA block for a whole data stream, so a large LZMA
bundle is often one block. A block above the per-decode budget below keeps the
directory: only members inside that block report an `unavailable` read naming
the budget, which this release cannot raise. An LZMA dictionary sizes only
the decoder window, which is bounded by the block's declared output, so a large
dictionary on a small block decodes. A stream may end at its declared size or
with an end marker; any other trailing bytes are malformed.
Bundles declaring asset-bundle encryption keep their header facts but report
unknown directory coverage; no decryption is attempted. The reused 0x200
encryption/padding flag is interpreted by producer revision, and ambiguous
revisions retain unknown layout coverage. Unread block counts and data offsets
are null, never invented zeroes.
These are malformed-input errors: a declared size that exceeds the input file
or excludes its header, directory or data blocks; node paths outside the block
stream; and counts that cannot fit their table. Directory and data ranges use
the declared bundle extent, so appended carrier bytes neither replace an
end-positioned directory nor become members.
## Resource bounds
These are resource policies, not format maxima.
| Resource | Bound |
| ----------------------------------------------- | -------------------------------------------------------------------------------------- |
| Encoded and decoded directory buffers | 16 MiB each |
| Buffers, parsed rows, entries, path projections | 64 MiB shared admission, reserved before allocation |
| One LZ4 or LZMA decode | 64 MiB output and LZMA window, plus its bounded encoded input; plain blocks stream |
| Active member streams per reader | 4 |
| Archive plaintext work | Larger of 256 MiB and 500 × declared bundle size |
| Member I/O operations | 4 × the larger of 1,024 and nonempty blocks + directory nodes + readable 64 KiB chunks |
Declared stream bytes, readable logical occurrences (including aliases) and
cumulative block-decode attempts are each charged against the plaintext-work
ceiling. The member-I/O budget admits ordinary passes while bounding repeated
tiny-block reads independently of output bytes. Empty-output blocks retain their
source count and offsets but are removed from member read plans. Unavailable
content is retained as named occurrences with unknown coverage and no invented
content hash.
Use summary delivery and retained inventory views for selected fields, filters
and paging; full Evidence remains available.
## CLI
```sh
rea inspect-artifact assets/hotfix.bundle --detail summary \
--evidence_output evidence/hotfix.json --json
rea extract-artifact assets/hotfix.bundle --json
```
Detection is byte-based on the `UnityFS\0` signature, so `.bundle`, `.ab`,
`assets_*` YooAsset bundles and extensionless files all classify as `unityfs`
containers.
## Verification scope
Synthetic fixtures cover format 6–8 layouts, padding independent of header
alignment, directories, malformed tables, allocation admission, overlapping
members, LZMA blocks and malformed LZMA, cancellation inside decoding, paused iteration,
close during I/O, cleanup retry, empty blocks with unsupported codecs, cache
freshness and independent output buffers. Compiled CLI and full/compact MCP
tests cover inventory and extraction.
Public bundles from the UnityPy corpus, pinned to commit
`417d59522a998fdee9a9c2b29fe35f3063222457`, are checked against independent
UnityPy 1.24.2 raw member bytes:
| Bundle | Unity version | Result |
| ------------------ | --------------------- | ------------------------------------------------------------------------------------------ |
| `atlas_test` | 2018.4.11f1, format 6 | Members match (SHA-256 `61ee5afb5f5c64f0a4f65f039d09b2509198363e713423289c895067ac215177`) |
| `banner_1` | 2018.4.4f1 | Members match |
| `char_118_yuki.ab` | 5.6.7f1 | Members match |
| `xinzexi_2_n_tex` | 2017.4.37f1, LZMA | Unverified for this change (sample not rerun); synthetic LZMA round-trips only |
UnityPy-produced repacks additionally verify format 7/8, padded and unpadded
front tables, end tables, and LZMA directories. They are library-produced, not
Unity engine executions, and synthetic fixtures alone do not establish real
Unity version coverage. SerializedFile objects, textures and runtime behavior
are outside this reader's verification scope.