91 $file = fopen( $fileName,
'r' );
93 return $zdr->execute();
109 return $zdr->execute();
135 private const ZIP64_EXTRA_HEADER = 0x0001;
138 private const SEGSIZE = 16384;
141 private const GENERAL_UTF8 = 11;
144 private const GENERAL_CD_ENCRYPTED = 13;
155 if ( isset( $options[
'zip64'] ) ) {
156 $this->zip64 = $options[
'zip64'];
165 private function execute() {
166 if ( !$this->file ) {
167 return StatusValue::newFatal(
'zip-file-open-error' );
170 $status = StatusValue::newGood();
172 $this->readEndOfCentralDirectoryRecord();
173 if ( $this->zip64 ) {
174 [ $offset, $size ] = $this->findZip64CentralDirectory();
175 $this->readCentralDirectory( $offset, $size );
177 if ( $this->eocdr[
'CD size'] == 0xffffffff
178 || $this->eocdr[
'CD offset'] == 0xffffffff
179 || $this->eocdr[
'CD entries total'] == 0xffff
181 $this->error(
'zip-unsupported',
'Central directory header indicates ZIP64, ' .
182 'but we are in legacy mode. Rejecting this upload is necessary to avoid ' .
183 'opening vulnerabilities on clients using OpenJDK 7 or later.' );
186 [ $offset, $size ] = $this->findOldCentralDirectory();
187 $this->readCentralDirectory( $offset, $size );
189 }
catch ( ZipDirectoryReaderError $e ) {
190 $status->fatal( $e->getErrorCode() );
193 fclose( $this->file );
205 private function error( $code, $debugMessage ): never {
206 wfDebug( __CLASS__ .
": Fatal error: $debugMessage" );
207 throw new ZipDirectoryReaderError( $code );
215 private function readEndOfCentralDirectoryRecord() {
219 'CD start disk' => 2,
220 'CD entries this disk' => 2,
221 'CD entries total' => 2,
224 'file comment length' => 2,
226 $structSize = $this->getStructSize( $info );
227 $startPos = $this->getFileLength() - 65536 - $structSize;
228 if ( $startPos < 0 ) {
232 if ( $this->getFileLength() === 0 ) {
233 $this->error(
'zip-wrong-format',
"The file is empty." );
236 $block = $this->getBlock( $startPos );
237 $sigPos = strrpos( $block,
"PK\x05\x06" );
238 if ( $sigPos ===
false ) {
239 $this->error(
'zip-wrong-format',
240 "zip file lacks EOCDR signature. It probably isn't a zip file." );
243 $this->eocdr = $this->unpack( substr( $block, $sigPos ), $info );
244 $this->eocdr[
'EOCDR size'] = $structSize + $this->eocdr[
'file comment length'];
246 if ( $structSize + $this->eocdr[
'file comment length'] != strlen( $block ) - $sigPos ) {
248 $this->error(
'zip-wrong-format',
'there is a ZIP signature but it is not at ' .
249 'the end of the file. It could be an OLE file with a ZIP file embedded.' );
251 if ( $this->eocdr[
'disk'] !== 0
252 || $this->eocdr[
'CD start disk'] !== 0
254 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR)' );
256 $this->eocdr += $this->unpack(
258 [
'file comment' => [
'string', $this->eocdr[
'file comment length'] ] ],
259 $sigPos + $structSize );
260 $this->eocdr[
'position'] = $startPos + $sigPos;
267 private function readZip64EndOfCentralDirectoryLocator() {
269 'signature' => [
'string', 4 ],
270 'eocdr64 start disk' => 4,
271 'eocdr64 offset' => 8,
272 'number of disks' => 4,
274 $structSize = $this->getStructSize( $info );
276 $start = $this->getFileLength() - $this->eocdr[
'EOCDR size'] - $structSize;
277 $block = $this->getBlock( $start, $structSize );
278 $this->eocdr64Locator = $data = $this->unpack( $block, $info );
280 if ( $data[
'signature'] !==
"PK\x06\x07" ) {
284 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory locator' );
292 private function readZip64EndOfCentralDirectoryRecord() {
293 if ( $this->eocdr64Locator[
'eocdr64 start disk'] != 0
294 || $this->eocdr64Locator[
'number of disks'] != 0
296 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64 locator)' );
300 'signature' => [
'string', 4 ],
302 'version made by' => 2,
303 'version needed' => 2,
305 'CD start disk' => 4,
306 'CD entries this disk' => 8,
307 'CD entries total' => 8,
311 $structSize = $this->getStructSize( $info );
312 $block = $this->getBlock( $this->eocdr64Locator[
'eocdr64 offset'], $structSize );
313 $this->eocdr64 = $data = $this->unpack( $block, $info );
314 if ( $data[
'signature'] !==
"PK\x06\x06" ) {
315 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory record' );
317 if ( $data[
'disk'] !== 0
318 || $data[
'CD start disk'] !== 0
320 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64)' );
330 private function findOldCentralDirectory() {
331 $size = $this->eocdr[
'CD size'];
332 $offset = $this->eocdr[
'CD offset'];
333 $endPos = $this->eocdr[
'position'];
337 if ( $offset + $size != $endPos ) {
338 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
339 'of central directory record' );
342 return [ $offset, $size ];
351 private function findZip64CentralDirectory() {
355 $size = $this->eocdr[
'CD size'];
356 $offset = $this->eocdr[
'CD offset'];
357 $numEntries = $this->eocdr[
'CD entries total'];
358 $endPos = $this->eocdr[
'position'];
359 if ( $size == 0xffffffff
360 || $offset == 0xffffffff
361 || $numEntries == 0xffff
363 $this->readZip64EndOfCentralDirectoryLocator();
365 if ( isset( $this->eocdr64Locator[
'eocdr64 offset'] ) ) {
366 $this->readZip64EndOfCentralDirectoryRecord();
367 if ( isset( $this->eocdr64[
'CD offset'] ) ) {
368 $size = $this->eocdr64[
'CD size'];
369 $offset = $this->eocdr64[
'CD offset'];
370 $endPos = $this->eocdr64Locator[
'eocdr64 offset'];
376 if ( $offset + $size != $endPos ) {
377 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
378 'of central directory record' );
381 return [ $offset, $size ];
389 private function readCentralDirectory( $offset, $size ) {
390 $block = $this->getBlock( $offset, $size );
393 'signature' => [
'string', 4 ],
394 'version made by' => 2,
395 'version needed' => 2,
397 'compression method' => 2,
401 'compressed size' => 4,
402 'uncompressed size' => 4,
404 'extra field length' => 2,
405 'comment length' => 2,
406 'disk number start' => 2,
407 'internal attrs' => 2,
408 'external attrs' => 4,
409 'local header offset' => 4,
411 $fixedSize = $this->getStructSize( $fixedInfo );
414 while ( $pos < $size ) {
415 $data = $this->unpack( $block, $fixedInfo, $pos );
418 if ( $data[
'signature'] !==
"PK\x01\x02" ) {
419 $this->error(
'zip-bad',
'Invalid signature found in directory entry' );
423 'name' => [
'string', $data[
'name length'] ],
424 'extra field' => [
'string', $data[
'extra field length'] ],
425 'comment' => [
'string', $data[
'comment length'] ],
427 $data += $this->unpack( $block, $variableInfo, $pos );
428 $pos += $this->getStructSize( $variableInfo );
430 if ( $this->zip64 && (
431 $data[
'compressed size'] == 0xffffffff
432 || $data[
'uncompressed size'] == 0xffffffff
433 || $data[
'local header offset'] == 0xffffffff )
435 $zip64Data = $this->unpackZip64Extra( $data[
'extra field'] );
437 $data = $zip64Data + $data;
441 if ( $this->testBit( $data[
'general bits'], self::GENERAL_CD_ENCRYPTED ) ) {
442 $this->error(
'zip-unsupported',
'central directory encryption is not supported' );
448 $time = $data[
'mod time'];
449 $date = $data[
'mod date'];
451 $year = 1980 + ( $date >> 9 );
452 $month = ( $date >> 5 ) & 15;
454 $hour = ( $time >> 11 ) & 31;
455 $minute = ( $time >> 5 ) & 63;
456 $second = ( $time & 31 ) * 2;
457 $timestamp = sprintf(
"%04d%02d%02d%02d%02d%02d",
458 $year, $month, $day, $hour, $minute, $second );
461 if ( $this->testBit( $data[
'general bits'], self::GENERAL_UTF8 ) ) {
462 $name = $data[
'name'];
464 $name = iconv(
'CP437',
'UTF-8', $data[
'name'] );
470 'mtime' => $timestamp,
471 'size' => $data[
'uncompressed size'],
472 'compression' => $data[
'compression method'],
473 'local_header_offset' => $data[
'local header offset'],
474 'compressed_size' => $data[
'compressed size'],
476 ( $this->callback )( $userData );
485 private function unpackZip64Extra( $extraField ) {
490 $extraHeaderSize = $this->getStructSize( $extraHeaderInfo );
493 'uncompressed size' => 8,
494 'compressed size' => 8,
495 'local header offset' => 8,
496 'disk number start' => 4,
500 while ( $extraPos < strlen( $extraField ) ) {
501 $extra = $this->unpack( $extraField, $extraHeaderInfo, $extraPos );
502 $extraPos += $extraHeaderSize;
503 $extra += $this->unpack( $extraField,
504 [
'data' => [
'string', $extra[
'size'] ] ],
506 $extraPos += $extra[
'size'];
508 if ( $extra[
'id'] == self::ZIP64_EXTRA_HEADER ) {
509 return $this->unpack( $extra[
'data'], $zip64ExtraInfo );
520 private function getFileLength() {
521 if ( $this->fileLength ===
null ) {
522 $stat = fstat( $this->file );
523 $this->fileLength = $stat[
'size'];
526 return $this->fileLength;
539 private function getBlock( $start, $length =
null ) {
540 $fileLength = $this->getFileLength();
541 if ( $start >= $fileLength ) {
542 $this->error(
'zip-bad',
"getBlock() requested position $start, " .
543 "file length is $fileLength" );
545 $length ??= $fileLength - $start;
546 $end = $start + $length;
547 if ( $end > $fileLength ) {
548 $this->error(
'zip-bad',
"getBlock() requested end position $end, " .
549 "file length is $fileLength" );
551 $startSeg = (int)floor( $start / self::SEGSIZE );
552 $endSeg = (int)ceil( $end / self::SEGSIZE );
555 for ( $segIndex = $startSeg; $segIndex <= $endSeg; $segIndex++ ) {
556 $block .= $this->getSegment( $segIndex );
559 $block = substr( $block,
560 $start - $startSeg * self::SEGSIZE,
563 if ( strlen( $block ) < $length ) {
564 $this->error(
'zip-bad',
'getBlock() returned an unexpectedly small amount of data' );
583 private function getSegment( $segIndex ) {
584 if ( !isset( $this->buffer[$segIndex] ) ) {
585 $bytePos = $segIndex * self::SEGSIZE;
586 if ( $bytePos >= $this->getFileLength() ) {
587 $this->buffer[$segIndex] =
'';
591 if ( fseek( $this->file, $bytePos ) ) {
592 $this->error(
'zip-bad',
"seek to $bytePos failed" );
594 $seg = fread( $this->file, self::SEGSIZE );
595 if ( $seg ===
false ) {
596 $this->error(
'zip-bad',
"read from $bytePos failed" );
598 $this->buffer[$segIndex] = $seg;
601 return $this->buffer[$segIndex];
609 private function getStructSize( $struct ) {
611 foreach ( $struct as $type ) {
612 if ( is_array( $type ) ) {
613 [ , $fieldSize ] = $type;
643 private function unpack( $string, $struct, $offset = 0 ) {
644 $size = $this->getStructSize( $struct );
645 if ( $offset + $size > strlen( $string ) ) {
646 $this->error(
'zip-bad',
'unpack() would run past the end of the supplied string' );
651 foreach ( $struct as $key => $type ) {
652 if ( is_array( $type ) ) {
653 [ $typeName, $fieldSize ] = $type;
654 switch ( $typeName ) {
656 $data[$key] = substr( $string, $pos, $fieldSize );
660 throw new UnexpectedValueException( __METHOD__ .
": invalid type \"$typeName\"" );
664 $length = intval( $type );
669 for ( $i = $length - 1; $i >= 0; $i-- ) {
671 $value += ord( $string[$pos + $i] );
675 if ( $value > 2 ** 52 ) {
676 $this->error(
'zip-unsupported',
'number too large to be stored in a double. ' .
677 'This could happen if we tried to unpack a 64-bit structure ' .
678 'at an invalid location.' );
680 $data[$key] = $value;
696 private function testBit( $value, $bitIndex ) {
697 return (
bool)( ( $value >> $bitIndex ) & 1 );