Skip to content

Core\Zip::entries

Lists the names of the files and folders in a zip archive, in the order the archive stores them.

The archive is checked before any name is returned. If one name is unsafe, the call throws a ParseError and returns no list. A name is unsafe when it starts at the root, such as /etc/passwd, or goes up a folder with ... A symbolic link, a repeated name and a name inside more than 64 folders are unsafe too. Bytes that are not a zip archive also throw a ParseError.

Each name is a tainted string, because the person who made the archive chose it.

The archive can be bytes, an open file, an uploaded Core\Request\Part or a Core\Request\BodyStream. A file is read in small pieces. An upload is kept in memory while it is read.

Good to know: listing reads only the table of contents at the end of the archive. So the time depends on the number of files in the archive, not on their size.

The examples below list an archive file, catch an unsafe archive, and check an upload.

Parameters

  • $archiveCore\Bytes::SourceThe archive. It can be bytes, an open Core\IO\File, an uploaded Core\Request\Part or a Core\Request\BodyStream. Only its central directory is read. So the time to list an archive depends on the number of entries, not on the size of the archive. A file is read in small pieces and is never held whole.

Return value

Returns array<tainted string>. One tainted string per entry. A name is always tainted, whatever the type of the archive was. The name was written by whoever built the archive. An archive written directly in a test is no safer than a downloaded one.

Errors

  • ParseError$archive is not a valid zip archive, or one of its entries is unsafe. An entry is unsafe when it has an absolute path or a .. part, or is a symlink. It is also unsafe when it is nested in more than 64 folders, or repeats a name. A bad archive never throws an IOError. That error is for a failing disk.

Examples

List the files in an archive

<?nvs
// Shows how to list the names in a zip archive file.
// A small archive, written as base64 text so it fits in this file. It contains
// `readme.txt`, the folder `images/`, `images/logo.png` and `index.html`.
bytes $archive = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVyCidH3BQAAAAUAAAAKAAAAcmVhZG1lLnR4dEhlbGxvUEsDBBQAAAAAAAAAIVwAAAAAAAAAAAAAAAAHAAAAaW1hZ2VzL1BLAwQUAAAAAAAAACFcGkG/FQMAAAADAAAADwAAAGltYWdlcy9sb2dvLnBuZ1BOR1BLAwQUAAAAAAAAACFcj7atpwsAAAALAAAACgAAAGluZGV4Lmh0bWw8aDE+SGk8L2gxPlBLAQIUABQAAAAAAAAAIVyCidH3BQAAAAUAAAAKAAAAAAAAAAAAAACkgQAAAAByZWFkbWUudHh0UEsBAhQAFAAAAAAAAAAhXAAAAAAAAAAAAAAAAAcAAAAAAAAAAAAAAO1BLQAAAGltYWdlcy9QSwECFAAUAAAAAAAAACFcGkG/FQMAAAADAAAADwAAAAAAAAAAAAAApIFSAAAAaW1hZ2VzL2xvZ28ucG5nUEsBAhQAFAAAAAAAAAAhXI+2racLAAAACwAAAAoAAAAAAAAAAAAAAKSBggAAAGluZGV4Lmh0bWxQSwUGAAAAAAQABADiAAAAtQAAAAAA');
// The program writes the archive to a file first, so that it has one to open.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'site.zip');
Core\IO::write($path, $archive);
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// `Core\Zip::entries` takes the open file and returns the names in the order the
// archive stores them. A folder name ends with `/`. This prints four lines.
foreach (Core\Zip::entries($file) as tainted string $name) {
echo $name, "\n";
}
// The result is an ordinary array, so you can count it. This prints 4.
echo Core\Arr::count(Core\Zip::entries($file)), "\n";
$file->close();
Output
readme.txt
images/
images/logo.png
index.html
4

An archive with an unsafe name

