1
0

DataDirectoryProtected.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\Http\Client\IClientService;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. use OCP\SetupCheck\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Checks if the data directory can not be accessed from outside
  17. */
  18. class DataDirectoryProtected implements ISetupCheck {
  19. use CheckServerResponseTrait;
  20. public function __construct(
  21. protected IL10N $l10n,
  22. protected IConfig $config,
  23. protected IURLGenerator $urlGenerator,
  24. protected IClientService $clientService,
  25. protected LoggerInterface $logger,
  26. ) {
  27. }
  28. public function getCategory(): string {
  29. return 'network';
  30. }
  31. public function getName(): string {
  32. return $this->l10n->t('Data directory protected');
  33. }
  34. public function run(): SetupResult {
  35. $datadir = str_replace(\OC::$SERVERROOT . '/', '', $this->config->getSystemValue('datadirectory', ''));
  36. $dataUrl = $this->urlGenerator->getWebroot() . '/' . $datadir . '/.ocdata';
  37. $noResponse = true;
  38. foreach ($this->runHEAD($dataUrl, httpErrors:false) as $response) {
  39. $noResponse = false;
  40. if ($response->getStatusCode() === 200) {
  41. return SetupResult::error($this->l10n->t('Your data directory and files are probably accessible from the internet. The .htaccess file is not working. It is strongly recommended that you configure your web server so that the data directory is no longer accessible, or move the data directory outside the web server document root.'));
  42. } else {
  43. $this->logger->debug('[expected] Could not access data directory from outside.', ['url' => $dataUrl]);
  44. }
  45. }
  46. if ($noResponse) {
  47. return SetupResult::warning($this->l10n->t('Could not check that the data directory is protected. Please check manually that your server does not allow access to the data directory.') . "\n" . $this->serverConfigHelp());
  48. }
  49. return SetupResult::success();
  50. }
  51. }