|
| 1 | +--TEST-- |
| 2 | +GH-24740 (Unbounded size of .phar/.metadata* entry in TAR phar) |
| 3 | +--EXTENSIONS-- |
| 4 | +phar |
| 5 | +--INI-- |
| 6 | +memory_limit=16M |
| 7 | +--FILE-- |
| 8 | +<?php |
| 9 | +function tar_header(string $name, int $size): string |
| 10 | +{ |
| 11 | + $header = str_repeat("\0", 512); |
| 12 | + $header = substr_replace($header, $name, 0, strlen($name)); |
| 13 | + $header = substr_replace($header, sprintf("%07o\0", 0644), 100, 8); |
| 14 | + $header = substr_replace($header, sprintf("%07o\0", 0), 108, 8); |
| 15 | + $header = substr_replace($header, sprintf("%07o\0", 0), 116, 8); |
| 16 | + $header = substr_replace($header, sprintf("%011o\0", $size), 124, 12); |
| 17 | + $header = substr_replace($header, sprintf("%011o\0", 1700000000), 136, 12); |
| 18 | + $header[156] = '0'; |
| 19 | + $header = substr_replace($header, "ustar\0", 257, 6); |
| 20 | + $header = substr_replace($header, "00", 263, 2); |
| 21 | + |
| 22 | + $checksum = 0; |
| 23 | + for ($i = 0; $i < 512; $i++) { |
| 24 | + $checksum += ($i >= 148 && $i < 156) ? 0x20 : ord($header[$i]); |
| 25 | + } |
| 26 | + |
| 27 | + return substr_replace($header, sprintf("%06o\0 ", $checksum), 148, 8); |
| 28 | +} |
| 29 | + |
| 30 | +$fname = __DIR__ . '/gh24740.tar'; |
| 31 | +file_put_contents($fname, tar_header('.phar/.metadata.bin', 0x7FFFFE00) . str_repeat("\0", 1024)); |
| 32 | + |
| 33 | +try { |
| 34 | + new PharData($fname); |
| 35 | +} catch (UnexpectedValueException $e) { |
| 36 | + echo $e->getMessage(), "\n"; |
| 37 | +} |
| 38 | +?> |
| 39 | +--CLEAN-- |
| 40 | +<?php |
| 41 | +@unlink(__DIR__ . '/gh24740.tar'); |
| 42 | +?> |
| 43 | +--EXPECTF-- |
| 44 | +phar error: tar-based phar "%sgh24740.tar" has invalid metadata in magic file ".phar/.metadata.bin" |
0 commit comments