Skip to content

[4.x] Add ListableInterface and check ZIP data by its end of central directory - #67

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-list-contents
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-list-contents

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

Two additions that callers need to validate archives before extracting them; both are backward compatible:

  • ListableInterface::listContents(string $archive): array, implemented by Zip (with ext-zip or the PHP reader) and Tar. It returns name (as stored, not sanitised), size (declared uncompressed size) and type (file, directory or other) for every entry without extracting anything. Until now there was no public way to see what an archive contains before writing it to disk. It is a separate interface, so ExtractableInterface and custom adapters are unaffected; gzip/bzip2 hold a single stream and are not listable.
  • Zip::checkZipData() only tested whether the local file header signature PK\x03\x04 occurs anywhere in the data, so arbitrary text containing it passed. It now requires an end of central directory record at the end of the data (followed by an optional comment of up to 65535 bytes) whose central directory lies inside the data. Empty archives and ZIP64 archives are accepted.

Typed signatures for ExtractableInterface follow in a separate 5.x PR.

Testing Instructions

  • ZipTest::testListContents (native and PHP reader, with a directory entry and a ../ name), TarTest::testListContents, and the missing-archive cases.
  • ZipTest::testCheckZipDataUsesTheEndOfCentralDirectory (text with the signature, too short, empty archive with and without comment, record pointing outside the data) and testCheckZipDataRejectsTruncatedArchives. The existing testCheckZipData (real ZIP and TAR fixtures) and testCreate still pass.

All new tests fail on 4.x-dev. vendor/bin/phpunit, phpcs on src/ and PHPStan are clean.

Documentation Changes Required

docs/overview.md gains a section "Listing the entries of an archive", including the stricter checkZipData(). The owner's documentation PR #49 says "Zip::checkZipData() is a weak test"; whichever of the two is merged second should drop that paragraph.

Behaviour change in 4.x: checkZipData() returns false for data that is not a complete ZIP archive (e.g. truncated downloads), where it used to return true.

🤖 Generated with Claude Code

There was no public way to list the entries of an archive before
extracting it, which callers need to validate names and sizes. Add
ListableInterface with listContents(), implemented by Zip (natively or
with the PHP reader) and Tar.

Zip::checkZipData() only tested whether the local file header signature
occurs anywhere in the data, so any text containing "PK\x03\x04" passed.
Check for an end of central directory record at the end of the data
that points to a central directory inside it instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant