1
0

DataDirectoryProtected.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\CheckServerResponseTrait;
  13. use OCP\SetupCheck\ISetupCheck;
  14. use OCP\SetupCheck\SetupResult;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * Checks if the data directory can not be accessed from outside
  18. */
  19. class DataDirectoryProtected implements ISetupCheck {
  20. use CheckServerResponseTrait;
  21. public function __construct(
  22. protected IL10N $l10n,
  23. protected IConfig $config,
  24. protected IURLGenerator $urlGenerator,
  25. protected IClientService $clientService,
  26. protected LoggerInterface $logger,
  27. ) {
  28. }
  29. public function getCategory(): string {
  30. return 'network';
  31. }
  32. public function getName(): string {
  33. return $this->l10n->t('Data directory protected');
  34. }
  35. public function run(): SetupResult {
  36. $dataDir = str_replace(\OC::$SERVERROOT . '/', '', $this->config->getSystemValueString('datadirectory', ''));
  37. $dataUrl = $this->urlGenerator->linkTo('', $dataDir . '/.ncdata');
  38. $noResponse = true;
  39. foreach ($this->runRequest('GET', $dataUrl, [ 'httpErrors' => false ]) as $response) {
  40. $noResponse = false;
  41. if ($response->getStatusCode() < 400) {
  42. // Read the response body
  43. $body = $response->getBody();
  44. if (is_resource($body)) {
  45. $body = stream_get_contents($body, 64);
  46. }
  47. if (str_contains($body, '# Nextcloud data directory')) {
  48. 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.'));
  49. }
  50. } else {
  51. $this->logger->debug('[expected] Could not access data directory from outside.', ['url' => $dataUrl]);
  52. }
  53. }
  54. if ($noResponse) {
  55. 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());
  56. }
  57. return SetupResult::success();
  58. }
  59. }