1
0
mirror of https://github.com/php/php-src.git synced 2026-04-22 07:28:09 +02:00
Files
archived-php-src/ext/phar/tests/phar_metadata_write4.phpt
T
Tyson Andre 0c238ede01 [RFC] Only unserialize Phar metadata when getMetadata() is called
In other words, don't automatically unserialize when the magic
phar:// stream wrappers are used.
RFC: https://wiki.php.net/rfc/phar_stop_autoloading_metadata

Also, change the signature from `getMetadata()`
to `getMetadata(array $unserialize_options = [])`.
Start throwing earlier if setMetadata() is called and serialization threw.

See https://externals.io/message/110856 and
https://bugs.php.net/bug.php?id=76774

This was refactored to add a phar_metadata_tracker for the following reasons:
- The way to properly copy a zval was previously implicit and undocumented
  (e.g. is it a pointer to a raw string or an actual value)
- Avoid unnecessary serialization and unserialization in the most common case
- If a metadata value is serialized once while saving a new/modified phar file,
  this allows reusing the same serialized string.
- Have as few ways to copy/clone/lazily parse metadata (etc.) as possible,
  so that code changes can be limited to only a few places in the future.
- Performance is hopefully not a concern - copying a string should be faster
  than unserializing a value, and metadata should be rare in most cases.

Remove unnecessary skip in a test(Compression's unused)

Add additional assertions about usage of persistent phars

Improve robustness of `Phar*->setMetadata()`

- Add sanity checks for edge cases freeing metadata, when destructors
  or serializers modify the phar recursively.
- Typical use cases of php have phar.readonly=1 and would not be affected.

Closes GH-5855
2020-08-03 13:28:51 -04:00

103 lines
3.0 KiB
PHP

--TEST--
Phar with object in metadata
--SKIPIF--
<?php
if (!extension_loaded("phar")) die("skip");
?>
--INI--
phar.require_hash=0
phar.readonly=0
--FILE--
<?php
class EchoesOnWakeup {
public function __wakeup() {
echo "In __wakeup " . spl_object_id($this) . "\n";
}
public function __destruct() {
echo "In __destruct " . spl_object_id($this) . "\n";
}
}
class ThrowsOnSerialize {
public function __sleep() {
throw new RuntimeException("In sleep");
}
}
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.php';
$pname = 'phar://' . $fname;
$file = "<?php __HALT_COMPILER(); ?>";
$files = array();
$files['a'] = array('cont' => 'a', 'meta' => new EchoesOnWakeup());
include 'files/phar_test.inc';
foreach($files as $name => $cont) {
var_dump(file_get_contents($pname.'/'.$name));
}
unset($files);
$phar = new Phar($fname);
echo "Loading metadata for 'a' without allowed_classes\n";
var_dump($phar['a']->getMetadata(['allowed_classes' => []]));
echo "Loading metadata for 'a' with allowed_classes\n";
var_dump($phar['a']->getMetadata(['allowed_classes' => true]));
unset($phar);
// NOTE: Phar will use the cached value of metadata if setMetaData was called on that Phar path before.
// Save the writes to the phar and use a different file path.
$fname_new = "$fname.copy.php";
copy($fname, $fname_new);
$phar = new Phar($fname_new);
echo "Loading metadata from 'a' from the new phar\n";
var_dump($phar['a']->getMetadata());
echo "Loading metadata from 'a' from the new phar with unserialize options\n";
var_dump($phar['a']->getMetadata(['allowed_classes' => true]));
// PharEntry->setMetaData will do the following:
// 1. serialize, checking for exceptions
// 2. free the original data, checking for exceptions or the data getting set from destructors or error handlers.
// 3. set the new data.
try {
var_dump($phar['a']->setMetadata(new ThrowsOnSerialize()));
} catch (RuntimeException $e) {
echo "Caught {$e->getMessage()} at {$e->getFile()}:{$e->getLine()}\n";
unset($e);
}
var_dump($phar['a']->getMetadata([]));
var_dump($phar['a']->getMetadata(['allowed_classes' => false]));
?>
--CLEAN--
<?php
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php.copy.php');
?>
--EXPECTF--
In __destruct 1
string(1) "a"
Loading metadata for 'a' without allowed_classes
object(__PHP_Incomplete_Class)#3 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(14) "EchoesOnWakeup"
}
Loading metadata for 'a' with allowed_classes
In __wakeup 2
object(EchoesOnWakeup)#2 (0) {
}
In __destruct 2
Loading metadata from 'a' from the new phar
In __wakeup 3
object(EchoesOnWakeup)#3 (0) {
}
In __destruct 3
Loading metadata from 'a' from the new phar with unserialize options
In __wakeup 2
object(EchoesOnWakeup)#2 (0) {
}
In __destruct 2
Caught In sleep at %sphar_metadata_write4.php:12
In __wakeup 3
object(EchoesOnWakeup)#3 (0) {
}
In __destruct 3
object(__PHP_Incomplete_Class)#4 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(14) "EchoesOnWakeup"
}