You are here

public function SaveUploadTest::testHandleFileMunge in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/SaveUploadTest.php \Drupal\Tests\file\Functional\SaveUploadTest::testHandleFileMunge()

Test file munge handling.

File

core/modules/file/tests/src/Functional/SaveUploadTest.php, line 331

Class

SaveUploadTest
Tests the file_save_upload() function.

Namespace

Drupal\Tests\file\Functional

Code

public function testHandleFileMunge() {

  // Ensure insecure uploads are disabled for this test.
  $this
    ->config('system.file')
    ->set('allow_insecure_uploads', 0)
    ->save();
  $original_image_uri = $this->image
    ->getFileUri();
  $this->image = file_move($this->image, $original_image_uri . '.foo.' . $this->imageExtension);

  // Reset the hook counters to get rid of the 'move' we just called.
  file_test_reset();
  $extensions = $this->imageExtension;
  $edit = [
    'files[file_test_upload]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'extensions' => $extensions,
  ];
  $munged_filename = $this->image
    ->getFilename();
  $munged_filename = substr($munged_filename, 0, strrpos($munged_filename, '.'));
  $munged_filename .= '_.' . $this->imageExtension;
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
  $this
    ->assertRaw(t('File name is @filename', [
    '@filename' => $munged_filename,
  ]), 'File was successfully munged.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'insert',
  ]);

  // Reset the hook counters.
  file_test_reset();

  // Ensure we don't munge the .foo extension if it is in the list of allowed
  // extensions.
  $extensions = 'foo ' . $this->imageExtension;
  $edit = [
    'files[file_test_upload]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'extensions' => $extensions,
  ];
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
  $this
    ->assertRaw(t('File name is @filename', [
    '@filename' => $this->image
      ->getFilename(),
  ]), 'File was not munged when all extensions within it are allowed.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'insert',
  ]);

  // Ensure we don't munge files if we're allowing any extension.
  $this->image = file_move($this->image, $original_image_uri . '.foo.txt.' . $this->imageExtension);

  // Reset the hook counters.
  file_test_reset();
  $edit = [
    'files[file_test_upload]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => 'empty_array',
  ];
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found no security message.');
  $this
    ->assertRaw(t('File name is @filename', [
    '@filename' => $this->image
      ->getFilename(),
  ]), 'File was not munged when allowing any extension.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'insert',
  ]);

  // Test that a dangerous extension such as .php is munged even if it is in
  // the list of allowed extensions.
  $this->image = file_move($this->image, $original_image_uri . '.php.' . $this->imageExtension);

  // Reset the hook counters.
  file_test_reset();
  $extensions = 'php ' . $this->imageExtension;
  $edit = [
    'files[file_test_upload]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'extensions' => $extensions,
  ];
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
  $this
    ->assertRaw(t('File name is @filename', [
    '@filename' => 'image-test.png.php_.png',
  ]), 'File was successfully munged.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'insert',
  ]);

  // Reset the hook counters.
  file_test_reset();

  // Dangerous extensions are munged even when all extensions are allowed.
  $edit = [
    'files[file_test_upload]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => 'empty_array',
  ];
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
  $this
    ->assertRaw(t('File name is @filename.', [
    '@filename' => 'image-test.png_.php_.png_.txt',
  ]), 'File was successfully munged.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'insert',
  ]);

  // Dangerous extensions are munged if is renamed to end in .txt.
  $this->image = file_move($this->image, $original_image_uri . '.cgi.' . $this->imageExtension . '.txt');

  // Reset the hook counters.
  file_test_reset();

  // Dangerous extensions are munged even when all extensions are allowed.
  $edit = [
    'files[file_test_upload]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => 'empty_array',
  ];
  $this
    ->drupalPostForm('file-test/upload', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
  $this
    ->assertRaw(t('File name is @filename.', [
    '@filename' => 'image-test.png_.cgi_.png_.txt',
  ]), 'File was successfully munged.');
  $this
    ->assertRaw(t('You WIN!'), 'Found the success message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
    'insert',
  ]);

  // Reset the hook counters.
  file_test_reset();

  // Ensure that setting $validators['file_validate_extensions'] = ['']
  // rejects all files without munging or renaming.
  $edit = [
    'files[file_test_upload][]' => \Drupal::service('file_system')
      ->realpath($this->image
      ->getFileUri()),
    'allow_all_extensions' => 'empty_string',
  ];
  $this
    ->drupalPostForm('file-test/save_upload_from_form_test', $edit, t('Submit'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoRaw(t('For security reasons, your upload has been renamed'), 'Found security message.');
  $this
    ->assertRaw(t('Epic upload FAIL!'), 'Found the failure message.');

  // Check that the correct hooks were called.
  $this
    ->assertFileHooksCalled([
    'validate',
  ]);
}