phar_metadata_write3.phpt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. --TEST--
  2. Phar with unsafe object in metadata does not unserialize on reading a file.
  3. --EXTENSIONS--
  4. phar
  5. --INI--
  6. phar.require_hash=0
  7. phar.readonly=0
  8. --FILE--
  9. <?php
  10. class EchoesOnWakeup {
  11. public function __wakeup() {
  12. echo "In wakeup\n";
  13. }
  14. }
  15. $fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.php';
  16. $pname = 'phar://' . $fname;
  17. $file = "<?php __HALT_COMPILER(); ?>";
  18. $files = array();
  19. $files['a'] = array('cont' => 'contents of file a');
  20. include 'files/phar_test.inc';
  21. echo "Reading file contents through stream wrapper\n";
  22. foreach($files as $name => $cont) {
  23. var_dump(file_get_contents($pname.'/'.$name));
  24. }
  25. $phar = new Phar($fname);
  26. echo "Original metadata\n";
  27. var_dump($phar->getMetadata());
  28. $phar->setMetadata(new EchoesOnWakeup());
  29. unset($phar);
  30. // NOTE: Phar will use the cached value of metadata if setMetaData was called on that Phar path before.
  31. // Save the writes to the phar and use a different file path.
  32. $fname_new = "$fname.copy.php";
  33. copy($fname, $fname_new);
  34. $phar = new Phar($fname_new);
  35. echo "Calling getMetadata\n";
  36. var_dump($phar->getMetadata());
  37. echo "Calling getMetadata with no allowed_classes\n";
  38. var_dump($phar->getMetadata(['allowed_classes' => []]));
  39. echo "Calling getMetadata with EchoesOnWakeup allowed\n";
  40. var_dump($phar->getMetadata(['allowed_classes' => [EchoesOnWakeup::class]]));
  41. // Part of this is a test that there are no unexpected behaviors when both selMetadata and getMetadata are used
  42. $phar->setMetaData([new EchoesOnWakeup(), new stdClass()]);
  43. echo "Calling getMetadata with too low max_depth\n";
  44. var_dump($phar->getMetadata(['max_depth' => 1]));
  45. echo "Calling getMetadata with some allowed classes\n";
  46. var_dump($phar->getMetadata(['allowed_classes' => [EchoesOnWakeup::class]]));
  47. echo "Calling getMetadata with no options returns the original metadata value\n";
  48. var_dump($phar->getMetadata());
  49. unset($phar);
  50. ?>
  51. --CLEAN--
  52. <?php
  53. unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
  54. unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php.copy.php');
  55. ?>
  56. --EXPECTF--
  57. Reading file contents through stream wrapper
  58. string(18) "contents of file a"
  59. Original metadata
  60. NULL
  61. Calling getMetadata
  62. In wakeup
  63. object(EchoesOnWakeup)#2 (0) {
  64. }
  65. Calling getMetadata with no allowed_classes
  66. object(__PHP_Incomplete_Class)#2 (1) {
  67. ["__PHP_Incomplete_Class_Name"]=>
  68. string(14) "EchoesOnWakeup"
  69. }
  70. Calling getMetadata with EchoesOnWakeup allowed
  71. In wakeup
  72. object(EchoesOnWakeup)#2 (0) {
  73. }
  74. Calling getMetadata with too low max_depth
  75. Warning: Phar::getMetadata(): Maximum depth of 1 exceeded. The depth limit can be changed using the max_depth unserialize() option or the unserialize_max_depth ini setting in %sphar_metadata_write3.php on line 39
  76. Notice: Phar::getMetadata(): Error at offset 34 of 59 bytes in %sphar_metadata_write3.php on line 39
  77. bool(false)
  78. Calling getMetadata with some allowed classes
  79. In wakeup
  80. array(2) {
  81. [0]=>
  82. object(EchoesOnWakeup)#4 (0) {
  83. }
  84. [1]=>
  85. object(__PHP_Incomplete_Class)#5 (1) {
  86. ["__PHP_Incomplete_Class_Name"]=>
  87. string(8) "stdClass"
  88. }
  89. }
  90. Calling getMetadata with no options returns the original metadata value
  91. array(2) {
  92. [0]=>
  93. object(EchoesOnWakeup)#2 (0) {
  94. }
  95. [1]=>
  96. object(stdClass)#3 (0) {
  97. }
  98. }