<?nvs
// Shows what happens when an archive contains an unsafe name.
// This archive contains `notes.txt` and `../../config.php`. The second name
// points two folders above the folder you unpack into.
bytes $archive = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVyGphA2BQAAAAUAAAAJAAAAbm90ZXMudHh0aGVsbG9QSwMEFAAAAAAAAAAhXFm9+eMNAAAADQAAABAAAAAuLi8uLi9jb25maWcucGhwPD9waHAgZXZpbCgpO1BLAQIUABQAAAAAAAAAIVyGphA2BQAAAAUAAAAJAAAAAAAAAAAAAACkgQAAAABub3Rlcy50eHRQSwECFAAUAAAAAAAAACFcWb354w0AAAANAAAAEAAAAAAAAAAAAAAApIEsAAAALi4vLi4vY29uZmlnLnBocFBLBQYAAAAAAgACAHUAAABnAAAAAAA=');
// `entries` checks every name first. One unsafe name throws a `ParseError`,
// so you get no list at all, not even `notes.txt`. This prints "not listed".
try {
Core\Zip::entries($archive);
echo "listed\n";
} catch (ParseError $error) {
echo "not listed\n";
}
// The error message names the unsafe entry. This prints `true`.
try {
Core\Zip::entries($archive);
} catch (ParseError $error) {
echo Core\Str::contains($error->message, '../../config.php') ? 'true' : 'false', "\n";
}
// Bytes that are not a zip archive throw a `ParseError` too.
try {
Core\Zip::entries('just some text' as bytes);
} catch (ParseError $error) {
echo "not an archive\n";
}
Output
not listed
true
not an archive

Check an uploaded theme

<?nvs
// Shows how to check an uploaded theme before the program accepts it.
class Themes
{
// Returns why the upload is not accepted, or "accepted".
public static function check(bytes $upload): string
{
try {
array<tainted string> $names = Core\Zip::entries($upload);
} catch (ParseError $error) {
return 'not a safe zip archive';
}
// A theme may contain at most 50 files.
if (Core\Arr::count($names) > 50) {
return 'too many files';
}
// A theme must contain `theme.json` at the top level.
foreach ($names as tainted string $name) {
if ($name == 'theme.json') {
return 'accepted';
}
}
return 'theme.json is missing';
}
}
// This upload contains `theme.json`, `style.css` and `logo.png`.
bytes $good = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVyX/RwUDwAAAA8AAAAKAAAAdGhlbWUuanNvbnsibmFtZSI6IkRhcmsifVBLAwQUAAAAAAAAACFcWXGH4gYAAAAGAAAACQAAAHN0eWxlLmNzc2JvZHl7fVBLAwQUAAAAAAAAACFcGkG/FQMAAAADAAAACAAAAGxvZ28ucG5nUE5HUEsBAhQAFAAAAAAAAAAhXJf9HBQPAAAADwAAAAoAAAAAAAAAAAAAAKSBAAAAAHRoZW1lLmpzb25QSwECFAAUAAAAAAAAACFcWXGH4gYAAAAGAAAACQAAAAAAAAAAAAAApIE3AAAAc3R5bGUuY3NzUEsBAhQAFAAAAAAAAAAhXBpBvxUDAAAAAwAAAAgAAAAAAAAAAAAAAKSBZAAAAGxvZ28ucG5nUEsFBgAAAAADAAMApQAAAI0AAAAAAA==');
// This upload contains only `style.css` and `logo.png`.
bytes $incomplete = Core\Encoding::fromBase64('UEsDBBQAAAAAAAAAIVxZcYfiBgAAAAYAAAAJAAAAc3R5bGUuY3NzYm9keXt9UEsDBBQAAAAAAAAAIVwaQb8VAwAAAAMAAAAIAAAAbG9nby5wbmdQTkdQSwECFAAUAAAAAAAAACFcWXGH4gYAAAAGAAAACQAAAAAAAAAAAAAApIEAAAAAc3R5bGUuY3NzUEsBAhQAFAAAAAAAAAAhXBpBvxUDAAAAAwAAAAgAAAAAAAAAAAAAAKSBLQAAAGxvZ28ucG5nUEsFBgAAAAACAAIAbQAAAFYAAAAAAA==');
// This prints "accepted", "theme.json is missing" and "not a safe zip archive".
echo Themes::check($good), "\n";
echo Themes::check($incomplete), "\n";
echo Themes::check('an image, not an archive' as bytes), "\n";
Output
accepted
theme.json is missing
not a safe zip archive