MimeAnalyzer: fix ZIP parsing failure
authorTim Starling <tstarling@wikimedia.org>
Thu, 25 Jul 2019 03:29:44 +0000 (13:29 +1000)
committerTim Starling <tstarling@wikimedia.org>
Thu, 25 Jul 2019 03:40:18 +0000 (13:40 +1000)
commitd8e06a46a86d694a0d01238b04b51735b59a7846
treeefee49ffaf3c40f3490befe9ecf54b7a2226d2c8
parenteee30a9434ba5ba2dc2f1c2b3b5632543fcbc773
MimeAnalyzer: fix ZIP parsing failure

unpack() actually returns an array with indexes starting from 1, not
zero, so unpack(...)[0] gives a notice and always returns null. It is
lucky that ZIPs normally have zero-length comments, so this would have
had little impact on file type detection aside from log spam.

Also, add a check to make sure the unpack() will not read beyond
the end of the file. Without this, unpack() could generate a warning.

The bug was introduced by me in f12db3804882272794b.

Add tests. The test files were generated by appending an EOCDR signature
and some extra bytes to 1bit-png.png.

Bug: T223728
Change-Id: I6fab63102d1d8eea92cdcce5ab6d1eb747a0a890
includes/libs/mime/MimeAnalyzer.php
tests/phpunit/data/media/zip-comment-overflow.png [new file with mode: 0644]
tests/phpunit/data/media/zip-kind-of-valid-2.png [new file with mode: 0644]
tests/phpunit/data/media/zip-kind-of-valid.png [new file with mode: 0644]
tests/phpunit/data/media/zip-sig-near-end.png [new file with mode: 0644]
tests/phpunit/includes/libs/mime/MimeAnalyzerTest.php