MTimeProtectedFileStorageBase.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\Tests\Component\PhpStorage;
  3. use Drupal\Component\Utility\Crypt;
  4. use Drupal\Component\Utility\Random;
  5. /**
  6. * Base test class for MTime protected storage.
  7. */
  8. abstract class MTimeProtectedFileStorageBase extends PhpStorageTestBase {
  9. /**
  10. * The PHP storage class to test.
  11. *
  12. * This should be overridden by extending classes.
  13. */
  14. protected $storageClass;
  15. /**
  16. * The secret string to use for file creation.
  17. *
  18. * @var string
  19. */
  20. protected $secret;
  21. /**
  22. * Test settings to pass to storage instances.
  23. *
  24. * @var array
  25. */
  26. protected $settings;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function setUp() {
  31. parent::setUp();
  32. // Random generator.
  33. $random = new Random();
  34. $this->secret = $random->name(8, TRUE);
  35. $this->settings = [
  36. 'directory' => $this->directory,
  37. 'bin' => 'test',
  38. 'secret' => $this->secret,
  39. ];
  40. }
  41. /**
  42. * Tests basic load/save/delete operations.
  43. *
  44. * @covers ::load
  45. * @covers ::save
  46. * @covers ::delete
  47. * @covers ::exists
  48. */
  49. public function testCRUD() {
  50. $php = new $this->storageClass($this->settings);
  51. $this->assertCRUD($php);
  52. }
  53. /**
  54. * Tests the security of the MTimeProtectedFileStorage implementation.
  55. *
  56. * We test two attacks: first changes the file mtime, then the directory
  57. * mtime too.
  58. *
  59. * We need to delay over 1 second for mtime test.
  60. * @medium
  61. */
  62. public function testSecurity() {
  63. $php = new $this->storageClass($this->settings);
  64. $name = 'simpletest.php';
  65. $php->save($name, '<?php');
  66. $expected_root_directory = $this->directory . '/test';
  67. if (substr($name, -4) === '.php') {
  68. $expected_directory = $expected_root_directory . '/' . substr($name, 0, -4);
  69. }
  70. else {
  71. $expected_directory = $expected_root_directory . '/' . $name;
  72. }
  73. $directory_mtime = filemtime($expected_directory);
  74. $expected_filename = $expected_directory . '/' . Crypt::hmacBase64($name, $this->secret . $directory_mtime) . '.php';
  75. // Ensure the file exists and that it and the containing directory have
  76. // minimal permissions. fileperms() can return high bits unrelated to
  77. // permissions, so mask with 0777.
  78. $this->assertTrue(file_exists($expected_filename));
  79. $this->assertSame(0444, fileperms($expected_filename) & 0777);
  80. $this->assertSame(0777, fileperms($expected_directory) & 0777);
  81. // Ensure the root directory for the bin has a .htaccess file denying web
  82. // access.
  83. $this->assertSame(file_get_contents($expected_root_directory . '/.htaccess'), call_user_func([$this->storageClass, 'htaccessLines']));
  84. // Ensure that if the file is replaced with an untrusted one (due to another
  85. // script's file upload vulnerability), it does not get loaded. Since mtime
  86. // granularity is 1 second, we cannot prevent an attack that happens within
  87. // a second of the initial save().
  88. sleep(1);
  89. for ($i = 0; $i < 2; $i++) {
  90. $php = new $this->storageClass($this->settings);
  91. $GLOBALS['hacked'] = FALSE;
  92. $untrusted_code = "<?php\n" . '$GLOBALS["hacked"] = TRUE;';
  93. chmod($expected_directory, 0700);
  94. chmod($expected_filename, 0700);
  95. if ($i) {
  96. // Now try to write the file in such a way that the directory mtime
  97. // changes and invalidates the hash.
  98. file_put_contents($expected_filename . '.tmp', $untrusted_code);
  99. rename($expected_filename . '.tmp', $expected_filename);
  100. }
  101. else {
  102. // On the first try do not change the directory mtime but the filemtime
  103. // is now larger than the directory mtime.
  104. file_put_contents($expected_filename, $untrusted_code);
  105. }
  106. chmod($expected_filename, 0400);
  107. chmod($expected_directory, 0100);
  108. $this->assertSame(file_get_contents($expected_filename), $untrusted_code);
  109. $this->assertSame($this->expected[$i], $php->exists($name));
  110. $this->assertSame($this->expected[$i], $php->load($name));
  111. $this->assertSame($this->expected[$i], $GLOBALS['hacked']);
  112. }
  113. unset($GLOBALS['hacked']);
  114. }
  115. }