81 $file = fopen( $fileName,
'r' );
83 return $zdr->execute();
99 return $zdr->execute();
125 private const ZIP64_EXTRA_HEADER = 0x0001;
128 private const SEGSIZE = 16384;
131 private const GENERAL_UTF8 = 11;
134 private const GENERAL_CD_ENCRYPTED = 13;
145 if ( isset( $options[
'zip64'] ) ) {
146 $this->zip64 = $options[
'zip64'];
155 private function execute() {
156 if ( !$this->file ) {
157 return StatusValue::newFatal(
'zip-file-open-error' );
160 $status = StatusValue::newGood();
162 $this->readEndOfCentralDirectoryRecord();
163 if ( $this->zip64 ) {
164 [ $offset, $size ] = $this->findZip64CentralDirectory();
165 $this->readCentralDirectory( $offset, $size );
167 if ( $this->eocdr[
'CD size'] == 0xffffffff
168 || $this->eocdr[
'CD offset'] == 0xffffffff
169 || $this->eocdr[
'CD entries total'] == 0xffff
171 $this->error(
'zip-unsupported',
'Central directory header indicates ZIP64, ' .
172 'but we are in legacy mode. Rejecting this upload is necessary to avoid ' .
173 'opening vulnerabilities on clients using OpenJDK 7 or later.' );
176 [ $offset, $size ] = $this->findOldCentralDirectory();
177 $this->readCentralDirectory( $offset, $size );
179 }
catch ( ZipDirectoryReaderError $e ) {
180 $status->fatal( $e->getErrorCode() );
183 fclose( $this->file );
195 private function error( $code, $debugMessage ): never {
196 wfDebug( __CLASS__ .
": Fatal error: $debugMessage" );
197 throw new ZipDirectoryReaderError( $code );
205 private function readEndOfCentralDirectoryRecord() {
209 'CD start disk' => 2,
210 'CD entries this disk' => 2,
211 'CD entries total' => 2,
214 'file comment length' => 2,
216 $structSize = $this->getStructSize( $info );
217 $startPos = $this->getFileLength() - 65536 - $structSize;
218 if ( $startPos < 0 ) {
222 if ( $this->getFileLength() === 0 ) {
223 $this->error(
'zip-wrong-format',
"The file is empty." );
226 $block = $this->getBlock( $startPos );
227 $sigPos = strrpos( $block,
"PK\x05\x06" );
228 if ( $sigPos ===
false ) {
229 $this->error(
'zip-wrong-format',
230 "zip file lacks EOCDR signature. It probably isn't a zip file." );
233 $this->eocdr = $this->unpack( substr( $block, $sigPos ), $info );
234 $this->eocdr[
'EOCDR size'] = $structSize + $this->eocdr[
'file comment length'];
236 if ( $structSize + $this->eocdr[
'file comment length'] != strlen( $block ) - $sigPos ) {
238 $this->error(
'zip-wrong-format',
'there is a ZIP signature but it is not at ' .
239 'the end of the file. It could be an OLE file with a ZIP file embedded.' );
241 if ( $this->eocdr[
'disk'] !== 0
242 || $this->eocdr[
'CD start disk'] !== 0
244 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR)' );
246 $this->eocdr += $this->unpack(
248 [
'file comment' => [
'string', $this->eocdr[
'file comment length'] ] ],
249 $sigPos + $structSize );
250 $this->eocdr[
'position'] = $startPos + $sigPos;
257 private function readZip64EndOfCentralDirectoryLocator() {
259 'signature' => [
'string', 4 ],
260 'eocdr64 start disk' => 4,
261 'eocdr64 offset' => 8,
262 'number of disks' => 4,
264 $structSize = $this->getStructSize( $info );
266 $start = $this->getFileLength() - $this->eocdr[
'EOCDR size'] - $structSize;
267 $block = $this->getBlock( $start, $structSize );
268 $this->eocdr64Locator = $data = $this->unpack( $block, $info );
270 if ( $data[
'signature'] !==
"PK\x06\x07" ) {
274 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory locator' );
282 private function readZip64EndOfCentralDirectoryRecord() {
283 if ( $this->eocdr64Locator[
'eocdr64 start disk'] != 0
284 || $this->eocdr64Locator[
'number of disks'] != 0
286 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64 locator)' );
290 'signature' => [
'string', 4 ],
292 'version made by' => 2,
293 'version needed' => 2,
295 'CD start disk' => 4,
296 'CD entries this disk' => 8,
297 'CD entries total' => 8,
301 $structSize = $this->getStructSize( $info );
302 $block = $this->getBlock( $this->eocdr64Locator[
'eocdr64 offset'], $structSize );
303 $this->eocdr64 = $data = $this->unpack( $block, $info );
304 if ( $data[
'signature'] !==
"PK\x06\x06" ) {
305 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory record' );
307 if ( $data[
'disk'] !== 0
308 || $data[
'CD start disk'] !== 0
310 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64)' );
320 private function findOldCentralDirectory() {
321 $size = $this->eocdr[
'CD size'];
322 $offset = $this->eocdr[
'CD offset'];
323 $endPos = $this->eocdr[
'position'];
327 if ( $offset + $size != $endPos ) {
328 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
329 'of central directory record' );
332 return [ $offset, $size ];
341 private function findZip64CentralDirectory() {
345 $size = $this->eocdr[
'CD size'];
346 $offset = $this->eocdr[
'CD offset'];
347 $numEntries = $this->eocdr[
'CD entries total'];
348 $endPos = $this->eocdr[
'position'];
349 if ( $size == 0xffffffff
350 || $offset == 0xffffffff
351 || $numEntries == 0xffff
353 $this->readZip64EndOfCentralDirectoryLocator();
355 if ( isset( $this->eocdr64Locator[
'eocdr64 offset'] ) ) {
356 $this->readZip64EndOfCentralDirectoryRecord();
357 if ( isset( $this->eocdr64[
'CD offset'] ) ) {
358 $size = $this->eocdr64[
'CD size'];
359 $offset = $this->eocdr64[
'CD offset'];
360 $endPos = $this->eocdr64Locator[
'eocdr64 offset'];
366 if ( $offset + $size != $endPos ) {
367 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
368 'of central directory record' );
371 return [ $offset, $size ];
379 private function readCentralDirectory( $offset, $size ) {
380 $block = $this->getBlock( $offset, $size );
383 'signature' => [
'string', 4 ],
384 'version made by' => 2,
385 'version needed' => 2,
387 'compression method' => 2,
391 'compressed size' => 4,
392 'uncompressed size' => 4,
394 'extra field length' => 2,
395 'comment length' => 2,
396 'disk number start' => 2,
397 'internal attrs' => 2,
398 'external attrs' => 4,
399 'local header offset' => 4,
401 $fixedSize = $this->getStructSize( $fixedInfo );
404 while ( $pos < $size ) {
405 $data = $this->unpack( $block, $fixedInfo, $pos );
408 if ( $data[
'signature'] !==
"PK\x01\x02" ) {
409 $this->error(
'zip-bad',
'Invalid signature found in directory entry' );
413 'name' => [
'string', $data[
'name length'] ],
414 'extra field' => [
'string', $data[
'extra field length'] ],
415 'comment' => [
'string', $data[
'comment length'] ],
417 $data += $this->unpack( $block, $variableInfo, $pos );
418 $pos += $this->getStructSize( $variableInfo );
420 if ( $this->zip64 && (
421 $data[
'compressed size'] == 0xffffffff
422 || $data[
'uncompressed size'] == 0xffffffff
423 || $data[
'local header offset'] == 0xffffffff )
425 $zip64Data = $this->unpackZip64Extra( $data[
'extra field'] );
427 $data = $zip64Data + $data;
431 if ( $this->testBit( $data[
'general bits'], self::GENERAL_CD_ENCRYPTED ) ) {
432 $this->error(
'zip-unsupported',
'central directory encryption is not supported' );
438 $time = $data[
'mod time'];
439 $date = $data[
'mod date'];
441 $year = 1980 + ( $date >> 9 );
442 $month = ( $date >> 5 ) & 15;
444 $hour = ( $time >> 11 ) & 31;
445 $minute = ( $time >> 5 ) & 63;
446 $second = ( $time & 31 ) * 2;
447 $timestamp = sprintf(
"%04d%02d%02d%02d%02d%02d",
448 $year, $month, $day, $hour, $minute, $second );
451 if ( $this->testBit( $data[
'general bits'], self::GENERAL_UTF8 ) ) {
452 $name = $data[
'name'];
454 $name = iconv(
'CP437',
'UTF-8', $data[
'name'] );
460 'mtime' => $timestamp,
461 'size' => $data[
'uncompressed size'],
463 ( $this->callback )( $userData );
472 private function unpackZip64Extra( $extraField ) {
477 $extraHeaderSize = $this->getStructSize( $extraHeaderInfo );
480 'uncompressed size' => 8,
481 'compressed size' => 8,
482 'local header offset' => 8,
483 'disk number start' => 4,
487 while ( $extraPos < strlen( $extraField ) ) {
488 $extra = $this->unpack( $extraField, $extraHeaderInfo, $extraPos );
489 $extraPos += $extraHeaderSize;
490 $extra += $this->unpack( $extraField,
491 [
'data' => [
'string', $extra[
'size'] ] ],
493 $extraPos += $extra[
'size'];
495 if ( $extra[
'id'] == self::ZIP64_EXTRA_HEADER ) {
496 return $this->unpack( $extra[
'data'], $zip64ExtraInfo );
507 private function getFileLength() {
508 if ( $this->fileLength ===
null ) {
509 $stat = fstat( $this->file );
510 $this->fileLength = $stat[
'size'];
513 return $this->fileLength;
526 private function getBlock( $start, $length =
null ) {
527 $fileLength = $this->getFileLength();
528 if ( $start >= $fileLength ) {
529 $this->error(
'zip-bad',
"getBlock() requested position $start, " .
530 "file length is $fileLength" );
532 $length ??= $fileLength - $start;
533 $end = $start + $length;
534 if ( $end > $fileLength ) {
535 $this->error(
'zip-bad',
"getBlock() requested end position $end, " .
536 "file length is $fileLength" );
538 $startSeg = (int)floor( $start / self::SEGSIZE );
539 $endSeg = (int)ceil( $end / self::SEGSIZE );
542 for ( $segIndex = $startSeg; $segIndex <= $endSeg; $segIndex++ ) {
543 $block .= $this->getSegment( $segIndex );
546 $block = substr( $block,
547 $start - $startSeg * self::SEGSIZE,
550 if ( strlen( $block ) < $length ) {
551 $this->error(
'zip-bad',
'getBlock() returned an unexpectedly small amount of data' );
570 private function getSegment( $segIndex ) {
571 if ( !isset( $this->buffer[$segIndex] ) ) {
572 $bytePos = $segIndex * self::SEGSIZE;
573 if ( $bytePos >= $this->getFileLength() ) {
574 $this->buffer[$segIndex] =
'';
578 if ( fseek( $this->file, $bytePos ) ) {
579 $this->error(
'zip-bad',
"seek to $bytePos failed" );
581 $seg = fread( $this->file, self::SEGSIZE );
582 if ( $seg ===
false ) {
583 $this->error(
'zip-bad',
"read from $bytePos failed" );
585 $this->buffer[$segIndex] = $seg;
588 return $this->buffer[$segIndex];
596 private function getStructSize( $struct ) {
598 foreach ( $struct as $type ) {
599 if ( is_array( $type ) ) {
600 [ , $fieldSize ] = $type;
630 private function unpack( $string, $struct, $offset = 0 ) {
631 $size = $this->getStructSize( $struct );
632 if ( $offset + $size > strlen( $string ) ) {
633 $this->error(
'zip-bad',
'unpack() would run past the end of the supplied string' );
638 foreach ( $struct as $key => $type ) {
639 if ( is_array( $type ) ) {
640 [ $typeName, $fieldSize ] = $type;
641 switch ( $typeName ) {
643 $data[$key] = substr( $string, $pos, $fieldSize );
647 throw new UnexpectedValueException( __METHOD__ .
": invalid type \"$typeName\"" );
651 $length = intval( $type );
656 for ( $i = $length - 1; $i >= 0; $i-- ) {
658 $value += ord( $string[$pos + $i] );
662 if ( $value > 2 ** 52 ) {
663 $this->error(
'zip-unsupported',
'number too large to be stored in a double. ' .
664 'This could happen if we tried to unpack a 64-bit structure ' .
665 'at an invalid location.' );
667 $data[$key] = $value;
683 private function testBit( $value, $bitIndex ) {
684 return (
bool)( ( $value >> $bitIndex ) & 1 );