3 namespace Drupal\Tests\Component\PhpStorage;
5 use Drupal\Component\Utility\Crypt;
8 * Base test class for MTime protected storage.
10 abstract class MTimeProtectedFileStorageBase extends PhpStorageTestBase {
13 * The PHP storage class to test.
15 * This should be overridden by extending classes.
17 protected $storageClass;
20 * The secret string to use for file creation.
27 * Test settings to pass to storage instances.
36 protected function setUp() {
39 $this->secret = $this->randomMachineName();
42 'directory' => $this->directory,
44 'secret' => $this->secret,
49 * Tests basic load/save/delete operations.
56 public function testCRUD() {
57 $php = new $this->storageClass($this->settings);
58 $this->assertCRUD($php);
62 * Tests the security of the MTimeProtectedFileStorage implementation.
64 * We test two attacks: first changes the file mtime, then the directory
67 * We need to delay over 1 second for mtime test.
70 public function testSecurity() {
71 $php = new $this->storageClass($this->settings);
72 $name = 'simpletest.php';
73 $php->save($name, '<?php');
74 $expected_root_directory = $this->directory . '/test';
75 if (substr($name, -4) === '.php') {
76 $expected_directory = $expected_root_directory . '/' . substr($name, 0, -4);
79 $expected_directory = $expected_root_directory . '/' . $name;
81 $directory_mtime = filemtime($expected_directory);
82 $expected_filename = $expected_directory . '/' . Crypt::hmacBase64($name, $this->secret . $directory_mtime) . '.php';
84 // Ensure the file exists and that it and the containing directory have
85 // minimal permissions. fileperms() can return high bits unrelated to
86 // permissions, so mask with 0777.
87 $this->assertTrue(file_exists($expected_filename));
88 $this->assertSame(fileperms($expected_filename) & 0777, 0444);
89 $this->assertSame(fileperms($expected_directory) & 0777, 0777);
91 // Ensure the root directory for the bin has a .htaccess file denying web
93 $this->assertSame(file_get_contents($expected_root_directory . '/.htaccess'), call_user_func([$this->storageClass, 'htaccessLines']));
95 // Ensure that if the file is replaced with an untrusted one (due to another
96 // script's file upload vulnerability), it does not get loaded. Since mtime
97 // granularity is 1 second, we cannot prevent an attack that happens within
98 // a second of the initial save().
100 for ($i = 0; $i < 2; $i++) {
101 $php = new $this->storageClass($this->settings);
102 $GLOBALS['hacked'] = FALSE;
103 $untrusted_code = "<?php\n" . '$GLOBALS["hacked"] = TRUE;';
104 chmod($expected_directory, 0700);
105 chmod($expected_filename, 0700);
107 // Now try to write the file in such a way that the directory mtime
108 // changes and invalidates the hash.
109 file_put_contents($expected_filename . '.tmp', $untrusted_code);
110 rename($expected_filename . '.tmp', $expected_filename);
113 // On the first try do not change the directory mtime but the filemtime
114 // is now larger than the directory mtime.
115 file_put_contents($expected_filename, $untrusted_code);
117 chmod($expected_filename, 0400);
118 chmod($expected_directory, 0100);
119 $this->assertSame(file_get_contents($expected_filename), $untrusted_code);
120 $this->assertSame($php->exists($name), $this->expected[$i]);
121 $this->assertSame($php->load($name), $this->expected[$i]);
122 $this->assertSame($GLOBALS['hacked'], $this->expected[$i]);
124 unset($GLOBALS['hacked']);