You are here

function security_review_check_file_perms in Security Review 7

Same name and namespace in other branches
  1. 6 security_review.inc \security_review_check_file_perms()

Check that files aren't writeable by the server.

1 string reference to 'security_review_check_file_perms'
_security_review_security_checks in ./security_review.inc
Core Security Review's checks.

File

./security_review.inc, line 219
Stand-alone security checks and review system.

Code

function security_review_check_file_perms() {
  $result = TRUE;

  // Extract ending folder for file directory path.
  $file_path = './' . rtrim(variable_get('file_public_path', conf_path() . '/files'), '/');

  // Set files to ignore.
  $ignore = array(
    '..',
    'CVS',
    '.git',
    '.svn',
    '.bzr',
    realpath($file_path),
  );

  // Add temporary files directory if it's set.
  $temp_path = variable_get('file_temporary_path', '');
  if (!empty($temp_path)) {
    $ignore[] = realpath('./' . rtrim($temp_path, '/'));
  }

  // Add private files directory if it's set.
  $private_files = variable_get('file_private_path', '');
  if (!empty($private_files)) {

    // Remove leading slash if set.
    if (strrpos($private_files, '/') !== FALSE) {
      $private_files = substr($private_files, strrpos($private_files, '/') + 1);
    }
    $ignore[] = $private_files;
  }
  drupal_alter('security_review_file_ignore', $ignore);
  $parsed = array(
    realpath('.'),
  );
  $files = _security_review_check_file_perms_scan('.', $parsed, $ignore);

  // Try creating or appending files.
  // Assume it doesn't work.
  $create_status = $append_status = FALSE;
  $append_message = t("Your web server should not be able to write to your modules directory. This is a security vulnerable. Consult the Security Review file permissions check help for mitigation steps.");
  $directory = drupal_get_path('module', 'security_review');

  // Write a file with the timestamp
  $file = './' . $directory . '/file_write_test.' . date('Ymdhis');
  if ($file_create = @fopen($file, 'w')) {
    $create_status = fwrite($file_create, date('Ymdhis') . ' - ' . $append_message . "\n");
    fclose($file_create);
  }

  // Try to append to our IGNOREME file.
  $file = './' . $directory . '/IGNOREME.txt';
  if ($file_append = @fopen($file, 'a')) {
    $append_status = fwrite($file_append, date('Ymdhis') . ' - ' . $append_message . "\n");
    fclose($file_append);
  }
  if (count($files) || $create_status || $append_status) {
    $result = FALSE;
  }
  return array(
    'result' => $result,
    'value' => $files,
  );
}